Intersection.MeshRay with meshFaceIndices Overload using GHPython

Hi All,

I’m trying to implement this Intersection.MeshRay overload through GHPython (i.e. that return the “faces on mesh that ray intersects”). I think I’ve got it working, but just wanted to check that I’m going about it properly. Calling the overload through IronPython seems a wee bit convoluted this way:

import Rhino as rc
from System import Array
import clr

def meshRayIntersect(mesh,point,vector):
    
    """ Intersect a mesh with a ray, including intersected mesh face IDs """
    
    ray = rc.Geometry.Ray3d(point,vector)
    faceIDs = clr.StrongBox[Array[int]]()
    rayP = rc.Geometry.Intersect.Intersection.MeshRay(mesh,ray,faceIDs)
    if rayP >= 0.0:
        rayPt = ray.PointAt(rayP)
        faceIDs = list(faceIDs.Value)
        return rayPt,faceIDs
    else:
        return None, None

XPt, XIDs = meshRayIntersect(MyMesh,MyPoint,MyVector)

That is, having to instantiate an Array inside a StrongBox prior to calling rc.Geometry.Intersect.Intersection.MeshRay just to return the faceIDs. I might be way off here :man_shrugging:

Cheers,

Anders

Edit: Removed small bug, in case anyone implements the code directly.

1 Like

I checked, and this really seems to be the only way to call that method.

Usually for C# out parameters there’s the alternative to omit the parameter and expect a tuple, but in this case the overload is hidden by the existence of the 2-arguments method.

All I can say, is that you can also write clr.Reference[Array[int]](), which is the way they refer to this in the docs. StrongBox and Reference are analogues.

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

3 Likes

Thanks Giulio. I was getting C++ flashbacks and assumed I was doing something wrong here. Don’t think I’ve come across this type of overload before with RhinoCommon/IronPython, where “normally” tuple unpacking and such seem to work.

Edit: Also on a side note: Would this method ever return more than one face ID (and thereby negate the need for returning/filling an array)?