cfs-nxlib 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. cfs_nxlib-0.5.0/LICENSE.md +202 -0
  2. cfs_nxlib-0.5.0/PKG-INFO +227 -0
  3. cfs_nxlib-0.5.0/README.md +204 -0
  4. cfs_nxlib-0.5.0/pyproject.toml +84 -0
  5. cfs_nxlib-0.5.0/setup.cfg +4 -0
  6. cfs_nxlib-0.5.0/src/cfs_nxlib.egg-info/PKG-INFO +227 -0
  7. cfs_nxlib-0.5.0/src/cfs_nxlib.egg-info/SOURCES.txt +42 -0
  8. cfs_nxlib-0.5.0/src/cfs_nxlib.egg-info/dependency_links.txt +1 -0
  9. cfs_nxlib-0.5.0/src/cfs_nxlib.egg-info/entry_points.txt +3 -0
  10. cfs_nxlib-0.5.0/src/cfs_nxlib.egg-info/requires.txt +1 -0
  11. cfs_nxlib-0.5.0/src/cfs_nxlib.egg-info/top_level.txt +1 -0
  12. cfs_nxlib-0.5.0/src/nxlib/__init__.py +71 -0
  13. cfs_nxlib-0.5.0/src/nxlib/_status.py +226 -0
  14. cfs_nxlib-0.5.0/src/nxlib/exceptions.py +19 -0
  15. cfs_nxlib-0.5.0/src/nxlib/geometry.py +197 -0
  16. cfs_nxlib-0.5.0/src/nxlib/io.py +74 -0
  17. cfs_nxlib-0.5.0/src/nxlib/models/__init__.py +24 -0
  18. cfs_nxlib-0.5.0/src/nxlib/models/units.py +80 -0
  19. cfs_nxlib-0.5.0/src/nxlib/nxopen/__init__.py +19 -0
  20. cfs_nxlib-0.5.0/src/nxlib/nxopen/common.py +47 -0
  21. cfs_nxlib-0.5.0/src/nxlib/nxopen/component.py +37 -0
  22. cfs_nxlib-0.5.0/src/nxlib/nxopen/export.py +49 -0
  23. cfs_nxlib-0.5.0/src/nxlib/nxopen/nx_geometry.py +245 -0
  24. cfs_nxlib-0.5.0/src/nxlib/nxopen/nx_geometry.pyi +108 -0
  25. cfs_nxlib-0.5.0/src/nxlib/nxopen/part.py +147 -0
  26. cfs_nxlib-0.5.0/src/nxlib/nxopen/section.py +94 -0
  27. cfs_nxlib-0.5.0/src/nxlib/nxopen/serializers/__init__.py +15 -0
  28. cfs_nxlib-0.5.0/src/nxlib/nxopen/serializers/attributes.py +142 -0
  29. cfs_nxlib-0.5.0/src/nxlib/nxopen/teamcenter.py +57 -0
  30. cfs_nxlib-0.5.0/src/nxlib/nxopen/uf.py +95 -0
  31. cfs_nxlib-0.5.0/src/nxlib/nxtest/__init__.py +17 -0
  32. cfs_nxlib-0.5.0/src/nxlib/nxtest/_nx_run_test.py +68 -0
  33. cfs_nxlib-0.5.0/src/nxlib/nxtest/main.py +108 -0
  34. cfs_nxlib-0.5.0/src/nxlib/tc_auth.py +96 -0
  35. cfs_nxlib-0.5.0/src/nxlib/utility/__init__.py +14 -0
  36. cfs_nxlib-0.5.0/src/nxlib/utility/common.py +52 -0
  37. cfs_nxlib-0.5.0/src/nxlib/utility/install.py +159 -0
  38. cfs_nxlib-0.5.0/src/nxlib/utility/main.py +198 -0
  39. cfs_nxlib-0.5.0/src/nxlib/utility/run.py +212 -0
  40. cfs_nxlib-0.5.0/tests/test_auth.py +43 -0
  41. cfs_nxlib-0.5.0/tests/test_geometry_json_roundtrip.py +181 -0
  42. cfs_nxlib-0.5.0/tests/test_install.py +169 -0
  43. cfs_nxlib-0.5.0/tests/test_run.py +170 -0
  44. cfs_nxlib-0.5.0/tests/test_version.py +24 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,227 @@
