Python_III_RhinoCommon_5_RhinoCommon_Examples Documentation RhinoCommon API

🦗⬇️⬇️⬇️ Download the gh script start file here ⬇️⬇️⬇️🦗 –> 🦗⬇️⬇️⬇️ Download the rhino file here ⬇️⬇️⬇️🦗 –>

Python_III_RhinoCommon_5_RhinoCommon_Examples:


Curves and Planes - Create Curve from Points and Divide it to Planes


import Rhino.Geometry as r

######################################################################################################################################################
#Task create a curve and divide it to planes
#Hint: 
#1) To create a curve use method - r.NurbsCurve.CreateInterpolatedCurve(...) - https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Curve_CreateInterpolatedCurve.htm
#2) To divide curve into planes you need to use two functions curve.DivideByCount(...) and curve.GetPerpendicularFrames(...) - https://developer.rhino3d.com/api/RhinoCommon/html/Overload_Rhino_Geometry_Curve_DivideByCount.htm https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Curve_GetPerpendicularFrames.htm
######################################################################################################################################################

#create curve from points
curve = r.NurbsCurve.CreateInterpolatedCurve(points,3)

#divide curve to planes
division_parameters = curve.DivideByCount(divisions,True)
planes = curve.GetPerpendicularFrames(division_parameters)

Create Loft



import Rhino.Geometry as r

######################################################################################################################################################
#Create loft from curves
#Hint: 
#1) Use following method - r.Brep.CreateFromLoft(...) - https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Brep_CreateFromLoft.htm
######################################################################################################################################################

#create curve from points
curve = r.NurbsCurve.CreateInterpolatedCurve(points,3)

#divide curve to planes
division_parameters = curve.DivideByCount(divisions,True)
planes = curve.GetPerpendicularFrames(division_parameters)

def create_rectangles_from_planes_and_loft(planes_):
    #rotate planes and create rectangles
    angle = 0
    
    rectangles = []
    planes_rotated = []
    for i in range(len(division_parameters)):
        
        #rotate planes
        plane = r.Plane(planes_[i])
        plane.Rotate(angle,plane.ZAxis, plane.Origin)
        angle += rotation_angle
        planes_rotated.append(planes_rotated)
        
        #create rectangles
        rectangle = r.Rectangle3d(plane,r.Interval(-rect_size*0.5,rect_size*0.5),r.Interval(-rect_size*0.5,rect_size*0.5)).ToNurbsCurve()
        
        #output
        rectangles.append(rectangle)
        
    #replace r.Brep with the method r.Brep.CreateFromLoft(...)
    loft = r.Brep()
    loft = r.Brep.CreateFromLoft(rectangles,r.Point3d.Unset,r.Point3d.Unset,r.LoftType.Normal,False)
    
    #output
    return loft, planes_rotated, rectangles

loft_, planes_rotated_, rectangles_ = create_rectangles_from_planes_and_loft(planes)

Create Surface from Points



import Rhino.Geometry as r

######################################################################################################################################################
#Task Create Surface from Points
#Hint: 
#Use method - r.NurbsSurface.CreateFromPoints(...) - https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_NurbsSurface_CreateFromPoints.htm
######################################################################################################################################################

u_count = 3
v_count = int(len(points)/u_count)

#output a surface using method r.NurbsSurface.CreateFromPoints(...)
a = r.NurbsSurface.CreateFromPoints(points,u_count,v_count,2,2)

Create Mesh - add mesh faces


import Rhino.Geometry as r

#Task create a mesh
#Step1 - Add vertices - mesh.Vertices.Add(...) https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Collections_MeshVertexList_Add.htm
#Step2 - Add faces - mesh.Faces.AddFace(...) https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Collections_MeshFaceList_AddFace_1.htm

def create_mesh(points, faces, colors):
    
    #Create and empty mesh
    mesh = r.Mesh()
    
    #Add vertices, loop iterates individual points
    for i in points:
        mesh.Vertices.Add(i)
    
    #Add faces, loop iterates three integers at a time
    for i in range(0, len(faces), 3):
        mesh.Faces.AddFace(faces[i],faces[i+1],faces[i+2])
    
    #Add colors:
    for i in colors:
        mesh.VertexColors.Add(i)
    
    #Additional methods to clean up the mesh
    mesh.Normals.ComputeNormals();
    mesh.Compact();
    
    return mesh

