a collection of NXOpen Python tutorials
In order to start with NX Python programing you need to prepare your NX settings. First step is to find the Developer
tab at the top NX ribbon:
Fig.1 - Find or activate the Developer tab.
if the tab does not exists already, right click on the top ribbon and find the Developer
tab and activate it.
One great way to develop or learn NX Python programing is to record macros while using the GUI. By default NX saves macros in Visual Basic. To change that to Python, you need to go to File > Preferences > User Interface
:
Fig.2 - Go to the User Interface Preference panel.
or press "Ctrl + 2" keyboard shortcut. Then in the User Interface Preferences
change the Journal Language
to Python:
Fig.3 - Change the Journal Language to Python.
Let's start with a very simple "Hello World!" example where a Message windows will show a text message (i.e., string
).
#ex0001.py
import NXOpen.UF
def main():
NXOpen.UF.UFSession.GetUFSession().Ui.DisplayMessage("Hello World!", 1)
if __name__ == '__main__':
main()
to run the code you need to go to the Developer
tab and find the Play
button:
Fig.4 - Find the Play button.
In the Journal Manager
panel you can Browse
to find and run the code:
Fig.5 - Find the Play button.
The result will be as the picture below:
Fig.6 - Message box window.
The message box window is good for alerting the using of specific issues, however the texts are not searchable or selectable. Alternatively, we could use:
#ex0002.py
import NXOpen
def main():
listing_window = NXOpen.Session.GetSession().ListingWindow
listing_window.Open()
listing_window.WriteFullline("Hello world!")
listing_window.Close()
if __name__ == '__main__':
main()
and the result must be
pic6_NX_information_window
Fig.6 - Information box window.