问题描述
我目前正在使用Rhino中的python脚本编辑器来开发脚本。由于我目前在Windows计算机上工作,因此脚本编辑器使用IronPython作为语言。
I’m currently developing a script using the python script editor in Rhino. As I’m currently working in a Windows machine, the script editor uses IronPython as language.
在同一脚本中,我想与FE软件(Straus7)进行交互具有Python API。这样做时,我遇到了一些问题,因为ctypes模块在IronPython中似乎无法像在常规Python 中那样工作。特别是,在使用以下命令初始化数组时发现了问题:
In the same script, I want to interact with an FE software (Straus7) which has a Python API. When doing so, I have experienced some problems as the ctypes module does not seem to work in IronPython the same way it does in regular Python. Especially, I’m finding problems when initializing arrays using the command:
ctypes.c_double*3
例如,如果我想获得FE模型中节点#1的XYZ坐标,我可以使用Python编写以下:
For example, if I want to obtain the XYZ coordinates of a node #1 in the FE model, I regular Python I would write the following:
XYZType = ctypes.c_double*3
XYZ = XYZType()
node_num = 1
st.St7GetNodeXYZ(1,node_num,XYZ)
这将返回一个变量XYZ是一个3D数组,使得:
And this returns me a variable XYZ which is a 3D array such that:
XYZ -> <straus_userfunctions.c_double_Array_3 at 0xc5787b0>
XYZ[0] = -0.7xxxxx -> (X_coord)
XYZ[1] = -0.8xxxxx -> (Y_coord)
XYZ[2] = -0.9xxxxx -> (Z_coord)
另一方面,我在 IronPython中复制了相同的脚本,出现以下错误消息
On the other side, I copy the same exact script in IronPython, the following error message appears
Message: expected c_double, got c_double_Array_3
很显然,如果我将变量XYZ更改为c_double,然后它变成一个仅包含一个条目的double变量,它对应于数组的第一个元素(在本例中为X坐标)
Obviously, If I change the variable XYZ to c_double; then it becomes a double variable which contains only a single entry, which corresponds to the first element of the array (in this case, the X-coordinate)
像所有FEM软件一样令人讨厌的是,矩阵和数组的使用被广泛使用。因此,我想问一下是否有人现在可以解决这种情况。
This situation is quite annoying as all FEM softwares, the usage of matrices and arrays is widely used. Consequently, I wanted to ask if anyone nows a simple fix to this situation.
我当时正在考虑使用数组第一个元素的内存分配来获取其余的内容
I was thinking of using the memory allocation of the first element of the array to obtain the rest but I’m not so sure how to do so.
非常感谢。 Gerard
Thanks a lot. Gerard
推荐答案
我发现在使用IronPython时,您需要将三双数组显式转换为指针加倍。因此,如果您将草with与Strand7 / Straus7 API结合使用,则需要添加如下代码:
I've found when working with IronPython you need to explicitly cast the "Array of three doubles" to a "Pointer to double". So if you're using Grasshopper with the Strand7 / Straus7 API you will need to add an extra bit like this:
import St7API
import ctypes
# Make the pointer conversion functions
PI = ctypes.POINTER(ctypes.c_long)
PD = ctypes.POINTER(ctypes.c_double)
XYZType = ctypes.c_double*3
XYZ = XYZType()
node_num = 1
# Cast arrays whenever you pass them to St7API from IronPython
St7API.St7GetNodeXYZ(1, node_num, PD(XYZ))
我没有访问权限到IronPython或Strand7 / Straus7,但是从内存中可以做到。如果这对您不起作用,您可以给Strand7支持发送电子邮件-通常,您会在一天左右的时间内收到有关此类信息的反馈。
I don't have access to IronPython or Strand7 / Straus7 at the moment but from memory that will do it. If that doesn't work for you you can email Strand7 Support - you would typically get feedback on something like this within a day or so.
这篇关于IronPython:消息:预期的c_double,得到了c_double_Array_3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!