a collection of NXOpen Python tutorials
To start with NX Python programming, you need to prepare your NX settings. The 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 programming 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 window 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 users 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
Fig.6 - Information box window.