def __init__(self, shaft, blade, n_blades): self.shaft_solid = shaft.generate_solid() blade.apply_transformations(reflect=True) blade_solid = blade.generate_solid(max_deg=2, display=False, errors=None) blades = [] blades.append(blade_solid) for i in range(n_blades-1): blade.rotate(rad_angle=1.0*2.0*np.pi/float(n_blades)) blade_solid = blade.generate_solid(max_deg=2, display=False, errors=None) blades.append(blade_solid) blades_combined = blades[0] for i in range(len(blades)-1): boolean_union = BRepAlgoAPI_Fuse(blades_combined, blades[i+1]) boolean_union.Build() if not boolean_union.IsDone(): raise RuntimeError('Unsuccessful assembling of blade') blades_combined = boolean_union.Shape() boolean_union = BRepAlgoAPI_Fuse(self.shaft_solid, blades_combined) boolean_union.Build() result_compound = boolean_union.Shape() sewer = BRepBuilderAPI_Sewing(1e-2) sewer.Add(result_compound) sewer.Perform() self.sewed_full_body = sewer.SewedShape()
def generate_solid(self): """ Generate an assembled solid shaft using the BRepBuilderAPI_MakeSolid algorithm. This method requires PythonOCC to be installed. :raises RuntimeError: if the assembling of the solid shaft is not completed successfully :return: solid shaft :rtype: OCC.Core.TopoDS.TopoDS_Solid """ ext = os.path.splitext(self.filename)[1][1:] if ext == 'stl': shaft_compound = read_stl_file(self.filename) elif ext == 'iges': iges_reader = IGESControl_Reader() iges_reader.ReadFile(self.filename) iges_reader.TransferRoots() shaft_compound = iges_reader.Shape() else: raise Exception('The shaft file is not in iges/stl formats') sewer = BRepBuilderAPI_Sewing(1e-2) sewer.Add(shaft_compound) sewer.Perform() result_sewed_shaft = sewer.SewedShape() shaft_solid_maker = BRepBuilderAPI_MakeSolid() shaft_solid_maker.Add(OCC.Core.TopoDS.topods_Shell(result_sewed_shaft)) if not shaft_solid_maker.IsDone(): raise RuntimeError('Unsuccessful assembling of solid shaft') shaft_solid = shaft_solid_maker.Solid() return shaft_solid
def __init__(self, faces, tol=None, cut_free_edges=False, non_manifold=False): if tol is None: tol = max([f.tol_max for f in faces]) builder = BRepBuilderAPI_Sewing(tol, True, True, cut_free_edges, non_manifold) for f in faces: builder.Add(f.object) builder.Perform() shape = Shape.wrap(builder.SewedShape()) self._shells = [] self._shell = None self._nshells = 0 if shape.is_compound: self._shells = shape.shells self._nshells = len(self._shells) elif shape.is_face: self._shell = shape.to_shell() self._nshells = 1 elif shape.is_shell: self._shell = shape self._nshells = 1 else: raise RuntimeError('Invalid shape type in ShellBySewing.')
def extrusion_to_solid(extrusion, name): from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing, BRepBuilderAPI_MakeSolid from OCC.Core.TopoDS import topods from OCC.Core.BRepGProp import brepgprop_VolumeProperties from OCC.Core.GProp import GProp_GProps from OCCUtils.Topology import Topo d = extrusion_to_ruled_surfaces(extrusion, cap=True) sew = BRepBuilderAPI_Sewing() make_solid = BRepBuilderAPI_MakeSolid() faces = {} for k in d.keys(): faces[name + '_' + k] = d[k] sew.Add(d[k].Shape()) sew.Perform() shell = sew.SewedShape() make_solid.Add(topods.Shell(shell)) gp = GProp_GProps() #make_solid.Add(shell) solid = make_solid.Solid() brepgprop_VolumeProperties(shell, gp) print gp.Mass() return Part(name, faces, solid)
def reconstruct_parts(array): shells = [] from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing for i, si in enumerate(array): shell = BRepBuilderAPI_Sewing() for face in si.kept: shell.Add(face) shell.Perform() shells.append(shell.SewedShape()) return shells
def get_faceted_L_shape(x, y, z): pnt_A = gp_Pnt(x + 0, y + 0, z + 0) pnt_B = gp_Pnt(x + 20, y + 0, z + 0) pnt_C = gp_Pnt(x + 20, y + 10, z + 0) pnt_D = gp_Pnt(x + 0, y + 10, z + 0) pnt_E = gp_Pnt(x + 0, y + 0, z + 20) pnt_F = gp_Pnt(x + 10, y + 0, z + 20) pnt_G = gp_Pnt(x + 10, y + 10, z + 20) pnt_H = gp_Pnt(x + 0, y + 10, z + 20) pnt_I = gp_Pnt(x + 10, y + 0, z + 10) pnt_J = gp_Pnt(x + 10, y + 10, z + 10) pnt_K = gp_Pnt(x + 20, y + 0, z + 10) pnt_L = gp_Pnt(x + 20, y + 10, z + 10) face_1 = make_face_from_4_points(pnt_A, pnt_B, pnt_C, pnt_D) face_2 = make_face_from_4_points(pnt_B, pnt_C, pnt_L, pnt_K) face_3 = make_face_from_4_points(pnt_E, pnt_F, pnt_G, pnt_H) face_4 = make_face_from_4_points(pnt_A, pnt_E, pnt_H, pnt_D) face_5 = make_face_from_4_points(pnt_G, pnt_F, pnt_I, pnt_J) face_6 = make_face_from_4_points(pnt_I, pnt_K, pnt_L, pnt_J) polygon_1 = BRepBuilderAPI_MakePolygon() polygon_1.Add(pnt_A) polygon_1.Add(pnt_B) polygon_1.Add(pnt_K) polygon_1.Add(pnt_I) polygon_1.Add(pnt_F) polygon_1.Add(pnt_E) polygon_1.Close() face_7 = BRepBuilderAPI_MakeFace(polygon_1.Wire()).Face() polygon_2 = BRepBuilderAPI_MakePolygon() polygon_2.Add(pnt_D) polygon_2.Add(pnt_H) polygon_2.Add(pnt_G) polygon_2.Add(pnt_J) polygon_2.Add(pnt_L) polygon_2.Add(pnt_C) polygon_2.Close() face_8 = BRepBuilderAPI_MakeFace(polygon_2.Wire()).Face() sew = BRepBuilderAPI_Sewing() for face in [ face_1, face_2, face_3, face_4, face_5, face_6, face_7, face_8 ]: sew.Add(face) sew.Perform() return sew.SewedShape()
def sew_shapes(shapes, tolerance=0.001): sew = BRepBuilderAPI_Sewing(tolerance) for shp in shapes: if isinstance(shp, list): for i in shp: sew.Add(i) else: sew.Add(shp) sew.Perform() print("n degenerated shapes", sew.NbDegeneratedShapes()) print("n deleted faces:", sew.NbDeletedFaces()) print("n free edges", sew.NbFreeEdges()) print("n multiple edges:", sew.NbMultipleEdges()) result = ShapeToTopology()(sew.SewedShape()) return result
def named_cartesian_box(xmin=-10., xmax=10., ymin=-10., ymax=10., zmin=-10., zmax=10.): from youbastard.geometry.Polyline3D import Polyline3D from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing, BRepBuilderAPI_MakeSolid from OCC.Core.TopoDS import topods from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox p0 = Point((xmin, ymin, zmin)) p1 = Point((xmin, ymax, zmin)) p2 = Point((xmax, ymax, zmin)) p3 = Point((xmax, ymin, zmin)) p4 = Point((xmin, ymin, zmax)) p5 = Point((xmin, ymax, zmax)) p6 = Point((xmax, ymax, zmax)) p7 = Point((xmax, ymin, zmax)) pol_Xmin = Polyline3D([p0, p1, p5, p4, p0][::-1]) pol_Xmax = Polyline3D([p2, p3, p7, p6, p2][::-1]) pol_Ymin = Polyline3D([p1, p2, p6, p5, p1][::-1]) pol_Ymax = Polyline3D([p0, p4, p7, p3, p0][::-1]) pol_Zmin = Polyline3D([p0, p3, p2, p1, p0][::-1]) pol_Zmax = Polyline3D([p5, p6, p7, p4, p5][::-1]) dicoface = { 'Xmin': face_polyline3d(pol_Xmin).Shape(), 'Xmax': face_polyline3d(pol_Xmax).Shape(), 'Ymin': face_polyline3d(pol_Ymin).Shape(), 'Ymax': face_polyline3d(pol_Ymax).Shape(), 'Zmin': face_polyline3d(pol_Zmin).Shape(), 'Zmax': face_polyline3d(pol_Zmax).Shape() } sew = BRepBuilderAPI_Sewing() make_solid = BRepBuilderAPI_MakeSolid() for k in dicoface.keys(): sew.Add(dicoface[k]) sew.Perform() shell = sew.SewedShape() make_solid.Add(topods.Shell(shell)) box = make_solid.Solid() box = BRepPrimAPI_MakeBox(gp_Pnt(xmin, ymin, zmin), gp_Pnt(xmax, ymax, zmax)) return box, dicoface
def combine_faces(compshape, sew_tolerance): """ Method to combine faces in a shell by adding connectivity and continuity :param compshape: TopoDS_Shape :param sew_tolerance: tolerance for sewing :return: Topo_Shell """ offsew = BRepOffsetAPI_FindContigousEdges(sew_tolerance) sew = BRepBuilderAPI_Sewing(sew_tolerance) face_explorers = TopExp_Explorer(compshape, TopAbs_FACE) n_faces = 0 # cycle on Faces while face_explorers.More(): tface = topods.Face(face_explorers.Current()) sew.Add(tface) offsew.Add(tface) n_faces += 1 face_explorers.Next() offsew.Perform() offsew.Dump() sew.Perform() shell = sew.SewedShape() sew.Dump() shell = topods.Shell(shell) shell_fixer = ShapeFix_Shell() shell_fixer.FixFaceOrientation(shell) """ if shell_fixer.Perform(): print("{} shells fixed! ".format(shell_fixer.NbShells())) else: print "Shells not fixed! " new_shell = shell_fixer.Shell() if brepalgo_IsValid(new_shell): print "Shell valid! " else: print "Shell failed! " """ return new_shell
def reconstruct_object(array): from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing, BRepBuilderAPI_MakeSolid from OCC.Core.BRepGProp import brepgprop_VolumeProperties from OCC.Core.GProp import GProp_GProps from OCC.Core.TopoDS import topods obj = BRepBuilderAPI_Sewing() for i, si in enumerate(array): obj.Add(si) obj.Perform() shell = obj.SewedShape() make_solid = BRepBuilderAPI_MakeSolid() make_solid.Add(topods.Shell(shell)) gp = GProp_GProps() #make_solid.Add(shell) solid = make_solid.Solid() brepgprop_VolumeProperties(shell, gp) print 'RECONSTRUCTED SOLID VOLUME :' print gp.Mass() return solid
def cut_from_parallelepiped(shape, offset=0.001): myBox1 = get_oriented_bounding_box(shape, offset=offset) myBox2 = get_oriented_bounding_box(shape, xWidht=offset) myBox3 = get_oriented_bounding_box(shape, yWidht=offset) myBox4 = get_oriented_bounding_box(shape, zWidht=offset) myBox5 = get_oriented_bounding_box(shape, xxWidht=offset) myBox6 = get_oriented_bounding_box(shape, yyWidht=offset) myBox7 = get_oriented_bounding_box(shape, zzWidht=offset) sew = BRepBuilderAPI_Sewing() for face in TopologyExplorer(shape).faces(): sew.Add(face) sew.Perform() solid = MakeSolidFromShell(sew.SewedShape()) myCut1 = BRepAlgoAPI_Cut(myBox1, solid).Shape() myCut2 = BRepAlgoAPI_Cut(myCut1, myBox2).Shape() myCut3 = BRepAlgoAPI_Cut(myCut2, myBox3).Shape() myCut4 = BRepAlgoAPI_Cut(myCut3, myBox4).Shape() myCut5 = BRepAlgoAPI_Cut(myCut4, myBox5).Shape() myCut6 = BRepAlgoAPI_Cut(myCut5, myBox6).Shape() myCut7 = BRepAlgoAPI_Cut(myCut6, myBox7).Shape() return myCut7
class SewShape(object): """ Sew the shape. :param afem.topology.entities.Shape shape: The context shape to sew. :param float tol: Sewing tolerance. If *None* is provided then the average tolerance of the shape will be used. If no shape is provided, then a default value of 1.0e-7 is used. :param float min_tol: Minimum tolerance. :param float max_tol: Maximum tolerance. :param bool cut_free_edges: Option for cutting of free edges. :param bool non_manifold: Option for non-manifold processing. .. note:: If *shape* is *None* then the user is expected to manually load the shape and perform the operation. """ def __init__(self, shape=None, tol=None, min_tol=None, max_tol=None, cut_free_edges=False, non_manifold=False): if tol is None: if shape is None: tol = 1.0e-7 else: tol = shape.tol_max self._tool = BRepBuilderAPI_Sewing(tol, True, True, cut_free_edges, non_manifold) if min_tol is not None: self._tool.SetMinTolerance(min_tol) if max_tol is not None: self._tool.SetMaxTolerance(max_tol) if shape is not None: self._tool.Load(shape.object) self._tool.Perform() def load(self, shape): """ Load the context shape to sew. :param afem.topology.entities.Shape shape: The shape. :return: None. """ self._tool.Load(shape.object) def add(self, shape): """ Add a shape to be sewed or controlled. :param afem.topology.entities.Shape shape: The shape. :return: None. """ self._tool.Add(shape.object) def perform(self): """ Perform the sewing operation. :return: None. """ self._tool.Perform() @property def sewed_shape(self): """ :return: The sewed shape. May be a null shape if nothing is constructed. :rtype: afem.topology.entities.Shape """ return Shape.wrap(self._tool.SewedShape()) @property def n_free_edges(self): """ :return: Number of free edges. :rtype: int """ return self._tool.NbFreeEdges() @property def free_edges(self): """ :return: Free edges. :rtype: list(afem.topology.entities.Edge) """ edges = [] for i in range(1, self.n_free_edges + 1): e = Edge(self._tool.FreeEdge(i)) edges.append(e) return edges @property def n_multiple_edges(self): """ :return: Number of edges connected to more than two faces. :rtype: int """ return self._tool.NbMultipleEdges() @property def multiple_edges(self): """ :return: Multiple edges. :rtype: list(afem.topology.entities.Edge) """ edges = [] for i in range(1, self.n_free_edges + 1): e = Edge(self._tool.MultipleEdge(i)) edges.append(e) return edges @property def n_manifold_edges(self): """ :return: Number of manifold edges. :rtype: int """ return self._tool.NbContigousEdges() @property def manifold_edges(self): """ :return: Manifold edges. :rtype: list(afem.topology.entities.Edge) """ edges = [] for i in range(1, self.n_free_edges + 1): e = Edge(self._tool.ContigousEdge(i)) edges.append(e) return edges def is_modified(self, shape): """ Check to see if input shape has been modified. :param afem.topology.entities.Shape shape: The shape. :return: *True* if modified, *False* if not. :rtype: bool """ self._tool.IsModified(shape.object) def modified(self, shape): """ Get a modified shape. :param afem.topology.entities.Shape shape: The shape. :return: The modified shape. :rtype: afem.topology.entities.Shape """ return Shape.wrap(self._tool.Modified(shape.object)) def is_modified_subshape(self, subshape): """ Check to see if input sub-shape has been modified. :param afem.topology.entities.Shape subshape: The sub-shape. :return: *True* if modified, *False* if not. :rtype: bool """ self._tool.IsModifiedSubShape(subshape.object) def modified_subshape(self, subshape): """ Get a modified sub-shape. :param afem.topology.entities.Shape subshape: The sub-shape. :return: The modified sub-shape. :rtype: afem.topology.entities.Shape """ return Shape.wrap(self._tool.ModifiedSubShape(subshape.object))
e3 = BRepBuilderAPI_MakeEdge(v2, v1).Edge() e4 = BRepBuilderAPI_MakeEdge(v3, v2).Edge() e5 = BRepBuilderAPI_MakeEdge(v3, v1).Edge() # create wires w0 = BRepBuilderAPI_MakeWire(e5, e3, e4).Wire() w1 = BRepBuilderAPI_MakeWire(e1, e3, e0).Wire() w2 = BRepBuilderAPI_MakeWire(e0, e5, e2).Wire() w3 = BRepBuilderAPI_MakeWire(e2, e4, e1).Wire() # then create faces f0 = BRepBuilderAPI_MakeFace(w0).Face() f1 = BRepBuilderAPI_MakeFace(w1).Face() f2 = BRepBuilderAPI_MakeFace(w2).Face() f3 = BRepBuilderAPI_MakeFace(w3).Face() # sew the faces together to create a shell sew = BRepBuilderAPI_Sewing() sew.Add(f0) sew.Add(f1) sew.Add(f2) sew.Add(f3) sew.Perform() tetrahedron_shell = sew.SewedShape() # display the result display, start_display, add_menu, add_function_to_menu = init_display() display.DisplayShape(tetrahedron_shell, update=True) start_display()
display.SetSelectionModeFace() # switch to Face selection mode display.register_select_callback(recognize_clicked) # first loads the STEP file and display p = os.path.dirname(os.path.abspath(__file__)) shpTmpl = read_step_file( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'step_examples', '21_141_0046_022_0.stp')) shp = read_step_file( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'step_examples', '21_141_0046_022_0_stock.stp')) sew = BRepBuilderAPI_Sewing() for face in TopologyExplorer(shpTmpl).faces(): sew.Add(face) sew.Perform() solid = MakeSolidFromShell(sew.SewedShape()) sew = BRepBuilderAPI_Sewing() for face in TopologyExplorer(shp).faces(): sew.Add(face) sew.Perform() solid1 = MakeSolidFromShell(sew.SewedShape()) myCut1 = BRepAlgoAPI_Cut(solid, solid1).Shape() step_writer = STEPControl_Writer() Interface_Static_SetCVal("write.step.schema", "AP242") # transfer shapes and write file step_writer.Transfer(myCut1, STEPControl_StepModelType(STEPControl_AsIs)) status = step_writer.Write(
def getShape(self, pos, rotation) : import cadquery as cq #from OCC.Core.gp import gp_Ax2, gp_Pnt, gp_Dir from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakePolygon from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeSolid from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut from OCC.Core.TopoDS import topods print("Get Shape gPolyhedra") x = pos[0] y = pos[1] z = pos[2] sewing = BRepBuilderAPI_Sewing() # Add top polygon print('top polygon') top = regPolygon(self.Num, self.Zplanes[0][1], \ self.Zplanes[0][2]) maxR = self.Zplanes[0][1] height = self.Zplanes[0][2] sewing.Add(makeFace(top)) n = len(self.Zplanes) for i in range(1,n) : bot = regPolygon(self.Num, self.Zplanes[i][1], \ self.Zplanes[i][2]) if self.Zplanes[i][1] > maxR : maxR = self.Zplanes[i][1] height = height + self.Zplanes[i][2] for i in range(0, self.Num) : face = makeFace([top[i],top[i+1],bot[i+1],bot[i]]) sewing.Add(face) top = bot # Add bottom polygon print('bottom polygon') sewing.Add(makeFace(bot)) sewing.Perform() sewedShape = sewing.SewedShape() outer = BRepBuilderAPI_MakeSolid(topods.Shell(sewedShape)) sewing = BRepBuilderAPI_Sewing() # Add top polygon print('top polygon') top = regPolygon(self.Num, self.Zplanes[0][0], \ self.Zplanes[0][2]) sewing.Add(makeFace(top)) n = len(self.Zplanes) for i in range(1,n) : bot = regPolygon(self.Num, self.Zplanes[i][0], \ self.Zplanes[i][2]) for i in range(0, self.Num) : face = makeFace([top[i],top[i+1],bot[i+1],bot[i]]) sewing.Add(face) top = bot # Add bottom polygon print('bottom polygon') sewing.Add(makeFace(bot)) sewing.Perform() sewedShape = sewing.SewedShape() inner = BRepBuilderAPI_MakeSolid(topods.Shell(sewedShape)) poly = BRepAlgoAPI_Cut(outer.Shape(), inner.Shape()).Shape() if self.Sector.completeRev() == False : print("Need to section") if self.Sector.less90() == True : print("Common") shape = self.Sector.makeCommon(maxR, height, poly) else : print("Cut") shape = self.Sector.makeCut(maxR, height, poly) if self.Sector.getStart() == 0 : return shape else : return self.Sector.rotate(shape) return poly.Shape()