cancel
Showing results for 
Search instead for 
Did you mean: 

NXOpen WindowsForms in C#

Creator
Creator

Hi guys,

 

Iam currently starting to work with NXOpen in Siemens NX10 and Microsoft Visual Studio 2012. So Ive used the given templates by NX to start a new project.

 

Right now Iam trying to simply open a Windows Form, get some user interaction (for example: Entering a number) and save it to NX.

 

Iam saving the apps as .dlls and start them by a custom button in NX. Ive managed to open up my windows form but the problem is: Once terminated, I cannot reopen the form by using the button. Instead Iam getting an error from NX to look up the protocol, which sais the following:

 

"Loaded assembly: System.Windows.Forms.resources, Version=4.0.0.0, Culture=de, PublicKeyToken=b77a5c561934e089 from C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms.resources\v4.0_4.0.0.0_de_b77a5c561934e089\System.Windows.Forms.resources.dll
Caught exception while running: Main
System.InvalidOperationException: SetCompatibleTextRenderingDefault muss aufgerufen werden, bevor das erste IWin32Window-Objekt in der Anwendung erstellt wird.
bei System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(Boolean defaultValue)
bei formtest.Program.Main(String[] args) in c:\Users\evilmaker\Documents\Visual Studio 2012\Projects\formtest\formtest\Program.cs:Zeile 55."

 

Sorry for the german. The problem is somehow "SetCompatibleTextRenderingDefault". I will give you my code right here:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using NXOpen;
using NXOpen.UF;

namespace formtest
{
public class Program
{
// class members
private static Session theSession;
private static UI theUI;
private static UFSession theUfSession;
public static Program theProgram;
public static bool isDisposeCalled;

 

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
public Program()
{
try
{
theSession = Session.GetSession();
theUI = UI.GetUI();
theUfSession = UFSession.GetUFSession();
isDisposeCalled = false;

}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
}
}

//------------------------------------------------------------------------------
// Explicit Activation
// This entry point is used to activate the application explicitly
//------------------------------------------------------------------------------
public static int Main(string[] args)
{
int retValue = 0;
try

{
theProgram = new Program();

//Starten der Form
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----

}
return retValue;
}

//------------------------------------------------------------------------------
// Following method disposes all the class members
//------------------------------------------------------------------------------
public void Dispose()
{
try
{
if (isDisposeCalled == false)
{
//TODO: Add your application code here
}
isDisposeCalled = true;
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----

}
}

public static int GetUnloadOption(string arg)
{
//Unloads the image explicitly, via an unload dialog
//return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);

//Unloads the image immediately after execution within NX
// return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);

//Unloads the image when the NX session terminates
return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
}

}
}

 

Maybe someone can help me how to properly initialize a windows form. Which mistake am I doing? Is there some sort of tutorial somewhere?

 

 

4 REPLIES

Re: NXOpen WindowsForms in C#

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

I suggest to change "Unload Options" to: Immediately.

In GetUnloadOption(string arg) proc:
comment out line:
return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
and uncomment:
return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);

Marek Pawlus, NCmatic

Production: NX 11.0.2
Development: C#, Tcl/Tk, CSE

Re: NXOpen WindowsForms in C#

Creator
Creator

Thank you so much! That worked.

 

May I ask if Iam seeing this correctly:

 

Before you suggestion NX unloaded the code only after termination of NX itself and now it unloads it immediately after execution in NX?

Re: NXOpen WindowsForms in C#

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

I imagine it's behavior the same way as you described.

What's more, using unload/at termination, if you use in NX dll from where it is located in Visual Studio project (bin\Debug or bin\Release folder), you will be not able to compile your project again in VS, until you close NX.

 

You can find whole explanation (quite detailed) in NX Doc, in:

Programming Tools --> NX Open --> NX Open Programmer's Guide --> Executing NX Open Automation

Marek Pawlus, NCmatic

Production: NX 11.0.2
Development: C#, Tcl/Tk, CSE

Re: NXOpen WindowsForms in C#

Siemens Phenom Siemens Phenom
Siemens Phenom
There are several tutorial examples that use Windows forms in the first few chapters of the "Getting Started with SNAP" guide. They use The VB language, but translation into C# is trivial.

Depending on what you want to do, you might find that WinForms are a dead end. You can enter numbers and text, but selecting NX objects is going to be very difficult. You might want to look at NX Block-Based dialogs, instead. These are also covered in the SNAP guide.
山田
yamada