100% found this document useful (1 vote)
398 views5 pages

NXOpen Python: Getting Started Guide

This document provides an introduction to using NXOpen with Python, detailing the requirements, methods for running scripts, and basic script structure. It includes examples for creating geometry, sketches, features, and transformations, along with debugging tips and resources for further learning. The content is designed to help users automate and extend Siemens NX capabilities using Python.

Uploaded by

132101003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
398 views5 pages

NXOpen Python: Getting Started Guide

This document provides an introduction to using NXOpen with Python, detailing the requirements, methods for running scripts, and basic script structure. It includes examples for creating geometry, sketches, features, and transformations, along with debugging tips and resources for further learning. The content is designed to help users automate and extend Siemens NX capabilities using Python.

Uploaded by

132101003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GETTING STARTED WITH NXOPEN IN PYTHON

1. Introduction

NXOpen is the official API provided by Siemens NX that allows automation, scripting, and extension
of NX capabilities using languages such as Python, C++, and Java. Python is one of the easiest
ways to start working with NXOpen because NX provides a built■in Python interpreter.

2. Requirements

- Siemens NX installed

- NXOpen Python environment (built■in)

- Basic Python knowledge

3. Running Python Scripts in NX

There are three main ways:

1. File → Execute → NX/Open → Python

2. Journal Replay (automatically generated Python code)

3. External Python interpreter with environment variables:

- UGII_ROOT_DIR

- UGII_BASE_DIR

- PATH includes NX libs

4. Basic Structure of an NXOpen Python Script

Every NXOpen Python script follows the same pattern:

import NXOpen

def main():

the_session = [Link]()

work_part = the_session.[Link]

display_part = the_session.[Link]

if __name__ == "__main__":

main()

The “commit()” function is used to finalize and apply builder operations.


5. Creating Basic Geometry

Example: Create a line

import NXOpen

def main():

the_session = [Link]()

work_part = the_session.[Link]

pt1 = NXOpen.Point3d(0, 0, 0)

pt2 = NXOpen.Point3d(100, 0, 0)

line = work_part.[Link](pt1, pt2)

[Link]("MyLine")

if __name__ == "__main__":

main()

6. Sketch Creation Example

def main():

the_session = [Link]()

work_part = the_session.[Link]

sketch_builder = work_part.[Link](None)

# Define plane

plane = work_part.[Link](

NXOpen.Point3d(0,0,0),

NXOpen.Vector3d(0,0,1),

NXOpen.Vector3d(1,0,0)

sketch_builder.PlaneReference = plane
sketch = sketch_builder.Commit() # commit() finalizes the builder

sketch_builder.Destroy()

# Draw in sketch

work_part.[Link](sketch)

pt1 = NXOpen.Point3d(0, 0, 0)

pt2 = NXOpen.Point3d(0, 100, 0)

line = work_part.[Link](pt1, pt2)

work_part.[Link]()

if __name__ == "__main__":

main()

7. Creating Features (Extrude Example)

def main():

the_session = [Link]()

work_part = the_session.[Link]

# Create a rectangle sketch

# ... sketch creation code

extrude_builder = work_part.[Link](None)

extrude_builder.DistanceTolerance = 0.001

extrude_builder.[Link] =
[Link]

# Select curves

extrude_builder.[Link](sketch_curves)

extrude_builder.[Link] = 0

extrude_builder.[Link] = 50

extrude_feature = extrude_builder.Commit()

extrude_builder.Destroy()
if __name__ == "__main__":

main()

8. Understanding NXOpen Builders

Most NX operations use "builders":

- Create a builder

- Set parameters

- Call commit()

- Destroy builder

Example:

sketch = sketch_builder.Commit()

sketch_builder.Destroy()

commit() finalizes the operation; without it, nothing is created in the part.

9. Selecting Objects

To select objects:

ui = [Link]()

selection = [Link]

You can select faces, edges, curves, and bodies.

10. Transformations Example

def main():

the_session = [Link]()

work_part = the_session.[Link]

line = work_part.[Link](

NXOpen.Point3d(0,0,0),

NXOpen.Point3d(50,0,0)

)
matrix = NXOpen.Matrix3x3()

[Link] = 0; [Link] = -1; [Link] = 0

[Link] = 1; [Link] = 0; [Link] = 0

[Link] = 0; [Link] = 0; [Link] = 1

[Link](matrix)

if __name__=="__main__":

main()

11. Exporting Files (STEP, STL, etc.)

def export_stl():

the_session = [Link]()

work_part = the_session.[Link]

stl_builder = work_part.[Link]()

stl_builder.OutputFile = "[Link]"

stl_builder.Commit()

stl_builder.Destroy()

12. Debugging NXOpen Scripts

- Use NX log window

- Use try/except blocks

- Use print() → visible in NX syslog

13. Learning More

- NXOpen API Reference inside NX installation

- Recording journals

- Siemens documentation

This document gives you all fundamentals plus working examples to start using NXOpen in Python.

Common questions

Powered by AI

An NXOpen Python script to create a simple geometry like a line follows a specific pattern. The script starts by importing the NXOpen module and defining a main() function. Within main(), a session is obtained using NXOpen.Session.GetSession(), and the work part is accessed via the_session.Parts.Work. Geometry points are defined using NXOpen.Point3d, and a line is created using work_part.Curves.CreateLine(pt1, pt2). The 'commit()' function finalizes the operation, ensuring the builder's operations are applied, as without committing, changes are not created in the part .

Python scripts in Siemens NX using NXOpen can be executed in three main ways: 1) through File → Execute → NX/Open → Python, 2) by Replay of automatically generated journal Python code, and 3) using an external Python interpreter set up with specific environment variables. For the third method, the necessary environment variables include UGII_ROOT_DIR, UGII_BASE_DIR, and ensuring that PATH includes NX libraries .

