Intersection.RayShoot Method

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]
                    else:
                        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.AddPoint(point)
            rs.AddLine(point, pts[i])
    
    print "{} point hits, {} missed projections".format(hits, len(pts)-hits)
    
ShootOneRayCustom()
2 Likes