def create_cross_sections(self): crvdomain = rs.CurveDomain(self.curve_object) self.cross_sections = [] self.cross_section_planes = [] t_step = (crvdomain[1] - crvdomain[0]) / self.SAMPLES pi_step_size = math.pi / self.SAMPLES pi_step = 0 prev_normal = None prev_perp = None for t in rs.frange(crvdomain[0], crvdomain[1], t_step): crvcurvature = rs.CurveCurvature(self.curve_object, t) crosssectionplane = None if not crvcurvature: crosssectionplane = self.cross_section_plane_no_curvature( t, prev_normal, prev_perp) else: crosssectionplane = self.cross_section_plane_curvature( crvcurvature, prev_normal, prev_perp) if crosssectionplane: prev_perp = crosssectionplane.XAxis prev_normal = crosssectionplane.YAxis pi_scalar = self.create_scalar(pi_step) radii = self.ellipse_radii(pi_scalar) csec = rs.AddEllipse(crosssectionplane, radii[0], radii[1]) self.cross_sections.append(csec) self.cross_section_planes.append(crosssectionplane) pi_step += pi_step_size
def __generate_form_levels(self, spine_curve): crvdomain = rs.CurveDomain(spine_curve) printedState = "" crosssection_planes = [] crosssection_plane_nums = [] crosssections = [] t_step = (crvdomain[1] - crvdomain[0]) / ( self.object_properties["number_of_lofts"] - 1) t = crvdomain[0] for t in rs.frange(crvdomain[0], crvdomain[1], t_step): if (self.emotion_properties["vertical_AR"][str(int(t + 1))] != None): crvcurvature = rs.CurveCurvature(spine_curve, t) crosssectionplane = None if not crvcurvature: crvPoint = rs.EvaluateCurve(spine_curve, t) crvTangent = rs.CurveTangent(spine_curve, t) crvPerp = (0, 0, 1) crvNormal = rs.VectorCrossProduct(crvTangent, crvPerp) printedState = printedState + str(crvNormal) crosssectionplane = rs.PlaneFromNormal( crvPoint, crvTangent) if (t == 0): crosssectionplane = rs.PlaneFromNormal([0, 0, 0], [0, 0, 1]) else: crvPoint = crvcurvature[0] crvTangent = crvcurvature[1] crvPerp = rs.VectorUnitize(crvcurvature[4]) crvNormal = rs.VectorCrossProduct(crvTangent, crvPerp) printedState = printedState + str(crvNormal) crosssectionplane = rs.PlaneFromNormal( crvPoint, crvTangent, crvNormal) if crosssectionplane: crosssection_planes.append(crosssectionplane) crosssection_plane_nums.append(str(int(t + 1))) if len(crosssection_plane_nums) > 0: last_element = crosssection_plane_nums.pop( len(crosssection_plane_nums) - 1) crosssection_plane_nums.insert(0, last_element) for index in xrange(len(crosssection_plane_nums)): crosssections.append( self.__generate_individual_levels( crosssection_planes[index], crosssection_plane_nums[index])) if not crosssections: return crosssections.append(crosssections.pop(0)) rs.AddLoftSrf(crosssections, closed=False, loft_type=int( round(self.emotion_properties["vertical_wrapping"]))) return crosssection_planes[0]
def offset_vector(self, point, cross_section_index, point_index): modulo = len(self.point_lists[cross_section_index]) closest_point = rs.CurveClosestPoint( self.cross_sections[cross_section_index], point) crv = rs.CurveCurvature(self.cross_sections[cross_section_index], closest_point) crvTangent = crv[1] crvPerp = rs.VectorUnitize(crv[4]) unit_vector = rs.VectorUnitize(crvTangent) return [ rs.VectorScale(unit_vector, 0.205), rs.VectorReverse(rs.VectorCrossProduct(crvTangent, crvPerp)) ]
def flatWorm(): curveObject = rs.GetObject("pick a backbone curve", 4, True, False) samples = rs.GetInteger("# of crosssections", 100, 5) bend_radius = rs.GetReal("bend radius", 0.5, 0.001) # r1 perp_radius = rs.GetReal("ribbon plan radius", 2.0, 0.001) #r2 crvDom = rs.CurveDomain( curveObject ) # domain != length // why do we use domains? == "The domain is the set of all possible input values to the function that defines the curve or surface." crossSections = [] # empty array to store sections t_step = (crvDom[1] - crvDom[0]) / samples # this is starting to be a pattern! t = crvDom[0] # start pt for loop at the start of the line for t in rs.frange(crvDom[0], crvDom[1], t_step): # loop thru entire domain w/ floats crvCurvature = rs.CurveCurvature( curveObject, t ) # evaluate curve with a circle - gives 3d normals & gives radius info crossSecPlane = None if not crvCurvature: crvPoint = rs.EvaluateCurve(curveObject, t) crvTan = rs.CurveTangent(curveObject, t) # get tangent vector crvPerp = (0, 0, 1) crvNorm = rs.VectorCrossProduct(crvTan, crvPerp) # = product of 2 vectors crossSecPlane = rs.PlaneFromFrame(crvPoint, crvPerp, crvNorm) else: crvPoint = crvCurvature[0] crvTan = crvCurvature[1] crvPerp = rs.VectorUnitize(crvCurvature[4]) crvNorm = rs.VectorCrossProduct(crvTan, crvPerp) # look up crossSecPlane = rs.PlaneFromFrame(crvPoint, crvPerp, crvNorm) if crossSecPlane: csec = rs.AddEllipse( crossSecPlane, bend_radius, perp_radius ) # draw ellipse at tan/normal to point along curve with radii crossSections.append(csec) # add ellipses to an array t += t_step # step through domain rs.AddLoftSrf(crossSections) # loft list of curves rs.DeleteObjects( crossSections) # delete original list of curves as cleanup
def FlatWorm(): curve_object = rs.GetObject("Pick a backbone curve", 4, True, False) if not curve_object: return samples = rs.GetInteger("Number of cross sections", 100, 5) if not samples: return bend_radius = rs.GetReal("Bend plane radius", 0.5, 0.001) if not bend_radius: return perp_radius = rs.GetReal("Ribbon plane radius", 2.0, 0.001) if not perp_radius: return crvdomain = rs.CurveDomain(curve_object) crosssections = [] t_step = (crvdomain[1]-crvdomain[0])/samples t = crvdomain[0] while t<=crvdomain[1]: crvcurvature = rs.CurveCurvature(curve_object, t) crosssectionplane = None if not crvcurvature: crvPoint = rs.EvaluateCurve(curve_object, t) crvTangent = rs.CurveTangent(curve_object, t) crvPerp = (0,0,1) crvNormal = rs.VectorCrossProduct(crvTangent, crvPerp) crosssectionplane = rs.PlaneFromFrame(crvPoint, crvPerp, crvNormal) else: crvPoint = crvcurvature[0] crvTangent = crvcurvature[1] crvPerp = rs.VectorUnitize(crvcurvature[4]) crvNormal = rs.VectorCrossProduct(crvTangent, crvPerp) crosssectionplane = rs.PlaneFromFrame(crvPoint, crvPerp, crvNormal) if crosssectionplane: csec = rs.AddEllipse(crosssectionplane, bend_radius, perp_radius) crosssections.append(csec) t += t_step if not crosssections: return rs.AddLoftSrf(crosssections) rs.DeleteObjects(crosssections)
def addcurvaturegraphsection(idCrv, t0, t1, samples, scale): if (t1 - t0) <= 0.0: return N = -1 tstep = (t1 - t0) / samples t = t0 points = [] objects = [] while t <= (t1 + (0.5 * tstep)): if t >= t1: t = t1 - 1e-10 N += 1 cData = rs.CurveCurvature(idCrv, t) if not cData: points.append(rs.EvaluateCurve(idCrv, t)) else: c = rs.VectorScale(cData[4], scale) a = cData[0] b = rs.VectorSubtract(a, c) objects.append(rs.AddLine(a, b)) points.append(b) t += tstep objects.append(rs.AddInterpCurve(points)) return objects
#Finding Curve Parameters--------------------------------------------------------------- #Curve parameters are different than the physical length of the curve #Find the midpoint of the curve (midpoint of length) and a user defined #parameter within the curve domain then label with TextDot import rhinoscriptsyntax as rs #prompt user for curve to examine crv = rs.GetObject("Please Select Curve", 4) #get the curve domain crvDom = rs.CurveDomain(crv) rs.MessageBox("The domain of the curve is: " + str(crvDom)) #place the text label at the curve length midpoint using the curvature vector as a guide vect1 = rs.CurveCurvature(crv, rs.CurveClosestPoint(crv, rs.CurveMidPoint(crv)))[4] vect1 = rs.VectorUnitize(vect1) * 2 #create the label dotPt = rs.CopyObject(rs.CurveMidPoint(crv), vect1) rs.AddTextDot("Midpoint", dotPt) rs.ObjectColor(rs.AddLine(rs.CurveMidPoint(crv), dotPt), (255, 0, 0)) rs.ObjectColor(rs.AddPoint(rs.CurveMidPoint(crv)), (255, 0, 0)) #Prompt user for curve parameter default at mid (curve domains do not always start at 0) param = rs.GetReal("Enter Curve Parameter, default is mid", (crvDom[1] - crvDom[0]) / 2, crvDom[0], crvDom[1]) #evaluate the curve at the middle parameter and place a dot crvPt = rs.EvaluateCurve(crv, param) rs.ObjectColor(rs.AddPoint(crvPt), (0, 0, 255))
def ProfileCurveThings (treeIn, profCrv, profPointTreeOut, vectorTreeOut, vectorLineTree, tOut): MaxCoord = GridCornerBig (treeIn) x_max = MaxCoord.X y_max = MaxCoord.Y z_max = MaxCoord.Z profPointList = [] profVecDirection = [] vectorList = [] curvatureTree = datatree [sys.Object] () directionTree = datatree [sys.Object] () Crv = rs.coercecurve (profCrv) start = rs.CurveStartPoint (Crv) end = rs.CurveEndPoint (Crv) if start.Y > end.Y : Crv = rh.Geometry.Curve.Reverse (Crv) for i in range(treeIn.BranchCount): treeBranch = treeIn.Branch(i) treePath = treeIn.Path(i) for j in range(treeBranch.Count): elem = treeBranch[j] x = elem.X y = elem.Y z = elem.Z Yarany = y / y_max crvPoint = rh.Geometry.Curve.PointAtNormalizedLength(profCrv, Yarany) crvPoint = rs.coerce3dpoint (crvPoint) t = rs.CurveClosestPoint (profCrv, crvPoint) curvature = rs.CurveCurvature(profCrv, t)[4] tangent = rs.CurveCurvature(profCrv, t)[1] angle1 = rs.VectorAngle ([0,1,0], curvature) angle2 = rs.VectorAngle ([0,1,0], tangent) curvatureRot = curvature curvatureRot = rs.coerce3dpoint (curvatureRot) rs.RotateObject (curvatureRot, [0,0,0], 90, [0,0,1]) curvatureRot = rs.coerce3dvector (curvatureRot) angle = rs.VectorAngle (curvatureRot, tangent) curvature = rs.coerce3dpoint(curvature) if 179.99 < angle <180.01: angleDirect = 0 else: angleDirect = 1 profPointList.append (crvPoint) profVecDirection.append (angleDirect) vectorList.append (curvature) curvatureTree.Add (curvature, treePath) profPointTreeOut.Add (crvPoint, treePath) directionTree.Add (angleDirect, treePath) tOut.Add (Yarany, treePath) profPoint_x_List = [] for i in range (profPointList.Count): profPoint = profPointList [i] profPoint_x = profPoint.X profPoint_x_List.append (profPoint_x) a = 0 for i in range (profPoint_x_List.Count): x = profPoint_x_List [i] Xabs = math.fabs (x) if Xabs > a: b = i a = Xabs c = x directByMax = profVecDirection [b] vectorByMax = vectorList [b] pointByMax = profPointList [b] stableByMax = pointByMax pointX = math.fabs (stableByMax.X) moveByMax = pointByMax unitVecByMax = rs.VectorUnitize (vectorByMax) rs.MoveObject (moveByMax, 5* unitVecByMax) moveX = math.fabs (moveByMax.X) if moveX > pointX: mirror = 1 else: mirror = 0 for i in range(curvatureTree.BranchCount): treeBranch = curvatureTree.Branch(i) treePath = curvatureTree.Path(i) pointBranch = profPointTreeOut.Branch(i) directionBranch = directionTree.Branch(i) for j in range(treeBranch.Count): curvature = treeBranch [j] crvPoint = pointBranch [j] direction = directionBranch [j] curvature = rs.coerce3dpoint(curvature) if mirror == 0: if direction != directByMax: rs.RotateObject (curvature, [0,0,0], 180, [0,0,1]) if mirror == 1: if direction == directByMax: rs.RotateObject (curvature, [0,0,0], 180, [0,0,1]) curvature = rs.coerce3dvector(curvature) unitCrvVec = rs.VectorUnitize (curvature) if c < 0: unitCrvVec2 = - unitCrvVec else: unitCrvVec2 = unitCrvVec unitCrvVec = rs.coerce3dvector (unitCrvVec) unitCrvVec2 = rs.coerce3dvector (unitCrvVec2) moveCrvPoint = crvPoint + (unitCrvVec*5) moveCrvPoint = rs.coerce3dpoint (moveCrvPoint) vectorLine = rs.AddLine (crvPoint, moveCrvPoint) vectorTreeOut.Add (unitCrvVec2, treePath) vectorLineTree.Add (vectorLine, treePath)
def coorxy(theta, alpha, beta, r): import math as ma x = r * ma.sin(beta) * ma.cos(theta) * (ma.e**(theta / ma.tan(alpha))) y = r * ma.sin(beta) * ma.sin(theta) * (ma.e**(theta / ma.tan(alpha))) return (x, y) #对数螺旋线基本方程。 crvdomain = rs.CurveDomain(curve_object) sections = [] t_step = (crvdomain[1] - crvdomain[0]) / sample t = crvdomain[0] a_step = 20 * pi / sample a = 0 for t in rs.frange(crvdomain[0], crvdomain[1], t_step): a = a + a_step curvecurvature = rs.CurveCurvature(curve_object, t) sectionplane = None curvept = curvecurvature[0] curvetangent = curvecurvature[1] curveperp = (0, 0, 1) curvenormal = rs.VectorCrossProduct(curveperp, curvetangent) sectionplane = rs.PlaneFromFrame(curvept, curveperp, curvenormal) if sectionplane: coor = coorxy(a, al, be, r) x = coor[0] y = coor[1] x1 = rs.VectorUnitize(curveperp) y1 = rs.VectorUnitize(curvenormal) pt = rs.VectorAdd(x * x1, y * y1) secptvec = pt + curvept secpt = rs.AddPoint(secptvec)