def selectObjperVolume(ids, targetV): volumes = [] objs = [rs.coercegeometry(id) for id in ids] for i, obj in enumerate(objs): if isinstance(obj, Rhino.Geometry.Brep): if obj.IsSolid: volumes.append((rs.SurfaceVolume(obj)[0], ids[i])) elif isinstance(obj, Rhino.Geometry.Extrusion): if obj.IsSolid: volumes.append((rs.SurfaceVolume(obj)[0], ids[i])) elif isinstance(obj, Rhino.Geometry.Mesh): if obj.IsClosed: volumes.append((rs.MeshVolume([obj])[1], ids[i])) if len(volumes) > 0: subtractL = [] for i, v in enumerate(volumes): subtractL.append((abs(v[0] - targetV), v[0], v[1])) subtractL.sort() rs.SelectObject(subtractL[0][2]) print "Selected object with %s %s volume" % (subtractL[0][1], unitSystem()) return subtractL[0][2] else: print "No closed object selected" return None
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 alloyWeight(): #alloy density density = { "Sterling Silver": 0.0105, "18K White": 0.0176, "18K Yellow": 0.0155, "9K White": 0.014, "9K Yellow": 0.012, "Platinum": 0.0215 } #create option list alloy = [] for y in density: alloy.append(y) try: #select object objs = rs.GetObject("Select PolySurfaces", rs.filter.polysurface) if objs is None: print "Aborted" return #check for naked edges if not rs.IsObjectSolid(objs): rs.ClearCommandHistory() print("\nPolySurface not solid") return #check for valid selection if objs: choice = rd.ListBox(alloy, "Select Alloy", "Alloys") wgt = round(rs.SurfaceVolume(objs)[0] * density.get(choice), 2) rs.ClearCommandHistory() print("\nVolume: " + str(round(rs.SurfaceVolume(objs)[0], 2)) + " amounts to " + str(wgt) + "g of " + choice) else: rs.ClearCommandHistory() print("\nNo valid PolySurface Selected!") except: print "Operation Aborted" Rhino.Commands.Result.Cancel
def VolumeLiters(): """Report the volume in Litres of closed surfaces, polysurfaces, or meshes.""" input_obj = rs.SelectedObjects() if not input_obj: input_obj = rs.GetObjects("Select objects") if not input_obj: return volume = 0.0 for o in input_obj: if rs.IsMesh(o): a, b, c = rs.MeshVolume(o) volume += b elif rs.IsObjectSolid(o): a, b = rs.SurfaceVolume(o) volume += a if volume == 0.0: return volume = round(volume / (rs.UnitScale(rs.UnitSystem(), 3) * 10)**3, 3) print "Volume = {} liters".format(volume)
insulation_Panels = insulation_Panels_temp #print insulation_Panels ideal_vol = ipLength * ipHeight * ipThick rs.DeleteObject(window_frame) yellow_Count = [] red_Count = [] for item in range(0, len(insulation_Panels), 1): item = insulation_Panels[item] #test = rs.IsPolysurface(item) #print test item_vol = rs.SurfaceVolume(item) #print item_vol if item_vol != None: item_vol = item_vol[0] item_vol = math.ceil(item_vol) #print item_vol if item_vol < ideal_vol: rs.ObjectColor(item, (255, 255, 0)) yellow_Count.append(item) else: rs.ObjectColor(item, (255, 0, 0)) red_Count.append(item) print "Total number of insulation panels required is %d" % len( insulation_Panels)
import rhinoscriptsyntax as rs import Rhino def getUnitSystemDescription(): unitSystemInt = rs.UnitSystem() if unitSystemInt == 2: return "mm3" elif unitSystemInt == 3: return "cm3" elif unitSystemInt == 4: return "m3" elif unitSystemInt == 8: return "in3" elif unitSystemInt == 9: return "ft3" maxVolume = rs.GetReal("Max Volume in " + getUnitSystemDescription()) object_ids = [] object_ids = rs.ObjectsByType(16) for object_id in object_ids: #check to see if the object is closed before calculating volume if rs.IsObjectSolid(object_id): blockVolume = rs.SurfaceVolume(object_id)[0] if blockVolume < maxVolume: rs.SelectObject(object_id)
##################################################### #### #### #### Creating DWL and obtaining submerged volume #### #### #### ##################################################### aux_plane = rs.PlaneFromFrame( [-5,-50,draft], [1,0,0], [0,1,0] ) dwl = rs.AddPlaneSurface(aux_plane, 100, 100) inter = rs.BooleanIntersection(copy0, dwl, False) Nabla = rs.SurfaceVolumeCentroid(inter) if Nabla: cb = rs.AddPoint(Nabla[0]) Volume0 = rs.SurfaceVolume(inter) Initial_Volume = Volume0[0] #################################### #### #### #### Heeled volume calculations #### #### #### #################################### currentl2 = rs.CurrentLayer("Heel_20") copyh = rs.CopyObject(copy0) rs.ObjectLayer(copyh, "Heel_20") rs.LayerVisible("Heel_0", False)
def FilamentCalculator(): """3D Printing filament length, weight, volume calculator.""" input_obj = rs.SelectedObjects() if not input_obj: input_obj = rs.GetObjects("Select objects") if not input_obj: return volume = 0.0 for o in input_obj: if rs.IsMesh(o): a, b, c = rs.MeshVolume(o) volume += b elif rs.IsObjectSolid(o): a, b = rs.SurfaceVolume(o) volume += a if volume == 0.0: return filaments = { "PLA": 1.24, "ABS": 1.05, "ASA": 1.07, "PETG": 1.27, "PETT": 1.45, "HIPS": 1.07, "TPU": 1.30, "PMMA": 1.18, "Nylon": 1.08, "Polycarbonate": 1.20, "Copperfill": 3.90 } filament = rs.GetString("Material:", "PLA", [a for a in filaments]) density = filaments[filament] volume = volume / rs.UnitScale(rs.UnitSystem(), 3)**3 weight = volume * filaments[filament] l1 = volume / (math.pi * (0.175 / 2)**2) / 100 l2 = volume / (math.pi * (0.285 / 2)**2) / 100 l3 = volume / (math.pi * (0.3 / 2)**2) / 100 volume = round(volume, 3) weight = round(weight, 3) l1 = round(l1, 2) l2 = round(l2, 2) l3 = round(l3, 2) message = """{f}: Density = {d} grams / cubic centimeter Volume = {v} cubic centimeters Weight ~ {w} grams 1.75 mm filament length ~ {l1} meters 2.85 mm filament length ~ {l2} meters 3.00 mm filament length ~ {l3} meters""" message = message.format(f=filament, d=density, v=volume, w=weight, l1=l1, l2=l2, l3=l3) rs.MessageBox(message, buttons=0, title="FilamentCalculator:") print(message)
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 rev(Ge, Lis, Src, div): udiv = div vdiv = div Pt_L = Lis[0][0] srf = [] FirstRay = [] SecondRay = [] First_RefPoint = [] drawray = [] Reverb = [] for i in Ge: srf.append(i[0]) sph = (rs.AddSphere(Src[1], Src[2])) Src_pt = (Src[1]) u = rs.SurfaceDomain(sph, 0) v = rs.SurfaceDomain(sph, 1) pts = [] for i in range(0, udiv + 1, 1): for j in range(0, vdiv + 1, 1): pt = (i / udiv, j / vdiv, 0) sphP = rs.SurfaceParameter(sph, pt) newpt = rs.EvaluateSurface(sph, sphP[0], sphP[1]) pts.append(rs.AddPoint(newpt)) Dir = [] for p in pts: Dir.append(rs.VectorCreate(p, Src_pt)) Reflexion = [] for d in Dir: Reflexion.append(rs.ShootRay(srf, Src_pt, d, reflections=4)) Project = [] for v in Reflexion: Cl_Pt = [] Ray_v = [] try: Project.append(v[1]) Ray_v.append(rs.AddPolyline(v)) except: pass for u in Ray_v: pt_on = rs.CurveClosestPoint(u, Pt_L) cl = rs.EvaluateCurve(u, pt_on) Dicl = (rs.Distance(Pt_L, cl)) if Dicl <= ((Lis[0])[3]): try: First_RefPoint = rs.CurveClosestPoint(u, v[1]) Second_RefPoint = rs.CurveClosestPoint(u, v[2]) endc = ((rs.CurveClosestPoint( u, (rs.CurveEndPoint(u))))) if pt_on > Second_RefPoint: SecondRay.append(pt_on / endc * (rs.CurveLength(u))) drawray.append(u) elif pt_on > First_RefPoint: FirstRay.append(pt_on / endc * (rs.CurveLength(u))) except: pass box = rs.AddBox(rs.BoundingBox(Project)) boxarea = round((rs.SurfaceArea(box)[0]), 2) Cube = [] Cube.append(box) surfacetorev = [] for s in srf: ptons = [] for p in Project: if rs.Distance((rs.BrepClosestPoint(s, p)[0]), p) < 0.1: ptons.append(p) if len(ptons) > 0: surfacetorev.append(s) surfaceab = [] for x in Ge: if x[0] in surfacetorev: surfaceab.append(x[2]) SrfandAb = [(surfacetorev[i], surfaceab[i]) for i in range(0, len(surfacetorev))] bbox = box box = round(((rs.SurfaceVolume(box))[0]), 1) srfvol = [] srfvolex = [] absvol = [] srfarea = [] srfrev = [] areaabs = [] surfacecenter = [] absidx = [] absvoltot = [] for i in SrfandAb: if rs.SurfaceVolume(i[0]) > 0: srfvol.append(i[0]) absvol.append(i[1]) else: srfarea.append((rs.SurfaceArea(i[0]))[0]) absvoltot.append(i[1]) srfvolex = rs.ExplodePolysurfaces(srfvol) for i in srfvolex: ptonsrf = [] usefulsrf = [] for p in Project: if rs.Distance((rs.BrepClosestPoint(i, p)[0]), p) < 0.01: ptonsrf.append(p) usefulsrf.append(i) if len(ptonsrf) > 0: srfarea.append(rs.SurfaceArea(i)[0]) srfrev.append(i) for i in srfrev: surfacecenter.append(rs.SurfaceAreaCentroid(i)[0]) for b in srfvol: for i in surfacecenter: if rs.Distance((rs.BrepClosestPoint(b, i)[0]), i) < 0.01: absidx.append(srfvol.index(b)) for i in absidx: absvoltot.append(absvol[i]) try: areaabs = [ srfarea[i] * (absvoltot[i]) for i in range(0, len(absvoltot)) ] except: raise Exception( 'One source must be too deep inside a geometry, try to get it out or to move it a little bit !' ) Builtareaabs = 0 for i in areaabs: Builtareaabs += i BuiltArea = 0 for i in srfarea: BuiltArea += i BuiltArea = round(BuiltArea, 2) EmptyArea = 2 * (round(boxarea - BuiltArea, 2)) if EmptyArea < 0: EmptyArea = 0 TR = 1000 * (0.16 * box) / (Builtareaabs + (EmptyArea * 1)) FRValue = 0 for f in FirstRay: FV = ((((Lis[0])[3]) * 15) / f) FRValue += FV if FRValue >= 125: FRValue = 125 SRValue = 0 for s in SecondRay: SV = ((((Lis[0])[3]) * 20) / s) SRValue += SV if SRValue > 125: SRValue = 125 Reverb.append(round(FRValue)) Reverb.append(round(SRValue)) Reverb.append(round(TR, 2)) return Reverb