/Scripts/rhinoscript/surface.py
http://github.com/mcneel/rhinopython · Python · 3346 lines · 2528 code · 8 blank · 810 comment · 255 complexity · f807e40fc8ae9ef505581397f8f1e264 MD5 · raw file
Large files are truncated click here to view the full file
- import scriptcontext
- import math
- import Rhino
- import System.Guid
- import utility as rhutil
- import object as rhobject
- from System.Collections.Generic import List
- def AddBox(corners):
- """Adds a box shaped polysurface to the document
- Parameters:
- corners ([point, point, point ,point, point, point ,point,point]) 8 points that define the corners of the box. Points need to
- be in counter-clockwise order starting with the bottom rectangle of the box
- Returns:
- guid: identifier of the new object on success
- Example:
- import rhinoscriptsyntax as rs
- box = rs.GetBox()
- if box: rs.AddBox(box)
- See Also:
- AddCone
- AddCylinder
- AddSphere
- AddTorus
- """
- box = rhutil.coerce3dpointlist(corners, True)
- brep = Rhino.Geometry.Brep.CreateFromBox(box)
- if not brep: raise ValueError("unable to create brep from box")
- rc = scriptcontext.doc.Objects.AddBrep(brep)
- if rc==System.Guid.Empty: raise Exception("unable to add brep to document")
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddCone(base, height, radius, cap=True):
- """Adds a cone shaped polysurface to the document
- Parameters:
- base (point|plane): 3D origin point of the cone or a plane with an apex at the origin
- and normal along the plane's z-axis
- height (point|number): 3D height point of the cone if base is a 3D point. The height
- point defines the height and direction of the cone. If base is a
- plane, height is a numeric value
- radius (number): the radius at the base of the cone
- cap (bool, optional): cap base of the cone
- Returns:
- guid: identifier of the new object on success
- Example:
- import rhinoscriptsyntax as rs
- radius = 5.0
- base = rs.GetPoint("Base of cone")
- if base:
- height = rs.GetPoint("Height of cone", base)
- if height: rs.AddCone(base, height, radius)
- See Also:
- AddBox
- AddCylinder
- AddSphere
- AddTorus
- """
- plane = None
- height_point = rhutil.coerce3dpoint(height)
- if height_point is None:
- plane = rhutil.coerceplane(base, True)
- else:
- base_point = rhutil.coerce3dpoint(base, True)
- normal = base_point - height_point
- height = normal.Length
- plane = Rhino.Geometry.Plane(height_point, normal)
- cone = Rhino.Geometry.Cone(plane, height, radius)
- brep = Rhino.Geometry.Brep.CreateFromCone(cone, cap)
- rc = scriptcontext.doc.Objects.AddBrep(brep)
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddCutPlane(object_ids, start_point, end_point, normal=None):
- """Adds a planar surface through objects at a designated location. For more
- information, see the Rhino help file for the CutPlane command
- Parameters:
- objects_ids ([guid, ...]): identifiers of objects that the cutting plane will
- pass through
- start_point, end_point (line): line that defines the cutting plane
- normal (vector, optional): vector that will be contained in the returned planar
- surface. In the case of Rhino's CutPlane command, this is the
- normal to, or Z axis of, the active view's construction plane.
- If omitted, the world Z axis is used
- Returns:
- guid: identifier of new object on success
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- objs = rs.GetObjects("Select objects for cut plane")
- if objs:
- point0 = rs.GetPoint("Start of cut plane")
- if point0:
- point1 = rs.GetPoint("End of cut plane", point0)
- if point1: rs.AddCutPlane( objs, point0, point1 )
- See Also:
- AddPlaneSurface
- """
- objects = []
- bbox = Rhino.Geometry.BoundingBox.Unset
- for id in object_ids:
- rhobj = rhutil.coercerhinoobject(id, True, True)
- geometry = rhobj.Geometry
- bbox.Union( geometry.GetBoundingBox(True) )
- start_point = rhutil.coerce3dpoint(start_point, True)
- end_point = rhutil.coerce3dpoint(end_point, True)
- if not bbox.IsValid: return scriptcontext.errorhandler()
- line = Rhino.Geometry.Line(start_point, end_point)
- if normal: normal = rhutil.coerce3dvector(normal, True)
- else: normal = scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane().Normal
- surface = Rhino.Geometry.PlaneSurface.CreateThroughBox(line, normal, bbox)
- if surface is None: return scriptcontext.errorhandler()
- id = scriptcontext.doc.Objects.AddSurface(surface)
- if id==System.Guid.Empty: return scriptcontext.errorhandler()
- scriptcontext.doc.Views.Redraw()
- return id
- def AddCylinder(base, height, radius, cap=True):
- """Adds a cylinder-shaped polysurface to the document
- Parameters:
- base (point|plane): The 3D base point of the cylinder or the base plane of the cylinder
- height (point|number): if base is a point, then height is a 3D height point of the
- cylinder. The height point defines the height and direction of the
- cylinder. If base is a plane, then height is the numeric height value
- of the cylinder
- radius (number): radius of the cylinder
- cap (bool, optional): cap the cylinder
- Returns:
- guid: identifier of new object if successful
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- radius = 5.0
- base = rs.GetPoint("Base of cylinder")
- if base:
- height = rs.GetPoint("Height of cylinder", base)
- if height: rs.AddCylinder( base, height, radius )
- See Also:
- AddBox
- AddCone
- AddSphere
- AddTorus
- """
- cylinder=None
- height_point = rhutil.coerce3dpoint(height)
- if height_point:
- #base must be a point
- base = rhutil.coerce3dpoint(base, True)
- normal = height_point-base
- plane = Rhino.Geometry.Plane(base, normal)
- height = normal.Length
- circle = Rhino.Geometry.Circle(plane, radius)
- cylinder = Rhino.Geometry.Cylinder(circle, height)
- else:
- #base must be a plane
- if type(base) is Rhino.Geometry.Point3d: base = [base.X, base.Y, base.Z]
- base = rhutil.coerceplane(base, True)
- circle = Rhino.Geometry.Circle(base, radius)
- cylinder = Rhino.Geometry.Cylinder(circle, height)
- brep = cylinder.ToBrep(cap, cap)
- id = scriptcontext.doc.Objects.AddBrep(brep)
- if id==System.Guid.Empty: return scriptcontext.errorhandler()
- scriptcontext.doc.Views.Redraw()
- return id
- def AddEdgeSrf(curve_ids):
- """Creates a surface from 2, 3, or 4 edge curves
- Parameters:
- curve_ids ([guid, ...]): list or tuple of curves
- Returns:
- guid: identifier of new object if successful
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- curves = rs.GetObjects("Select 2, 3, or 4 curves", rs.filter.curve)
- if curves and len(curves)>1 ): rs.AddEdgeSrf(curves)
- See Also:
- AddPlanarSrf
- AddSrfControlPtGrid
- AddSrfPt
- AddSrfPtGrid
- """
- curves = [rhutil.coercecurve(id, -1, True) for id in curve_ids]
- brep = Rhino.Geometry.Brep.CreateEdgeSurface(curves)
- if brep is None: return scriptcontext.errorhandler()
- id = scriptcontext.doc.Objects.AddBrep(brep)
- if id==System.Guid.Empty: return scriptcontext.errorhandler()
- scriptcontext.doc.Views.Redraw()
- return id
- def AddNetworkSrf(curves, continuity=1, edge_tolerance=0, interior_tolerance=0, angle_tolerance=0):
- """Creates a surface from a network of crossing curves
- Parameters:
- curves ([guid, ...]): curves from which to create the surface
- continuity (number, optional): how the edges match the input geometry
- 0 = loose
- 1 = position
- 2 = tangency
- 3 = curvature
- Returns:
- guid: identifier of new object if successful
- Example:
- import rhinoscriptsyntax as rs
- curve_ids = rs.GetObjects("Select curves in network", 4, True, True)
- if len(curve_ids) > 0:
- rs.AddNetworkSrf(curve_ids)
- See Also:
-
- """
- curves = [rhutil.coercecurve(curve, -1, True) for curve in curves]
- surf, err = Rhino.Geometry.NurbsSurface.CreateNetworkSurface(curves, continuity, edge_tolerance, interior_tolerance, angle_tolerance)
- if surf:
- rc = scriptcontext.doc.Objects.AddSurface(surf)
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddNurbsSurface(point_count, points, knots_u, knots_v, degree, weights=None):
- """Adds a NURBS surface object to the document
- Parameters:
- point_count ([number, number]) number of control points in the u and v direction
- points ({point, ...]): list of 3D points
- knots_u ([number, ...]): knot values for the surface in the u direction.
- Must contain point_count[0]+degree[0]-1 elements
- knots_v ([number, ...]): knot values for the surface in the v direction.
- Must contain point_count[1]+degree[1]-1 elements
- degree ([number, number]): degree of the surface in the u and v directions.
- weights [(number, ...]): weight values for the surface. The number of elements in
- weights must equal the number of elements in points. Values must be
- greater than zero.
- Returns:
- guid: identifier of new object if successful
- None on error
- Example:
- import rhinoscriptsyntax as rs
- obj = rs.GetObject("Pick a surface", rs.filter.surface)
- if obj:
- point_count = rs.SurfacePointCount(obj)
- points = rs.SurfacePoints(obj)
- knots = rs.SurfaceKnots(obj)
- degree = rs.SurfaceDegree(obj)
- if rs.IsSurfaceRational(obj):
- weights = rs.SurfaceWeights(obj)
- obj = rs.AddNurbsSurface(point_count, points, knots[0], knots[1], degree, weights)
- else:
- obj = rs.AddNurbsSurface(point_count, points, knots[0], knots[1], degree)
- if obj: rs.SelectObject(obj)
- See Also:
- IsSurfaceRational
- SurfaceDegree
- SurfaceKnotCount
- SurfaceKnots
- SurfacePointCount
- SurfacePoints
- SurfaceWeights
- """
- if len(points)<(point_count[0]*point_count[1]):
- return scriptcontext.errorhandler()
- ns = Rhino.Geometry.NurbsSurface.Create(3, weights!=None, degree[0]+1, degree[1]+1, point_count[0], point_count[1])
- #add the points and weights
- controlpoints = ns.Points
- index = 0
- for i in range(point_count[0]):
- for j in range(point_count[1]):
- if weights:
- cp = Rhino.Geometry.ControlPoint(points[index], weights[index])
- controlpoints.SetControlPoint(i,j,cp)
- else:
- cp = Rhino.Geometry.ControlPoint(points[index])
- controlpoints.SetControlPoint(i,j,cp)
- index += 1
- #add the knots
- for i in range(ns.KnotsU.Count): ns.KnotsU[i] = knots_u[i]
- for i in range(ns.KnotsV.Count): ns.KnotsV[i] = knots_v[i]
- if not ns.IsValid: return scriptcontext.errorhandler()
- id = scriptcontext.doc.Objects.AddSurface(ns)
- if id==System.Guid.Empty: return scriptcontext.errorhandler()
- scriptcontext.doc.Views.Redraw()
- return id
- def AddPatch(object_ids, uv_spans_tuple_OR_surface_object_id, tolerance=None, trim=True, point_spacing=0.1, flexibility=1.0, surface_pull=1.0, fix_edges=False):
- """Fits a surface through curve, point, point cloud, and mesh objects.
- Parameters:
- object_ids ({guid, ...]): a list of object identifiers that indicate the objects to use for the patch fitting.
- Acceptable object types include curves, points, point clouds, and meshes.
- uv_spans_tuple_OR_surface_object_id ([number, number]|guid): the U and V direction span counts for the automatically generated surface OR
- The identifier of the starting surface. It is best if you create a starting surface that is similar in shape
- to the surface you are trying to create.
- tolerance (number, optional): The tolerance used by input analysis functions. If omitted, Rhino's document absolute tolerance is used.
- trim (bool, optional): Try to find an outside curve and trims the surface to it. The default value is True.
- point_spacing (number, optional): The basic distance between points sampled from input curves. The default value is 0.1.
- flexibility (number, optional): Determines the behavior of the surface in areas where its not otherwise controlled by the input.
- Lower numbers make the surface behave more like a stiff material, higher, more like a flexible material.
- That is, each span is made to more closely match the spans adjacent to it if there is no input geometry
- mapping to that area of the surface when the flexibility value is low. The scale is logarithmic.
- For example, numbers around 0.001 or 0.1 make the patch pretty stiff and numbers around 10 or 100
- make the surface flexible. The default value is 1.0.
- surface_pull (number, optional): Similar to stiffness, but applies to the starting surface. The bigger the pull, the closer
- the resulting surface shape will be to the starting surface. The default value is 1.0.
- fix_edges (bool, optional): Clamps the edges of the starting surface in place. This option is useful if you are using a
- curve or points for deforming an existing surface, and you do not want the edges of the starting surface
- to move. The default if False.
- Returns:
- guid: Identifier of the new surface object if successful.
- None: on error.
- Example:
- See Also:
- """
- # System.Collections.List instead of Python list because IronPython is
- # having problems converting a list to an IEnumerable<GeometryBase> which
- # is the 1st argument for Brep.CreatePatch
- geometry = List[Rhino.Geometry.GeometryBase]()
- u_span = 10
- v_span = 10
- rc = None
- id = rhutil.coerceguid(object_ids, False)
- if id: object_ids = [id]
- for object_id in object_ids:
- rhobj = rhutil.coercerhinoobject(object_id, False, False)
- if not rhobj: return None
- geometry.Add( rhobj.Geometry )
- if not geometry: return None
-
- surface = None
- if uv_spans_tuple_OR_surface_object_id:
- if type(uv_spans_tuple_OR_surface_object_id) is tuple:
- u_span = uv_spans_tuple_OR_surface_object_id[0]
- v_span = uv_spans_tuple_OR_surface_object_id[1]
- else:
- surface = rhutil.coercesurface(uv_spans_tuple_OR_surface_object_id, False)
- if not tolerance: tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- b = System.Array.CreateInstance(bool, 4)
- for i in range(4): b[i] = fix_edges
- brep = Rhino.Geometry.Brep.CreatePatch(geometry, surface, u_span, v_span, trim, False, point_spacing, flexibility, surface_pull, b, tolerance)
- if brep:
- rc = scriptcontext.doc.Objects.AddBrep(brep)
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddPipe(curve_id, parameters, radii, blend_type=0, cap=0, fit=False):
- """Creates a single walled surface with a circular profile around a curve
- Parameters:
- curve_id (guid): identifier of rail curve
- parameters, radii ([number, ...]): list of radius values at normalized curve parameters
- blend_type (number, optional): 0(local) or 1(global)
- cap (number, optional): 0(none), 1(flat), 2(round)
- fit (bool, optional): attempt to fit a single surface
- Returns:
- list(guid, ...): identifiers of new objects created
- Example:
- import rhinoscriptsyntax as rs
- curve = rs.GetObject("Select curve to create pipe around", rs.filter.curve, True)
- if curve:
- domain = rs.CurveDomain(curve)
- rs.AddPipe(curve, 0, 4)
- See Also:
-
- """
- rail = rhutil.coercecurve(curve_id, -1, True)
- abs_tol = scriptcontext.doc.ModelAbsoluteTolerance
- ang_tol = scriptcontext.doc.ModelAngleToleranceRadians
- if type(parameters) is int or type(parameters) is float: parameters = [parameters]
- if type(radii) is int or type(radii) is float: radii = [radii]
- parameters = map(float,parameters)
- radii = map(float,radii)
- cap = System.Enum.ToObject(Rhino.Geometry.PipeCapMode, cap)
- breps = Rhino.Geometry.Brep.CreatePipe(rail, parameters, radii, blend_type==0, cap, fit, abs_tol, ang_tol)
- rc = [scriptcontext.doc.Objects.AddBrep(brep) for brep in breps]
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddPlanarSrf(object_ids):
- """Creates one or more surfaces from planar curves
- Parameters:
- object_ids ({guid, ...]): curves to use for creating planar surfaces
- Returns:
- list(guid, ...): identifiers of surfaces created on success
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- objs = rs.GetObjects("Select planar curves to build surface", rs.filter.curve)
- if objs: rs.AddPlanarSrf(objs)
- See Also:
- AddEdgeSrf
- AddSrfControlPtGrid
- AddSrfPt
- AddSrfPtGrid
- """
- id = rhutil.coerceguid(object_ids, False)
- if id: object_ids = [id]
- curves = [rhutil.coercecurve(id,-1,True) for id in object_ids]
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- breps = Rhino.Geometry.Brep.CreatePlanarBreps(curves, tolerance)
- if breps:
- rc = [scriptcontext.doc.Objects.AddBrep(brep) for brep in breps]
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddPlaneSurface(plane, u_dir, v_dir):
- """Create a plane surface and add it to the document.
- Parameters:
- plane (plane): The plane.
- u_dir (number): The magnitude in the U direction.
- v_dir (number): The magnitude in the V direction.
- Returns:
- guid: The identifier of the new object if successful.
- None: if not successful, or on error.
- Example:
- import rhinoscriptsyntax as rs
- rs.AddPlaneSurface( rs.WorldXYPlane(), 5.0, 3.0 )
- See Also:
- AddCutPlane
- AddEdgeSrf
- AddSrfControlPtGrid
- AddSrfPt
- AddSrfPtGrid
- IsPlaneSurface
- """
- plane = rhutil.coerceplane(plane, True)
- u_interval = Rhino.Geometry.Interval(0, u_dir)
- v_interval = Rhino.Geometry.Interval(0, v_dir)
- plane_surface = Rhino.Geometry.PlaneSurface(plane, u_interval, v_interval)
- if plane_surface is None: return scriptcontext.errorhandler()
- rc = scriptcontext.doc.Objects.AddSurface(plane_surface)
- if rc==System.Guid.Empty: return scriptcontext.errorhandler()
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddLoftSrf(object_ids, start=None, end=None, loft_type=0, simplify_method=0, value=0, closed=False):
- """Adds a surface created by lofting curves to the document.
- - no curve sorting performed. pass in curves in the order you want them sorted
- - directions of open curves not adjusted. Use CurveDirectionsMatch and
- ReverseCurve to adjust the directions of open curves
- - seams of closed curves are not adjusted. Use CurveSeam to adjust the seam
- of closed curves
- Parameters:
- object_ids ({guid, guid, ...]): ordered list of the curves to loft through
- start (point, optional): starting point of the loft
- end (point, optional): ending point of the loft
- loft_type (number, optional): type of loft. Possible options are:
- 0 = Normal. Uses chord-length parameterization in the loft direction
- 1 = Loose. The surface is allowed to move away from the original curves
- to make a smoother surface. The surface control points are created
- at the same locations as the control points of the loft input curves.
- 2 = Straight. The sections between the curves are straight. This is
- also known as a ruled surface.
- 3 = Tight. The surface sticks closely to the original curves. Uses square
- root of chord-length parameterization in the loft direction
- 4 = Developable. Creates a separate developable surface or polysurface
- from each pair of curves.
- simplify_method (number, optional): Possible options are:
- 0 = None. Does not simplify.
- 1 = Rebuild. Rebuilds the shape curves before lofting. modified by `value` below
- 2 = Refit. Refits the shape curves to a specified tolerance. modified by `value` below
- value (number, optional): Additional value based on the specified `simplify_method`:
- Simplify - Description
- Rebuild(1) - then value is the number of control point used to rebuild
- Rebuild(1) - is specified and this argument is omitted, then curves will be
- rebuilt using 10 control points.
- Refit(2) - then value is the tolerance used to rebuild.
- Refit(2) - is specified and this argument is omitted, then the document's
- absolute tolerance us used for refitting.
- closed (bool, optional): close the loft back to the first curve
- Returns:
- list(guid, ...):Array containing the identifiers of the new surface objects if successful
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- objs = rs.GetObjects("Pick curves to loft", rs.filter.curve)
- if objs: rs.AddLoftSrf(objs)
- See Also:
- CurveDirectionsMatch
- CurveSeam
- ReverseCurve
- """
- if loft_type<0 or loft_type>5: raise ValueError("loft_type must be 0-4")
- if simplify_method<0 or simplify_method>2: raise ValueError("simplify_method must be 0-2")
- # get set of curves from object_ids
- curves = [rhutil.coercecurve(id,-1,True) for id in object_ids]
- if len(curves)<2: return scriptcontext.errorhandler()
- if start is None: start = Rhino.Geometry.Point3d.Unset
- if end is None: end = Rhino.Geometry.Point3d.Unset
- start = rhutil.coerce3dpoint(start, True)
- end = rhutil.coerce3dpoint(end, True)
-
- lt = Rhino.Geometry.LoftType.Normal
- if loft_type==1: lt = Rhino.Geometry.LoftType.Loose
- elif loft_type==2: lt = Rhino.Geometry.LoftType.Straight
- elif loft_type==3: lt = Rhino.Geometry.LoftType.Tight
- elif loft_type==4: lt = Rhino.Geometry.LoftType.Developable
- breps = None
- if simplify_method==0:
- breps = Rhino.Geometry.Brep.CreateFromLoft(curves, start, end, lt, closed)
- elif simplify_method==1:
- value = abs(value)
- rebuild_count = int(value)
- breps = Rhino.Geometry.Brep.CreateFromLoftRebuild(curves, start, end, lt, closed, rebuild_count)
- elif simplify_method==2:
- refit = abs(value)
- if refit==0: refit = scriptcontext.doc.ModelAbsoluteTolerance
- breps = Rhino.Geometry.Brep.CreateFromLoftRefit(curves, start, end, lt, closed, refit)
- if not breps: return scriptcontext.errorhandler()
- idlist = []
- for brep in breps:
- id = scriptcontext.doc.Objects.AddBrep(brep)
- if id!=System.Guid.Empty: idlist.append(id)
- if idlist: scriptcontext.doc.Views.Redraw()
- return idlist
- def AddRevSrf(curve_id, axis, start_angle=0.0, end_angle=360.0):
- """Create a surface by revolving a curve around an axis
- Parameters:
- curve_id (guid): identifier of profile curve
- axis (line): line for the rail revolve axis
- start_angle, end_angle (number, optional): start and end angles of revolve
- Returns:
- guid: identifier of new object if successful
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- curve = rs.AddLine((5,0,0), (10,0,10))
- rs.AddRevSrf( curve, ((0,0,0), (0,0,1)) )
- See Also:
-
- """
- curve = rhutil.coercecurve(curve_id, -1, True)
- axis = rhutil.coerceline(axis, True)
- start_angle = math.radians(start_angle)
- end_angle = math.radians(end_angle)
- srf = Rhino.Geometry.RevSurface.Create(curve, axis, start_angle, end_angle)
- if not srf: return scriptcontext.errorhandler()
- ns = srf.ToNurbsSurface()
- if not ns: return scriptcontext.errorhandler()
- rc = scriptcontext.doc.Objects.AddSurface(ns)
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddSphere(center_or_plane, radius):
- """Add a spherical surface to the document
- Parameters:
- center_or_plane (point|plane): center point of the sphere. If a plane is input,
- the origin of the plane will be the center of the sphere
- radius (number): radius of the sphere in the current model units
- Returns:
- guid: identifier of the new object on success
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- radius = 2
- center = rs.GetPoint("Center of sphere")
- if center: rs.AddSphere(center, radius)
- See Also:
- AddBox
- AddCone
- AddCylinder
- AddTorus
- """
- c_or_p = rhutil.coerce3dpoint(center_or_plane)
- if c_or_p is None:
- c_or_p = rhutil.coerceplane(center_or_plane)
- if c_or_p is None: return None
- sphere = Rhino.Geometry.Sphere(c_or_p, radius)
- rc = scriptcontext.doc.Objects.AddSphere(sphere)
- if rc==System.Guid.Empty: return scriptcontext.errorhandler()
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddSrfContourCrvs(object_id, points_or_plane, interval=None):
- """Adds a spaced series of planar curves resulting from the intersection of
- defined cutting planes through a surface or polysurface. For more
- information, see Rhino help for details on the Contour command
- Parameters:
- object_id (guid): object identifier to contour
- points_or_plane ([point,point]|plane): either a list/tuple of two points or a plane
- if two points, they define the start and end points of a center line
- if a plane, the plane defines the cutting plane
- interval (number, optional): distance between contour curves.
- Returns:
- guid: ids of new contour curves on success
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- obj = rs.GetObject("Select object", rs.filter.surface + rs.filter.polysurface)
- startpoint = rs.GetPoint("Base point of center line")
- endpoint = rs.GetPoint("Endpoint of center line", startpoint)
- rs.AddSrfContourCrvs( obj, (startpoint, endpoint) )
- See Also:
- CurveContourPoints
- """
- brep = rhutil.coercebrep(object_id)
- plane = rhutil.coerceplane(points_or_plane)
- curves = None
- if plane:
- curves = Rhino.Geometry.Brep.CreateContourCurves(brep, plane)
- else:
- start = rhutil.coerce3dpoint(points_or_plane[0], True)
- end = rhutil.coerce3dpoint(points_or_plane[1], True)
- if not interval:
- bbox = brep.GetBoundingBox(True)
- v = bbox.Max - bbox.Min
- interval = v.Length / 50.0
- curves = Rhino.Geometry.Brep.CreateContourCurves(brep, start, end, interval)
- rc = []
- for crv in curves:
- id = scriptcontext.doc.Objects.AddCurve(crv)
- if id!=System.Guid.Empty: rc.append(id)
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddSrfControlPtGrid(count, points, degree=(3,3)):
- """Creates a surface from a grid of points
- Parameters:
- count ([number, number])tuple of two numbers defining number of points in the u,v directions
- points ([point, ...]): list of 3D points
- degree ([number, number]): two numbers defining degree of the surface in the u,v directions
- Returns:
- guid: The identifier of the new object if successful.
- None: if not successful, or on error.
- Example:
- See Also:
- """
- points = rhutil.coerce3dpointlist(points, True)
- surf = Rhino.Geometry.NurbsSurface.CreateFromPoints(points, count[0], count[1], degree[0], degree[1])
- if not surf: return scriptcontext.errorhandler()
- id = scriptcontext.doc.Objects.AddSurface(surf)
- if id!=System.Guid.Empty:
- scriptcontext.doc.Views.Redraw()
- return id
- def AddSrfPt(points):
- """Creates a new surface from either 3 or 4 corner points.
- Parameters:
- points ([point, point, point, point]): list of either 3 or 4 corner points
- Returns:
- guid: The identifier of the new object if successful.
- None: if not successful, or on error.
- Example:
- import rhinoscriptsyntax as rs
- points = rs.GetPoints(True, message1="Pick 3 or 4 corner points")
- if points: rs.AddSrfPt(points)
- See Also:
- AddEdgeSrf
- AddSrfControlPtGrid
- AddSrfPtGrid
- """
- points = rhutil.coerce3dpointlist(points, True)
- surface=None
- if len(points)==3:
- surface = Rhino.Geometry.NurbsSurface.CreateFromCorners(points[0], points[1], points[2])
- elif len(points)==4:
- surface = Rhino.Geometry.NurbsSurface.CreateFromCorners(points[0], points[1], points[2], points[3])
- if surface is None: return scriptcontext.errorhandler()
- rc = scriptcontext.doc.Objects.AddSurface(surface)
- if rc==System.Guid.Empty: return scriptcontext.errorhandler()
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddSrfPtGrid(count, points, degree=(3,3), closed=(False,False)):
- """Creates a surface from a grid of points
- Parameters:
- count ([number, number}): tuple of two numbers defining number of points in the u,v directions
- points ([point, ...]): list of 3D points
- degree ([number, number], optional): two numbers defining degree of the surface in the u,v directions
- closed ([bool, bool], optional): two booleans defining if the surface is closed in the u,v directions
- Returns:
- guid: The identifier of the new object if successful.
- None: if not successful, or on error.
- Example:
- See Also:
- """
- points = rhutil.coerce3dpointlist(points, True)
- surf = Rhino.Geometry.NurbsSurface.CreateThroughPoints(points, count[0], count[1], degree[0], degree[1], closed[0], closed[1])
- if not surf: return scriptcontext.errorhandler()
- id = scriptcontext.doc.Objects.AddSurface(surf)
- if id!=System.Guid.Empty:
- scriptcontext.doc.Views.Redraw()
- return id
- def AddSweep1(rail, shapes, closed=False):
- """Adds a surface created through profile curves that define the surface
- shape and one curve that defines a surface edge.
- Parameters:
- rail (guid): identifier of the rail curve
- shapes ([guid, ...]): one or more cross section shape curves
- closed (bool, optional): If True, then create a closed surface
- Returns:
- list(guid, ...): of new surface objects if successful
- None: if not successful, or on error
- Example:
- import rhinoscriptsyntax as rs
- rail = rs.GetObject("Select rail curve", rs.filter.curve)
- if rail:
- shapes = rs.GetObjects("Select cross-section curves", rs.filter.curve)
- if shapes: rs.AddSweep1( rail, shapes )
- See Also:
- AddSweep2
- CurveDirectionsMatch
- ReverseCurve
- """
- rail = rhutil.coercecurve(rail, -1, True)
- shapes = [rhutil.coercecurve(shape, -1, True) for shape in shapes]
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- breps = Rhino.Geometry.Brep.CreateFromSweep(rail, shapes, closed, tolerance)
- if not breps: return scriptcontext.errorhandler()
- rc = [scriptcontext.doc.Objects.AddBrep(brep) for brep in breps]
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddSweep2(rails, shapes, closed=False):
- """Adds a surface created through profile curves that define the surface
- shape and two curves that defines a surface edge.
- Parameters:
- rails ([guid, guid]): identifiers of the two rail curve
- shapes ([guid, ...]): one or more cross section shape curves
- closed (bool, optional): If True, then create a closed surface
- Returns:
- list(guid, ...): of new surface objects if successful
- None: if not successful, or on error
- Example:
- import rhinoscriptsyntax as rs
- rails = rs.GetObjects("Select two rail curve", rs.filter.curve)
- if rails and len(rails)==2:
- shapes = rs.GetObjects("Select cross-section curves", rs.filter.curve)
- if shapes: rs.AddSweep2(rails, shapes)
- See Also:
- AddSweep1
- CurveDirectionsMatch
- ReverseCurve
- """
- rail1 = rhutil.coercecurve(rails[0], -1, True)
- rail2 = rhutil.coercecurve(rails[1], -1, True)
- shapes = [rhutil.coercecurve(shape, -1, True) for shape in shapes]
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- breps = Rhino.Geometry.Brep.CreateFromSweep(rail1, rail2, shapes, closed, tolerance)
- if not breps: return scriptcontext.errorhandler()
- rc = [scriptcontext.doc.Objects.AddBrep(brep) for brep in breps]
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddRailRevSrf(profile, rail, axis, scale_height=False):
- """Adds a surface created through profile curves that define the surface
- shape and two curves that defines a surface edge.
- Parameters:
- profile (guid): identifier of the profile curve
- rail (guid): identifier of the rail curve
- axis ([point, point]): A list of two 3-D points identifying the start point and end point of the rail revolve axis, or a Line
- scale_height (bool, optional): If True, surface will be locally scaled. Defaults to False
- Returns:
- guid: identifier of the new object if successful
- None: if not successful, or on error
- Example:
- import rhinoscriptsyntax as rs
- profile = rs.GetObject("Select a profile", rs.filter.curve)
- if profile:
- rail = rs.GetObject("Select a rail", rs.filter.curve)
- if rail:
- rs.AddRailRevSrf(profile, rail, ((0,0,0),(0,0,1)))
- See Also:
- AddSweep1
- CurveDirectionsMatch
- ReverseCurve
- """
- profile_inst = rhutil.coercecurve(profile, -1, True)
- rail_inst = rhutil.coercecurve(rail, -1, True)
- axis_start = rhutil.coerce3dpoint(axis[0], True)
- axis_end = rhutil.coerce3dpoint(axis[1], True)
- line = Rhino.Geometry.Line(axis_start, axis_end)
- surface = Rhino.Geometry.NurbsSurface.CreateRailRevolvedSurface(profile_inst, rail_inst, line, scale_height)
- if not surface: return scriptcontext.errorhandler()
- rc = scriptcontext.doc.Objects.AddSurface(surface)
- scriptcontext.doc.Views.Redraw()
- return rc
- def AddTorus(base, major_radius, minor_radius, direction=None):
- """Adds a torus shaped revolved surface to the document
- Parameters:
- base (point): 3D origin point of the torus or the base plane of the torus
- major_radius, minor_radius (number): the two radii of the torus
- directions (point): A point that defines the direction of the torus when base is a point.
- If omitted, a torus that is parallel to the world XY plane is created
- Returns:
- guid: The identifier of the new object if successful.
- None: if not successful, or on error.
- Example:
- import rhinoscriptsyntax as rs
- major_radius = 5.0
- minor_radius = major_radius - 2.0
- base = rs.GetPoint("Base of torus")
- if base:
- direction = rs.GetPoint("Direction of torus", base)
- if direction:
- rs.AddTorus( base, major_radius, minor_radius, direction )
- See Also:
- AddBox
- AddCone
- AddCylinder
- AddSphere
- """
- baseplane = None
- basepoint = rhutil.coerce3dpoint(base)
- if basepoint is None:
- baseplane = rhutil.coerceplane(base, True)
- if direction!=None: return scriptcontext.errorhandler()
- if baseplane is None:
- direction = rhutil.coerce3dpoint(direction, False)
- if direction: direction = direction-basepoint
- else: direction = Rhino.Geometry.Vector3d.ZAxis
- baseplane = Rhino.Geometry.Plane(basepoint, direction)
- torus = Rhino.Geometry.Torus(baseplane, major_radius, minor_radius)
- revsurf = torus.ToRevSurface()
- rc = scriptcontext.doc.Objects.AddSurface(revsurf)
- scriptcontext.doc.Views.Redraw()
- return rc
- def BooleanDifference(input0, input1, delete_input=True):
- """Performs a boolean difference operation on two sets of input surfaces
- and polysurfaces. For more details, see the BooleanDifference command in
- the Rhino help file
- Parameters:
- input0 ([guid, ...]): list of surfaces to subtract from
- input1 ([guid, ...]): list of surfaces to be subtracted
- delete_input (bool, optional): delete all input objects
- Returns:
- list(guid, ...): of identifiers of newly created objects on success
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- filter = rs.filter.surface | rs.filter.polysurface
- input0 = rs.GetObjects("Select first set of surfaces or polysurfaces", filter)
- if input0:
- input1 = rs.GetObjects("Select second set of surfaces or polysurfaces", filter)
- if input1: rs.BooleanDifference(input0, input1)
- See Also:
- BooleanIntersection
- BooleanUnion
- """
- if type(input0) is list or type(input0) is tuple: pass
- else: input0 = [input0]
-
- if type(input1) is list or type(input1) is tuple: pass
- else: input1 = [input1]
- breps0 = [rhutil.coercebrep(id, True) for id in input0]
- breps1 = [rhutil.coercebrep(id, True) for id in input1]
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- newbreps = Rhino.Geometry.Brep.CreateBooleanDifference(breps0, breps1, tolerance)
- if newbreps is None: return scriptcontext.errorhandler()
-
- rc = [scriptcontext.doc.Objects.AddBrep(brep) for brep in newbreps]
- if delete_input:
- for id in input0: scriptcontext.doc.Objects.Delete(id, True)
- for id in input1: scriptcontext.doc.Objects.Delete(id, True)
- scriptcontext.doc.Views.Redraw()
- return rc
- def BooleanIntersection(input0, input1, delete_input=True):
- """Performs a boolean intersection operation on two sets of input surfaces
- and polysurfaces. For more details, see the BooleanIntersection command in
- the Rhino help file
- Parameters:
- input0 ([guid, ...]): list of surfaces
- input1 ([guid, ...]): list of surfaces
- delete_input (bool, optional): delete all input objects
- Returns:
- list(guid, ...): of identifiers of newly created objects on success
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- input0 = rs.GetObjects("Select first set of surfaces or polysurfaces", rs.filter.surface | rs.filter.polysurface)
- if input0:
- input1 = rs.GetObjects("Select second set of surfaces or polysurfaces", rs.filter.surface | rs.filter.polysurface)
- if input1: rs.BooleanIntersection( input0, input1 )
- See Also:
- BooleanDifference
- BooleanUnion
- """
- if type(input0) is list or type(input0) is tuple: pass
- else: input0 = [input0]
-
- if type(input1) is list or type(input1) is tuple: pass
- else: input1 = [input1]
- breps0 = [rhutil.coercebrep(id, True) for id in input0]
- breps1 = [rhutil.coercebrep(id, True) for id in input1]
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- newbreps = Rhino.Geometry.Brep.CreateBooleanIntersection(breps0, breps1, tolerance)
- if newbreps is None: return scriptcontext.errorhandler()
- rc = [scriptcontext.doc.Objects.AddBrep(brep) for brep in newbreps]
- if delete_input:
- for id in input0: scriptcontext.doc.Objects.Delete(id, True)
- for id in input1: scriptcontext.doc.Objects.Delete(id, True)
- scriptcontext.doc.Views.Redraw()
- return rc
- def BooleanUnion(input, delete_input=True):
- """Performs a boolean union operation on a set of input surfaces and
- polysurfaces. For more details, see the BooleanUnion command in the
- Rhino help file
- Parameters:
- input ([guid, ...]): list of surfaces to union
- delete_input (bool, optional): delete all input objects
- Returns:
- list(guid, ...): of identifiers of newly created objects on success
- None on error
- Example:
- import rhinoscriptsyntax as rs
- input = rs.GetObjects("Select surfaces or polysurfaces to union", rs.filter.surface | rs.filter.polysurface)
- if input and len(input)>1: rs.BooleanUnion(input)
- See Also:
- BooleanDifference
- BooleanUnion
- """
- if len(input)<2: return scriptcontext.errorhandler()
- breps = [rhutil.coercebrep(id, True) for id in input]
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- newbreps = Rhino.Geometry.Brep.CreateBooleanUnion(breps, tolerance)
- if newbreps is None: return scriptcontext.errorhandler()
-
- rc = [scriptcontext.doc.Objects.AddBrep(brep) for brep in newbreps]
- if delete_input:
- for id in input: scriptcontext.doc.Objects.Delete(id, True)
- scriptcontext.doc.Views.Redraw()
- return rc
- def BrepClosestPoint(object_id, point):
- """Returns the point on a surface or polysurface that is closest to a test
- point. This function works on both untrimmed and trimmed surfaces.
- Parameters:
- object_id (guid): The object's identifier.
- point (point): The test, or sampling point.
- Returns:
- tuple(point, [number, number], [number, number], vector): of closest point information if successful. The list will
- contain the following information:
- Element Type Description
- 0 Point3d The 3-D point at the parameter value of the
- closest point.
- 1 (U, V) Parameter values of closest point. Note, V
- is 0 if the component index type is brep_edge
- or brep_vertex.
- 2 (type, index) The type and index of the brep component that
- contains the closest point. Possible types are
- brep_face, brep_edge or brep_vertex.
- 3 Vector3d The normal to the brep_face, or the tangent
- to the brep_edge.
- None: if not successful, or on error.
- Example:
- import rhinoscriptsyntax as rs
- obj = rs.GetObject("Select a surface", rs.filter.surface)
- if obj:
- point = rs.GetPoint("Pick a test point")
- if point:
- arrCP = rs.BrepClosestPoint(obj, point)
- if arrCP:
- rs.AddPoint(point)
- rs.AddPoint( arrCP[0] )
- See Also:
- EvaluateSurface
- IsSurface
- SurfaceClosestPoint
- """
- brep = rhutil.coercebrep(object_id, True)
- point = rhutil.coerce3dpoint(point, True)
- rc = brep.ClosestPoint(point, 0.0)
- if rc[0]:
- type = int(rc[2].ComponentIndexType)
- index = rc[2].Index
- return rc[1], (rc[3], rc[4]), (type, index), rc[5]
- def CapPlanarHoles(surface_id):
- """Caps planar holes in a surface or polysurface
- Parameters:
- surface_id (guid): The identifier of the surface or polysurface to cap.
- Returns:
- bool: True or False indicating success or failure
- Example:
- import rhinoscriptsyntax as rs
- surface = rs.GetObject("Select surface or polysurface to cap", rs.filter.surface | rs.filter.polysurface)
- if surface: rs.CapPlanarHoles( surface )
- See Also:
- ExtrudeCurve
- ExtrudeCurvePoint
- ExtrudeCurveStraight
- ExtrudeSurface
- """
- brep = rhutil.coercebrep(surface_id, True)
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance
- newbrep = brep.CapPlanarHoles(tolerance)
- if newbrep:
- if newbrep.SolidOrientation == Rhino.Geometry.BrepSolidOrientation.Inward:
- newbrep.Flip()
- surface_id = rhutil.coerceguid(surface_id)
- if surface_id and scriptcontext.doc.Objects.Replace(surface_id, newbrep):
- scriptcontext.doc.Views.Redraw()
- return True
- return False
- def DuplicateEdgeCurves(object_id, select=False):
- """Duplicates the edge curves of a surface or polysurface. For more
- information, see the Rhino help file for information on the DupEdge
- command.
- Parameters:
- object_id (guid): The identifier of the surface or polysurface object.
- select (bool, optional): Select the duplicated edge curves. The default is not
- to select (False).
- Returns:
- list(guid, ..): identifying the newly created curve objects if successful.
- None: if not successful, or on error.
- Example:
- import rhinoscriptsyntax as rs
- obj = rs.GetObject("Select surface or polysurface", rs.filter.surface | rs.filter.polysurface)
- if obj:
- rs.DuplicateEdgeCurves( obj, True )
- rs.DeleteObject( obj )
- See Also:
- IsPolysurface
- IsSurface
- """
- brep = rhutil.coercebrep(object_id, True)
- out_curves = brep.DuplicateEdgeCurves()
- curves = []
- for curve in out_curves:
- if curve.IsValid:
- rc = scriptcontext.doc.Objects.AddCurve(curve)
- curve.Dispose()
- if rc==System.Guid.Empty: return None
- curves.append(rc)
- if select:
- rhobject = rhutil.coercerhinoobject(rc)
- rhobject.Select(True)
- if curves: scriptcontext.doc.Views.Redraw()
- return curves
- def DuplicateSurfaceBorder(surface_id, type=0):
- """Create curves that duplicate a surface or polysurface border
- Parameters:
- surface_id (guid): identifier of a surface
- type (number, optional): the border curves to return
- 0=both exterior and interior,
- 1=exterior
- 2=interior
- Returns:
- list(guid, ...): list of curve ids on success
- None: on error
- Example:
- import rhinoscriptsyntax as rs
- surface = rs.GetObject("Select surface or polysurface", rs.filter.surface | rs.filter.polysurface)
- if surface: rs.DuplicateSurfaceBorder( surface )
- See Also:
- DuplicateEdgeCurves
- DuplicateMeshBorder
- """
- brep = rhutil.coercebrep(surface_id, True)
- inner = type==0 or type==2
- outer = type==0 or type==1
- curves = brep.DuplicateNakedEdgeCurves(outer, inner)
- if curves is None: return scriptcontext.errorhandler()
- tolerance = scriptcontext.doc.ModelAbsoluteTolerance * 2.1
- curves = Rhino.Geometry.Curve.JoinCurves(curves, tolerance)
- if curves is None: return scriptcontext.errorhandler()
- rc = [scriptcontext.doc.Objects.AddCurve(c) for c in curves]
- scriptcontext.doc.Views.Redraw()
- return rc
- def EvaluateSurface(surface_id, u, v):
- """Evaluates a surface at a U,V parameter
- Parameters:
- surface_id (guid): the object's identifier.
- u, v ({number, number]): u, v parameters to evaluate.
- Returns:
- point: a 3-D point if successful
- None: if not successful
- Example:
- import rhinoscriptsyntax as rs
- objectId = rs.GetObject("Select a surface")
- if rs.IsSurface(objectId):
- domainU = rs.SurfaceDomain(objectId, 0)
- domainV = rs.SurfaceDomain(objectId, 1)
- u = domainU[1]/2.0
- v = domainV[1]/2.0
- point = rs.EvaluateSurface(objectId, u, v)
- rs.AddPoint( point )
- See Also:
- IsSurface
- SurfaceClosestPoint
- """
- surface = rhutil.coercesurface(surface_id, True)
- rc = surface.PointAt(u,v)
- if rc.IsValid: return rc
- return scriptcontext.errorhandler()
- def ExtendSurface(surface_id, parameter, length, smooth=True):
- """Lengthens an untrimmed surface object
- Parameters:
- surface_id (guid): identifier of a surface
- parameter ([number, number}): tuple of two values definfing the U,V parameter to evaluate.
- The surface edge closest to the U,V parameter will be the edge that is
- extended
- length (number): amount to extend to surface
- smooth (bool, optional): If True, the surface is extended smoothly curving from the
- edge. If False, the surface is extended in a straight line from the edge
- Returns:
- bool: True or False indicating success or failure
- Example:
- import rhinoscriptsyntax as rs
- pick = rs.GetObjectEx("Select surface to extend", rs.filter.surface)
- if pick:
- parameter = rs.SurfaceClosestPoint(pick[0], pick[3])
- rs.ExtendSurface(pick[0], parameter, 5.0)
- See Also:
- IsSurface
- """
- surface = rhutil.coercesurface(surface_id, True)
- edge = surface.ClosestSide(parameter[0], parameter[1])
- newsrf = surface.Extend(edge, length, smooth)
- if newsrf:
- surface_id = rhutil.coerceguid(surface_id)
- if surface_id: scriptcontext.doc.Objects.Replace(surface_id, newsrf)
- scriptcontext.doc.Views.Redraw()
- return newsrf is not None
- def ExplodePolysurfaces(object_ids, delete_input=False):
- """Explodes, or unjoins, one or more polysurface objects. Polysurfaces
- will be exploded into separate surfaces
- Parameters:
- object_ids ([guid, ...]): identifiers of polysurfaces to explode
- delete_input 9bool, optional): delete input objects after exploding
- Returns:
- list(guid, ...): of identifiers of exploded pieces on success
- Example:
- import rhinoscriptsyntax as rs
- obj = rs.GetObject("Select polysurface to explode", rs.filter.polysurface)
- if rs.IsPolysurface(obj):
- rs.ExplodePolysurfaces( obj )
- See Also:
- IsPolysurface
- IsSurface
- """
- id = rhutil.coerceguid(object_ids, False)
- if id: object_ids = [id]
- ids = []
- for id in object_ids:
- brep = rhutil.coercebrep(id, True)
- if brep.Faces.Count>1:
- for i in range(brep.Faces.Count):
- copyface = brep.Faces[i].DuplicateFace(False)
- face_id = scriptcontext.doc.Objects.AddBrep(copyface)
- if face_id!=System.Guid.Empty: ids.append(face_id)
- if delete_input: scriptcontext.doc.Objects.Delete(id, True)
- scriptcontext.doc.Views.Redraw()
- return ids
- def ExtractIsoCurve(surface_id, parameter, direction):
- """Extracts isoparametric curves from a surface
- Parameters:
- surface_id (guid): identifier of a surface
- parameter ([number, number]): u,v parameter of the surface to evaluate
- direction (number): Direction to evaluate
- 0 = u
- 1 = v
- 2 = both
- R…