def OrientObject(object_id, reference, target, flags=0): """Orients a single object based on input points Parameters: object_id = String or Guid. The identifier of an object reference = list of 3-D reference points target = list of 3-D target points flags[opt]: 1 = copy object 2 = scale object """ object_id = rhutil.coerceguid(object_id, True) from_array = rhutil.coerce3dpointlist(reference) to_array = rhutil.coerce3dpointlist(target) if from_array is None or to_array is None: raise ValueError("Could not convert reference or target to point list") from_count = len(from_array) to_count = len(to_array) if from_count < 2 or to_count < 2: raise Exception("point lists must have at least 2 values") copy = ((flags & 1) == 1) scale = ((flags & 2) == 2) xform_final = None if from_count > 2 and to_count > 2: #Orient3Pt from_plane = Rhino.Geometry.Plane(from_array[0], from_array[1], from_array[2]) to_plane = Rhino.Geometry.Plane(to_array[0], to_array[1], to_array[2]) if not from_plane.IsValid or not to_plane.IsValid: raise Exception("unable to create valid planes from point lists") xform_final = Rhino.Geometry.Transform.PlaneToPlane( from_plane, to_plane) else: #Orient2Pt xform_move = Rhino.Geometry.Transform.Translation(to_array[0] - from_array[0]) xform_scale = Rhino.Geometry.Transform.Identity v0 = from_array[1] - from_array[0] v1 = to_array[1] - to_array[0] if scale: len0 = v0.Length len1 = v1.Length if len0 < 0.000001 or len1 < 0.000001: raise Exception("vector lengths too short") scale = len1 / len0 if abs(1.0 - scale) >= 0.000001: plane = Rhino.Geometry.Plane(from_array[0], v0) xform_scale = Rhino.Geometry.Transform.Scale( plane, scale, scale, scale) v0.Unitize() v1.Unitize() xform_rotate = Rhino.Geometry.Transform.Rotation(v0, v1, from_array[0]) xform_final = xform_move * xform_scale * xform_rotate rc = scriptcontext.doc.Objects.Transform(object_id, xform_final, not copy) if rc == System.Guid.Empty: return scriptcontext.errorhandler() scriptcontext.doc.Views.Redraw() return rc
def OrientObject(object_id, reference, target, flags=0): """Orients a single object based on input points Parameters: object_id = String or Guid. The identifier of an object reference = list of 3-D reference points target = list of 3-D target points flags[opt]: 1 = copy object 2 = scale object """ object_id = rhutil.coerceguid(object_id, True) from_array = rhutil.coerce3dpointlist(reference) to_array = rhutil.coerce3dpointlist(target) if from_array is None or to_array is None: raise ValueError("Could not convert reference or target to point list") from_count = len(from_array) to_count = len(to_array) if from_count<2 or to_count<2: raise Exception("point lists must have at least 2 values") copy = ((flags & 1) == 1) scale = ((flags & 2) == 2) xform_final = None if from_count>2 and to_count>2: #Orient3Pt from_plane = Rhino.Geometry.Plane(from_array[0], from_array[1], from_array[2]) to_plane = Rhino.Geometry.Plane(to_array[0], to_array[1], to_array[2]) if not from_plane.IsValid or not to_plane.IsValid: raise Exception("unable to create valid planes from point lists") xform_final = Rhino.Geometry.Transform.PlaneToPlane(from_plane, to_plane) else: #Orient2Pt xform_move = Rhino.Geometry.Transform.Translation( to_array[0]-from_array[0] ) xform_scale = Rhino.Geometry.Transform.Identity v0 = from_array[1] - from_array[0] v1 = to_array[1] - to_array[0] if scale: len0 = v0.Length len1 = v1.Length if len0<0.000001 or len1<0.000001: raise Exception("vector lengths too short") scale = len1 / len0 if abs(1.0-scale)>=0.000001: plane = Rhino.Geometry.Plane(from_array[0], v0) xform_scale = Rhino.Geometry.Transform.Scale(plane, scale, scale, scale) v0.Unitize() v1.Unitize() xform_rotate = Rhino.Geometry.Transform.Rotation(v0, v1, from_array[0]) xform_final = xform_move * xform_scale * xform_rotate rc = scriptcontext.doc.Objects.Transform(object_id, xform_final, not copy) if rc==System.Guid.Empty: return scriptcontext.errorhandler() scriptcontext.doc.Views.Redraw() return rc
def PointCloudKNeighbors(pt_cloud, needle_points, amount=1): """Returns amount indices of points in a point cloud that are near needle_points. Parameters: pt_cloud (guid|[point, ...]): the point cloud to be searched, or the "hay stack". This can also be a list of points. needle_points (guid|[point, ...]): a list of points to search in the point_cloud. This can also be specified as a point cloud. amount (int, optional): the amount of required closest points. Defaults to 1. Returns: [int, int,...]: a list of indices of the found points, if amount equals 1. [[int, ...], ...]: nested lists with amount items within a list, with the indices of the found points. Example: import rhinoscriptsyntax as rs id = rs.GetObject("Select point cloud", rs.filter.pointcloud) if id: result = rs.PointCloudKNeighbors(id, [(0,0,0)]) if result: print("The closest point to origin has index : %s." % result[0]) See Also: AddPointCloud IsPointCloud PointCloudPoints """ needles = rhutil.coercegeometry(needle_points, False) if isinstance(needles, Rhino.Geometry.PointCloud): needles = needles.AsReadOnlyListOfPoints() else: needles = rhutil.coerce3dpointlist(needle_points, True) pc_geom = rhutil.coercegeometry(pt_cloud, False) if isinstance(pc_geom, Rhino.Geometry.PointCloud): if len(needles) > 100: search = Rhino.Geometry.RTree.PointCloudKNeighbors else: search = Rhino.Collections.RhinoList.PointCloudKNeighbors return __simplify_PointCloudKNeighbors( search(pc_geom, needles, amount), amount) if len(needles) > 100: search = Rhino.Geometry.RTree.Point3dKNeighbors else: search = Rhino.Collections.RhinoList.Point3dKNeighbors if isinstance( pt_cloud, System.Collections.Generic.IEnumerable[Rhino.Geometry.Point3d]): return __simplify_PointCloudKNeighbors( search(pt_cloud, needles, amount), amount) pts = rhutil.coerce3dpointlist(pt_cloud, True) return __simplify_PointCloudKNeighbors(search(pts, needles, amount), amount)
def ProjectPointToMesh(points, mesh_ids, direction): """Projects one or more points onto one or more meshes Parameters: points = one or more 3D points mesh_ids = identifiers of one or more meshes direction = direction vector to project the points Returns: list of projected points on success Example: import rhinoscriptsyntax as rs mesh = rs.GetObject("Select mesh to project onto", rs.filter.mesh) objects = rs.GetObjects("Select points to project", rs.filter.point) points = [rs.PointCoordinates(obj) for obj in objects] # project down... results = rs.ProjectPointToMesh(points, mesh, (0,0,-1)) rs.AddPoints( results ) See Also: ProjectCurveToMesh ProjectCurveToSurface ProjectPointToSurface """ pts = rhutil.coerce3dpointlist(points, False) if pts is None: pts = [rhutil.coerce3dpoint(points, True)] direction = rhutil.coerce3dvector(direction, True) id = rhutil.coerceguid(mesh_ids, False) if id: mesh_ids = [id] meshes = [rhutil.coercemesh(id, True) for id in mesh_ids] tolerance = scriptcontext.doc.ModelAbsoluteTolerance rc = Rhino.Geometry.Intersect.Intersection.ProjectPointsToMeshes(meshes, pts, direction, tolerance) return rc
def PointsAreCoplanar(points, tolerance=1.0e-12): """Verifies that a list of 3D points are coplanar Parameters: points = list of 3D points tolerance[opt] = tolerance to use when verifying Returns: True or False Example: import rhinoscriptsyntax as rs def SurfacesAreCoplanar(srf1, srf2): if( not rs.IsSurface(srf1) or not rs.IsSurface(srf2) ): return False pts1 = rs.SurfacePoints(srf1) pts2 = rs.SurfacePoints(srf2) if( pts1==None or pts2==None ): return False pts1.extend(pts2) return rs.PointsAreCoplanar(pts1) x = rs.GetObject( "First surface to test", rs.filter.surface) y = rs.GetObject( "Second surface to test", rs.filter.surface) print SurfacesAreCoplanar(x, y) See Also: IsPoint IsPointCloud PointCoordinates """ points = rhutil.coerce3dpointlist(points, True) return Rhino.Geometry.Point3d.ArePointsCoplanar(points, tolerance)
def ProjectPointToSurface(points, surface_ids, direction): """Projects one or more points onto one or more surfaces or polysurfaces Parameters: points = one or more 3D points surface_ids = identifiers of one or more surfaces/polysurfaces direction = direction vector to project the points Returns: list of projected points on success Example: import rhinoscriptsyntax as rs surface = rs.GetObject("Select surface to project onto", rs.filter.surface) objects = rs.GetObjects("Select points to project", rs.filter.point) points = [rs.PointCoordinates(obj) for obj in objects] results = rs.ProjectPointToSurface(points, surface, (0,0,-1)) rs.AddPoints(results) See Also: ProjectCurveToMesh ProjectCurveToSurface ProjectPointToMesh """ pts = rhutil.coerce3dpointlist(points) if pts is None: pts = [rhutil.coerce3dpoint(points, True)] direction = rhutil.coerce3dvector(direction, True) id = rhutil.coerceguid(surface_ids, False) if id: surface_ids = [id] breps = [rhutil.coercebrep(id, True) for id in surface_ids] tolerance = scriptcontext.doc.ModelAbsoluteTolerance return Rhino.Geometry.Intersect.Intersection.ProjectPointsToBreps( breps, pts, direction, tolerance)
def PointArrayClosestPoint(points, test_point): """Finds the point in a list of 3D points that is closest to a test point Parameters: points = list of points test_point = the point to compare against Returns: index of the element in the point list that is closest to the test point Example: import rhinoscriptsyntax as rs cloud= rs.GetObject("Select point cloud") if cloud: point = rs.GetPoint("Point to test") if point: cloud = rs.PointCloudPoints(cloud) index = rs.PointArrayClosestPoint(cloud, point) if index is not None: point_id = rs.AddPoint(cloud[index]) rs.SelectObject( point_id ) See Also: CurveClosestPoint SurfaceClosestPoint """ points = rhutil.coerce3dpointlist(points, True) test_point = rhutil.coerce3dpoint(test_point, True) index = Rhino.Collections.Point3dList.ClosestIndexInList(points, test_point) if index>=0: return index
def PullPoints(object_id, points): """Pulls an array of points to a surface or mesh object. For more information, see the Rhino help file Pull command Parameters: object_id = the identifier of the surface or mesh object that pulls points = list of 3D points Returns: list of 3D points Example: import rhinoscriptsyntax as rs surface = rs.GetObject("Select surface that pulls", rs.filter.surface) objects = rs.GetObjects("Select points to pull", rs.filter.point) points = [rs.PointCoordinates(obj) for obj in objects] results = rs.PullPoints( surface, points ) rs.AddPoints( results ) See Also: PullCurve """ id = rhutil.coerceguid(object_id, True) points = rhutil.coerce3dpointlist(points, True) mesh = rhutil.coercemesh(id, False) if mesh: points = mesh.PullPointsToMesh(points) return list(points) brep = rhutil.coercebrep(id, False) if brep and brep.Faces.Count == 1: tolerance = scriptcontext.doc.ModelAbsoluteTolerance points = brep.Faces[0].PullPointsToFace(points, tolerance) return list(points) return []
def ProjectPointToSurface(points, surface_ids, direction): """Projects one or more points onto one or more surfaces or polysurfaces Parameters: points = one or more 3D points surface_ids = identifiers of one or more surfaces/polysurfaces direction = direction vector to project the points Returns: list of projected points on success Example: import rhinoscriptsyntax as rs surface = rs.GetObject("Select surface to project onto", rs.filter.surface) objects = rs.GetObjects("Select points to project", rs.filter.point) points = [rs.PointCoordinates(obj) for obj in objects] results = rs.ProjectPointToSurface(points, surface, (0,0,-1)) rs.AddPoints(results) See Also: ProjectCurveToMesh ProjectCurveToSurface ProjectPointToMesh """ pts = rhutil.coerce3dpointlist(points) if pts is None: pts = [rhutil.coerce3dpoint(points, True)] direction = rhutil.coerce3dvector(direction, True) id = rhutil.coerceguid(surface_ids, False) if id: surface_ids = [id] breps = [rhutil.coercebrep(id, True) for id in surface_ids] tolerance = scriptcontext.doc.ModelAbsoluteTolerance return Rhino.Geometry.Intersect.Intersection.ProjectPointsToBreps(breps, pts, direction, tolerance)
def PullPoints(object_id, points): """Pulls an array of points to a surface or mesh object. For more information, see the Rhino help file Pull command Parameters: object_id = the identifier of the surface or mesh object that pulls points = list of 3D points Returns: list of 3D points Example: import rhinoscriptsyntax as rs surface = rs.GetObject("Select surface that pulls", rs.filter.surface) objects = rs.GetObjects("Select points to pull", rs.filter.point) points = [rs.PointCoordinates(obj) for obj in objects] results = rs.PullPoints( surface, points ) rs.AddPoints( results ) See Also: PullCurve """ id = rhutil.coerceguid(object_id, True) points = rhutil.coerce3dpointlist(points, True) mesh = rhutil.coercemesh(id, False) if mesh: points = mesh.PullPointsToMesh(points) return list(points) brep = rhutil.coercebrep(id, False) if brep and brep.Faces.Count==1: tolerance = scriptcontext.doc.ModelAbsoluteTolerance points = brep.Faces[0].PullPointsToFace(points, tolerance) return list(points) return []
def AddPointCloud(points, colors=None): """Adds point cloud object to the document Parameters: points ([point, ....]): list of values where every multiple of three represents a point colors ([color, ...]): list of colors to apply to each point Returns: guid: identifier of point cloud on success Example: import rhinoscriptsyntax as rs points = (0,0,0), (1,1,1), (2,2,2), (3,3,3) rs.AddPointCloud(points) See Also: IsPointCloud PointCloudCount PointCloudPoints """ points = rhutil.coerce3dpointlist(points, True) if colors and len(colors) == len(points): pc = Rhino.Geometry.PointCloud() for i in range(len(points)): color = rhutil.coercecolor(colors[i], True) pc.Add(points[i], color) points = pc rc = scriptcontext.doc.Objects.AddPointCloud(points) if rc == System.Guid.Empty: raise Exception("unable to add point cloud to document") scriptcontext.doc.Views.Redraw() return rc
def ObjectGripLocations(object_id, points=None): """Returns or modifies the location of all grips owned by an object. The locations of the grips are returned in a list of Point3d with each position in the list corresponding to that grip's index. To modify the locations of the grips, you must provide a list of points that contain the same number of points at grips Parameters: object_id = identifier of the object points [opt] = list of 3D points identifying the new grip locations Returns: if points is not specified, the current location of all grips if points is specified, the previous location of all grips None if not successful """ rhobj = rhutil.coercerhinoobject(object_id, True, True) if not rhobj.GripsOn: return scriptcontext.errorhandler() grips = rhobj.GetGrips() if grips is None: return scriptcontext.errorhandler() rc = [grip.CurrentLocation for grip in grips] if points and len(points)==len(grips): points = rhutil.coerce3dpointlist(points, True) for i, grip in enumerate(grips): point = points[i] grip.CurrentLocation = point scriptcontext.doc.Objects.GripUpdate(rhobj, True) scriptcontext.doc.Views.Redraw() return rc
def AddPointCloud(points, colors=None): """Adds point cloud object to the document Parameters: points = list of values where every multiple of three represents a point colors[opt] = list of colors to apply to each point Returns: identifier of point cloud on success Example: import rhinoscriptsyntax as rs points = (0,0,0), (1,1,1), (2,2,2), (3,3,3) rs.AddPointCloud(points) See Also: IsPointCloud PointCloudCount PointCloudPoints """ points = rhutil.coerce3dpointlist(points, True) if colors and len(colors)==len(points): pc = Rhino.Geometry.PointCloud() for i in range(len(points)): color = rhutil.coercecolor(colors[i],True) pc.Add(points[i],color) points = pc rc = scriptcontext.doc.Objects.AddPointCloud(points) if rc==System.Guid.Empty: raise Exception("unable to add point cloud to document") scriptcontext.doc.Views.Redraw() return rc
def ObjectGripLocations(object_id, points=None): """Returns or modifies the location of all grips owned by an object. The locations of the grips are returned in a list of Point3d with each position in the list corresponding to that grip's index. To modify the locations of the grips, you must provide a list of points that contain the same number of points at grips Parameters: object_id = identifier of the object points [opt] = list of 3D points identifying the new grip locations Returns: if points is not specified, the current location of all grips if points is specified, the previous location of all grips None if not successful """ rhobj = rhutil.coercerhinoobject(object_id, True, True) if not rhobj.GripsOn: return scriptcontext.errorhandler() grips = rhobj.GetGrips() if grips is None: return scriptcontext.errorhandler() rc = [grip.CurrentLocation for grip in grips] if points and len(points) == len(grips): points = rhutil.coerce3dpointlist(points, True) for i, grip in enumerate(grips): point = points[i] grip.CurrentLocation = point scriptcontext.doc.Objects.GripUpdate(rhobj, True) scriptcontext.doc.Views.Redraw() return rc
def PointArrayClosestPoint(points, test_point): """Finds the point in a list of 3D points that is closest to a test point Parameters: points = list of points test_point = the point to compare against Returns: index of the element in the point list that is closest to the test point Example: import rhinoscriptsyntax as rs cloud= rs.GetObject("Select point cloud") if cloud: point = rs.GetPoint("Point to test") if point: cloud = rs.PointCloudPoints(cloud) index = rs.PointArrayClosestPoint(cloud, point) if index is not None: point_id = rs.AddPoint(cloud[index]) rs.SelectObject( point_id ) See Also: CurveClosestPoint SurfaceClosestPoint """ points = rhutil.coerce3dpointlist(points, True) test_point = rhutil.coerce3dpoint(test_point, True) index = Rhino.Collections.Point3dList.ClosestIndexInList( points, test_point) if index >= 0: return index
def ProjectPointToMesh(points, mesh_ids, direction): """Projects one or more points onto one or more meshes Parameters: points = one or more 3D points mesh_ids = identifiers of one or more meshes direction = direction vector to project the points Returns: list of projected points on success Example: import rhinoscriptsyntax as rs mesh = rs.GetObject("Select mesh to project onto", rs.filter.mesh) objects = rs.GetObjects("Select points to project", rs.filter.point) points = [rs.PointCoordinates(obj) for obj in objects] # project down... results = rs.ProjectPointToMesh(points, mesh, (0,0,-1)) rs.AddPoints( results ) See Also: ProjectCurveToMesh ProjectCurveToSurface ProjectPointToSurface """ pts = rhutil.coerce3dpointlist(points, False) if pts is None: pts = [rhutil.coerce3dpoint(points, True)] direction = rhutil.coerce3dvector(direction, True) id = rhutil.coerceguid(mesh_ids, False) if id: mesh_ids = [id] meshes = [rhutil.coercemesh(id, True) for id in mesh_ids] tolerance = scriptcontext.doc.ModelAbsoluteTolerance rc = Rhino.Geometry.Intersect.Intersection.ProjectPointsToMeshes( meshes, pts, direction, tolerance) return rc
def PointsAreCoplanar(points, tolerance=1.0e-12): """Verifies that a list of 3D points are coplanar Parameters: points = list of 3D points tolerance[opt] = tolerance to use when verifying Returns: True or False """ points = rhutil.coerce3dpointlist(points, True) return Rhino.Geometry.Point3d.ArePointsCoplanar(points, tolerance)
def AddLeader(points, view_or_plane=None, text=None): """Adds a leader to the document. Leader objects are planar. The 3D points passed to this function should be co-planar Parameters: points = list of (at least 2) 3D points view_or_plane [opt] = If a view is specified, points will be constrained to the view's construction plane. If a view is not specified, points will be constrained to a plane fit through the list of points text [opt] = leader's text string Returns: identifier of the new leader on success None on error Example: import rhinoscriptsyntax as rs points = rs.GetPoints(True, False, "Select leader points") if points: rs.AddLeader( points ) See Also: IsLeader LeaderText """ points = rhutil.coerce3dpointlist(points) if points is None or len(points) < 2: raise ValueError("points must have at least two items") rc = System.Guid.Empty view = None if text and not isinstance(text, str): text = str(text) if not view_or_plane: if len(points) == 2: plane = scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane( ) rc = scriptcontext.doc.Objects.AddLeader( text, plane, [Rhino.Geometry.Point2d(p.X, p.Y) for p in points]) else: rc = scriptcontext.doc.Objects.AddLeader(text, points) else: plane = rhutil.coerceplane(view_or_plane) if not plane: view = __viewhelper(view_or_plane) plane = view.ActiveViewport.ConstructionPlane() points2d = [] for point in points: cprc, s, t = plane.ClosestParameter(point) if not cprc: return scriptcontext.errorhandler() points2d.append(Rhino.Geometry.Point2d(s, t)) if text is None: rc = scriptcontext.doc.Objects.AddLeader(plane, points2d) else: if not isinstance(text, str): text = str(text) rc = scriptcontext.doc.Objects.AddLeader(text, plane, points2d) if rc == System.Guid.Empty: return scriptcontext.errorhandler() scriptcontext.doc.Views.Redraw() return rc
def PointCloudClosestPoints(pt_cloud, needle_points, distance): """Returns a list of lists of point indices in a point cloud that are closest to needle_points. Each inner list references all points within or on the surface of a sphere of distance radius. Parameters: pt_cloud (guid|[point, ...]): the point cloud to be searched, or the "hay stack". This can also be a list of points. needle_points (guid|[point, ...]): a list of points to search in the point_cloud. This can also be specified as a point cloud. distance (float): the included limit for listing points. Returns: [[int, ...], ...]: a list of lists with the indices of the found points. Example: import rhinoscriptsyntax as rs id = rs.GetObject("Select point cloud", rs.filter.pointcloud) if id: result = rs.PointCloudClosestPoints(id, [[0,0,0]], 1.0) if result and result[0]: print("The first point next to origin within a 1.0 unit radius is: %s." % result[0][0]) else: print("There is no point in the point cloud within a 1.0 unit radius sphere from origin.") See Also: AddPointCloud IsPointCloud PointCloudPoints """ needles = rhutil.coercegeometry(needle_points, False) if isinstance(needles, Rhino.Geometry.PointCloud): needles = needles.AsReadOnlyListOfPoints() else: needles = rhutil.coerce3dpointlist(needle_points, True) pc_geom = rhutil.coercegeometry(pt_cloud, False) if isinstance(pc_geom, Rhino.Geometry.PointCloud): return __simplify_PointCloudClosestPoints( Rhino.Geometry.RTree.PointCloudClosestPoints( pc_geom, needles, distance)) if isinstance( pt_cloud, System.Collections.Generic.IEnumerable[Rhino.Geometry.Point3d]): return __simplify_PointCloudClosestPoints( Rhino.Geometry.RTree.Point3dClosestPoints(pt_cloud, needles, distance)) pts = rhutil.coerce3dpointlist(pt_cloud, True) return __simplify_PointCloudClosestPoints( Rhino.Geometry.RTree.Point3dClosestPoints(pts, needles, distance))
def PlaneFitFromPoints(points): """Returns a plane that was fit through an array of 3D points. Parameters: points = An array of 3D points. Returns: The plane if successful None if not successful """ points = rhutil.coerce3dpointlist(points, True) rc, plane = Rhino.Geometry.Plane.FitPlaneToPoints(points) if rc==Rhino.Geometry.PlaneFitResult.Success: return plane
def AddPoints(points): """Adds one or more point objects to the document Parameters: points = list of points Returns: list of Guid identifiers of the new objects on success """ points = rhutil.coerce3dpointlist(points, True) rc = [scriptcontext.doc.Objects.AddPoint(point) for point in points] scriptcontext.doc.Views.Redraw() return rc
def PointArrayTransform(points, xform): """Transforms a list of 3D points Parameters: points = list of 3D points xform = transformation to apply Returns: list of transformed points on success """ points = rhutil.coerce3dpointlist(points, True) xform = rhutil.coercexform(xform, True) return [xform*point for point in points]
def PointArrayBoundingBox(points, view_or_plane=None, in_world_coords=True): """Returns either a world axis-aligned or a construction plane axis-aligned bounding box of an array of 3-D point locations. Parameters: points = A list of 3-D points view_or_plane[opt] = Title or id of the view that contains the construction plane to which the bounding box should be aligned -or- user defined plane. If omitted, a world axis-aligned bounding box will be calculated in_world_coords[opt] = return the bounding box as world coordinates or construction plane coordinates. Note, this option does not apply to world axis-aligned bounding boxes. Returns: Eight 3D points that define the bounding box. Points returned in counter- clockwise order starting with the bottom rectangle of the box. None on error Example: See Also: BoundingBox """ points = rhutil.coerce3dpointlist(points) if not points: return None bbox = Rhino.Geometry.BoundingBox(points) xform = None plane = rhutil.coerceplane(view_or_plane) if plane is None and view_or_plane: view = view_or_plane modelviews = scriptcontext.doc.Views.GetStandardRhinoViews() for item in modelviews: viewport = item.MainViewport if type(view) is str and viewport.Name == view: plane = viewport.ConstructionPlane() break elif type(view) is System.Guid and viewport.Id == view: plane = viewport.ConstructionPlane() break if plane is None: return scriptcontext.errorhandler() if plane: xform = Rhino.Geometry.Transform.ChangeBasis( Rhino.Geometry.Plane.WorldXY, plane) bbox = xform.TransformBoundingBox(bbox) if not bbox.IsValid: return scriptcontext.errorhandler() corners = list(bbox.GetCorners()) if in_world_coords and plane is not None: plane_to_world = Rhino.Geometry.Transform.ChangeBasis( plane, Rhino.Geometry.Plane.WorldXY) for pt in corners: pt.Transform(plane_to_world) return corners
def AddPointCloud(points): """Adds point cloud object to the document Parameters: points = list of values where every multiple of three represents a point Returns: identifier of point cloud on success """ points = rhutil.coerce3dpointlist(points, True) rc = scriptcontext.doc.Objects.AddPointCloud(points) if rc==System.Guid.Empty: raise Exception("unable to add point cloud to document") scriptcontext.doc.Views.Redraw() return rc
def PointArrayClosestPoint(points, test_point): """Finds the point in a list of 3D points that is closest to a test point Parameters: points = list of points test_point = the point to compare against Returns: index of the element in the point list that is closest to the test point """ points = rhutil.coerce3dpointlist(points, True) test_point = rhutil.coerce3dpoint(test_point, True) index = Rhino.Collections.Point3dList.ClosestIndexInList(points, test_point) if index>=0: return index
def AddPointCloud(points): """Adds point cloud object to the document Parameters: points = list of values where every multiple of three represents a point Returns: identifier of point cloud on success """ points = rhutil.coerce3dpointlist(points, True) rc = scriptcontext.doc.Objects.AddPointCloud(points) if rc == System.Guid.Empty: raise Exception("unable to add point cloud to document") scriptcontext.doc.Views.Redraw() return rc
def AddLeader(points, view_or_plane=None, text=None): """Adds a leader to the document. Leader objects are planar. The 3D points passed to this function should be co-planar Parameters: points = list of (at least 2) 3D points view_or_plane [opt] = If a view is specified, points will be constrained to the view's construction plane. If a view is not specified, points will be constrained to a plane fit through the list of points text [opt] = leader's text string Returns: identifier of the new leader on success None on error Example: import rhinoscriptsyntax as rs points = rs.GetPoints(True, False, "Select leader points") if points: rs.AddLeader( points ) See Also: IsLeader LeaderText """ points = rhutil.coerce3dpointlist(points) if points is None or len(points)<2: raise ValueError("points must have at least two items") rc = System.Guid.Empty view = None if text and not isinstance(text, str): text = str(text) if not view_or_plane: if len(points) == 2: plane = scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane() rc = scriptcontext.doc.Objects.AddLeader(text, plane, [Rhino.Geometry.Point2d(p.X, p.Y) for p in points]) else: rc = scriptcontext.doc.Objects.AddLeader(text, points) else: plane = rhutil.coerceplane(view_or_plane) if not plane: view = __viewhelper(view_or_plane) plane = view.ActiveViewport.ConstructionPlane() points2d = [] for point in points: cprc, s, t = plane.ClosestParameter( point ) if not cprc: return scriptcontext.errorhandler() points2d.append( Rhino.Geometry.Point2d(s,t) ) if text is None: rc = scriptcontext.doc.Objects.AddLeader(plane, points2d) else: if not isinstance(text, str): text = str(text) rc = scriptcontext.doc.Objects.AddLeader(text, plane, points2d) if rc==System.Guid.Empty: return scriptcontext.errorhandler() scriptcontext.doc.Views.Redraw() return rc
def PointArrayBoundingBox(points, view_or_plane=None, in_world_coords=True): """Returns either a world axis-aligned or a construction plane axis-aligned bounding box of an array of 3-D point locations. Parameters: points = A list of 3-D points view_or_plane[opt] = Title or id of the view that contains the construction plane to which the bounding box should be aligned -or- user defined plane. If omitted, a world axis-aligned bounding box will be calculated in_world_coords[opt] = return the bounding box as world coordinates or construction plane coordinates. Note, this option does not apply to world axis-aligned bounding boxes. Returns: Eight 3D points that define the bounding box. Points returned in counter- clockwise order starting with the bottom rectangle of the box. None on error Example: See Also: BoundingBox """ points = rhutil.coerce3dpointlist(points) if not points: return None bbox = Rhino.Geometry.BoundingBox(points) xform = None plane = rhutil.coerceplane(view_or_plane) if plane is None and view_or_plane: view = view_or_plane modelviews = scriptcontext.doc.Views.GetStandardRhinoViews() for item in modelviews: viewport = item.MainViewport if type(view) is str and viewport.Name==view: plane = viewport.ConstructionPlane() break elif type(view) is System.Guid and viewport.Id==view: plane = viewport.ConstructionPlane() break if plane is None: return scriptcontext.errorhandler() if plane: xform = Rhino.Geometry.Transform.ChangeBasis(Rhino.Geometry.Plane.WorldXY, plane) bbox = xform.TransformBoundingBox(bbox) if not bbox.IsValid: return scriptcontext.errorhandler() corners = list(bbox.GetCorners()) if in_world_coords and plane is not None: plane_to_world = Rhino.Geometry.Transform.ChangeBasis(plane, Rhino.Geometry.Plane.WorldXY) for pt in corners: pt.Transform(plane_to_world) return corners
def LinePlaneIntersection(line, plane): """Calculates the intersection of a line and a plane. Parameters: line = Two 3D points identifying the starting and ending points of the line to intersect. plane = The plane to intersect. Returns: The 3D point of intersection is successful. None if not successful, or on error. """ plane = rhutil.coerceplane(plane, True) line_points = rhutil.coerce3dpointlist(line, True) line = Rhino.Geometry.Line(line_points[0], line_points[1]) rc, t = Rhino.Geometry.Intersect.Intersection.LinePlane(line, plane) if not rc: return scriptcontext.errorhandler() return line.PointAt(t)
def AddPoints(points): """Adds one or more point objects to the document Parameters: points ([point, ...]): list of points Returns: list(guid, ...): identifiers of the new objects on success Example: import rhinoscriptsyntax as rs points = rs.GetPoints(True, True, "Select points") if points: rs.AddPoints(points) See Also: AddPoint AddPointCloud """ points = rhutil.coerce3dpointlist(points, True) rc = [scriptcontext.doc.Objects.AddPoint(point) for point in points] scriptcontext.doc.Views.Redraw() return rc
def AddPoints(points): """Adds one or more point objects to the document Parameters: points = list of points Returns: list of Guid identifiers of the new objects on success Example: import rhinoscriptsyntax as rs points = rs.GetPoints(True, True, "Select points") if points: rs.AddPoints(points) See Also: AddPoint AddPointCloud """ points = rhutil.coerce3dpointlist(points, True) rc = [scriptcontext.doc.Objects.AddPoint(point) for point in points] scriptcontext.doc.Views.Redraw() return rc
def ProjectPointToSurface(points, surface_ids, direction): """Projects one or more points onto one or more surfaces or polysurfaces Parameters: points = one or more 3D points surface_ids = identifiers of one or more surfaces/polysurfaces direction = direction vector to project the points Returns: list of projected points on success """ pts = rhutil.coerce3dpointlist(points) if pts is None: pts = [rhutil.coerce3dpoint(points, True)] direction = rhutil.coerce3dvector(direction, True) id = rhutil.coerceguid(surface_ids, False) if id: surface_ids = [id] breps = [rhutil.coercebrep(id, True) for id in surface_ids] tolerance = scriptcontext.doc.ModelAbsoluteTolerance return Rhino.Geometry.Intersect.Intersection.ProjectPointsToBreps(breps, pts, direction, tolerance)
def AddPointCloud(points, colors=None): """Adds point cloud object to the document Parameters: points = list of values where every multiple of three represents a point colors[opt] = list of colors to apply to each point Returns: identifier of point cloud on success """ points = rhutil.coerce3dpointlist(points, True) if colors and len(colors)==len(points): pc = Rhino.Geometry.PointCloud() for i in range(len(points)): color = rhutil.coercecolor(colors[i],True) pc.Add(points[i],color) points = pc rc = scriptcontext.doc.Objects.AddPointCloud(points) if rc==System.Guid.Empty: raise Exception("unable to add point cloud to document") scriptcontext.doc.Views.Redraw() return rc
def ProjectPointToMesh(points, mesh_ids, direction): """Projects one or more points onto one or more meshes Parameters: points = one or more 3D points mesh_ids = identifiers of one or more meshes direction = direction vector to project the points Returns: list of projected points on success """ pts = rhutil.coerce3dpointlist(points, False) if pts is None: pts = [rhutil.coerce3dpoint(points, True)] direction = rhutil.coerce3dvector(direction, True) id = rhutil.coerceguid(mesh_ids, False) if id: mesh_ids = [id] meshes = [rhutil.coercemesh(id, True) for id in mesh_ids] tolerance = scriptcontext.doc.ModelAbsoluteTolerance rc = Rhino.Geometry.Intersect.Intersection.ProjectPointsToMeshes(meshes, pts, direction, tolerance) return rc
def PointArrayTransform(points, xform): """Transforms a list of 3D points Parameters: points = list of 3D points xform = transformation to apply Returns: list of transformed points on success Example: import rhinoscriptsyntax as rs obj = rs.GetObject("Select object") points = rs.BoundingBox(obj) xform = rs.XformRotation2(45.0, (0,0,1), (0,0,0)) points = rs.PointArrayTransform(points, xform) rs.AddPoints(points) See Also: PointArrayClosestPoint """ points = rhutil.coerce3dpointlist(points, True) xform = rhutil.coercexform(xform, True) return [xform*point for point in points]
def PointArrayTransform(points, xform): """Transforms a list of 3D points Parameters: points = list of 3D points xform = transformation to apply Returns: list of transformed points on success Example: import rhinoscriptsyntax as rs obj = rs.GetObject("Select object") points = rs.BoundingBox(obj) xform = rs.XformRotation2(45.0, (0,0,1), (0,0,0)) points = rs.PointArrayTransform(points, xform) rs.AddPoints(points) See Also: PointArrayClosestPoint """ points = rhutil.coerce3dpointlist(points, True) xform = rhutil.coercexform(xform, True) return [xform * point for point in points]
def PullPoints(object_id, points): """Pulls an array of points to a surface or mesh object. For more information, see the Rhino help file Pull command Parameters: object_id = the identifier of the surface or mesh object that pulls points = list of 3D points Returns: list of 3D points """ id = rhutil.coerceguid(object_id, True) points = rhutil.coerce3dpointlist(points, True) mesh = rhutil.coercemesh(id, False) if mesh: points = mesh.PullPointsToMesh(points) return list(points) brep = rhutil.coercebrep(id, False) if brep and brep.Faces.Count==1: tolerance = scriptcontext.doc.ModelAbsoluteTolerance points = brep.Faces[0].PullPointsToFace(points, tolerance) return list(points) return []
def LinePlaneIntersection(line, plane): """Calculates the intersection of a line and a plane. Parameters: line ([point, point]): Two 3D points identifying the starting and ending points of the line to intersect. plane (plane): The plane to intersect. Returns: point: The 3D point of intersection is successful. None: if not successful, or on error. Example: import rhinoscriptsyntax as rs plane = rs.WorldXYPlane() point = rs.LinePlaneIntersection(line, plane) See Also: LineLineIntersection PlanePlaneIntersection """ plane = rhutil.coerceplane(plane, True) line_points = rhutil.coerce3dpointlist(line, True) line = Rhino.Geometry.Line(line_points[0], line_points[1]) rc, t = Rhino.Geometry.Intersect.Intersection.LinePlane(line, plane) if not rc: return scriptcontext.errorhandler() return line.PointAt(t)
def ObjectGripLocations(object_id, points=None): """Returns or modifies the location of all grips owned by an object. The locations of the grips are returned in a list of Point3d with each position in the list corresponding to that grip's index. To modify the locations of the grips, you must provide a list of points that contain the same number of points at grips Parameters: object_id (guid): identifier of the object points ([point, ...], optional) list of 3D points identifying the new grip locations Returns: list(point, ...): if points is not specified, the current location of all grips list(point, ...): if points is specified, the previous location of all grips None: if not successful Example: import rhinoscriptsyntax as rs obj = rs.GetObject("Select curve", rs.filter.curve) if obj: rs.EnableObjectGrips( obj ) points = rs.ObjectGripLocations(obj) for point in points: print point See Also: EnableObjectGrips ObjectGripCount ObjectGripLocation """ rhobj = rhutil.coercerhinoobject(object_id, True, True) if not rhobj.GripsOn: return scriptcontext.errorhandler() grips = rhobj.GetGrips() if grips is None: return scriptcontext.errorhandler() rc = [grip.CurrentLocation for grip in grips] if points and len(points) == len(grips): points = rhutil.coerce3dpointlist(points, True) for i, grip in enumerate(grips): point = points[i] grip.CurrentLocation = point scriptcontext.doc.Objects.GripUpdate(rhobj, True) scriptcontext.doc.Views.Redraw() return rc
def PlaneFitFromPoints(points): """Returns a plane that was fit through an array of 3D points. Parameters: points = An array of 3D points. Returns: The plane if successful None if not successful Example: import rhinoscriptsyntax as rs points = rs.GetPoints() if points: plane = rs.PlaneFitFromPoints(points) if plane: magX = plane.XAxis.Length magY = plane.YAxis.Length rs.AddPlaneSurface( plane, magX, magY ) See Also: PlaneFromFrame PlaneFromNormal PlaneFromPoints """ points = rhutil.coerce3dpointlist(points, True) rc, plane = Rhino.Geometry.Plane.FitPlaneToPoints(points) if rc==Rhino.Geometry.PlaneFitResult.Success: return plane