将Rhinoscriptsyntax从Python移植到C#

时间:2018-10-30 22:35:47

标签: c# python grasshopper rhino3d

我正在将RhinoScript(Python)函数的集合移植到C#,以便开发自定义Grasshopper组件的集合。

我的问题是我无法访问某些RhinoScript方法,例如VectorUnitize()VectorScale()PointAdd()

我似乎找不到在C#中包含这些引用的任何引用。有人对这种事情有任何经验,可以为我指明正确的方向吗?

我正在使用的RhinoScript:

# FIND THE ALIGNMENT VECTOR
aVec = self.AlignmentVector(neighborAgents, neighborAgentsDistances)
if rs.VectorLength(aVec) > 0:
    aVec = rs.VectorUnitize(aVec)
aVec = rs.VectorScale(aVec, self.alignment)

# FIND THE SEPARATION VECTOR
sVec = self.SeparationVector(neighborAgents, neighborAgentsDistances)
if rs.VectorLength(sVec) > 0:
    sVec = rs.VectorUnitize(sVec)
sVec = rs.VectorScale(sVec, self.separation)

# FIND THE COHESION VECTOR
cVec = self.CohesionVector(neighborAgents)
if rs.VectorLength(cVec) > 0:
    cVec = rs.VectorUnitize(cVec)
cVec = rs.VectorScale(cVec, self.cohesion)

# ADD ALL OF THE VECTOR TOGETHER to find the new position of the agent
acc = [0, 0, 0]
acc = rs.PointAdd(acc, aVec)
acc = rs.PointAdd(acc, sVec)
acc = rs.PointAdd(acc, cVec)

# update the self vector
self.vec = rs.PointAdd(self.vec, acc)
self.vec = rs.VectorUnitize(self.vec)

我到目前为止所拥有的(不多:/):

// Find the alignment Vector
Vector3d aVec = AlignmentVector(neighborAgents, neighborAgentsDistances);
if (aVec.Length > 0)
{
    aVec.Unitize();
}
aVec = ????

2 个答案:

答案 0 :(得分:2)

对于Vector3d documentation of the Add function,您还可以使用Vector3d的重载+运算符。对于大多数此类几何类型,RhinoCommon提供了预期的重载。

要缩放矢量,您需要将其乘以标量,例如alignmentseparationcohesion

Vector3d vec1 = getyourvector();
vec1.Unitize();
vec1 *= alignment;

Vector3d vec2 = getyourvector();
vec2.Unitize();
vec2 *= cohesion;

Vector3d vec3 = getyourvector();
vec3.Unitize();
vec3 *= separation;

Vector3d acc;
acc += vec1;
acc += vec2;
acc += vec3;

答案 1 :(得分:1)

在这里您可以看到如何使用Rhino Common实现所有的Rhino脚本功能: https://github.com/mcneel/rhinoscriptsyntax/tree/rhino-6.x/Scripts/rhinoscript