a = create_mesh(p,f,c)

Subdivide Surface to a Diamond Pattern



    pts = []
    interval = s.Domain(0)
    interval1 = s.Domain(1)
    step_u = (interval.T1 - interval.T0) / u_divisions
    step_v = (interval1.T1 - interval1.T0) / v_divisions
    sum_step_u = 0
    
    
    for i in range(u_divisions):
        a = 1 if i % 2 == 0 else 0
        b = 2 if i % 2 == 0 else 3
    
        sum_step_v = 0
        for j in range(v_divisions):
            
            p0 = s.PointAt(sum_step_u, sum_step_v)
            p1 = s.PointAt(sum_step_u + step_u, sum_step_v)
            
            s.PointAt(sum_step_u, sum_step_v + step_v * 0.5)
            s.NormalAt(sum_step_u, sum_step_v + step_v * 0.5)
            s.PointAt(sum_step_u + step_u, sum_step_v + step_v * 0.5)
            s.NormalAt(sum_step_u + step_u, sum_step_v + step_v * 0.5)
            
            p2 = s.PointAt(sum_step_u, sum_step_v + step_v)
            p3 = s.PointAt(sum_step_u + step_u, sum_step_v + step_v)
            p4 = s.PointAt(sum_step_u + step_u * 0.5, sum_step_v + step_v * 1.5)
            p5 = s.PointAt(sum_step_u + step_u * 0.5, sum_step_v + step_v * 0.5)
            p6 = s.PointAt(sum_step_u + step_u * 0.5, sum_step_v - step_v * 0.5)
            p7 = (p5 + p6) / 2
            p8 = (p4 + p5) / 2
            
            if (j == 0):
                ############################################
                #start triangles
                ############################################
                p9 = s.PointAt(sum_step_u + step_u * 0.5, interval1.T0)
                line = r.Line(p5, p6)
                cp = line.ClosestPoint(p9, False)
                
                if (base_planes.Count > 0):
                    if (Math.Abs(base_planes.First().ZAxis.X) + Math.Abs(base_planes.First().ZAxis.Y) + Math.Abs(base_planes.First().ZAxis.Z) > 0.01):
                        result, t=r.Intersect.Intersection.LinePlane(line, base_planes.First())
                        cp = line.PointAt(t)
                
                pts+=[ p5, cp, p1 ]
                face.append(r.MeshFace(v, v + 1, v + 2))
                v += 3
                
                pts+=[ cp, p5, p0 ]
                face.append(r.MeshFace(v, v + 1, v + 2))
                v += 3
            
            pts+=[p1, p3, p5 ]
            face.append(r.MeshFace(v, v + 1, v + 2))
            v += 3
            
            pts+=[ p2, p0, p5]
            face.append(r.MeshFace(v, v + 1, v + 2))
            v += 3
            
            if (j != v_divisions - 1):
                pts+=[ p4, p5, p3 ]
                face.append(r.MeshFace(v, v + 1, v + 2))
                v += 3
                
                pts +=[ p5, p4, p2]
                face.append(r.MeshFace(v, v + 1, v + 2))
                v += 3
            else:
                ############################################
                #//end triangles
                ############################################
                p9 = s.PointAt(sum_step_u + step_u * 0.5, interval1.T1)
                line = r.Line(p4, p5)
                cp = line.ClosestPoint(p9, False)
                
                if (base_planes.Count > 0):
                    if (Math.Abs(base_planes.Last().ZAxis.X) + Math.Abs(base_planes.Last().ZAxis.Y) + Math.Abs(base_planes.Last().ZAxis.Z) > 0.01):
                        result, t = Rhino.Geometry.Intersect.Intersection.LinePlane(line, base_planes.Last())
                        cp = line.PointAt(t)
                        
                pts+=[ cp, p5, p3]
                face.Add(r.MeshFace(v, v + 1, v + 2))
                v += 3
                
                pts+=[ p5, cp, p2]
                face.Add(r.MeshFace(v, v + 1, v + 2))
                v += 3
                
            sum_step_v += step_v
            
        sum_step_u += step_u
        
    for i in pts:
        m.Vertices.Add(i)

    m.Faces.AddFaces(face)
    m.RebuildNormals()
    return m

a = diamond_subdivision(surface,u,v,[])