Is there a ghPython equivalent in RhinoCommon for the Grasshopper Linear Array component (or other Array components)?
I currently access the Linear Array component from Grasshopper using the ghpythonlib.components.LinearArray but I will probably not be able to use this Node in Code functionality once I refactor the Python code to C#. I want to make this original code future proof for the next phase.
If there isn’t a RhinoCommon equivalent, could somebody from McNeel post the definition from Linear Array that I can plug into my Python code using RhinoCommon?
Here is the C# equivalent. Perhaps you can convert this to Python:
private void RunScript(GeometryBase G, Vector3d D, int N, ref object A)
{
if (G != null && D.IsValid && N >= 0)
{
var list = new List<GeometryBase>(N);
for (var i = 0; i < N; i++)
{
var xform = Transform.Translation(D * i);
var geom = G.Duplicate();
geom.Transform(xform);
list.Add(geom);
}
A = list.ToArray();
}
It’s really helpful to see how the components are written. I wish I could see all of them… Are they available anywhere? It is a great learning source for coding best practices. I already learned a lot from the if statement and what methods I could chain onto python objects, such as the geometry.duplicate().
The other reason I wanted to write my own Linear Array function is that the ghLinearArray returned a Named Tuple with nested list items, so extracting the geometry was overly complicated. I couldn’t easily access the name but only the list item accessor, such as [0].
Here is how I refactored it. I needed to add a direction != None or else the component didn’t like the direction.IsValid for None type. I had to change this in my code because I was arraying plane objects which didn’t have some of the geometry base functionality like .duplicate().
"""Grasshopper Linear Array component in Python.
Inputs:
geometry: GeometryBase that will be arrayed
direction: Vector3D that will set the array direction
number: Integer that sets the number of array items
Output:
array: Geometry Array"""
import Rhino as rc
if geometry != None and direction != None and direction.IsValid and number >= 0:
array = []
i = 0
while i < number:
i += 1
xform = rc.Geometry.Transform.Translation(direction * i)
geom = geometry.Duplicate()
geom.Transform(xform)
array.Add(geom)