Cancel
Showing results for 
Search instead for 
Did you mean: 

Debug NX Open code through external exe application

Valued Contributor
Valued Contributor

Hello,

I am trying to use external application (.exe console application), created by NX11_VB_Wizard, with NX11.

This is simple application with single module bellow:

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

    '  Explicit Activation
    '      This entry point is used to activate the application explicitly
    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUfSession As UFSession = UFSession.GetUFSession()

        ' TODO: Add your application code here 
        Dim pointTag As NXOpen.Tag = Tag.Null
        Dim point1() As Double = {10.0, 20.0, 0.0}

        theUfSession.Curve.CreatePoint(point1, pointTag)

        MsgBox("Point is created!")

    End Sub

End Module

While NX Part Session is opened, loading application through NX (File >Execute>NX Open) works fine.

What I would like to do is to execute code by double click on the exe or through Visual Studio 2015>Debug.

I have received some errors:

  1. An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll - Solved by copying all libraries from "C:\Program Files\Siemens\NX 11.0\NXBIN\managed" to debug folder.
  2. An unhandled exception of type 'System.DllNotFoundException' occurred in NXOpen.dll
    Additional information: Unable to load DLL 'libpart': The specified module could not be found. (Exception from HRESULT: 0x8007007E) - I have tried to solve by copying "libpart.dll" from C:\Program Files\Siemens\NX 11.0\NXBIN to debug folder. Didn't solve the error Smiley Sad .

I have also tried by setting the Environment Variables:

UGII_BASE_DIR=C:\Program Files\Siemens\NX 11.0

UGII_ROOT_DIR=C:\Program Files\Siemens\NX 11.0\NXBIN

UGII_USER_DIR=C:\Users\<user>\Desktop\NX_external_app\NX_external_app\NX_external_app\bin\Debug

 

Also, I have added a new path into Path variable (both User and System) for %UGII_ROOT_DIR%.

More info:

  1. Target CPU is set to x64.
  2. .Net Framework is set to version 4.6

Nothing solved the error. Do you have some idea how to solve?

 

Any help is very appreciated!

 

Sincerely,

Danijel

13 REPLIES

Re: Debug NX Open code through external exe application

Siemens Phenom Siemens Phenom
Siemens Phenom

You should execute from a NX command prompt to ensure that all of the necessary env variables are set correctly. If you examine the NX Command prompt shortcut, you will see it does something like this:

 

C:\WINDOWS\system32\cmd.exe /k C:\apps\Siemens\NX\11.0\ugii\ugiicmd.bat "C:\apps\Siemens\NX\11.0" AUTO

Also see the help topic here:

 

Executing Batch Applications

Re: Debug NX Open code through external exe application

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

You need to open or create a part file in your code, since you cannot create objects without a work part.

Remember that NX will be started in the "no part" application and you cannot display message boxes or interact with the UI in batch mode either.

You need something like below before creating objects:

Dim DummyPart As BasePart = theSession.Parts.NewBaseDisplay("dummy", Part.Units.Millimeters)

Hope this helps.

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.2, FBM, MRL 3.1.7 | TcUA 10.1 MP7 Patch 0 (10.1.7.0) | TcVis 11.4
Development: C (ITK), .NET, Tcl/Tk Testing: NX12.0 | AWC 3.4 Preparing: NX12.0

Employees of the customers, together we are strong Smiley Wink
How to Get the Most from Your Signature in the Community
NX Customization - Best Practice Guide

Re: Debug NX Open code through external exe application

Valued Contributor
Valued Contributor

Hello @JimB,

Thank you for your reply.

Yes, I have already tried to run executable by following instructions from the NX Open Programmer's Guide.

It says:

An NX Open application .exe file can be executed directly from a command line as any other executable. Since this is a managed application, you will need to do one of the following:

  • Copy the NX .NET libraries to your local working directory. To do so, copy all of the libraries from the %UGII_BASE_DIR%\ugii\managed directory to your working directory. Use standard operating system command to execute the application.

Tried this way. What I noticed as a difference, specified folder "managed" is not a member of "UGII" directory (C:\Program Files\Siemens\NX 11.0\UGII), but "NXBIN" directory (C:\Program Files\Siemens\NX 11.0\NXBIN\managed). This is by default NX11.0 installation. Please correct me if I am wrong.

 

  • Copy your .EXE to UGII_BASE_DIR\ugii\managed. Use standard operating system command to execute the application.