NXOpen scripts manage transformations on geometric objects by applying transformations like translation, rotation, and scaling through matrices. For instance, a line is created using work_part.Curves.CreateLine() between two points. To transform, a matrix (3x3) is initialized with values defining the transformation. For example: matrix.Xx = 0; matrix.Xy = -1; matrix.Xz = 0; matrix.Yx = 1; matrix.Yy = 0; matrix.Yz = 0; matrix.Zx = 0; matrix.Zy = 0; matrix.Zz = 1. This matrix represents a 90-degree rotation around the z-axis and is applied to the line using line.Transform(matrix). This process allows methodical transformation of geometrical entities .

To debug NXOpen scripts and enhance reliability, several resources and methods are recommended. The NX log window is utilized for real-time debugging information. Incorporating try/except blocks into the script helps manage exceptions and potential errors gracefully. Using print() functions for logging outputs to the NX syslog aids in tracking script execution and diagnosing issues. These methods provide a comprehensive approach to debugging scripts, ensuring robust script functionality .

In NXOpen scripts, object selections are managed through the use of the selection manager. A UI interface is first obtained with NXOpen.UI.GetUI(), followed by accessing the selection manager via ui.SelectionManager. This setup allows selection of various objects like faces, edges, curves, and bodies within NX. This tool is essential for interactive or automated processes involving object manipulation and provides a vital interface for further operations on selected entities in the script .

The 'commit()' function is crucial in NXOpen scripting as it finalizes and applies builder operations to the NX model. In the context of builder operations, 'commit()' converts the builder's set parameters and pending operations into actual features or geometries in the part model. Without executing 'commit()', operations do not reflect in the part, making it essential for realizing design manipulations. It ensures that modifications align with the intended part design, making it a pivotal part of the builder pattern used consistently across NXOpen scripts .

To gain more knowledge about the NXOpen API and improve scripting skills, various resources are recommended. The NXOpen API Reference, available within the NX installation, is a key resource. Additionally, users can learn by recording journals that create scripts from user interactions. Siemens documentation is also mentioned as a vital resource for in-depth understanding and advanced learning about NXOpen capabilities .

In NXOpen, methodologies for exporting files involve the creation of builders specific to the desired file format. For exporting in STL format, you obtain the session and work part using NXOpen.Session.GetSession() and the_session.Parts.Work. An STL builder is created with work_part.FacetManager.CreateStlBuilder(). The output file path is set via stl_builder.OutputFile. To complete the export, stl_builder.Commit() is called, finalizing and saving the STL file. Finally, the builder is destroyed with stl_builder.Destroy() to manage resources efficiently .

To create a sketch on a specific plane using NXOpen with Python, you start by obtaining a session with NXOpen.Session.GetSession() and getting the work part via the_session.Parts.Work. A sketch builder is initiated with work_part.Sketches.CreateSketchBuilder(None). A plane is defined using work_part.Planes.CreatePlane() with specific point and vector parameters. The plane is referenced in the sketch builder, which is then committed to finalize creation using sketch_builder.Commit(). Activation of the sketch is done with work_part.Sketches.SetActiveSketch(sketch), allowing drawing in the sketch. After operations, the sketch is deactivated with work_part.Sketches.DeactivateSketch().

Creating an extrude feature in NXOpen demonstrates the use of builders, a fundamental pattern in NX operations. The process begins by obtaining a session and work part. A builder for extrusion is created using work_part.Features.CreateExtrudeBuilder(None). Parameters such as distance tolerance and Boolean operation type are set, and curves are selected through extrude_builder.Section.AddCurve(). Start and end limits are defined with extrude_builder.Limits.StartExtend.Value and extrude_builder.Limits.EndExtend.Value. The builder is finalized using extrude_builder.Commit(), crucial for applying changes, and then the builder is destroyed with extrude_builder.Destroy() to free resources. This pattern ensures structured creation and application of features .

You might also like