Rhinocommon Python with StrongBox Example

Continuing the discussion from How can I use Plane.FitPlaneToPoints from Python correctly?:

I was also confused when I initially tried to use a function that modified an input variable in-place, using rhinocommon and Python. I got this error when I tried passing in a python boolean to Mesh.TopologyEdges.GetEdgesForFace(Int32,Boolean[]):

TypeError: expected StrongBox[Array[bool]], got bool

So, to avoid similar confusion, here is an example using the Mesh.TopologyEdges.GetEdgesForFace(Int32,Boolean[])
method. The method “gets the indices of the edges that surround the given face.”

import clr
import System
import Rhino.Geometry as geom
import rhinoscriptsyntax as rs
import scriptcontext

def get_edges_orientations_for_face(mesh,face):
    returns a list of tuples: [(edgeIndex,alignedWithFace),...]
    where edgeIndex is the index of the TopologyEdge in the mesh
    and alignedWithFace is a boolean which is True if the edge direction is consistent with the face
    orientations = clr.StrongBox[System.Array[bool]]()
    edges =  mesh.TopologyEdges.GetEdgesForFace(face,orientations)
    edges = list(edges)
    orientations = list(orientations.Value)
    edges_with_orientations = zip(edges,orientations)
    return edges_with_orientations

def make_mesh():
    creates an upright square mesh with two triangular faces
    vertices = []
    face_vertices = []
    rhino_mesh = create_mesh(vertices,face_vertices)
    return rhino_mesh

def create_mesh(vertices,face_vertices):
    '''add a mesh to doc and get the Rhino.Geometry.Mesh object''' 
    mesh_GUID = rs.AddMesh( vertices, face_vertices )
    obj = scriptcontext.doc.Objects.Find(mesh_GUID)
    return obj.Geometry

if __name__ == "__main__":
    mesh = make_mesh()
    face = 0
    edges_with_orientations = get_edges_orientations_for_face(mesh,face)
    print "for face {}, got oriented edge tuples: {}".format(face,edges_with_orientations)

I found it very helpful to look at IronPython documentation. The section on Typed Arrays helped significantly.

1 Like