def create_part(data): outer_cyl = BRepPrimAPI_MakeCylinder(data["d_out"], data["l"]).Shape() inner_cyl = BRepPrimAPI_MakeCylinder(data["d_in"], data["l"]).Shape() cut = BRepAlgoAPI_Cut() L1 = TopTools_ListOfShape() L1.Append(outer_cyl) L2 = TopTools_ListOfShape() L2.Append(inner_cyl) cut.SetArguments(L1) cut.SetTools(L2) cut.SetFuzzyValue(5e-5) cut.SetRunParallel(False) cut.Build() shape = cut.Shape() anchors = {"bottom": {"p": [0.0, 0.0, 0.0], "u": [0.0, 0.0, -1.0], "v": [1.0, 0.0, 0.0]}, "top": {"p": [0.0, 0.0, data["l"]], "u": [0.0, 0.0, 1.0], "v": [1.0, 0.0, 0.0]}} properties = None return shape, anchors, properties
def fuzzy_cut(shape_A, shape_B, tol=5e-5, parallel=False): """ returns shape_A - shape_B """ cut = BRepAlgoAPI_Cut() L1 = TopTools_ListOfShape() L1.Append(shape_A) L2 = TopTools_ListOfShape() L2.Append(shape_B) cut.SetArguments(L1) cut.SetTools(L2) cut.SetFuzzyValue(tol) cut.SetRunParallel(parallel) cut.Build() return cut.Shape()
def _loop_topo(self, edges=True): if self.done: self._reinitialize() topologyType = topods_Edge if edges else topods_Vertex seq = [] hashes = [] # list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.wire_explorer.More(): # loop edges if edges: current_item = self.wire_explorer.Current() # loop vertices else: current_item = self.wire_explorer.CurrentVertex() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.wire_explorer.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = topologyType(occ_iterator.Value()) seq.append(topo_to_add) occ_iterator.Next() self.done = True return iter(seq)
def general_split_algorithm(array, tool): from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut from OCC.Core.TopTools import TopTools_ListOfShape cut = BRepAlgoAPI_Cut() L1 = TopTools_ListOfShape() for s in array: L1.Append(s) L2 = TopTools_ListOfShape() for t in tool: L2.Append(t) cut.SetArguments(L1) cut.SetTools(L2) cut.SetFuzzyValue(1.e-18) cut.SetRunParallel(False) cut.Build() return cut.Shape()
def _thicksolid(proto, t, refs): facesToRemove = TopTools_ListOfShape() for p in refs: facesToRemove.Append(_near_face(proto, p).Face()) algo = BRepOffsetAPI_MakeThickSolid() algo.MakeThickSolidByJoin(proto.Shape(), facesToRemove, t, 1.e-3) return Shape(algo.Shape())
def test_NCollection_List(self) -> None: """Check that python proxy for NCollection_List is ok""" l = TopTools_ListOfShape() shp1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() shp2 = BRepPrimAPI_MakeBox(20, 30, 40).Shape() l.Append(shp1) l.Append(shp2) self.assertEqual(l.Size(), 2) self.assertEqual(len(l), 2) l.RemoveFirst() self.assertEqual(len(l), 1)
def makeWire(self): """Generate a wire from the edges in self.edgeList.""" wireBldr = BRepBuilderAPI_MakeWire() occ_seq = TopTools_ListOfShape() for edge in self.edgeList: occ_seq.Append(edge) wireBldr.Add(occ_seq) if wireBldr.IsDone(): self.wire = wireBldr.Wire() status = True else: status = False return status
def thick_solid(event=None): S = BRepPrimAPI_MakeBox(150, 200, 110).Shape() topo = TopologyExplorer(S) vert = next(topo.vertices()) shapes = TopTools_ListOfShape() for f in topo.faces_from_vertex(vert): shapes.Append(f) _thick_solid = BRepOffsetAPI_MakeThickSolid(S, shapes, 15, 0.01) display.EraseAll() display.DisplayShape(_thick_solid.Shape()) display.FitAll()
def shell(event=None): if (win.lineEditStack and win.faceStack): text = win.lineEditStack.pop() faces = TopTools_ListOfShape() for face in win.faceStack: faces.Append(face) win.faceStack = [] workPart = win.activePart wrkPrtUID = win.activePartUID shellT = float(text) * win.unitscale newPart = BRepOffsetAPI_MakeThickSolid(workPart, faces, -shellT, 1.e-3).Shape() win.getNewPartUID(newPart, ancestor=wrkPrtUID) win.statusBar().showMessage('Shell operation complete') win.clearCallback() else: win.registerCallback(shellC) display.SetSelectionModeFace() statusText = "Select face(s) to remove then specify shell thickness." win.statusBar().showMessage(statusText)
def shell(event=None): """Shell active part""" if win.lineEditStack and win.faceStack: text = win.lineEditStack.pop() faces = TopTools_ListOfShape() for face in win.faceStack: faces.Append(face) win.faceStack = [] workPart = win.activePart uid = win.activePartUID shellT = float(text) * win.unitscale newPart = BRepOffsetAPI_MakeThickSolid(workPart, faces, -shellT, 1.0e-3).Shape() win.erase_shape(uid) doc.replaceShape(uid, newPart) win.draw_shape(uid) win.setActivePart(uid) win.statusBar().showMessage("Shell operation complete") win.clearCallback() else: win.registerCallback(shellC) display.SetSelectionModeFace() statusText = "Select face(s) to remove then specify shell thickness." win.statusBar().showMessage(statusText)
def startBottle(startOnly=True): # minus the neck fillet, shelling & threads partName = "Bottle-start" # The points we'll use to create the profile of the bottle's body aPnt1 = gp_Pnt(-width / 2.0, 0, 0) aPnt2 = gp_Pnt(-width / 2.0, -thickness / 4.0, 0) aPnt3 = gp_Pnt(0, -thickness / 2.0, 0) aPnt4 = gp_Pnt(width / 2.0, -thickness / 4.0, 0) aPnt5 = gp_Pnt(width / 2.0, 0, 0) aArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4) aSegment1 = GC_MakeSegment(aPnt1, aPnt2) aSegment2 = GC_MakeSegment(aPnt4, aPnt5) # Could also construct the line edges directly using the points # instead of the resulting line. aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value()) aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value()) aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value()) # Create a wire out of the edges aWire = BRepBuilderAPI_MakeWire(aEdge1.Edge(), aEdge2.Edge(), aEdge3.Edge()) # Quick way to specify the X axis xAxis = gp_OX() # Set up the mirror aTrsf = gp_Trsf() aTrsf.SetMirror(xAxis) # Apply the mirror transformation aBRespTrsf = BRepBuilderAPI_Transform(aWire.Wire(), aTrsf) # Get the mirrored shape back out of the transformation # and convert back to a wire aMirroredShape = aBRespTrsf.Shape() # A wire instead of a generic shape now aMirroredWire = topods.Wire(aMirroredShape) # Combine the two constituent wires mkWire = BRepBuilderAPI_MakeWire() mkWire.Add(aWire.Wire()) mkWire.Add(aMirroredWire) myWireProfile = mkWire.Wire() # The face that we'll sweep to make the prism myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile) # We want to sweep the face along the Z axis to the height aPrismVec = gp_Vec(0, 0, height) myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face(), aPrismVec) # Add fillets to all edges through the explorer mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape()) anEdgeExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_EDGE) while anEdgeExplorer.More(): anEdge = topods.Edge(anEdgeExplorer.Current()) mkFillet.Add(thickness / 12.0, anEdge) anEdgeExplorer.Next() myBody = mkFillet.Shape() # Create the neck of the bottle neckLocation = gp_Pnt(0, 0, height) neckAxis = gp_DZ() neckAx2 = gp_Ax2(neckLocation, neckAxis) myNeckRadius = thickness / 4.0 myNeckHeight = height / 10.0 mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2, myNeckRadius, myNeckHeight) myBody = BRepAlgoAPI_Fuse(myBody, mkCylinder.Shape()) if startOnly: # quit here uid = win.getNewPartUID(myBody.Shape(), name=partName) win.redraw() return partName = "Bottle-complete" # Our goal is to find the highest Z face and remove it faceToRemove = None zMax = -1 # We have to work our way through all the faces to find the highest Z face aFaceExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_FACE) while aFaceExplorer.More(): aFace = topods.Face(aFaceExplorer.Current()) if face_is_plane(aFace): aPlane = geom_plane_from_face(aFace) # We want the highest Z face, so compare this to the previous faces aPnt = aPlane.Location() aZ = aPnt.Z() if aZ > zMax: zMax = aZ faceToRemove = aFace aFaceExplorer.Next() facesToRemove = TopTools_ListOfShape() facesToRemove.Append(faceToRemove) myBody = BRepOffsetAPI_MakeThickSolid(myBody.Shape(), facesToRemove, -thickness / 50.0, 0.001) # Set up our surfaces for the threading on the neck neckAx2_Ax3 = gp_Ax3(neckLocation, gp_DZ()) aCyl1 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 0.99) aCyl2 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 1.05) # Set up the curves for the threads on the bottle's neck aPnt = gp_Pnt2d(2.0 * math.pi, myNeckHeight / 2.0) aDir = gp_Dir2d(2.0 * math.pi, myNeckHeight / 4.0) anAx2d = gp_Ax2d(aPnt, aDir) aMajor = 2.0 * math.pi aMinor = myNeckHeight / 10.0 anEllipse1 = Geom2d_Ellipse(anAx2d, aMajor, aMinor) anEllipse2 = Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4.0) anArc1 = Geom2d_TrimmedCurve(anEllipse1, 0, math.pi) anArc2 = Geom2d_TrimmedCurve(anEllipse2, 0, math.pi) anEllipsePnt1 = anEllipse1.Value(0) anEllipsePnt2 = anEllipse1.Value(math.pi) aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2) # Build edges and wires for threading anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1) anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment.Value(), aCyl1) anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2) anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value(), aCyl2) threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1.Edge(), anEdge2OnSurf1.Edge()) threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2.Edge(), anEdge2OnSurf2.Edge()) # Compute the 3D representations of the edges/wires breplib.BuildCurves3d(threadingWire1.Shape()) breplib.BuildCurves3d(threadingWire2.Shape()) # Create the surfaces of the threading aTool = BRepOffsetAPI_ThruSections(True) aTool.AddWire(threadingWire1.Wire()) aTool.AddWire(threadingWire2.Wire()) aTool.CheckCompatibility(False) myThreading = aTool.Shape() # Build the resulting compound aRes = TopoDS_Compound() aBuilder = BRep_Builder() aBuilder.MakeCompound(aRes) aBuilder.Add(aRes, myBody.Shape()) aBuilder.Add(aRes, myThreading) uid = win.getNewPartUID(aRes, name=partName) win.redraw()
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCylinder from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut from OCC.Core.TopTools import TopTools_ListOfShape outer_cyl = BRepPrimAPI_MakeCylinder(2., 10.).Shape() inner_cyl = BRepPrimAPI_MakeCylinder(1., 10.).Shape() cut = BRepAlgoAPI_Cut() L1 = TopTools_ListOfShape() L1.Append(outer_cyl) L2 = TopTools_ListOfShape() L2.Append(inner_cyl) cut.SetArguments(L1) cut.SetTools(L2) cut.SetFuzzyValue(5e-5) cut.SetRunParallel(False) cut.Build() __shape__ = cut.Shape() __anchors__ = { "bottom": { "p": [0.0, 0.0, 0.0], "u": [0.0, 0.0, -1.0], "v": [1.0, 0.0, 0.0] }, "top": { "p": [0.0, 0.0, 10.0], "u": [0.0, 0.0, 1.0], "v": [1.0, 0.0, 0.0] } }
def _loop_topo(self, topologyType, topologicalEntity=None, topologyTypeToAvoid=None): ''' this could be a faces generator for a python TopoShape class that way you can just do: for face in srf.faces: processFace(face) ''' topoTypes = {TopAbs_VERTEX: TopoDS_Vertex, TopAbs_EDGE: TopoDS_Edge, TopAbs_FACE: TopoDS_Face, TopAbs_WIRE: TopoDS_Wire, TopAbs_SHELL: TopoDS_Shell, TopAbs_SOLID: TopoDS_Solid, TopAbs_COMPOUND: TopoDS_Compound, TopAbs_COMPSOLID: TopoDS_CompSolid} assert topologyType in topoTypes.keys(), '%s not one of %s' % ( topologyType, topoTypes.keys()) # use self.myShape if nothing is specified if topologicalEntity is None and topologyTypeToAvoid is None: self.topExp.Init(self.myShape, topologyType) elif topologicalEntity is None and topologyTypeToAvoid is not None: self.topExp.Init(self.myShape, topologyType, topologyTypeToAvoid) elif topologyTypeToAvoid is None: self.topExp.Init(topologicalEntity, topologyType) elif topologyTypeToAvoid: self.topExp.Init(topologicalEntity, topologyType, topologyTypeToAvoid) seq = [] hashes = [] # list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.topExp.More(): current_item = self.topExp.Current() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.topExp.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = self.topoFactory[topologyType](occ_iterator.Value()) seq.append(topo_to_add) occ_iterator.Next() if self.ignore_orientation: # filter out those entities that share the same TShape # but do *not* share the same orientation filter_orientation_seq = [] for i in seq: _present = False for j in filter_orientation_seq: if i.IsSame(j): _present = True break if _present is False: filter_orientation_seq.append(i) return filter_orientation_seq else: return iter(seq)
# We have to work our way through all the faces to find the highest Z face so we can remove it for the shell aFaceExplorer = TopExp_Explorer(myBody_step2.Shape(), TopAbs_FACE) while aFaceExplorer.More(): aFace = topods.Face(aFaceExplorer.Current()) if face_is_plane(aFace): aPlane = geom_plane_from_face(aFace) # We want the highest Z face, so compare this to the previous faces aPntLoc = aPlane.Location() aZ = aPntLoc.Z() if aZ > zMax: zMax = aZ aFaceExplorer.Next() facesToRemove = TopTools_ListOfShape() facesToRemove.Append(aFace) myBody_step3 = BRepOffsetAPI_MakeThickSolid(myBody_step2.Shape(), facesToRemove, -thickness / 50.0, 0.001) # Set up our surfaces for the threading on the neck neckAx2_Ax3 = gp_Ax3(neckLocation, gp_DZ()) aCyl1 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 0.99) aCyl2 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 1.05) # Set up the curves for the threads on the bottle's neck aPnt = gp_Pnt2d(2.0 * math.pi, myNeckHeight / 2.0) aDir = gp_Dir2d(2.0 * math.pi, myNeckHeight / 4.0) anAx2d = gp_Ax2d(aPnt, aDir)
def __call__(self, obj, dst=None): """ This method performs the deformation on the CAD geometry. If `obj` is a TopoDS_Shape, the method returns a TopoDS_Shape containing the deformed geometry. If `obj` is a filename, the method deforms the geometry contained in the file and writes the deformed shape to `dst` (which has to be set). :param obj: the input geometry. :type obj: str or TopoDS_Shape :param str dst: if `obj` is a string containing the input filename, `dst` refers to the file where the deformed geometry is saved. """ # Manage input if isinstance(obj, str): # if a input filename is passed if dst is None: raise ValueError( 'Source file is provided, but no destination specified') shape = self.read_shape(obj) elif isinstance(obj, TopoDS_Shape): shape = obj # Maybe do we need to handle also Compound? else: raise TypeError #create compound to store modified faces compound_builder = BRep_Builder() compound = TopoDS_Compound() compound_builder.MakeCompound(compound) # cycle on the faces to get the control points # iterator to faces (TopoDS_Shape) contained in the shape faces_explorer = TopExp_Explorer(shape, TopAbs_FACE) while faces_explorer.More(): # performing some conversions to get the right # format (BSplineSurface) # TopoDS_Face obtained from iterator face = topods_Face(faces_explorer.Current()) # performing some conversions to get the right # format (BSplineSurface) bspline_surface = self._bspline_surface_from_face(face) # add the required amount of poles in u and v directions self._enrich_surface_knots(bspline_surface) # deform the Bspline surface through FFD self._deform_bspline_surface(bspline_surface) # through moving the control points, we now changed the SURFACE # underlying FACE we are processing. we now need to obtain the # curves (actually, the WIRES) that define the bounds of the # surface and TRIM the surface with them, to obtain the new FACE #we now start really looping on the wires #we will create a single curve joining all the edges in the wire # the curve must be a bspline curve so we need to make conversions # through all the way # list that will contain the (single) outer wire of the face outer_wires = [] # list that will contain all the inner wires (holes) of the face inner_wires = [] # iterator to loop over TopoDS_Wire in the original (undeformed) # face wire_explorer = TopExp_Explorer(face, TopAbs_WIRE) while wire_explorer.More(): # wire obtained from the iterator wire = topods_Wire(wire_explorer.Current()) # getting a bpline curve joining all the edges of the wire composite_curve = self._bspline_curve_from_wire(wire) # adding all the required knots to the Bspline curve self._enrich_curve_knots(composite_curve) # deforming the Bspline curve through FFD self._deform_bspline_curve(composite_curve) # the GeomCurve corresponding to the whole edge has now # been deformed. Now we must make it become an proper # wire # list of shapes (needed by the wire generator) shapes_list = TopTools_ListOfShape() # edge (to be converted to wire) obtained from the modified # Bspline curve modified_composite_edge = \ BRepBuilderAPI_MakeEdge(composite_curve).Edge() # modified edge is added to shapes_list shapes_list.Append(modified_composite_edge) # wire builder wire_maker = BRepBuilderAPI_MakeWire() wire_maker.Add(shapes_list) # deformed wire is finally obtained modified_wire = wire_maker.Wire() # now, the wire can be outer or inner. we store the outer # and (possible) inner ones in different lists # this is because we first need to trim the surface # using the outer wire, and then we can trim it # with the wires corresponding to all the holes. # the wire order is important, in the trimming process if wire == breptools_OuterWire(face): outer_wires.append(modified_wire) else: inner_wires.append(modified_wire) wire_explorer.Next() # so once we finished looping on all the wires to modify them, # we first use the only outer one to trim the surface # face builder object face_maker = BRepBuilderAPI_MakeFace(bspline_surface, outer_wires[0]) # and then add all other inner wires for the holes for inner_wire in inner_wires: face_maker.Add(inner_wire) # finally, we get our trimmed face with all its holes trimmed_modified_face = face_maker.Face() # trimmed_modified_face is added to the modified faces compound compound_builder.Add(compound, trimmed_modified_face) # and move to the next face faces_explorer.Next() ## END SURFACES ################################################# if isinstance(dst, str): # if a input filename is passed # save the shape exactly to the filename, aka `dst` self.write_shape(dst, compound) else: return compound
aFace = topods.Face(aFaceExplorer.Current()) if face_is_plane(aFace): aPlane = geom_plane_from_face(aFace) # We want the highest Z face, so compare this to the previous faces aPnt = aPlane.Location() aZ = aPnt.Z() if aZ > zMax: zMax = aZ faceToRemove = aFace aFaceExplorer.Next() facesToRemove = TopTools_ListOfShape() facesToRemove.Append(faceToRemove) myBody = BRepOffsetAPI_MakeThickSolid(myBody.Shape(), facesToRemove, -thickness / 50.0, 0.001) # Set up our surfaces for the threading on the neck neckAx2_Ax3 = gp_Ax3(neckLocation, gp_DZ()) aCyl1 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 0.99) aCyl2 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 1.05) # Set up the curves for the threads on the bottle's neck aPnt = gp_Pnt2d(2.0 * math.pi, myNeckHeight / 2.0) aDir = gp_Dir2d(2.0 * math.pi, myNeckHeight / 4.0) anAx2d = gp_Ax2d(aPnt, aDir) aMajor = 2.0 * math.pi