1
+ Metadata-Version: 2.4
2
+ Name: cfs-nxlib
3
+ Version: 0.5.0
4
+ Summary: Reusable Python functions for the NXOpen Python API.
5
+ Author-email: Blair Frandeen <bfrandeen@cfs.energy>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/cfs-energy/nxlib
8
+ Project-URL: Repository, https://github.com/cfs-energy/nxlib
9
+ Project-URL: Issues, https://github.com/cfs-energy/nxlib/issues
10
+ Project-URL: Changelog, https://github.com/cfs-energy/nxlib/blob/main/CHANGELOG.md
11
+ Classifier: Operating System :: Microsoft :: Windows
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Topic :: Multimedia :: Graphics :: 3D Modeling
15
+ Classifier: Topic :: Software Development :: Libraries
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Classifier: Topic :: Software Development :: Testing :: Unit
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE.md
21
+ Requires-Dist: python-dotenv>=1
22
+ Dynamic: license-file
23
+
24
+ <!-- Copyright 2026 Commonwealth Fusion Systems (CFS), all rights reserved. -->
25
+ <!-- This entire source code file represents the sole intellectual property of CFS. -->
26
+ <!-- -->
27
+ <!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
28
+ <!-- you may not use this file except in compliance with the License. -->
29
+ <!-- You may obtain a copy of the License at -->
30
+ <!-- -->
31
+ <!-- http://www.apache.org/licenses/LICENSE-2.0 -->
32
+ <!-- -->
33
+ <!-- Unless required by applicable law or agreed to in writing, software -->
34
+ <!-- distributed under the License is distributed on an "AS IS" BASIS, -->
35
+ <!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
36
+ <!-- See the License for the specific language governing permissions and -->
37
+ <!-- limitations under the License. -->
38
+ # nxlib
39
+ Nxlib is a library and toolkit for enabling workflows that utilize the [NXOpen Python API](https://docs.sw.siemens.com/en-US/doc/209349590/PL20231101866122454.custom_api.nxopen_python_ref). Nxlib has the following core capabilities:
40
+
41
+ - ✅ Reusable library functions for common NX journaling operations.
42
+ - ✅ Utilities for ensuring that nxlib is discoverable by the NX Python interpreter.
43
+ - ✅ A test runner to enable unit and integration testing of NXOpen functions and modules.
44
+ - ✅ Capability to execute NX journals on a virtual machine for incorporation in CI workflows.
45
+
46
+ Simply put, nxlib enables automated data extraction from CAD, and much more.
47
+
48
+
49
+ You can learn the basics of using nxlib by working through the [example repository](example/).
50
+
51
+ # About NXOpen and Journals
52
+ An NX journal is a Python script that's run from within NX. A journal is run using the Python interpreter that is bundled into the NX executable.
53
+
54
+ What makes nxlib valuable is that it manages the interactions between two Python interpreters: the interpreter for your project, and the interpreter that's built into NX. The built-in NX interpreter is limited to Python 3.10 as of NX 2406, and it comes with no third-party packages. All code in the nxlib.nxopen library needs to be runnable in the NX interpreter. Therefore nxlib:
55
+
56
+ - Has zero production dependencies for code that runs with NXOpen.
57
+ - Is _developed and tested_ with Python 3.10.
58
+
59
+ However, nxlib can still be used as a dependency for projects that use a Python version >3.10, and for projects that depend on other third-party packages.
60
+
61
+ # Installation & Setup
62
+ ## Installation
63
+ Add `cfs-nxlib` to your project with uv:
64
+ ```shell
65
+ uv add cfs-nxlib
66
+ ```
67
+ or with pip:
68
+ ```shell
69
+ pip install cfs-nxlib
70
+ ```
71
+
72
+ ## Setting up your environment variables
73
+ NX and Teamcenter licensing and authentication rely on having several environment variables
74
+ set up correctly. Most of these environment variables should have been set up with your NX installation, but this is not always the case.
75
+
76
+ The [.env.sample](.env.sample) file ships with nxlib, and includes the relevant environment variables for using nxlib. Work with your system administrator to fill this out, rename it to `.env`, and place it in the root of your project.
77
+
78
+ ## Linking nxlib to the NX interpreter
79
+ Installing a library of functions for use with NXOpen is not as trivial as running `pip install cfs-nxlib`, since the Python interpreter used by NX is not the same as your system-level Python installation or your virtual environment. This presents the challenge of ensuring that `nxlib` will be importable by the NX Python interpreter from your specific NX journal.
80
+
81
+ The recommended approach is to symbolically link the `nxlib` library to `%UGII_BASE_DIR%/NXBIN/python`, which is on the import search path for the built-in NX Python interpreter. Simply run
82
+ ```shell
83
+ uv run nxlib install
84
+ ```
85
+ This will allow you to `import nxlib` from any NX Python journal, without needing to restart NX or modify any environment variables. This approach is not viable for distribution of standalone scripts.
86
+
87
+ You can verify that the `nxlib` installation was successful by running
88
+ ```shell
89
+ uv run nxlib status
90
+ ```
91
+
92
+ > ⚠️**CAUTION**:
93
+ > You may choose to install `nxlib` into a virtual environment, or it may be installed as a dependency for another project. However, the Python interpreter that runs your journals is still the one that's built into NX. This Python interpreter is **not** part of your virtual environment, and it will not by default be able to import any third party libraries.
94
+
95
+ > ⚠️**CAUTION**:
96
+ > While you can have multiple `nxlib` installations across virtual environments, you can only have *one* symbolic link to `%UGII_BASE_DIR%/NXBIN/python`. If you want to be sure of the nxlib installation you're using, you can run `nxlib status -v`, or you can make a runtime assertion that whichever `nxlib` you imported is the one that NX is using: `assert nxlib.status.nxlib_symlinked`.
97
+ >
98
+ > You can run `uv run nxlib install --overwrite` to update the symlink, or `uv run nxlib remove` to delete it.
99
+
100
+ ## Setting up Teamcenter Authentication
101
+ In addition to needing an NX installation to run, a Teamcenter license is required to run managed NX sessions. You can authenticate to Teamcenter using SSO, or with a username and password. Nxlib will try to authenticate via password if the corresponding `TC_USERNAME`, `TC_PASSWORD`, `TC_PASSWORD_FILE` environment variables are set. You can override this behavior by passing `--auth=[sso|password]` when running `nxlib run` or `nxtest`.
102
+
103
+ ### Single Sign-on (SSO)
104
+ Most end-users of nxlib will use SSO to authenticate. Authenticating with SSO requires having your environment variables set correctly. See the section on environment variables above for enabling SSO.
105
+
106
+ If you are using SSO, there is nothing you need to do except occasionally authenticate as you normally would when running NX.
107
+
108
+ ### Username and Password or Password File
109
+ If SSO is not an option, nxlib also supports using a Teamcenter username along with a password or encrypted password file. This is not a typical user case for end-users, and is intended to be used for when nxlib is run in CI or on a virtual machine.
110
+
111
+ To authenticate with a username and password, the `TC_USERNAME` environment variable must be set appropriately, along with at least one of `TC_PASSWORD` or `TC_PASSWORD_FILE`.
112
+
113
+ # How to run NX Journals
114
+ NX journals can be called from within another Python script, from the command line, or from within an existing NX session.
115
+
116
+ ## Running journals from another Python script
117
+ In your Python code, you can run:
118
+ ```python
119
+ from nxlib import run_journal
120
+
121
+ run_journal("path/to/journal.py", ["journal", "arguments"])
122
+ ```
123
+
124
+ ## Running from the command line
125
+ nxlib has a handy command line utility for executing journals. This utility executes journals headlessly, without launching the NX GUI. This makes execution faster for large assemblies, as they do not have to be rendered graphically.
126
+
127
+ You can run an existing journal:
128
+ ```shell
129
+ uv run nxlib run my_journal.py
130
+ ```
131
+ Or you can just run arbitrary Python code through NX:
132
+ ```shell
133
+ uv run nxlib run -c "from nxlib import nxprint; nxprint('Hello world')"
134
+ ```
135
+ Try `nxlib run --help` for the full set of options.
136
+
137
+ ### Running with a custom Python virtual environment
138
+ You can run journals using your own Python virtual environment, including the libraries installed there. This can be done using the `--local` flag on the CLI command, or by setting `local=True` in the `run_journal` function.
139
+
140
+ This has the limitation in that the minor Python version that NX runs must match that of your local interpreter. As of NX2406, that version is Python 3.10.
141
+
142
+ ## Running from within NX
143
+ To run from within NX:
144
+
145
+ 1. In NX, press Alt+F8 to run a journal
146
+ 2. Click "Browse..." and navigate to the journal file you'd like to run
147
+ 3. Enter any arguments from the journal, one per line, in the box at the bottom of the window.
148
+ 4. Click "Run".
149
+
150
+ # Development
151
+ [uv](https://github.com/astral-sh/uv) is recommended for development of nxlib.
152
+
153
+ ## Quickstart
154
+ ```shell
155
+ # Clone the repository
156
+ git clone https://github.com/cfs-energy/nxlib.git
157
+ ```
158
+ ```shell
159
+ # Install nxlib and all dependencies
160
+ uv sync --all-extras
161
+
162
+ # Sync nxlib to the NX Python interpreter
163
+ uv run nxlib install
164
+
165
+ # Run the nxlib unit tests
166
+ uv run pytest
167
+
168
+ # Run the NX-based unit tests
169
+ uv run nxtest
170
+ ```
171
+
172
+ ## Python Version
173
+ The Python version used to develop nxlib is constrained to the Python minor version that's built into NX. This ensures that all code will be runnable with the NX interpreter. The `.python-version` file ensures that the correct version is used.
174
+
175
+ As of NX 2406, the minor Python version that ships with NX is 3.10. To check the Python version that shipped with your version of NX, run
176
+
177
+ ```shell
178
+ uv run nxlib run --native -c "import sys; print(sys.version)"
179
+ ```
180
+
181
+ It's recommended to update the `.python-version` file to match your development environment.
182
+
183
+ ## Writing Tests
184
+ There are two types of functions you'll want to test: NX journaling functions, and everything else. Tests of NX journaling functions are those tests where one or more test assertions must be made from within the NX Python interpreter. Tests of non-journaling functions, especially those that rely on third-party packages, likely need to be run with your local Python interpreter.
185
+
186
+ In order to keep test discovery of the two types of tests exclusive, segregate the test files as follows:
187
+ - Tests for NX journaling functions should live in files that match the pattern `nxtest*.py`
188
+ - Tests for everything else should live in files that match the pattern `test*.py` (as is common for most test suites & runners).
189
+
190
+ To run all tests, you need to run two separate commands:
191
+
192
+ ```shell
193
+ uv run nxtest # Run tests on NX journaling functions
194
+ uv run pytest # Run tests on everything else
195
+ ```
196
+
197
+ The `nxtest` command runs Python's built in unittest by default. If running with the `--local` flag, you can use `--runner=pytest`. This combination, or `--pytest` for short, is a wrapper for [pytest](https://docs.pytest.org/en/stable/), if pytest is part of your local environment. `nxtest` accepts most regular `unittest discover` and `pytest` command line arguments.
198
+
199
+ > ✏️**NOTE**:
200
+ > By default, `nxtest` runs in native mode. To run in managed Teamcenter mode, pass the `--teamcenter` argument on the command line. If any of your tests specifically require native or managed mode, you can mark your tests with the provided `@native` or `@teamcenter` decorators, respectively:
201
+
202
+ ```python
203
+ import unittest
204
+
205
+ import nxlib
206
+ from tests.fixtures.common import native, teamcenter
207
+
208
+ class MyTests(unittest.TestCase)
209
+ @teamcenter
210
+ def test_that_requires_teamcenter(self):
211
+ ...
212
+
213
+ @native
214
+ def test_that_requires_native(self):
215
+ ...
216
+ ```
217
+ > If you have tests that require both native and Teamcenter modes, you'll have to run them separately: `uv run nxtest && uv run nxtest --teamcenter`.
218
+
219
+ > ✏️**NOTE**:
220
+ > When running tests, it's crucial that the NX interpreter is using the same version of nxlib you are. Therefore the test runner will fail if `nxlib.status.nx_simlinked is False`. You can override this behavior with the `--allow-extern-nxlib` flag.
221
+
222
+ ## Adding type stubs & auto-complete for NXOpen in VSCode
223
+ You can get type hints and auto-complete for the `NXOpen` package in VSCode by running
224
+ ```
225
+ nxlib typings
226
+ ```
227
+ from your development root. This will copy the contents of `%UGII_BASE_DIR%\UGOPEN\pythonStubs` to your `typings/` folder. In VSCode, ensure that your `python.analysis.stubPath` points to the newly created folder for your workspace.
@@ -0,0 +1,204 @@
1
+ <!-- Copyright 2026 Commonwealth Fusion Systems (CFS), all rights reserved. -->
2
+ <!-- This entire source code file represents the sole intellectual property of CFS. -->
3
+ <!-- -->
4
+ <!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
5
+ <!-- you may not use this file except in compliance with the License. -->
6
+ <!-- You may obtain a copy of the License at -->
7
+ <!-- -->
8
+ <!-- http://www.apache.org/licenses/LICENSE-2.0 -->
9
+ <!-- -->
10
+ <!-- Unless required by applicable law or agreed to in writing, software -->
11
+ <!-- distributed under the License is distributed on an "AS IS" BASIS, -->
12
+ <!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
13
+ <!-- See the License for the specific language governing permissions and -->
14
+ <!-- limitations under the License. -->
15
+ # nxlib
16
+ Nxlib is a library and toolkit for enabling workflows that utilize the [NXOpen Python API](https://docs.sw.siemens.com/en-US/doc/209349590/PL20231101866122454.custom_api.nxopen_python_ref). Nxlib has the following core capabilities:
17
+
18
+ - ✅ Reusable library functions for common NX journaling operations.
19
+ - ✅ Utilities for ensuring that nxlib is discoverable by the NX Python interpreter.
20
+ - ✅ A test runner to enable unit and integration testing of NXOpen functions and modules.
21
+ - ✅ Capability to execute NX journals on a virtual machine for incorporation in CI workflows.
22
+
23
+ Simply put, nxlib enables automated data extraction from CAD, and much more.
24
+
25
+
26
+ You can learn the basics of using nxlib by working through the [example repository](example/).
27
+
28
+ # About NXOpen and Journals
29
+ An NX journal is a Python script that's run from within NX. A journal is run using the Python interpreter that is bundled into the NX executable.
30
+
31
+ What makes nxlib valuable is that it manages the interactions between two Python interpreters: the interpreter for your project, and the interpreter that's built into NX. The built-in NX interpreter is limited to Python 3.10 as of NX 2406, and it comes with no third-party packages. All code in the nxlib.nxopen library needs to be runnable in the NX interpreter. Therefore nxlib:
32
+
33
+ - Has zero production dependencies for code that runs with NXOpen.
34
+ - Is _developed and tested_ with Python 3.10.
35
+
36
+ However, nxlib can still be used as a dependency for projects that use a Python version >3.10, and for projects that depend on other third-party packages.
37
+
38
+ # Installation & Setup
39
+ ## Installation
40
+ Add `cfs-nxlib` to your project with uv:
41
+ ```shell
42
+ uv add cfs-nxlib
43
+ ```
44
+ or with pip:
45
+ ```shell
46
+ pip install cfs-nxlib
47
+ ```
48
+
49
+ ## Setting up your environment variables
50
+ NX and Teamcenter licensing and authentication rely on having several environment variables
51
+ set up correctly. Most of these environment variables should have been set up with your NX installation, but this is not always the case.
52
+
53
+ The [.env.sample](.env.sample) file ships with nxlib, and includes the relevant environment variables for using nxlib. Work with your system administrator to fill this out, rename it to `.env`, and place it in the root of your project.
54
+
55
+ ## Linking nxlib to the NX interpreter
56
+ Installing a library of functions for use with NXOpen is not as trivial as running `pip install cfs-nxlib`, since the Python interpreter used by NX is not the same as your system-level Python installation or your virtual environment. This presents the challenge of ensuring that `nxlib` will be importable by the NX Python interpreter from your specific NX journal.
57
+
58
+ The recommended approach is to symbolically link the `nxlib` library to `%UGII_BASE_DIR%/NXBIN/python`, which is on the import search path for the built-in NX Python interpreter. Simply run
59
+ ```shell
60
+ uv run nxlib install
61
+ ```
62
+ This will allow you to `import nxlib` from any NX Python journal, without needing to restart NX or modify any environment variables. This approach is not viable for distribution of standalone scripts.
63
+
64
+ You can verify that the `nxlib` installation was successful by running
65
+ ```shell
66
+ uv run nxlib status
67
+ ```
68
+
69
+ > ⚠️**CAUTION**:
70
+ > You may choose to install `nxlib` into a virtual environment, or it may be installed as a dependency for another project. However, the Python interpreter that runs your journals is still the one that's built into NX. This Python interpreter is **not** part of your virtual environment, and it will not by default be able to import any third party libraries.
71
+
72
+ > ⚠️**CAUTION**:
73
+ > While you can have multiple `nxlib` installations across virtual environments, you can only have *one* symbolic link to `%UGII_BASE_DIR%/NXBIN/python`. If you want to be sure of the nxlib installation you're using, you can run `nxlib status -v`, or you can make a runtime assertion that whichever `nxlib` you imported is the one that NX is using: `assert nxlib.status.nxlib_symlinked`.
74
+ >
75
+ > You can run `uv run nxlib install --overwrite` to update the symlink, or `uv run nxlib remove` to delete it.
76
+
77
+ ## Setting up Teamcenter Authentication
78
+ In addition to needing an NX installation to run, a Teamcenter license is required to run managed NX sessions. You can authenticate to Teamcenter using SSO, or with a username and password. Nxlib will try to authenticate via password if the corresponding `TC_USERNAME`, `TC_PASSWORD`, `TC_PASSWORD_FILE` environment variables are set. You can override this behavior by passing `--auth=[sso|password]` when running `nxlib run` or `nxtest`.
79
+
80
+ ### Single Sign-on (SSO)
81
+ Most end-users of nxlib will use SSO to authenticate. Authenticating with SSO requires having your environment variables set correctly. See the section on environment variables above for enabling SSO.
82
+
83
+ If you are using SSO, there is nothing you need to do except occasionally authenticate as you normally would when running NX.
84
+
85
+ ### Username and Password or Password File
86
+ If SSO is not an option, nxlib also supports using a Teamcenter username along with a password or encrypted password file. This is not a typical user case for end-users, and is intended to be used for when nxlib is run in CI or on a virtual machine.
87
+
88
+ To authenticate with a username and password, the `TC_USERNAME` environment variable must be set appropriately, along with at least one of `TC_PASSWORD` or `TC_PASSWORD_FILE`.
89
+
90
+ # How to run NX Journals
91
+ NX journals can be called from within another Python script, from the command line, or from within an existing NX session.
92
+
93
+ ## Running journals from another Python script
94
+ In your Python code, you can run:
95
+ ```python
96
+ from nxlib import run_journal
97
+
98
+ run_journal("path/to/journal.py", ["journal", "arguments"])
99
+ ```
100
+
101
+ ## Running from the command line
102
+ nxlib has a handy command line utility for executing journals. This utility executes journals headlessly, without launching the NX GUI. This makes execution faster for large assemblies, as they do not have to be rendered graphically.
103
+
104
+ You can run an existing journal:
105
+ ```shell
106
+ uv run nxlib run my_journal.py
107
+ ```
108
+ Or you can just run arbitrary Python code through NX:
109
+ ```shell
110
+ uv run nxlib run -c "from nxlib import nxprint; nxprint('Hello world')"
111
+ ```
112
+ Try `nxlib run --help` for the full set of options.
113
+
114
+ ### Running with a custom Python virtual environment
115
+ You can run journals using your own Python virtual environment, including the libraries installed there. This can be done using the `--local` flag on the CLI command, or by setting `local=True` in the `run_journal` function.
116
+
117
+ This has the limitation in that the minor Python version that NX runs must match that of your local interpreter. As of NX2406, that version is Python 3.10.
118
+
119
+ ## Running from within NX
120
+ To run from within NX:
121
+
122
+ 1. In NX, press Alt+F8 to run a journal
123
+ 2. Click "Browse..." and navigate to the journal file you'd like to run
124
+ 3. Enter any arguments from the journal, one per line, in the box at the bottom of the window.
125
+ 4. Click "Run".
126
+
127
+ # Development
128
+ [uv](https://github.com/astral-sh/uv) is recommended for development of nxlib.
129
+
130
+ ## Quickstart
131
+ ```shell
132
+ # Clone the repository
133
+ git clone https://github.com/cfs-energy/nxlib.git
134
+ ```
135
+ ```shell
136
+ # Install nxlib and all dependencies
137
+ uv sync --all-extras
138
+
139
+ # Sync nxlib to the NX Python interpreter
140
+ uv run nxlib install
141
+
142
+ # Run the nxlib unit tests
143
+ uv run pytest
144
+
145
+ # Run the NX-based unit tests
146
+ uv run nxtest
147
+ ```
148
+
149
+ ## Python Version
150
+ The Python version used to develop nxlib is constrained to the Python minor version that's built into NX. This ensures that all code will be runnable with the NX interpreter. The `.python-version` file ensures that the correct version is used.
151
+
152
+ As of NX 2406, the minor Python version that ships with NX is 3.10. To check the Python version that shipped with your version of NX, run
153
+
154
+ ```shell
155
+ uv run nxlib run --native -c "import sys; print(sys.version)"
156
+ ```
157
+
158
+ It's recommended to update the `.python-version` file to match your development environment.
159
+
160
+ ## Writing Tests
161
+ There are two types of functions you'll want to test: NX journaling functions, and everything else. Tests of NX journaling functions are those tests where one or more test assertions must be made from within the NX Python interpreter. Tests of non-journaling functions, especially those that rely on third-party packages, likely need to be run with your local Python interpreter.
162
+
163
+ In order to keep test discovery of the two types of tests exclusive, segregate the test files as follows:
164
+ - Tests for NX journaling functions should live in files that match the pattern `nxtest*.py`
165
+ - Tests for everything else should live in files that match the pattern `test*.py` (as is common for most test suites & runners).
166
+
167
+ To run all tests, you need to run two separate commands:
168
+
169
+ ```shell
170
+ uv run nxtest # Run tests on NX journaling functions
171
+ uv run pytest # Run tests on everything else
172
+ ```
173
+
174
+ The `nxtest` command runs Python's built in unittest by default. If running with the `--local` flag, you can use `--runner=pytest`. This combination, or `--pytest` for short, is a wrapper for [pytest](https://docs.pytest.org/en/stable/), if pytest is part of your local environment. `nxtest` accepts most regular `unittest discover` and `pytest` command line arguments.
175
+
176
+ > ✏️**NOTE**:
177
+ > By default, `nxtest` runs in native mode. To run in managed Teamcenter mode, pass the `--teamcenter` argument on the command line. If any of your tests specifically require native or managed mode, you can mark your tests with the provided `@native` or `@teamcenter` decorators, respectively:
178
+
179
+ ```python
180
+ import unittest
181
+
182
+ import nxlib
183
+ from tests.fixtures.common import native, teamcenter
184
+
185
+ class MyTests(unittest.TestCase)
186
+ @teamcenter
187
+ def test_that_requires_teamcenter(self):
188
+ ...
189
+
190
+ @native
191
+ def test_that_requires_native(self):
192
+ ...
193
+ ```
194
+ > If you have tests that require both native and Teamcenter modes, you'll have to run them separately: `uv run nxtest && uv run nxtest --teamcenter`.
195
+
196
+ > ✏️**NOTE**:
197
+ > When running tests, it's crucial that the NX interpreter is using the same version of nxlib you are. Therefore the test runner will fail if `nxlib.status.nx_simlinked is False`. You can override this behavior with the `--allow-extern-nxlib` flag.
198
+
199
+ ## Adding type stubs & auto-complete for NXOpen in VSCode
200
+ You can get type hints and auto-complete for the `NXOpen` package in VSCode by running
201
+ ```
202
+ nxlib typings
203
+ ```
204
+ from your development root. This will copy the contents of `%UGII_BASE_DIR%\UGOPEN\pythonStubs` to your `typings/` folder. In VSCode, ensure that your `python.analysis.stubPath` points to the newly created folder for your workspace.