Intersection.RayShoot Method

Looking for an example using Intersection.RayShoot Method in python.

I was hoping to find a function that would either select or return back the intersection of a surface. Not necessarily just looking for the point. Want point and Id of the intersecting surface.



Use Rhino.Geometry.Intersect.RayShootEvent RayShoot(System.Collections.Generic.IEnumerable<Rhino.Geometry.GeometryBase> geometry, Rhino.Geometry.Ray3d ray, int maxReflections)

The RayShootEvent object(s) which get returned has a property called BrepFaceIndex which you can use to get the face which was hit.

Hi @eric.bunn,

The method @thomas7 references is in the Rhino 7 WIP.

– Dale

1 Like

Guess I’ll be waiting for 7 for that one. I did find a post by Clement that had a function in it that might be a good work around for me and it returns back the hit face.

project pts on breps, find closest hits (c) 2016, Clement Greiner - CG3D

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def ShootOneRayCustom():
    pt_ids = rs.GetObjects('Select points', filter=rs.filter.point)
    if not pt_ids: return
    brep_ids = rs.GetObjects('Select nurbs', filter=rs.filter.polysurface)
    if not brep_ids: return
    pts = [rs.PointCoordinates(p) for p in pt_ids]
    breps = [rs.coercebrep(id) for id in brep_ids]
    vec = Rhino.Geometry.Vector3d(0,0,-1)
    tol = scriptcontext.doc.ModelAbsoluteTolerance
    intersector = Rhino.Geometry.Intersect.Intersection
    projected = [None] * len(pts)
    distances = [None] * len(pts)
    hit_breps = [None] * len(pts)
    # project each point 
    for i, p in enumerate(pts):
        # on each brep
        for j, brep in enumerate(breps):
            rc = intersector.ProjectPointsToBreps([brep], [p], vec, tol)
            if rc.Count > 0:
                # save the closest hit
                for hit_pt in rc:
                    if not projected[i]: 
                        projected[i] = hit_pt
                        distances[i] = p.DistanceTo(hit_pt)
                        hit_breps[i] = brep_ids[j]
                        d = p.DistanceTo(hit_pt)
                        if d < distances[i]:
                            projected[i] = hit_pt
                            distances[i] = d
                            hit_breps[i] = brep_ids[j]
    hits = 0
    for i, point in enumerate(projected):
        if point:
            hits += 1
            rs.AddLine(point, pts[i])
    print "{} point hits, {} missed projections".format(hits, len(pts)-hits)