Tried also to copy my exe application to “C:\Program Files\Siemens\NX 11.0\NXBIN\managed”  

 

  • Use run_managed.exe (%UGII_BASE_DIR%\ugii\run_managed.exe)

By running NX 11.0 Command prompt, I have tried by typing:

 

run_managed “C:\Users\<user>\Desktop\NX_external_app\NX_external_app\NX_external_app\bin\Debug\ NX_external_app.exe”

It seems that code is executed but nothing happened in my NX environment.

 

Also tried with dirrectory where I copied exe application by following Step 2 above:

run_managed “C:\Program Files\Siemens\NX 11.0\NXBIN\managed\NX_external_app.exe”

The same result.

Re: Debug NX Open code through external exe application

Valued Contributor
Valued Contributor

Hi @Stefan_Pendl,

Yes, it's my mistake. So I have added new line:

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

    '  Explicit Activation
    '      This entry point is used to activate the application explicitly
    Sub Main()

        Dim theSession As Session = Session.GetSession()

Dim DummyPart As BasePart = theSession.Parts.NewBaseDisplay("dummy", Part.Units.Millimeters) Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim pointTag As NXOpen.Tag = Tag.Null Dim point1() As Double = {10.0, 20.0, 0.0} theUfSession.Curve.CreatePoint(point1, pointTag) End Sub End Module

When I run exe by double click or through Visual Studio, NX doesn't start. The NX only starts if I set Start Action of debug mode to  Start external program: "C:\Program Files\Siemens\NX 11.0\NXBIN\ugraf.exe"

I am trying to figure out what I missed.

 

Re: Debug NX Open code through external exe application

Legend
Legend

Maybe I just don't get the clue what you want to do but:

If you start your external application (eg. by double-click on the exe) you will never start a visible NX session. Your program has its own NX session. You will see nothing (unless your programm has its own GUI). Your sample program just creates a part, creates a point and finishes. If you add a save you will see the resulting part on your file system.

 

Regards,

Jan

Jan Böttcher

Re: Debug NX Open code through external exe application

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

To debug your own standalone NX application use the following start command:

"%UGII_BASE_DIR%\NXBIN\run_managed.exe"

Do not copy any NX Open DLL at all, run_managed will make sure the DLLs will be found properly.

You only need UGRAF.exe if you are debugging NX Open DLLs.

Sorry, I am not of much help here, since I never have a need to use the debugger, theSession.LogFile.Write() is my debugger.

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.2, FBM, MRL 3.1.7 | TcUA 10.1 MP7 Patch 0 (10.1.7.0) | TcVis 11.4
Development: C (ITK), .NET, Tcl/Tk Testing: NX12.0 | AWC 3.4 Preparing: NX12.0

Employees of the customers, together we are strong Smiley Wink
How to Get the Most from Your Signature in the Community
NX Customization - Best Practice Guide

Re: Debug NX Open code through external exe application

Creator
Creator

when i was using solidworks, i used to create userform with one button with the below code in that button to create the instance of solidworks.can we use the same for nx ?

SldWorks swApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SolidWorks.Application")); 

 

Re: Debug NX Open code through external exe application

Valued Contributor
Valued Contributor

Hello @ijb,

When NX session is runned and I have created new part, I start executing my application from Visual Stutio>Debug (or just double click on exe file), I expect that code will be executed and as the result in the currently opened part I expect to see created point (something that my module does). That's it.

Re: Debug NX Open code through external exe application

Legend
Legend

If you have an external application as created by the wizard you will never be able to connect to a running interactive NX session.

2019-01-10 09_19_49-NX12 Open C# Wizard - NX12_Open_CS_Wizard1.jpg

In fact in  your case you will have two parallel NX sessions (the interactive one and the one you started with your application). You can find both in the task manager and both sessions have their own syslog file. They have nothing to do with each other.

 

To debug your external application just set breakpoints and run it from visual studio in debug mode.

 

If you run your exe from an interactive NX session by Execute/NX Open it is attached as a regular .net dll to your interactive session. In this case it behaves just like an internal application. Debug internal applications by starting ugraf.exe as debug task or attach the debugger to a running ugraf.exe. Then execute your application by Execute/NX Open.

 

There is a third type of application where an external process communicates with an existing (interactive or non interactive session). Look for 'remoting' in the documentation. But I don't think that is what you want.

 

Regards,

Jan

 

 

 

Jan Böttcher