cancel
Showing results for 
Search instead for 
Did you mean: 

Launching an NX Open python script in NX Open c++ program

Siemens Pioneer Siemens Pioneer
Siemens Pioneer

Hi,

 

I created an NX Open program in c++ to automate some tasks

Then I also created a python script to export screenshots in NX Open. 

 

I would like to automate this process. Is it possible to run a python NX Open script within the NX Open program that was written in C++? If so, what command can I use to launch the .py script?

 

Thanks,

Elaine

11 REPLIES

Re: Launching an NX Open python script in NX Open c++ program

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor
You can try NXOpen:Smiley Frustratedession::Execute()

http://www.eng-tips.com/viewthread.cfm?qid=327153 list GTAC sample code to use this function
SE-Advanced
Siemens Industry Software

Re: Launching an NX Open python script in NX Open c++ program

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Well this can be done using Execute method on Session.

 

I have python sample code below which execute other python script. You can write File1 in langauge of your chocie and execute python journal from it.

 

File1.py

#File1.py
import NXOpen
import traceback

import os


def main():
    try:
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Called main from File1")
        
        path = os.path.dirname(__file__)+'\File2.py'
        
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, path)
        
        parameters = []
        
        NXOpen.Session.GetSession().Execute(path, "NXJournal", "main", parameters)
        
    except Exception as ex:
        lw = NXOpen.Session.GetSession().ListingWindow
        lw.Open()
        lw.WriteLine(str(ex))


if __name__ == '__main__':
    main()
    

Fil2.py (Journal to be executed, note that this is in same location as file1 or dll which will execute it)

#File2.py
import NXOpen

def main():
    try:
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Called main from File2")       
    except Exception as ex:
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Exception in main from File2")


if __name__ == '__main__':
    main()
    
SE-Advanced
Siemens Industry Software

Re: Launching an NX Open python script in NX Open c++ program

Siemens Pioneer Siemens Pioneer
Siemens Pioneer

Thank you for the responses!

 

My implemenetation is in c++. I looked at the Class reference:

 

https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx_api/en_US/custom/open_c++_r...

 

And cannot find the execute function. Any example to do that in C++?

Re: Launching an NX Open python script in NX Open c++ program

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Well Execute is only available in (.Net Java and Python).

 

For C++ ExecuteManagedProgram is added in NX10.0.2

 

https://docs.plm.automation.siemens.com/data_services/resources/nx/11/nx_api/custom/en_US/open_c++_r...

 

Following is sample code,

Create New NXOpen C++ Project using NXOpen C++ Wizard (As Internal application to create dll, Entry point as Explicitly (ufusr) )

 

in do_it method add code below, provide full path of .py file to be executed.

 

void TestExecuteManagedLib::do_it()
{
    std::vector<NXOpen::NXString> parameters;

    NXOpen::Session::GetSession()->ExecuteManagedProgram("<FULL PATH TO PY FILE>", "main", parameters);

	
}
SE-Advanced
Siemens Industry Software

Re: Launching an NX Open python script in NX Open c++ program

Siemens Pioneer Siemens Pioneer
Siemens Pioneer

Thanks all! It's working fine now.

Re: Launching an NX Open python script in NX Open c++ program

Siemens Pioneer Siemens Pioneer
Siemens Pioneer

Actually I have an additional question. How can I assess the journal argument in the nxopen python script? I see example on vb (args()), but I couldn't find anything related to python.

 

Really appreciate that!

Re: Launching an NX Open python script in NX Open c++ program

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor
It is essentially the same, parameters in two lines below is to provide input argument to script being invoked.

parameters = []

NXOpen.Session.GetSession().Execute(path, "NXJournal", "main", parameters)

You can put your arguments as Strings (just like argv()) and they will be added in input arguments to script being invoked.

Number of arguments in Script and Number of elements in Parameters shall match, else you will receive error.
SE-Advanced
Siemens Industry Software

Re: Launching an NX Open python script in NX Open c++ program

Siemens Pioneer Siemens Pioneer
Siemens Pioneer

Thanks! Here is my python script. the goal is to read a file with  ___<aNumber>.xx   in with python script. And I want 'aNumber' to be an argument to be passed.

 

def main(aNumber) :

    theSession  = NXOpen.Session.GetSession()
    UFSession = NXOpen.UF.Disp()

    theSession.ListingWindow.Open()
    theSession.ListingWindow.WriteLine(str(aNumber))

 

    ## lines to read a file by using aNumber ##

 

if __name__ == '__main__':
    main(aNumber)

 

 

In "Journal Arguments" box I tried putting:

1

"1"

aNumber = 1

 

Still I cannot pass the argument to the main function. I must be doing something stupid. What am I doing wrong?

 

Thanks!

 

 

 

 

 

Re: Launching an NX Open python script in NX Open c++ program

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Here is how it is done

 

C++ code..

void MyClass::do_it()
{
    NXOpen::NXString arg1 = "1";
    NXOpen::NXString arg2 = "String argument";

    std::vector<NXOpen::NXString >parameters;
    parameters.push_back(arg1);
    parameters.push_back(arg2);

    NXOpen::Session::GetSession()->ExecuteManagedProgram("..\\File2.py", "main", parameters);
}

Python File2.py

#File2.py
import NXOpen
import sys

def main():
    try:
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Called main from File2")    
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, str(sys.argv))           
    except Exception as ex:
        NXOpen.UI.GetUI().NXMessageBox.Show("Test Code", NXOpen.NXMessageBox.DialogType.Information, "Exception in main from File2")


if __name__ == '__main__':
    main()
    
SE-Advanced
Siemens Industry Software