def Shape(self): ball_vecs = [] for i in self.balls: ball_vecs.append(gp_Vec(i.getPnt().XYZ())) v_ball_to_ball = ball_vecs[1] - ball_vecs[0] ax = gp_Ax2(gp_Pnt(ball_vecs[0].XYZ()), gp_Dir(v_ball_to_ball.XYZ())) mcyl = BRepPrimAPI_MakeCylinder(ax, self.d_o / 2.0, v_ball_to_ball.Magnitude()) cyl = mcyl.Shape() mch = BRepFilletAPI_MakeChamfer(cyl) endFaces = [] for face in Topo(cyl).faces(): adaptor = BRepAdaptor_Surface(face) if adaptor.GetType() == GeomAbs_Plane: endFaces.append(face) for edge in Topo(face).edges(): mch.Add(self.chamfer_distance, edge, face) try: chamferedCyl = mch.Shape() except: chamferedCyl = cyl print("chamfer on ForceTransferCylinder failed!") mc = BRepAlgoAPI_Cut(chamferedCyl, self.balls[0].Shape()) mc = BRepAlgoAPI_Cut(mc.Shape(), self.balls[1].Shape()) return mc.Shape()
def subtract(shape1, shape2): """Boolean difference of two shapes. Parameters ---------- shape1, shape2 : TopoDS_Shape Surfaces. Returns ------- difference : TopoDS_Shape The set difference :code:`shape1 \ shape2`. Notes ----- The implementation follows the following sources: - https://techoverflow.net/2019/06/14/how-to-fuse-topods_shapes-in-opencascade-boolean-and/ - https://github.com/tpaviot/pythonocc-demos/blob/master/examples/core_boolean_fuzzy_cut_emmenthaler.py#L41-L54 For Boolean operations in Open CASCADE, see its `documentation <https://dev.opencascade.org/doc/overview/html/specification__boolean_operations.html>`_. """ arguments = TopTools_ListOfShape() arguments.Append(shape1) tools = TopTools_ListOfShape() tools.Append(shape2) difference = BRepAlgoAPI_Cut() difference.SetTools(tools) difference.SetArguments(arguments) difference.Build() return difference.Shape()
def makeEllipticalAnnularSolid(rx_outer, ry_outer, rx_inner, ry_inner, z_min, z_max): # Make the outer part of the clamp ax = gp_Ax2(gp_Pnt(0, 0, z_min), gp_Dir(0, 0, 1), gp_Dir(1, 0, 0)) ge = Geom_Ellipse(ax, rx_outer, ry_outer) elip = ge.Elips() me = BRepBuilderAPI_MakeEdge(elip, 0, 2 * pi) edge = me.Edge() mw = BRepBuilderAPI_MakeWire(edge) wire = mw.Wire() pln = gp_Pln(gp_Pnt(0, 0, z_min), gp_Dir(0, 0, 1)) mf = BRepBuilderAPI_MakeFace(pln, wire) face = mf.Face() mp = BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, z_max - z_min)) body = mp.Shape() # Make the cutter for the inner hole body ax = gp_Ax2(gp_Pnt(0, 0, z_min - 1), gp_Dir(0, 0, 1), gp_Dir(1, 0, 0)) ge = Geom_Ellipse(ax, rx_inner, ry_inner) elip = ge.Elips() me = BRepBuilderAPI_MakeEdge(elip, 0, 2 * pi) edge = me.Edge() mw = BRepBuilderAPI_MakeWire(edge) wire = mw.Wire() pln = gp_Pln(gp_Pnt(0, 0, z_min - 1), gp_Dir(0, 0, 1)) mf = BRepBuilderAPI_MakeFace(pln, wire) face = mf.Face() mp = BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, z_max + 1)) innerHoleCutter = mp.Shape() # Cut out the middle mc = BRepAlgoAPI_Cut(body, innerHoleCutter) return mc.Shape()
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 boolean_cut(shapeToCutFrom, cuttingShape): from OCC.BRepAlgoAPI import BRepAlgoAPI_Cut cut = BRepAlgoAPI_Cut(shapeToCutFrom, cuttingShape) if not cut.BuilderCanWork(): raise AssertionError("input shapes invalid") _error = {0: '- Ok', 1: '- The Object is created but Nothing is Done', 2: '- Null source shapes is not allowed', 3: '- Check types of the arguments', 4: '- Can not allocate memory for the DSFiller', 5: '- The Builder can not work with such types of arguments', 6: '- Unknown operation is not allowed', 7: '- Can not allocate memory for the Builder', } print('error status:', _error[cut.ErrorStatus()]) cut.RefineEdges() cut.FuseEdges() shp = cut.Shape() return shp
def boolean_cut(shapeToCutFrom, cuttingShape): try: cut = BRepAlgoAPI_Cut(shapeToCutFrom, cuttingShape) print 'can work?', cut.BuilderCanWork() _error = { 0: '- Ok', 1: '- The Object is created but Nothing is Done', 2: '- Null source shapes is not allowed', 3: '- Check types of the arguments', 4: '- Can not allocate memory for the DSFiller', 5: '- The Builder can not work with such types of arguments', 6: '- Unknown operation is not allowed', 7: '- Can not allocate memory for the Builder', } print 'error status:', _error[cut.ErrorStatus()] cut.RefineEdges() cut.FuseEdges() shp = cut.Shape() cut.Destroy() return shp except: print 'FAILED TO BOOLEAN CUT' return shapeToCutFrom
def boolean_cut(shapeToCutFrom, cuttingShape, debug=False): """Boolean cut tool from PythonOCC-Utils""" try: cut = BRepAlgoAPI_Cut(shapeToCutFrom, cuttingShape) if debug: print(('can work?'), cut.BuilderCanWork()) _error = {0: '- Ok', 1: '- The Object is created but Nothing is Done', 2: '- Null source shapes is not allowed', 3: '- Check types of the arguments', 4: '- Can not allocate memory for the DSFiller', 5: '- The Builder can not work with such types of arguments', 6: '- Unknown operation is not allowed', 7: '- Can not allocate memory for the Builder', } print(('error status:'), _error[cut.ErrorStatus()]) # cut.RefineEdges() shp = cut.Shape() return shp except: print('FAILED TO BOOLEAN CUT') return shapeToCutFrom
def boolean_cut(shapeToCutFrom, cuttingShape): from OCC.BRepAlgoAPI import BRepAlgoAPI_Cut try: cut = BRepAlgoAPI_Cut(shapeToCutFrom, cuttingShape) #print("Can work?", cut.BuilderCanWork()) _error = { 0: '- Ok', 1: '- The Object is created but Nothing is Done', 2: '- Null source shapes is not allowed', 3: '- Check types of the arguments', 4: '- Can not allocate memory for the DSFiller', 5: '- The Builder can not work with such types of arguments', 6: '- Unknown operation is not allowed', 7: '- Can not allocate memory for the Builder', } #print("Error status:", _error[cut.ErrorStatus()]) cut.RefineEdges() cut.FuseEdges() shp = cut.Shape() #cut.Destroy() return shp except: print("Failed to boolean cut") return shapeToCutFrom
def center_hole(base): cylinder = BRepPrimAPI_MakeCylinder(center_radius, thickness).Shape() cut = BRepAlgoAPI_Cut(base, cylinder) return cut.Shape()
return shape profile = loadBRep("inputGeom/5_segment_wire.brep") for i in Topo(profile).wires(): wire = i profile = loadBRep("inputGeom/circ.brep") for i in Topo(profile).wires(): wire_1 = i body = makeEllipticalAnnularSolid(70, 55, 40, 25, 0, 30) cavityCutter = makeEllipticalAnnularSolid(65, 50, 45, 30, 5, 31) mc = BRepAlgoAPI_Cut(body, cavityCutter) part = mc.Shape() pieSlice = makePieSlice(100, 0, pi / 4.0, -1, 31) ball = makeBall(10) stringer = makeStringerWithContinuousSlot() trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(0, 0, -75)) mt = BRepBuilderAPI_Transform(stringer, trsf) mc = BRepAlgoAPI_Cut(ball, mt.Shape()) keyedBalls = mc.Shape() output = [profile] ms = MakeSlotShapedSolid()
def char_to_solid(self, glyph): layer = glyph.layers['Fore'] bodies = [] for contour in layer: i = 0 total = len(contour) curve_points = [] if total <= 2: # Can't make solid out of 1 or 2 points continue wire = BRepBuilderAPI_MakeWire() for point in contour: if point.on_curve: if i > 0: # Complete old curve curve_points.append(gp_Pnt(point.x, point.y, 0)) self.add_to_wire(curve_points, wire) if i < total: # Start new curve curve_points = [gp_Pnt(point.x, point.y, 0)] if i == total-1: first = contour[0] curve_points.append(gp_Pnt(first.x, first.y, 0)) self.add_to_wire(curve_points, wire) else: curve_points.append(gp_Pnt(point.x, point.y, 0)) if i == total-1: first = contour[0] curve_points.append(gp_Pnt(first.x, first.y, 0)) self.add_to_wire(curve_points, wire) i += 1 face = BRepBuilderAPI_MakeFace(wire.Wire()) extrusion_vector = gp_Vec(0, 0, self.thickness) prism = BRepPrimAPI_MakePrism(face.Shape(), extrusion_vector) bodies.append(dict( prism = prism, isClockwise = contour.isClockwise(), )) if len(bodies) > 0: if len(bodies) == 1: return bodies[0]['prism'].Shape() elif len(bodies) > 1: final = None positive_union = None for body in bodies: if body['isClockwise'] == 1: if positive_union: positive_union = BRepAlgoAPI_Fuse( positive_union.Shape(), body['prism'].Shape()) else: positive_union = body['prism'] negative_union = None for body in bodies: if body['isClockwise'] == 0: if negative_union: negative_union = BRepAlgoAPI_Fuse( negative_union.Shape(), body['prism'].Shape()) else: negative_union = body['prism'] if positive_union and negative_union: final = BRepAlgoAPI_Cut( positive_union.Shape(), negative_union.Shape()) elif positive_union: final = positive_union elif negative_union: final = negative_union return final.Shape()