def setObjAreaValue(obj): if rs.IsSurface(obj): setSrfAreaValue(obj) elif rs.IsPolysurface(obj) and rs.IsPolysurfaceClosed(obj): setBrepFA(obj) else: pass
def setObjZPair(obj): if rs.IsBlockInstance(obj): # pt = rs.CreatePoint(obj) return [obj, round(objBBPts(obj)[0].Z, 3)] elif rs.IsCurve(obj): return crvPtZpair(obj) elif rs.IsPolysurfaceClosed(obj): return brepPtZPair(obj) elif rs.IsSurface(obj): return srfPtZPair(obj) elif rs.IsPoint(obj): pt = rs.CreatePoint(obj) return [obj, round(pt.Z, 3)] # elif rs.IsBlockInstance(obj): # # pt = rs.CreatePoint(obj) # return [obj, round(objBBPts(obj)[0].Z, 3)] else: pass
def setObjAreaValue(obj): if rs.IsSurface(obj): setSrfAreaValue(obj) elif rs.IsPolysurface(obj) and rs.IsPolysurfaceClosed(obj): setBrepFA(obj) else: rs.SetUserText(obj, "area", 'na')
def splitModel(objs, cutLevel): point = Rhino.Geometry.Point3d(0,0,cutLevel) belowDir = rs.AddLine(point, [0,0,-9999]) aboveDir = rs.AddLine(point, [0,0,9999]) circle = rs.AddCircle(point, 9999) circleSrf = rs.AddPlanarSrf(circle) aboveGroup = rs.AddGroup("Above") belowGroup = rs.AddGroup("Below") for obj in objs: ptBtm = rs.BoundingBox(obj)[0] ptTop = rs.BoundingBox(obj)[6] if ptBtm[2]>cutLevel: intersecting = False rs.AddObjectToGroup(obj, "Above") #print "Object Above" elif ptTop[2]<cutLevel: intersecting = False rs.AddObjectToGroup(obj, "Below") #print "Object Below" else: intersecting = True if intersecting: if rs.IsBrep(obj): closed = False if rs.IsPolysurfaceClosed(obj): closed = True try: copy = rs.CopyObject(obj) splitSrfs = rs.SplitBrep(obj, circleSrf, True) for splitSrf in splitSrfs: #print "looping" if closed: rs.CapPlanarHoles(splitSrf) rs.MatchObjectAttributes(splitSrf, copy) ptBtm = rs.BoundingBox(splitSrf)[0] ptTop = rs.BoundingBox(splitSrf)[6] mdPtZ = (ptBtm[2] + ptTop[2]) / 2 if mdPtZ>cutLevel: rs.AddObjectToGroup(splitSrf, "Above") else: rs.AddObjectToGroup(splitSrf, "Below") rs.DeleteObject(copy) rs.DeleteObject(obj) except: None if rs.IsBlockInstance(obj): contents = rs.ExplodeBlockInstance(obj) for content in contents: objs.append(content) rs.DeleteObject(belowDir) rs.DeleteObject(aboveDir) rs.DeleteObject(circle) rs.DeleteObject(circleSrf)
def ColorBySize(): try: objs = rs.GetObjects("Select objects to color", 1073815613, preselect=True) if objs is None: return print "Select First Color" firstColor = rs.GetColor() if firstColor is None: return print "Select Second Color" secondColor = rs.GetColor(firstColor) if secondColor is None: return rs.EnableRedraw(False) colorLine = rs.AddLine(firstColor, secondColor) areas = [] for obj in objs: if rs.IsCurve(obj): if rs.IsCurveClosed(obj): areas.append(rs.CurveArea(obj)[0]) else: areas.append(rs.CurveLength(obj)) elif rs.IsSurface(obj): areas.append(rs.SurfaceArea(obj)[0]) elif rs.IsPolysurface(obj): if rs.IsPolysurfaceClosed(obj): areas.append(rs.SurfaceVolume(obj)[0]) elif rs.IsHatch(obj): areas.append(rs.Area(obj)) else: print "Only curves, hatches, and surfaces supported" return newAreas = list(areas) objAreas = zip(newAreas, objs) objAreas.sort() objSorted = [objs for newAreas, objs in objAreas] areas.sort() normalParams = utils.RemapList(areas, 0, 1) colors = [] for t in normalParams: param = rs.CurveParameter(colorLine, t) colors.append(rs.EvaluateCurve(colorLine, param)) for i, obj in enumerate(objSorted): rs.ObjectColor(obj, (colors[i].X, colors[i].Y, colors[i].Z)) rs.DeleteObject(colorLine) rs.EnableRedraw(True) return True except: return False
def setObjZPair(obj): if rs.IsCurve(obj): return crvPtZpair(obj) elif rs.IsPolysurfaceClosed(obj): return brepPtZPair(obj) elif rs.IsSurface(obj): return srfPtZPair(obj) elif rs.IsPoint(obj): pt = rs.CreatePoint(obj) return [obj, round(pt.Z, 3)] else: pass
def blkObjs(blkid): blockName = rs.BlockInstanceName(blkid) # objref = rs.coercerhinoobject(blkid) # idef = objref.InstanceDefinition # idefIndex = idef.Index lvl = levels[rs.GetUserText(blkid, 'level')] height = lvl['height'] xform = rs.BlockInstanceXform(blkid) objects = [x for x in rs.BlockObjects(blockName) if rs.IsPolysurfaceClosed(x)] objects = map(lambda x: rs.SetUserText(x, 'level', lvl), objects) # map(lambda x: rs.SetUserText(x, 'height', lvl)) blockInstanceObjects = rs.TransformObjects(objects, xform, True)
def ScanCompair(): mesh = rs.GetObject("Select Scan Data:", 32) if not mesh: return block = rs.GetObject("Select Closed Polysurface to Compair:", 16) if not block: return if not rs.IsPolysurfaceClosed(block): print "Polysurface is not closed" return ColorMesh(mesh, block) rs.HideObject(block) cmd = "-_SetDisplayMode " cmd += "Viewport=Active " cmd += "Mode=Shaded " cmd += "_Enter " rs.Command(cmd)
def SplitGeometry(objs, plane, dir=1): global diffRadius circle = Rhino.Geometry.Circle(plane, diffRadius) negShape = Rhino.Geometry.Cylinder(circle, diffRadius * dir) negShapeBrep = negShape.ToBrep(True, True) negShapeGeo = sc.doc.Objects.AddBrep(negShapeBrep) visibleGeometry = [] for obj in objs: if rs.IsBrep(obj): if rs.IsPolysurfaceClosed(obj): resultObjs = rs.BooleanDifference(obj, negShapeGeo, False) if resultObjs is None: visibleGeometry.append(obj) elif len(resultObjs) < 1: visibleGeometry.append(obj) else: for each in resultObjs: visibleGeometry.append(each) else: resultObjs = rs.SplitBrep(obj, negShapeGeo) if resultObjs is None: visibleGeometry.append(obj) elif len(resultObjs) < 1: visibleGeometry.append(obj) else: for each in resultObjs: if IsAbovePlane(each, plane.OriginZ): if dir == -1: visibleGeometry.append(each) else: rs.DeleteObject(each) else: if dir == 1: visibleGeometry.append(each) else: rs.DeleteObject(each) rs.DeleteObject(negShapeGeo) return visibleGeometry
def addRail(obj): if len(obj) > 1: obj = obj[0] point1 = rs.EvaluateSurface(obj, 1, 0) vec = rs.CreateVector(0, 0, height) point2 = rs.CopyObject(point1, vec) line = rs.AddLine(point1, point2) if point2: rs.DeleteObjects(point2) return line def makeWall(obj): rs.EnableRedraw(False) shape = wallProfile(obj) railCurve = addRail(shape) wall = rs.ExtrudeSurface(shape, railCurve) rs.DeleteObjects(shape) rs.DeleteObjects(railCurve) rs.EnableRedraw(False) return wall if polySrfs: rs.SelectObjects([ makeWall(polySrf) for polySrf in polySrfs if not rs.IsPolysurfaceClosed(polySrf) ])
def CreateLayer(self, name, color, surface): rs.AddLayer(name, color) rs.ObjectLayer(surface, name) if not rs.IsPolysurfaceClosed(surface): rs.MessageBox(name + " is not a closed polysurface")
def setBrepHeight(obj): if rs.IsPolysurface(obj) and rs.IsPolysurfaceClosed(obj): height = brepGetZ(obj) height = height[2] rs.SetUserText(obj, "height", str(height))
def VolumeMassWeightCentroids(_materials, _densities): # object ids objects = rs.GetObjects("Pick objects of the same material", 16, preselect=True) if objects: objectsClosed = [] for obj in objects: if rs.IsPolysurfaceClosed(obj) == True: objectsClosed.append(obj) if len(objectsClosed) > 0: # choose material pickedMaterial = rs.ListBox(_materials, "Choose the material", "Material", "Stainless Steel ") if pickedMaterial: for i in range(len(materials)): if _materials[i] == pickedMaterial: density = _densities[i] break # units correction unitsystemIndex = rs.UnitSystem() if unitsystemIndex == 0: print "No unit system. Operation terminated" return elif unitsystemIndex == 2: #mm density = density / 1000000 massFactor = 0.001 massUnits = "g" volumeUnits = "mm3" lengthUnits = "mm" elif unitsystemIndex == 3: #cm density = density / 1000 massFactor = 0.001 massUnits = "g" volumeUnits = "cm3" lengthUnits = "cm" elif unitsystemIndex == 4: #m density = density massFactor = 1 massUnits = "kg" volumeUnits = "m3" lengthUnits = "m" elif unitsystemIndex == 8: #inches density = density * 0.00057803667443635 massFactor = 0.0283495 massUnits = "oz" volumeUnits = "in3" lengthUnits = "in" elif unitsystemIndex == 9: #feet density = density * 0.998847 massFactor = 0.453592 massUnits = "lb" volumeUnits = "ft3" lengthUnits = "ft" else: print "You are not using one of these unit systems: mm, cm, m, in, ft. Function terminated" return # volumes, masses, weights, centroids volumes = [] masses = [] centroids = [] weights = [] for obj in objectsClosed: vol = rs.SurfaceVolume(obj)[0] volumes.append(vol) mass = density * vol masses.append(mass) weight = mass * massFactor * 9.81 # additionally multiply with "0.001" in case you want Kilonewtons instead of Newtons for weight unit weights.append(weight) centr = rs.SurfaceVolumeCentroid(obj)[0] centroids.append(centr) # export data to csv file filename = rs.SaveFileName("Save csv file", "*.csv||", None, "VolMassWeigCen", "csv") file = open(filename, 'w') headerline = "Object name, Volume(%s), Mass(%s), Weight(N), Centroid(%s)[x], Centroid(%s)[y], Centroid(%s)[z]\n" % ( volumeUnits, massUnits, lengthUnits, lengthUnits, lengthUnits) file.write(headerline) index = 0 for i in range(len(objectsClosed)): objectName = rs.ObjectName(objectsClosed[i]) if not objectName: index += 1 objectName = "obj%s" % (index) volu = volumes[i] mas = masses[i] weig = weights[i] x = centroids[i][0] y = centroids[i][1] z = centroids[i][2] line = "%s,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f \n" % ( objectName, volu, mas, weig, x, y, z) # adding an annotation text dot to Rhino: rs.AddPoint(centroids[i]) rs.AddTextDot(objectName, centroids[i]) file.write(line) file.close() print "Done" else: print "You did not choose material. Function terminated." return else: print "All of objects you picked are not closed (solids). Function terminated" return else: print "You did not choose appropriate or any objects. Function terminated." return
def main(): # get objects to export objs = rs.GetObjects("select objects to Make3d", 0, True, True) if not objs: print "make3d aborted"; return defaults = { 'material_thickness':18 } defaults = getDefaultValues(defaults, 'TNM_make3d') material_thickness = rs.GetReal("Material Thickness", number=defaults['material_thickness']) storeDefaultValues('TNM_make3d', {'material_thickness':material_thickness} ) rs.EnableRedraw(False) checkPos(objs) set_volumes = [] set_subtracts = [] fail_objects =[] for obj in objs: if rs.IsBlockInstance(obj): # get content copy = rs.CopyObject(obj) content = ce.explodeBlock( [copy] ) # filter objects to only curves and points copies = ce.filterObjects(content) copies = ce.joinCurves(copies) # copies = ce.joinCurves(copies) ce.simplify(copies) volumes, subtracts = convertToVolumes(copies, material_thickness) parts = volumes for subtract in subtracts: if rs.IsPolysurface(parts[0]) and rs.IsPolysurface(subtract) and rs.IsPolysurfaceClosed(parts[0]) and rs.IsPolysurfaceClosed(subtract): # print parts # print subtract temp_parts = rs.BooleanDifference(parts, [subtract], False) if temp_parts: print 'subtract success' rs.DeleteObjects(parts) rs.DeleteObject(subtract) parts = temp_parts else: print 'boolean differce failed on: %s' % subtract fail_objects.append(subtract) else: print 'boolean differce failed on: %s' % subtract fail_objects.append(subtract) # addResultToBlock(obj, result) else: # add warning message collision check print obj rs.UnselectAllObjects() rs.SelectObjects(fail_objects) intersect = None # for i, volume in enumerate(set_volumes): # for j, subtracts in enumerate(set_subtracts): # if(i != j): # print rs.ObjectLayer(volume) # intersect = rs.BooleanIntersection(rs.CopyObject(volume), subtracts, False) # if intersect: # rs.SelectObjects(intersect) # rs.DeleteObjects(subtracts) if intersect: rs.MessageBox("ERROR") rs.DeleteObjects(copies) ce.redraw()