This is an example project showing how nxlib is typically used to extract data from NX models using the NXOpen Python API. The general pattern that's used is as follows:
- Define a structure for the data that comes from NX.
- Open an NX model and serialize the data.
- Perform post-processing on the serialized data.
Problem Statement: A hidden message
A mysterious engineer from CFS has left us an NX .prt file, and we suspect there is a hidden message in it. We open the file, and it's just a bunch of random points. No matter what way we look at it, we can't seem to make heads or tails out of it.
We do know that CFS likes to work in poloidal coordinates because they eat a lot of donuts over there are making a donut-shaped device, so maybe if we converted each point from cartesian to poloidal coordinates we'd learn something. Let's give it a try.
We'll walk through the steps to unravel this mystery one by one. All of the code in this example repository should be runnable on a Windows machine with a working NX installation.
We use uv to package and manage our Python projects. If you don't already have uv, we recommend getting it setup on your system prior to proceeding.
The first step is to clone the repository and move into the example directory:
git clone git@github.com:cfs-energy/nxlib.git
cd nxlib/exampleNow we'll install the example project with uv, and make sure that the NX Python interpreter can see nxlib:
uv sync # Creates a virtual environment and installs all dependencies, including nxlib
uv run nxlib install # Symbolically link the nxlib installation to the NX interpreter pathIf all of that went well, you should be able to run
uv run nxlib statusand see that nxlib has been successfully symlinked.
Since we'll only be extracting points, we can extract them to a JSON file. While this approach is more data-intensive than a simple CSV, it does showcase how more complex geometric elements can be extracted & serialized from NX. The resulting data will look like this:
{
"points": [
{
"coords": [ 12.454430467617625, 15.648870940974554, 99.0 ],
"_type": "Point3d"
},
// ...
]
}The code that NX runs is in nx_get_points.py. It's standard practice to prefix NX journals with nx_ so we know they're separate from regular Python code. You'll note that this file does import from nxlib.nxopen, so trying to run it in our local environment will raise an error when it tries to eventually import NXOpen.
We can use argparse on our NX Journal to pass in command line arguments. This creates a clean & flexible interface with the main script for calling the journal.
Now that we've serialized the data, we can perform the transformation from cartesian to poloidal coordinates using numpy, and plot the result using matplotlib. main.py acts as the singular entry point for the entire project, as it calls the run_journal function to execute the journal. Everything happens headlessly without needing to manually execute the journal through the GUI.
We can run the script and see the result with a single command:
uv run python main.py assets/point-cloud.prt result.png --previewIf everything is working correctly, you should see the secret message open as an image on your computer.
While this example is at the same time simple and contrived, it does show how we typically build complex workflows that involve NX journals. The strength of nxlib is the capability to seamlessly integrate NX journals to solve complex problems that can't be done within NX alone.
