cancel
Showing results for 
Search instead for 
Did you mean: 

How to run NX Open .NET C# program

 

Hello,

 

This may be very simple question to this forum. 

 

I don't have any programming knowledge but often I use the VB journal code available from GTAC website and other sources. I simply copy code, in NX 'Alt+F11' paste the code and run it. Now I get some useful '.NET C# program' also. How can I run such C# code? It does not work (run) as VB code. Any specific process/way?

 

Thanks!

Ganesh Kadole, QA Analyst (PLM), SQS
Testing: NX 10 | NX 11 | Teamcenter-NX Integration
Teamcenter 11.2 | TC Vis 11.3 | Active Workspace 3.2
12 REPLIES

Re: How to run NX Open .NET C# program

Siemens Phenom Siemens Phenom
Siemens Phenom

Yes, journals written in both .NET languages (VB and VC#), as well as Python can be executed in this manner.

 

C/C++ and Java must be compiled prior to execution.

Re: How to run NX Open .NET C# program

Phenom
Phenom

When you browse to the journal file be sure to change the filter to "C# source files (*.cs)". If it is a complete journal, it should run just as VB code does. If it does not run, perhaps it is not a complete journal (it might only be a snippet from a larger journal) or perhaps some of the initialization code is missing.

 

If it is not a ".cs" file type, but rather a .dll or .exe, then you will need to use the file -> execute -> nxopen command. If the file has been signed, you will be able to run it; if it has not been signed, you can only run it if you have an author license.

Re: How to run NX Open .NET C# program

@JimB @cowski1

 

When I run the code by Alt+F11, It gives compile error. So what additional steps are required with this code to run it without error.

 

error.PNG

 

 

// This program collects all loaded components 
// from the assembly tree and copies them all 
// into the specified target directory. 

using System; 
using System.Collections; 
using System.IO; 
using System.Windows.Forms; 
using NXOpen; 
using NXOpen.Assemblies; 
using NXOpen.UF; 

class copy_assembly_to_target_directory 
{ 
    
    private static Session s = Session.GetSession(); 
    private static UFSession ufs = UFSession.GetUFSession(); 
    private static ListingWindow lw;
    
    public static void Main() 
    { 
        lw = s.ListingWindow; 
        ArrayList partlist = new ArrayList(); 
        string foldername = ""; 
        
        lw.Open(); 
        if ((select_directory(ref foldername) != DialogResult.OK)) { 
            lw.WriteLine("Input canceled...exit."); 
            return; 
        } 
        else { 
            lw.WriteLine("Target directory " + foldername); 
        } 
        
        Component root = s.Parts.Display.ComponentAssembly.RootComponent; 
        Part c_part = (Part)root.Prototype; 
        partlist.Add(c_part.FullPath); 
        
        AddComponentsToList(root, ref partlist); 
        
        CopyComponentsInList(partlist, foldername); 
        partlist.Clear(); 
        
    } 
    
    public static void AddComponentsToList(Component comp, ref ArrayList plist) 
    { 
        Part c_part = null; 
        int loaded; 
        bool modified; 
        bool nxman = false; 
        string cp_name = null; 
        
        foreach ( Component child in comp.GetChildren()) { 
            cp_name = child.DisplayName; 
            
            UFPart.LoadStatus loadStatus; 
            ufs.Assem.EnsureChildLoaded(ufs.Assem.AskInstOfPartOcc(child.Tag), out loadStatus); 
            if (loadStatus.failed == true) { 
                lw.WriteLine("Cannot load " + child.DisplayName); 
            } 
            else { 
                c_part = (Part)child.Prototype; 
                if(plist.Contains(c_part.FullPath) == false) plist.Add(c_part.FullPath); 
                AddComponentsToList(child, ref plist); 
            } 
            
        } 
    } 
    
    public static void CopyComponentsInList(ArrayList plist, string targetfolder) 
    { 
        
        IEnumerator en = plist.GetEnumerator(); 
        while (en.MoveNext()) { 
            FileInfo fi = new FileInfo((String)en.Current); 
            string targetfile = targetfolder + "\\" + fi.Name; 
            lw.WriteLine("Copying " + en.Current + " --> " + targetfile); 
            File.Copy((String)en.Current, targetfile, true); 
        } 
        
    } 
    
    
    public static DialogResult select_directory(ref String foldername) 
    { 
        
        FolderBrowserDialog fbd; 
        DialogResult result; 
        
        fbd = new FolderBrowserDialog(); 
        fbd.Description = "Select target directory"; 
        fbd.ShowNewFolderButton = true; 
        // start browsing at Desktop folder, uncomment for any other default directory 
        // fbd.SelectedPath = GetEnvironmentVariable("UGII_BASE_DIR"); 
        // fbd.SelectedPath = "c:\\temp\\target";
        result = fbd.ShowDialog(); 
        foldername = fbd.SelectedPath; 
        fbd.Dispose(); 
        return result; 
        
    } 

    public static int GetUnloadOption(string arg)
    {
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
    }
    
    
} 

 

 

Ganesh Kadole, QA Analyst (PLM), SQS
Testing: NX 10 | NX 11 | Teamcenter-NX Integration
Teamcenter 11.2 | TC Vis 11.3 | Active Workspace 3.2

Re: How to run NX Open .NET C# program

Phenom
Phenom

The error that stands out to me the most is "'Class' statement must end with a matching 'End Class'".

It appears that some code is missing; perhaps a copy & paste error.

Re: How to run NX Open .NET C# program


cowski1 wrote:

The error that stands out to me the most is "'Class' statement must end with a matching 'End Class'".

It appears that some code is missing; perhaps a copy & paste error.


https://solutions.industrysoftware.automation.siemens.com/view.php?sort=desc&pd=nx&file_type=html&q=...

 

This is the link of the code.

Ganesh Kadole, QA Analyst (PLM), SQS
Testing: NX 10 | NX 11 | Teamcenter-NX Integration
Teamcenter 11.2 | TC Vis 11.3 | Active Workspace 3.2

Re: How to run NX Open .NET C# program

Esteemed Contributor
Esteemed Contributor

You need to set the correct journal language through File => Preferences => User Interface.

For C# it should be set as shown below.C# Journal PreferencesC# Journal Preferences

You will have to change the Journal language when ever you use a different one.

There should be converters available on the net to convert C# source code to VB.NET

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.0, FBM, MRL 3.1.4 | TcUA 10.1 MP7 Patch 0 (10.1.7.0) | TcVis 10.1
Development: VB.NET, Tcl/Tk    Testing: NX11.0 EAP, NX12.0 EAP

How to Get the Most from Your Signature in the Community

Re: How to run NX Open .NET C# program

Siemens Phenom Siemens Phenom
Siemens Phenom

It is only necessary to change the Journal language preference for recording. Any type of journal (VB, VC#, Python) can be played back at any time without changing the journal language preference.

Re: How to run NX Open .NET C# program


Stefan_Pendl wrote:

You need to set the correct journal language through File => Preferences => User Interface.

For C# it should be set as shown below.C# Journal PreferencesC# Journal Preferences

You will have to change the Journal language when ever you use a different one.

There should be converters available on the net to convert C# source code to VB.NET


Thank you Stefen, now it works. Smiley Happy

 

What about "Open C API program"? 'C' is not in the list.

 

Ganesh Kadole, QA Analyst (PLM), SQS
Testing: NX 10 | NX 11 | Teamcenter-NX Integration
Teamcenter 11.2 | TC Vis 11.3 | Active Workspace 3.2

Re: How to run NX Open .NET C# program

Phenom
Phenom

"What about "Open C API program"? 'C' is not in the list."

 

The C/C++ and Java languages are not supported for journal recording or playback. You must compile C/C++ and Java code to run it in NX (as mentioned by @JimB above).

 

Edit: C/C++ is supported for recording, but not playback. Thanks to @Ken_A for the correction.