python中nx_Siemens NX (UG)中对于Python第三方包(函数库)的设置方法

从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 \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=;\DLLs;\Lib;\Lib\site-packages;%UGII_BASE_DIR%\ugii\python;

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的支持也是近几年的事,也许以后就不用这么麻烦了吧……

你可能感兴趣的:(python中nx)