從NX 10開始,NX Open增加了對Python的支持,使得NX腳本語言的多元性進一步得到擴充。但對於大多數用戶來說,選擇Python作為腳本語言,主要是想利用Python社區提供的各種函數庫或功能包,比如在科學計算和數據處理中常用的numpy、scipy、matplotlib、scipy等等。然而遺憾的是,NX中的Python只包含Python的基本功能,不能自行安裝其他函數庫或功能包,這使得Python存在的意義大為降低。不過NX提供選擇其它Python解釋器作為NX的解釋器,這樣就可以解決這個問題。
在Siemens的論壇上,有一個帖子詳細解釋了這個設置的方法:
其實這個方法在NX 10的說明頁面里有,但后續版本不知道移到哪里去了……
下面是從NX 10說明頁面復制的設置方法,其中ugii_env.dat文件可以通過在NX的安裝目錄里搜索定位
Using external Python distribution
The Python distribution included in NX is Python 3.3.2 with the PLY extension module included. If you use other Python extension modules, you can set up NX to use a Python distribution that you install that includes the other extension modules.
Specify the external Python distribution for NX to use with the UGII_PYTHON_LIBRARY_DIR and UGII_PYTHONPATH environment variables. You can set these environment variables in your site, group, or user environment variable customization in your ugii_env.dat file.
The UGII_PYTHON_LIBRARY_DIR variable specifies the directory that contains the Python shared library. The location of this shared library depends on how Python was installed on the system. For example, if you installed Python 3.3.2 on Windows for all users, the Python shared library (Python33.dll) is in the directory %SystemRoot%\System32. If you installed Python 3.3.2 locally, the Python shared library is in the directory <Python installation path>\DLLs.
The UGII_PYTHONPATH variable specifies the directories that the Python interpreter searches to find Python modules, including the NX Open Python extension modules. When you set this variable, include the location of the NX Open Python extension modules in addition to any other extension modules you are using. For example, if you installed Python 3.3.2 on Windows, UGII_PYTHONPATH should be set as follows:
-
UGII_PYTHONPATH=<Python installation path>;<Python installation path>\DLLs;<Python installation path>\Lib;<Python installation path>\Lib\site-packages;%UGII_BASE_DIR%\ugii\python;<other Python modules>
You can override the UGII_PYTHONPATH and UGII_PYTHON_LIBRARY_DIR settings by using the Override Python Parameters command in NX. If you override the UGII_PYTHONPATH variable, do not include the Python33.zip file in your UGII_PYTHONPATH setting, since it is not needed and conflicts with your external Python distribution.
上面論壇帖子也是基於NX 10進行的設置,經親測,在NX 12上可以使用。對於Python,不論是使用Anaconda(不推薦,后面解釋),還是自行安裝的numpy,都可以用。為了避免設置中出現問題,建議閱讀這個帖子的所有回帖。
需要提示的是,這里面還是有很多技巧的,否則經常不能使用,以下是本人設置的經驗:
1、需要通過NX幫助查看當前版本支持的Python版本,最好使用相同的版本,例如NX 12支持Python 3.6.1,那么Python版本最好為3.6。以下鏈接是NX 12的版本支持信息,其它版本的信息也可以在類似分類頁面中找到:
2、上文鏈接中nxopen庫文件所在位置在不同版本NX中位置會發生變化,比如在NX 12中,這個文件夾在NX安裝目錄中的NXBIN文件夾中,而對於NX 10,python在UGII文件夾里。我們可以在NX安裝目錄下搜索“Python*.zip”進行定位。
3、如果NX或第三方Python的安裝路徑里面包含空格,在ugii_env.dat中設定環境變量時,相關路徑需要用英文雙引號括起來。
4、在使用Anaconda提供的Python時,調用numpy或者基於numpy的函數庫時,可能會提示numpy加載出錯,錯誤信息大概是這個樣子:
ImportError:
Importing the multiarray numpy extension module failed. Most likely you are trying to ...
這個是因為找不到mkl的相關動態鏈接庫,這里沒有找到很好的解決方法。網上提供的一種方法是把Anaconda使用的所有與mkl相關的dll文件(可以通過在Anaconda目錄下搜索mkl_avx.dll文件定位)復制到numpy的目錄中(例如Anaconda3\Lib\site-packages\numpy\core),這樣的做法numpy可以正常載入,但是可能不會隨着Anaconda軟件庫的更新而自動替換相關文件,會有潛在的風險。這也是不建議直接使用Anaconda的原因。
5、在使用numpy或者其它基於numpy的第三方包時,NX可能會崩潰……不過在上文論壇帖子的回帖中提到了一種解決辦法,
這種方法是在python腳本首行添加
#nx: threaded
注意上面一行中的空格不能有增減。此語句使得NX支持多線程,但是也帶來一個問題,即當腳本修改了以后,必須重啟NX后才能正常載入新腳本,否則老腳本還是會被調用。這確實有些麻煩,只是權宜之計,不過一旦腳本改好了,這也不是什么大問題了吧……
下面我將我自己電腦上ugii_env.dat文件內容粘上來。這里使用的是NX 12(不是默認安裝目錄),Python使用的是官方的Python,numpy等第三方包通過https://www.lfd.uci.edu/~gohlke/pythonlibs/提供的預編譯包進行安裝。如果使用的是Anaconda,根據下面的方法進行相應的修改,並按上文方法復制mkl的相關文件,也是可以用的。
PYTHONPATH=C:\Python36;C:\Python36\Lib;C:\Python36\libs;C:\Python36\DLLs;C:\Python36\Lib\site-packages;"C:\Program Files\Siemens\NX120\NXBIN\python"
UGII_PYTHON_HOME=C:\Python36
UGII_PYTHON_DLL=python36.dll
UGII_PYTHON_LIBRARY_DIR=C:\Python36
UGII_PYTHONPATH=C:\Python36;C:\Python36\Lib;C:\Python36\libs;C:\Python36\DLLs;C:\Python36\Lib\site-packages;"C:\Program Files\Siemens\NX120\NXBIN\python"
TCL_LIBRARY=C:\Python36\tcl\tcl8.6
NX 對Python的支持也是近幾年的事,也許以后就不用這么麻煩了吧……