def drillWithHoles(shape): "returns a shape with a bunch of spheres removed from it" box = Bnd.Bnd_Box() b = BRepBndLib.BRepBndLib() b.Add(shape, box) cShape = shape (xMin, yMin, zMin, xMax, yMax, zMax) = box.Get() d = 0.05 * ((xMax - xMin)) di = 4.0 * d c = 0 #drill holes in a rectangular grid for x in frange6(xMin, xMax, di): for y in frange6(yMin, yMax, di): for z in frange6(zMin, zMax, di): c += 1 print "Inter %d " % c #make a sphere center = gp.gp_Pnt(x, y, z) hole = BRepPrimAPI.BRepPrimAPI_MakeSphere(center, d).Shape() cut = BRepAlgoAPI.BRepAlgoAPI_Cut(cShape, hole) cut.SetOperation(3) #3 is cut21 tmp = cut.Shape() #store the newly cut shape if cut.ErrorStatus() != 0: print "Error %d cutting" % cut.ErrorStatus() else: print "Success!" cShape = tmp return cShape
def CreateShape(self): outer = occprim.BRepPrimAPI_MakeCylinder(self.outer_radius,self.length).Shape() inner = occprim.BRepPrimAPI_MakeCylinder(self.inner_radius,self.length).Shape() self.occ_shape = occalgo.BRepAlgoAPI_Cut(outer,inner).Shape() T = gp_Trsf() T.SetTranslation(gp_Vec(0,0,-self.length/2)) self.occ_shape = occbuild.BRepBuilderAPI_Transform(self.occ_shape,T,False).Shape() self.OrientShape()
def _makeSlice(self, shapeToSlice, zLevel): s = Slice() #used to determine if a slice is identical to others. s.hatchDir = self.hatchReversed s.fillWidth = self.options.filling.fillWidth #change if layers are variable thickness s.sliceHeight = self.options.layerHeight s.zLevel = zLevel #make a cutting plane p = gp.gp_Pnt(0, 0, zLevel) origin = gp.gp_Pnt(0, 0, zLevel - 1) csys = gp.gp_Ax3(p, gp.gp().DZ()) cuttingPlane = gp.gp_Pln(csys) bff = BRepBuilderAPI.BRepBuilderAPI_MakeFace(cuttingPlane) face = bff.Face() #odd, a halfspace is faster than a box? hs = BRepPrimAPI.BRepPrimAPI_MakeHalfSpace(face, origin) hs.Build() halfspace = hs.Solid() #make the cut bc = BRepAlgoAPI.BRepAlgoAPI_Cut(shapeToSlice, halfspace) cutShape = bc.Shape() foundFace = False for face in Topo(cutShape).faces(): if self._isAtZLevel(zLevel, face): foundFace = True log.debug("Face is at zlevel" + str(zLevel)) s.addFace(face) #TestDisplay.display.showShape(face); log.debug("Face" + str(face)) if self.options.useSliceFactoring: mySum = s.getCheckSum() #print 'Slice Created, Checksum is',mySum; for otherSlice in self.slices: #print "Slice Checksum=",otherSlice.getCheckSum(); if mySum == otherSlice.getCheckSum(): log.info( "This slice matches another one exactly. using that so we can save time." ) return otherSlice.copyToZ(zLevel) if not foundFace: log.warn("No faces found after slicing at zLevel " + str(zLevel) + " !. Skipping This layer completely") return None else: return s
def CreateShape(self): self.occ_shape = self.elements[0].occ_shape if self.elements[0].complement: print('ERROR: complementary shape cannot be first in list') exit(1) for s in self.elements: if s.occ_shape!=self.occ_shape: if s.complement: self.occ_shape = occalgo.BRepAlgoAPI_Cut(self.occ_shape,s.occ_shape).Shape() else: self.occ_shape = occalgo.BRepAlgoAPI_Common(self.occ_shape,s.occ_shape).Shape() self.OrientShape()
def _makeSlice(self, shapeToSlice, zLevel): s = Slice() #change if layers are variable thickness s.sliceHeight = self.sliceHeight s.zLevel = zLevel #make a cutting plane p = gp.gp_Pnt(0, 0, zLevel) origin = gp.gp_Pnt(0, 0, zLevel - 1) csys = gp.gp_Ax3(p, gp.gp().DZ()) cuttingPlane = gp.gp_Pln(csys) bff = BRepBuilderAPI.BRepBuilderAPI_MakeFace(cuttingPlane) face = bff.Face() #odd, a halfspace is faster than a box? hs = BRepPrimAPI.BRepPrimAPI_MakeHalfSpace(face, origin) hs.Build() halfspace = hs.Solid() #make the cut bc = BRepAlgoAPI.BRepAlgoAPI_Cut(shapeToSlice, halfspace) cutShape = bc.Shape() #search the shape for faces at the specified zlevel texp = TopExp.TopExp_Explorer() texp.Init(cutShape, TopAbs.TopAbs_FACE) foundFace = False while (texp.More()): face = ts.Face(texp.Current()) if self._isAtZLevel(zLevel, face): foundFace = True logging.debug("Face is at zlevel" + str(zLevel)) s.addFace(face, self.saveSliceFaces) texp.Next() #free memory face.Nullify() bc.Destroy() texp.Clear() texp.Destroy() if not foundFace: logging.warn("No faces found after slicing at zLevel " + str(zLevel) + " !. Skipping This layer completely") return None else: return s
def makeSection(self, cuttingPlane, shapeToSection, zLevel): """ Uses halfspaces to make a cut. """ bff = BRepBuilderAPI.BRepBuilderAPI_MakeFace(cuttingPlane) face = bff.Face() origin = gp.gp_Pnt(0, 0, zLevel - 1) #odd, a halfspace is faster than a box? hs = BRepPrimAPI.BRepPrimAPI_MakeHalfSpace(face, origin) hs.Build() halfspace = hs.Solid() #make the cut bc = BRepAlgoAPI.BRepAlgoAPI_Cut(self.solid.shape, halfspace) cutShape = bc.Shape() ff = [] for face in Topo(cutShape).faces(): if OCCUtil.isFaceAtZLevel(zLevel, face): ff.append(face) return ff
def drillWithHolesFaster(shape): "returns a shape with a bunch of spheres removed from it" box = Bnd.Bnd_Box() b = BRepBndLib.BRepBndLib() b.Add(shape, box) (xMin, yMin, zMin, xMax, yMax, zMax) = box.Get() d = 0.05 * ((xMax - xMin)) di = 3.0 * d vec = gp.gp_Vec(gp.gp_Pnt(0, 0, 0), gp.gp_Pnt(0, 0, d)) cp = None compound = TopoDS.TopoDS_Compound() builder = BRep.BRep_Builder() builder.MakeCompound(compound) #drill holes in a rectangular grid for x in frange6(xMin, xMax, di): for y in frange6(yMin, yMax, di): for z in frange6(zMin, zMax, di): #make a sphere center = gp.gp_Pnt(x, y, z) hole = BRepPrimAPI.BRepPrimAPI_MakeSphere(center, d).Shape() #lets see if a square hole is faster! #w = squareWire(center,d ); #hb = BRepPrimAPI.BRepPrimAPI_MakePrism(w,vec,False,True); #hb.Build(); #hole = hb.Shape(); builder.Add(compound, hole) display.DisplayShape(compound) q = time.clock() cut = BRepAlgoAPI.BRepAlgoAPI_Cut(shape, compound) if cut.ErrorStatus() == 0: print "Cut Took %0.3f sec." % (time.clock() - q) return cut.Shape() else: print "Error Cutting: %d" % cut.ErrorStatus() return shape
def make_OAP(EFL, diameter, height, centre, direction, x_axis): FL = EFL/2. #focal length radius = diameter/2. outside = EFL + radius inside = EFL - radius length = (outside**2)/(4.*FL) - FL + height #pbl_shape = make_true_para(FL, 5.0, outside+1) pbl_shape = make_interp_parabola(FL, inside-1, outside+1) ax3 = gp.gp_Ax2(gp.gp_Pnt(EFL,0,-height), #origin gp.gp_Dir(0.,0.,1.), #main direction is X gp.gp_Dir(0.,1.,0.)) #X Direction is Y cyl_solid = BRepPrimAPI.BRepPrimAPI_MakeCylinder(ax3, radius, length) cut = BRepAlgoAPI.BRepAlgoAPI_Cut(cyl_solid.Shape(), pbl_shape) loc= position_shape(toshape(cut), centre, direction, x_axis) #nurbs = BRepBuilderAPI.BRepBuilderAPI_NurbsConvert(loc) return loc #toshape(nurbs)
def ScaleZ(self, val): v = self.Value(3, 3) self.SetValue(3, 3, val * v) def Translate(self, dx, dy, dz): self.SetTranslationPart(gp.gp_XYZ(dx, dy, dz)) t = GTransform() t.ScaleZ(2.0) trans = BRepBuilderAPI.BRepBuilderAPI_GTransform(sphere.Shape(), t) box = BRepPrimAPI.BRepPrimAPI_MakeBox(100, 100, 100) cut = BRepAlgoAPI.BRepAlgoAPI_Cut(box.Shape(), trans.Shape()) view(cut.Shape()) #step_export = STEPControl.STEPControl_Writer() #step_export.Transfer(trans.Shape(), STEPControl.STEPControl_AsIs) #step_export.Write("/home/bryan/test_ellipse.stp") #el = gp.gp_Elips(gp.gp_Ax2(gp.gp_Pnt(0,0,0), gp.gp_Dir(1,0,0)), 10, 5) #EL = Geom.Geom_Ellipse(el) #handle = Geom.Handle_Geom_Ellipse(EL) #revolve = BRepPrimAPI.BRepPrimAPI_MakeRevolution(handle, 180) #nurbs = #view(revolve.Shape())
sys.exit(0) cut_tool = BRepPrimAPI.BRepPrimAPI_MakeBox(gp.gp_Pnt(6, 6, 6), 5., 5., 5.).Shape() #tool_label = ts.NewChild(root) #ns_builder = TNaming.TNaming_Builder(tool_label) #ns_builder.Generated(cut_tool) # #topo = Topo(cut_tool) #for edge in topo.edges(): # sub_label = ts.NewChild(tool_label) # ns_builder = TNaming.TNaming_Builder(sub_label) # ns_builder.Generated(edge) bop = BRepAlgoAPI.BRepAlgoAPI_Cut(box, cut_tool) cut_shape = bop.Shape() cut_label = ts.NewChild(root) ns_builder = TNaming.TNaming_Builder(cut_label) ns_builder.Modify(box, cut_shape) #if bop.HasModified(): # mod_label = ts.NewChild(cut_label) # ns_builder = TNaming.TNaming_Builder(mod_label) # for edge in Topo(cut_shape).edges(): # modified = bop.Modified(edge) # itr = TopTools.TopTools_ListIteratorOfListOfShape(modified) # while itr.More(): # this = itr.Value() # ns_builder.Modify(edge, modified)
length = (outside**2) / (4. * FL) ax2 = gp.gp_Ax2( gp.gp_Pnt(0, 0, 0), #origin gp.gp_Dir(0., 0., 1.), #main direction is X gp.gp_Dir(1., 0., 0.)) #X Direction is Z pbl_shape = BRepPrimAPI.BRepPrimAPI_MakeRevolution(ax2, h_para, 1.0, outside) ax3 = gp.gp_Ax2( gp.gp_Pnt(EFL, 0, 0), #origin gp.gp_Dir(0., 0., 1.), #main direction is X gp.gp_Dir(0., 1., 0.)) #X Direction is Y cyl_solid = BRepPrimAPI.BRepPrimAPI_MakeCylinder(ax3, radius, length) nurb = BRepBuilderAPI.BRepBuilderAPI_NurbsConvert(pbl_shape.Shape()) cut = BRepAlgoAPI.BRepAlgoAPI_Cut(cyl_solid.Shape(), nurb.Shape()) #view(wire.Shape(), cyl_solid.Shape(), pbl_shape.Shape()) #view(cyl_solid.Shape(), pbl_shape.Shape()) view(cut.Shape()) exportList = [nurb.Shape()] step_export = STEPControl.STEPControl_Writer() for shape in exportList: step_export.Transfer(shape, STEPControl.STEPControl_AsIs) #step_export.Write("/home/bryan/ParabolicMirror.step")