def calcArea(srfs): areas = [] for srf in srfs: areas.append(rs.SurfaceArea(srf)[0]) totalArea = round(sum(areas), 2) totalAreaPy = round(totalArea/3.3058, 2) return [totalArea, totalAreaPy]
def calcArea(srfs): areas = [] for srf in srfs: areas.append(rs.SurfaceArea(srf)[0]) totalArea = sum(areas) totalAreaPy = totalArea / 3.3058 print area, areapy txt = rs.ClipboardText(area)
def calcAreas(srfs): areas = [] for srf in srfs: areas.append(rs.SurfaceArea(srf)[0]) # totalArea = round(castToM(False, sum(areas)), 2) # totalAreaPy = round(totalArea/3.3058, 2) totalArea = castToM(False, sum(areas)) totalAreaPy = totalArea / 3.3058 return [round(totalArea, 2), round(totalAreaPy, 2)]
def sortSurfaces(srfs, tol): srfToSelect = [] i = 0 for srf in srfs: if rs.SurfaceArea(srf)[0] < tol: srfToSelect.append(srf) rs.Prompt("Sorting: " + str(i) + "/" + str(len(srfs))) i += 1 return srfToSelect
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 SplitAndKeepSmallest(self, object, cutting): objects = rs.SplitBrep(object, cutting, True) # sort split object parts by surface area meta = [(i, rs.SurfaceArea(objects[i])[0]) for i in range(len(objects))] meta.sort(key=lambda iy: -iy[1]) # delete other parts for i in range(len(meta) - 1): object = objects[meta[i][0]] rs.DeleteObject(object) return objects[meta[len(meta) - 1][0]]
def _LSMetrics(self, CandidateSurface, ActualSemiSpan, CandidateSurfaceProjection_area, ChordFactor, ScaleFactor): # This is the objective function of the search for the lifting surface that # best matches the targets (i.e., the best scaling factor values). Should # delete the surface once the calculation is complete. SA = rs.SurfaceArea(CandidateSurface) PSA = CandidateSurfaceProjection_area AR = ((2.0*ActualSemiSpan)**2.0)/(2.0*PSA) RootChord = (self.ChordFunct(0)*ChordFactor)*ScaleFactor # Weighted combined metric WM =\ self.wTargetArea *((self.TargetArea - 2.0*PSA ) /self.TargetArea )**2.0+\ self.wTargetWettedArea *((self.TargetWettedArea - 2.0* SA[0]) /self.TargetWettedArea )**2.0+\ self.wTargetSpan *((self.TargetSpan - 2.0* ActualSemiSpan)/self.TargetSpan )**2.0+\ self.wTargetAspectRatio*((self.TargetAspectRatio- AR ) /self.TargetAspectRatio)**2.0+\ self.wTargetRootChord *((self.TargetRootChord - RootChord ) /self.TargetRootChord )**2.0 print("Proj.area:%3.2f Wet.area:%3.2f Span:%3.2f Aspect ratio:%3.2f Root chord:%3.2f" % (2.0*PSA, 2.0* SA[0], 2.0* ActualSemiSpan, AR, RootChord)) return WM
def GenerateLiftingSurface(self, ChordFactor, ScaleFactor, OptimizeChordScale=0): # This is the main method of this class. It builds a lifting surface # (wing, tailplane, etc.) with the given ChordFactor and ScaleFactor or # an optimized ChordFactor and ScaleFactor, with the local search started # from the two given values. x0 = [ChordFactor, ScaleFactor] if OptimizeChordScale: self._CheckOptParCorrectlySpec() self._NormaliseWeightings() self._PrintTargetsAndWeights() print("Optimizing scale factors...") # An iterative local hillclimber type optimizer is needed here. One # option might be SciPy's fmin as below: # x0, fopt, iter, funcalls, warnflag, allvecs = scipy.optimize.fmin(self._LSObjective, x0, retall=True, xtol=0.025, full_output=True) # However, SciPy is not supported on 64-bit Rhino installations, so # so here we use an alternative: a simple evoltionary optimizer # included with AirCONICS_tools. MaxIter = 50 xtol = 0.025 deltax = [x0[0]*0.25,x0[1]*0.25] x0, fopt = act.boxevopmin2d(self._LSObjective, x0, deltax, xtol, MaxIter) x0[0] = abs(x0[0]) x0[1] = abs(x0[1]) print("Optimum chord factor %5.3f, optimum scale factor %5.3f" % (x0[0], x0[1])) LS, ActualSemiSpan, LSP_area, RootChord, AR, WingTip = self._BuildLS(x0[0], x0[1]) self._ClearConstructionGeometry() # Moving the wing into position AP = rs.AddPoint(self.ApexPoint) MoveVec = rs.VectorCreate(AP, (0,0,0)) rs.MoveObject(LS, MoveVec) if WingTip: rs.MoveObject(WingTip, MoveVec) rs.DeleteObject(AP) # FINAL REPORT ON THE COMPLETED SURFACE SA = rs.SurfaceArea(LS) print("Wing complete. Key features (all related to both wings):") print("Proj.area: %5.4f Wet.area: %5.4f Span:%5.4f Aspect ratio: %5.4f Root chord: %5.4f" % (2*LSP_area, 2.0*SA[0], 2.0*ActualSemiSpan, AR, RootChord)) return LS, ActualSemiSpan, LSP_area, RootChord, AR, WingTip
def FracturesSurfaceArea(self, fracture_guid_list): """ determines the total surface area of all fractures in the medium Parameters ---------- fracture_guid_list: list a list of fractures' guids in the network """ # function to determine the surface area of all fractures in the domain # initialise fracturs surface area fractures_surface_area = 0 # loop to sum all areas of fractures for fracture in fracture_guid_list: # get surface area of the fracture fracture_area = rs.SurfaceArea(fracture) # increment the fractures surface area fractures_surface_area += fracture_area[0] return fractures_surface_area
import rhinoscriptsyntax as rs surf_id = rs.GetObject("Select a surface", 8) print surf_id area = rs.SurfaceArea(surf_id) domainU = rs.SurfaceDomain(surf_id, 0) domainV = rs.SurfaceDomain(surf_id, 1) print "area = ", area print "domain of u-direction = ", domainU print "domain of v-direction = ", domainV uv = (domainU[1] - domainU[0]) / 2, (domainV[1] - domainV[0]) / 2 center = rs.SurfaceEvaluate(surf_id, uv, 1) normal = rs.SurfaceNormal(surf_id, uv) print "xyz co-ordinate of center = ", center[0] print "normal vector = ", normal normal = rs.VectorUnitize(normal) normal = rs.VectorScale(normal, 5) start = center[0] end = rs.VectorAdd(start, normal) rs.AddPoint(center[0]) rs.AddLine(start, end)
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
def calcArea(srf): area = rs.SurfaceArea(srf)[0] totalArea = round(castToM(False, area), 2) totalAreaPy = round(totalArea / 3.3058, 2) return [totalArea, totalAreaPy]
def flipBowtie(srf_id, pts_for_srf, N2): # corrects for Rhino bug by flipping "bow tie" surfaces # I know there is a more consise way to code this, # but this is easy rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): [ptA1, ptB1, ptC1, ptD1] = pts_for_srf rs.DeleteObject(srf_id) srf_id = rs.AddSrfPt([ptB1, ptC1, ptD1, ptA1]) rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): rs.DeleteObject(srf_id) pts_for_srf = [ptC1, ptD1, ptA1, ptB1] srf_id = rs.AddSrfPt([ptC1, ptD1, ptA1, ptB1]) rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): rs.DeleteObject(srf_id) pts_for_srf = [ptD1, ptA1, ptC1, ptC1] srf_id = rs.AddSrfPt([ptD1, ptA1, ptC1, ptC1]) rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): rs.DeleteObject(srf_id) pts_for_srf = [ptA1, ptD1, ptC1, ptB1] srf_id = rs.AddSrfPt([ptA1, ptD1, ptC1, ptB1]) rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): rs.DeleteObject(srf_id) pts_for_srf = [ptD1, ptC1, ptB1, ptA1] srf_id = rs.AddSrfPt([ptD1, ptC1, ptB1, ptA1]) rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): rs.DeleteObject(srf_id) pts_for_srf = [ptC1, ptB1, ptA1, ptD1] srf_id = rs.AddSrfPt([ptC1, ptB1, ptA1, ptD1]) rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): rs.DeleteObject(srf_id) pts_for_srf = [ptB1, ptA1, ptD1, ptC1] srf_id = rs.AddSrfPt([ptB1, ptA1, ptD1, ptC1]) rs.SelectObject(srf_id) # select surface rs.Command("-_projecttocplane " + "Yes " + "-_Enter ") proj_id = rs.GetObject(preselect=True) if rs.SurfaceArea(proj_id)[0] < .75 * (N2**2): print("could not flip bowtie :( ") return pts_for_srf
def _BuildLS(self, ChordFactor, ScaleFactor): # Generates a tentative lifting surface, given the general, nondimensio- # nal parameters of the object (variations of chord length, dihedral, etc.) # and the two scaling factors. LEPoints = self._GenerateLeadingEdge() Sections = [] ProjectedSections = [] TEPoints_u = [] TEPoints_l = [] for i, LEP in enumerate(LEPoints): Eps = float(i)/self.SegmentNo Airfoil, Chrd = self.AirfoilFunct(Eps, LEP, self.ChordFunct, ChordFactor, self.DihedralFunct, self.TwistFunct) list.append(Sections, Airfoil) Pr = rs.ProjectCurveToSurface(Chrd,self.XoY_Plane,self.ProjVectorZ) list.append(ProjectedSections, Pr) list.append(TEPoints_l, rs.CurveEndPoint(Airfoil)) list.append(TEPoints_u, rs.CurveStartPoint(Airfoil)) rs.DeleteObjects(Chrd) LS = rs.AddLoftSrf(Sections,loft_type=self.LooseSurf) if LS==None: # Failed to fit loft surface. Try another fitting algorithm TECurve_u = rs.AddInterpCurve(TEPoints_u) TECurve_l = rs.AddInterpCurve(TEPoints_l) rails = [] list.append(rails, TECurve_u) list.append(rails, TECurve_l) # Are the first and last curves identical? # AddSweep fails if they are, so if that is the case, one is skipped CDev = rs.CurveDeviation(Sections[0],Sections[-1]) if CDev==None: shapes = Sections LS = rs.AddSweep2(rails, shapes, False) else: shapes = Sections[:-1] LS = rs.AddSweep2(rails, shapes, True) rs.DeleteObjects(rails) rs.DeleteObjects([TECurve_u, TECurve_l]) WingTip = None if self.TipRequired: TipCurve = Sections[-1] TipCurve = act.AddTEtoOpenAirfoil(TipCurve) WingTip = rs.AddPlanarSrf(TipCurve) rs.DeleteObject(TipCurve) # Calculate projected area # In some cases the projected sections cannot all be lofted in one go # (it happens when parts of the wing fold back onto themselves), so # we loft them section by section and we compute the area as a sum. LSP_area = 0 # Attempt to compute a projected area try: for i, LEP in enumerate(ProjectedSections): if i < len(ProjectedSections)-1: LSPsegment = rs.AddLoftSrf(ProjectedSections[i:i+2]) SA = rs.SurfaceArea(LSPsegment) rs.DeleteObject(LSPsegment) LSP_area = LSP_area + SA[0] except: print "Failed to compute projected area. Using half of surface area instead." LS_area = rs.SurfaceArea(LS) LSP_area = 0.5*LS_area[0] BB = rs.BoundingBox(LS) if BB: ActualSemiSpan = BB[2].Y - BB[0].Y else: ActualSemiSpan = 0.0 # Garbage collection rs.DeleteObjects(Sections) try: rs.DeleteObjects(ProjectedSections) except: print "Cleanup: no projected sections to delete" rs.DeleteObjects(LEPoints) # Scaling Origin = rs.AddPoint([0,0,0]) ScaleXYZ = (ScaleFactor, ScaleFactor, ScaleFactor) LS = rs.ScaleObject(LS, Origin, ScaleXYZ) if self.TipRequired and WingTip: WingTip = rs.ScaleObject(WingTip, Origin, ScaleXYZ) rs.DeleteObject(Origin) ActualSemiSpan = ActualSemiSpan*ScaleFactor LSP_area = LSP_area*ScaleFactor**2.0 RootChord = (self.ChordFunct(0)*ChordFactor)*ScaleFactor AR = ((2.0*ActualSemiSpan)**2.0)/(2*LSP_area) return LS, ActualSemiSpan, LSP_area, RootChord, AR, WingTip
def getSurfaceArea(message): surface = rs.GetObject(message) val = rs.SurfaceArea(surface) return val[0]
for face in faces: if rs.IsSurface(face): domainU = rs.SurfaceDomain(face, 0) domainV = rs.SurfaceDomain(face, 1) u = domainU[1] / 2.0 v = domainV[1] / 2.0 point = rs.EvaluateSurface(face, u, v) param = rs.SurfaceClosestPoint(face, point) normal = rs.SurfaceNormal(face, param) # print normal if normal.Z == -1: bndry.append(face) for bnd in bndry: area = rs.SurfaceArea(bnd)[0] areapy = area / 3.3058 print area, areapy txt = rs.ClipboardText(area) if faces: rs.DeleteObjects(faces) def calcArea(srfs): areas = [] for srf in srfs: areas.append(rs.SurfaceArea(srf)[0]) totalArea = sum(areas) totalAreaPy = totalArea / 3.3058 print area, areapy txt = rs.ClipboardText(area)
normal = rs.SurfaceNormal(face, param) normalVect = distance * rs.VectorReverse(normal) endPoint = points[1] + normalVect line3 = rs.AddLine(points[1], endPoint) curve1 = rs.AddFilletCurve(line, line2, radius) curve2 = rs.AddFilletCurve(line, line3, radius) curve = rs.JoinCurves([curve1, curve2], True) profile = rs.ExtrudeCurve(curve, path) splitSolids = rs.SplitBrep(solid, profile) area1 = rs.SurfaceArea(splitSolids[0]) area2 = rs.SurfaceArea(splitSolids[1]) area3 = rs.SurfaceArea(splitSolids[2]) rs.DeleteObject(solid) i = 0 swap = True while swap == True: swap = False for i in range(0, 1): if rs.SurfaceArea(splitSolids[i]) > rs.SurfaceArea(splitSolids[i + 1]): tmp = splitSolids[i + 1] splitSolids[i + 1] = splitSolids[i] splitSolids[i] = tmp
l = "Layer 0" + str(array_N2.index(N2) + 1) rs.CurrentLayer(layer=l) # draw a surface between four points, counterclockwise srf_id = rs.AddSrfPt(pts_for_srf) # flip "bowtie" surfaces # ASSUMES "Top" CPlane! pts_for_srf = flipBowtie(srf_id, pts_for_srf, N2) # redraw surface rs.AddSrfPt(pts_for_srf) srf_id = rs.AddSrfPt(pts_for_srf) # compute area of surface, add to total area srfArea = srfArea + rs.SurfaceArea(srf_id)[0] # delete surface rs.DeleteObject(srf_id) # reset layer rs.CurrentLayer(layer="Default") # move pt_start to ptB until ptB[0] = end_x, # at which point move pt_start to [start[0], start[1] + row * N2, start[2]], if round(ptB[0], 2) < round(end_x, 2): pt_start = ptB else: pt_start = [start[0], pt_start[1] + N2, start[2]] # save surface area
line = rs.AddLine(outerEndPoint, miterEndPoint) rs.HideObject(line) midPoint = rs.CurveMidPoint(line) normalVector = rs.VectorCrossProduct(outerVector, miterVector) cutPlane = rs.AddCutPlane(solid, intersectPoints[0], midPoint, normal = normalVector) rs.HideObject(cutPlane) splitSolids = rs.SplitBrep(solid, cutPlane, True) rs.CapPlanarHoles(splitSolids[0]) rs.CapPlanarHoles(splitSolids[1]) if rs.SurfaceArea(splitSolids[0]) > rs.SurfaceArea(splitSolids[1]): rs.DeleteObject(splitSolids[1]) else: rs.DeleteObject(splitSolids[0]) if not solid2: x = 0 else: splitSolids = rs.SplitBrep(solid2, cutPlane, True) rs.CapPlanarHoles(splitSolids[0]) rs.CapPlanarHoles(splitSolids[1]) if rs.SurfaceArea(splitSolids[0]) > rs.SurfaceArea(splitSolids[1]): rs.DeleteObject(splitSolids[1]) else:
def rc_collapse_box(): go = Rhino.Input.Custom.GetObject() go.GeometryFilter = Rhino.DocObjects.ObjectType.Brep default_KeepLayer = sc.sticky["KeepLayer"] if sc.sticky.has_key( "KeepLayer") else False default_IgnoreOpen = sc.sticky["IgnoreOpen"] if sc.sticky.has_key( "IgnoreOpen") else False opt_KeepLayer = Rhino.Input.Custom.OptionToggle(default_KeepLayer, "No", "Yes") opt_IgnoreOpen = Rhino.Input.Custom.OptionToggle(default_IgnoreOpen, "No", "Yes") go.SetCommandPrompt("Select Breps") go.AddOptionToggle("KeepLayer", opt_KeepLayer) go.AddOptionToggle("IgnoreOpenBreps", opt_IgnoreOpen) go.GroupSelect = True go.SubObjectSelect = False go.AcceptEnterWhenDone(True) go.AcceptNothing(True) go.EnableClearObjectsOnEntry(False) go.EnableUnselectObjectsOnExit(False) go.GroupSelect = True go.SubObjectSelect = False go.DeselectAllBeforePostSelect = False res = None bHavePreselectedObjects = True while True: res = go.GetMultiple(1, 0) if res == Rhino.Input.GetResult.Option: go.EnablePreSelect(False, True) continue #If not correct elif res != Rhino.Input.GetResult.Object: rs.Redraw() print "No breps were selected!" return Rhino.Commands.Result.Cancel if go.ObjectsWerePreselected: rs.Redraw() bHavePreselectedObjects = True go.EnablePreSelect(False, True) continue break OPT_IGNORE_OPEN = opt_IgnoreOpen.CurrentValue OPT_KEEP_LAYER = opt_KeepLayer.CurrentValue sticky["IgnoreOpen"] = OPT_IGNORE_OPEN sticky["KeepLayer"] = OPT_KEEP_LAYER rs.EnableRedraw(False) input_breps = [] for i in xrange(go.ObjectCount): b_obj = go.Object(i).Object() input_breps.append(b_obj.Id) current_cplane = sc.doc.Views.ActiveView.ActiveViewport.GetConstructionPlane( ) temp_cplane = current_cplane.Plane current_cplane.Plane = rs.WorldXYPlane() solid_brep_count = 0 for brep in input_breps: if not rs.IsObjectSolid(brep): solid_brep_count += 1 if OPT_IGNORE_OPEN: continue if OPT_KEEP_LAYER: brep_layer = rs.ObjectLayer(rs.coerceguid(brep)) exploded = rs.ExplodePolysurfaces(brep, True) remaining_srfs = [] for srf in exploded: norm = rs.SurfaceNormal(srf, [0.5, 0.5]) if 10 > rs.VectorAngle(norm, [0, 0, 1]) or 10 > rs.VectorAngle( norm, [0, 0, -1]): rs.DeleteObject(srf) else: remaining_srfs.append(srf) areas = [rs.SurfaceArea(s) for s in remaining_srfs] areas = [x[0] for x in areas] srfs, areas = zip( *sorted(zip(remaining_srfs, areas), key=lambda x: x[1])) pt1, _ = rs.SurfaceAreaCentroid(srfs[-1]) pt2, _ = rs.SurfaceAreaCentroid(srfs[-2]) vect = rs.VectorCreate(pt2, pt1) vect = rs.VectorDivide(vect, 2) rs.MoveObject(srfs[-1], vect) if OPT_KEEP_LAYER: rs.ObjectLayer(srfs[-1], brep_layer) rs.SelectObjects(srfs[-1]) rs.DeleteObjects(srfs[:-1]) rs.EnableRedraw(True) rs.Redraw() current_cplane.Plane = temp_cplane if solid_brep_count > 0: outcome = " and were not collapsed." if OPT_IGNORE_OPEN else " and may have been flattened incorrectly. Check input geometry." report = str(solid_brep_count) + " brep(s) were not closed" + outcome print report
def setAreaValue(obj): area = rs.SurfaceArea(obj)[0] area = round(area, 2) rs.SetUserText(obj, 'area', str(area))