def RandomPtOnSrf(srf): if srf is None: print "Not a surface" return dom_u = rs.SurfaceDomain(srf, 0) dom_v = rs.SurfaceDomain(srf, 1) while True: pt_u = random.uniform(dom_u[0], dom_u[1]) pt_v = random.uniform(dom_v[0], dom_v[1]) pt = rs.EvaluateSurface(srf, pt_u, pt_v) if rs.IsPointOnSurface(srf, pt): return pt
def convert_to_uv_space(srf,pts): tol = rs.UnitAbsoluteTolerance() uv_pts = [] for pt in pts: #need for issues in cases points lie on a seam if not rs.IsPointOnSurface (srf, pt): pts_dis = [] pts_dis.append((pt[0]+tol,pt[1],pt[2])) pts_dis.append((pt[0]-tol,pt[1],pt[2])) pts_dis.append((pt[0],pt[1]+tol,pt[2])) pts_dis.append((pt[0],pt[1]-tol,pt[2])) pts_dis.append((pt[0],pt[1],pt[2]+tol)) pts_dis.append((pt[0],pt[1],pt[2]-tol)) for pt_dis in pts_dis: data= rs.BrepClosestPoint(srf,pt_dis) if rs.IsPointOnSurface(srf,data[0]): pt = data[0] break u,v = rs.SurfaceClosestPoint(srf,pt) uv_pts.append((u,v,0)) #rs.AddTextDot(str(data[2] ) + " / " + str(rs.IsPointOnSurface (srf, pt)) + " / " + str(u) + " / " + str(v),pt) return uv_pts
def trim(self, curve, cutter, filter=1): resultLines = [] intersectedPoints = rs.CurveCurveIntersection(curve, cutter) if intersectedPoints == None: return None tmpSurface = rs.AddPlanarSrf(cutter) intersectedPoints = [n[1] for n in intersectedPoints] intersectedPoints.insert(0, rs.CurveStartPoint(curve)) intersectedPoints.insert(len(intersectedPoints), rs.CurveEndPoint(curve)) for i in range(len(intersectedPoints) - 1): x = intersectedPoints[i][0] + intersectedPoints[i + 1][0] y = intersectedPoints[i][1] + intersectedPoints[i + 1][1] z = intersectedPoints[i][2] + intersectedPoints[i + 1][2] mid = (x / 2.0, y / 2.0, z / 2.0) if tmpSurface is None: continue if rs.IsPointOnSurface(tmpSurface, mid): if filter == 1: resultLines.append( rs.AddLine(intersectedPoints[i], intersectedPoints[i + 1])) elif filter == 2: continue else: if filter == 1: continue elif filter == 2: resultLines.append( rs.AddLine(intersectedPoints[i], intersectedPoints[i + 1])) rs.DeleteObject(curve) if tmpSurface is not None: rs.DeleteObject(tmpSurface) return resultLines
def setSurfaceForSlicing(self): explodedSurfaces = rs.ExplodePolysurfaces(self.addtiveObj) editPoint = [] #get editPoint from polysurfaces if len(explodedSurfaces) == 0: #use obj meshed = rhino.Geometry.Mesh.CreateFromBrep( rs.coercebrep(self.addtiveObj)) editPoint = rs.MeshVertices(meshed[0]) else: for i in explodedSurfaces: meshed = rhino.Geometry.Mesh.CreateFromBrep(rs.coercebrep(i)) vertices = rs.MeshVertices(meshed[0]) editPoint.extend(vertices) rs.DeleteObjects(explodedSurfaces) minValue = [] maxValue = [] basePointForPlane = None basePointForDistance = None for i in range(len(editPoint)): if i == 0: basePointForPlane = editPoint[0] basePointForDistance = editPoint[0] for j in range(3): minValue.append(editPoint[0][j]) maxValue.append(editPoint[0][j]) continue else: if basePointForPlane[2] > editPoint[i][2]: basePointForPlane = editPoint[i] if basePointForDistance[2] < editPoint[i][2]: basePointForDistance = editPoint[i] for j in range(3): if minValue[j] > editPoint[i][j]: minValue[j] = editPoint[i][j] elif maxValue[j] < editPoint[i][j]: maxValue[j] = editPoint[i][j] #why? self.basePointForPlane = basePointForPlane plane = rs.PlaneFromNormal(basePointForPlane, self.normalVec) #calculating distance printing self.calcDistance(plane, editPoint) #make base surface pntForSur = [] line = (minValue[0], minValue[1], minValue[2]), (minValue[0], minValue[1], maxValue[2]) pntForSur.append(rs.LinePlaneIntersection(line, plane)) line = (minValue[0], maxValue[1], minValue[2]), (minValue[0], maxValue[1], maxValue[2]) pntForSur.append(rs.LinePlaneIntersection(line, plane)) line = (maxValue[0], maxValue[1], minValue[2]), (maxValue[0], maxValue[1], maxValue[2]) pntForSur.append(rs.LinePlaneIntersection(line, plane)) line = (maxValue[0], minValue[1], minValue[2]), (maxValue[0], minValue[1], maxValue[2]) pntForSur.append(rs.LinePlaneIntersection(line, plane)) lineForSur = [] for i in range(4): lineForSur.append(rs.AddLine(pntForSur[i], pntForSur[(i + 1) % 4])) joinedCurve = rs.JoinCurves(lineForSur) rs.DeleteObjects(lineForSur) curveForSur = rs.OffsetCurve(joinedCurve, rs.CurveNormal(joinedCurve), 30) self.sliceSurface = rs.AddPlanarSrf(curveForSur) if len(curveForSur) > 1 or rs.IsPointOnSurface( self.sliceSurface, rs.CurveStartPoint(joinedCurve)) is False: rs.DeleteObjects(curveForSur) if self.sliceSurface is not None: rs.DeleteObject(self.sliceSurface) curveForSur = rs.OffsetCurve(joinedCurve, rs.CurveNormal(joinedCurve), -30) self.sliceSurface = rs.AddPlanarSrf(curveForSur) rs.DeleteObjects(joinedCurve) rs.DeleteObjects(curveForSur) self.fixedLayerHeight = float( self.gcoder.getLayerHeight() * (1.0 / math.cos(math.radians(self.angleOfSurface)))) self.addtiveObj = rs.CopyObject(self.addtiveObj, (0, 0, self.fixedLayerHeight * 0.9)) self.sliceSurface = rs.MoveObject(self.sliceSurface, (0, 0, self.fixedLayerHeight * 0.9))
def apply_constraints(k, args): """Apply the constraints on the vertices of the mesh on a point, a curve or a surface. Parameters ---------- mesh : Mesh A mesh. constraints : dict A dictionary with constraints on mesh vertices: {vertex_key: (constraint_type, constraint_information)}. Returns ------- Raises ------ - """ mesh, constraints = args for vkey, constraint in constraints.items(): cstr_type, cstr_object = constraint if cstr_type == 'point': x, y, z = cstr_object attr = mesh.vertex[vkey] attr['x'] = x attr['y'] = y attr['z'] = z elif cstr_type == 'curve': xyz = mesh.vertex_coordinates(vkey) t = rs.CurveClosestPoint(cstr_object, xyz) x, y, z = rs.EvaluateCurve(cstr_object, t) attr = mesh.vertex[vkey] attr['x'] = x attr['y'] = y attr['z'] = z elif cstr_type == 'surface': xyz = mesh.vertex_coordinates(vkey) u, v = rs.SurfaceClosestPoint(cstr_object, xyz) x, y, z = rs.EvaluateSurface(cstr_object, u, v) if not rs.IsPointOnSurface(cstr_object, [x, y, z]): borders = surface_borders(cstr_object) xyz0 = [x, y, z] min_dist = -1 pt = None for border in borders: t = rs.CurveClosestPoint(border, xyz0) xyz = rs.EvaluateCurve(border, t) dist = rs.Distance(xyz, xyz0) if dist < min_dist or min_dist < 0: min_dist = dist pt = xyz x, y, z = pt rs.DeleteObjects(borders) attr = mesh.vertex[vkey] attr['x'] = x attr['y'] = y attr['z'] = z return 0
def create_bone(point, curve, length, width, radius, extend): if not extend: extend = 0.001 curve_surface = rs.AddPlanarSrf(curve) if not curve_surface: exp_curves = rs.ExplodeCurves(curve) curve_surface = rs.AddEdgeSrf(exp_curves) rs.DeleteObjects(exp_curves) print("Surface problem") # circle = rs.AddCircle(rs.CurveAreaCentroid(curve)[0],10000) # planar_surface = rs.AddPlanarSrf(circle) # projected_curve = rs.ProjectCurveToSurface(curve,planar_surface,(0,0,-1)) # if not projected_curve: rs.ProjectCurveToSurface(curve,planar_surface,(0,0,1)) # if not projected_curve: print "noooooo" # curve_surface = rs.AddPlanarSrf(projected_curve) # rs.DeleteObjects([circle,planar_surface,curve]) # curve = rs.JoinCurves(rs.DuplicateEdgeCurves(curve_surface, select=False)) # if not curve_surface: print "WARNING" main_point_param = rs.CurveClosestPoint(curve, point) curve_normal = rs.CurveNormal(curve) curve_plane = rs.CurvePlane(curve) tangent = rs.CurveTangent(curve, main_point_param) center_curve = rs.AddLine((0, 0, 0), rs.VectorScale(tangent, length)) rs.RotateObject(center_curve, (0, 0, 0), 90, curve_normal) rs.MoveObject(center_curve, rs.VectorCreate(point, (0, 0, 0))) if not rs.IsPointOnSurface(curve_surface, rs.CurveEndPoint(center_curve)): rs.RotateObject(center_curve, point, 180, curve_normal) normal = rs.VectorScale(tangent, 10000) normal_inverted = rs.VectorReverse(normal) side_curve = rs.OffsetCurveOnSurface(center_curve, curve_surface, width / 2) if not side_curve: side_curve = rs.OffsetCurveOnSurface(center_curve, curve_surface, -width / 2) side_curves = [ side_curve, rs.RotateObject( side_curve, rs.CurveMidPoint(center_curve), 180, rs.VectorCreate(rs.CurveStartPoint(center_curve), rs.CurveEndPoint(center_curve)), True) ] #side_curves = [side_curve,rs.MirrorObject(side_curve,rs.CurveStartPoint(center_curve),rs.CurveEndPoint(center_curve), True)] #side_curves = [rs.OffsetCurveOnSurface(center_curve,curve_surface, width/2),rs.OffsetCurveOnSurface(center_curve,curve_surface, -width/2)] for side_curve in side_curves: rs.ExtendCurveLength(side_curve, 0, 0, 2) rs.ObjectColor(side_curve, (255, 0, 0)) perimeter_curve = rs.AddCurve([ rs.CurveStartPoint(side_curves[0]), rs.CurveEndPoint(side_curves[0]), rs.CurveEndPoint(side_curves[1]), rs.CurveStartPoint(side_curves[1]), rs.CurveStartPoint(side_curves[0]) ], 1) inside_curve = rs.OffsetCurve(perimeter_curve, rs.CurveAreaCentroid(perimeter_curve)[0], radius * .7) external_curve = rs.OffsetCurve(perimeter_curve, rs.CurveAreaCentroid(perimeter_curve)[0], -extend) e_points = [ rs.CurvePoints(external_curve)[0], rs.CurvePoints(external_curve)[3] ] e_perimeter_curve = rs.AddCurve([ rs.CurveEndPoint(side_curves[1]), rs.CurveEndPoint(side_curves[0]), e_points[0], e_points[1], rs.CurveEndPoint(side_curves[1]) ], 1) center_plane_a = rs.PlaneFromPoints( rs.CurvePoints(inside_curve)[2], rs.CurvePoints(inside_curve)[1], rs.CurvePoints(inside_curve)[3]) center_plane_b = rs.PlaneFromPoints( rs.CurvePoints(inside_curve)[1], rs.CurvePoints(inside_curve)[0], rs.CurvePoints(inside_curve)[2]) circles = [ rs.AddCircle(center_plane_a, radius + RADIUS_TOLERANCE), rs.AddCircle(center_plane_b, radius + RADIUS_TOLERANCE) ] bone_curve = rs.CurveBooleanUnion( [e_perimeter_curve] + circles) if extend else rs.CurveBooleanUnion([perimeter_curve] + circles) rs.DeleteObjects([ inside_curve, center_curve, perimeter_curve, curve_surface, e_perimeter_curve, external_curve ] + side_curves + circles) return bone_curve
def is_point_on_surface(id, p): return rh.IsPointOnSurface(id, Pt(p))
def surf_f(u, v): plane = rh.SurfaceFrame(id, (u, v)) if rh.IsPointOnSurface(id, plane.Origin): return f(fromPlane(plane)) else: return False