def fillet(event=None): Box = BRepPrimAPI_MakeBox(gp_Pnt(-400, 0, 0), 200, 230, 180).Shape() fillet_ = BRepFilletAPI_MakeFillet(Box) # Add fillet on each edge for e in TopologyExplorer(Box).edges(): fillet_.Add(20, e) blendedBox = fillet_.Shape() P1 = gp_Pnt(250, 150, 75) S1 = BRepPrimAPI_MakeBox(300, 200, 200).Shape() S2 = BRepPrimAPI_MakeBox(P1, 120, 180, 70).Shape() Fuse = BRepAlgoAPI_Fuse(S1, S2) FusedShape = Fuse.Shape() fill = BRepFilletAPI_MakeFillet(FusedShape) for e in TopologyExplorer(FusedShape).edges(): fill.Add(e) for i in range(1, fill.NbContours() + 1): length = fill.Length(i) Rad = 0.15 * length fill.SetRadius(Rad, i, 1) blendedFusedSolids = fill.Shape() display.EraseAll() display.DisplayShape(blendedBox) display.DisplayShape(blendedFusedSolids) display.FitAll()
def hash_edge_lenght_to_face(faces): """ for every edge in the list `faces` loop through the edges of the face associate (hash) the edge lenght to point to the face :note: this approach would blow less if you use a tuple ( length, edge-mid-point ) the TopoDS_Edge entitiy has a HashCode method that might be actually a proper idea :param faces: :return: dict hashing all edge lengths """ _edge_length_to_face = {} _edge_length_to_edge = {} for f in faces: tp = TopologyExplorer(f) for e in tp.edges(): length = round(length_from_edge(e), 3) _edge_length_to_face[length] = f _edge_length_to_edge[length] = e return _edge_length_to_face, _edge_length_to_edge
def recognize_batch(event=None): """ Menu item : process all the faces of a single shape """ # then traverse the topology using the Topo class t = TopologyExplorer(shp) # loop over faces only for f in t.faces(): # call the recognition function recognize_face(f)
def import_as_multiple_shapes(event=None): compound = read_step_file(os.path.join('.', 'models', 'as1_pe_203.stp')) t = TopologyExplorer(compound) display.EraseAll() for solid in t.solids(): color = Quantity_Color(random.random(), random.random(), random.random(), Quantity_TOC_RGB) display.DisplayColoredShape(solid, color) display.FitAll()
def shape_faces_surface(): """ Compute the surface of each face of a shape """ # first create the shape the_shape = BRepPrimAPI_MakeBox(50., 30., 10.).Shape() # then loop over faces t = TopologyExplorer(the_shape) props = GProp_GProps() shp_idx = 1 for face in t.faces(): brepgprop_SurfaceProperties(face, props) face_surf = props.Mass() print("Surface for face nbr %i : %f" % (shp_idx, face_surf)) shp_idx += 1
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 brep_feat_local_revolution(event=None): S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = list(TopologyExplorer(S).faces()) F1 = faces[2] surf = BRep_Tool_Surface(F1) D = gp_OX() MW1 = BRepBuilderAPI_MakeWire() p1 = gp_Pnt2d(100., 100.) p2 = gp_Pnt2d(200., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW1.Add(BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)).Edge()) p1 = gp_Pnt2d(200., 100.) p2 = gp_Pnt2d(150., 200.) aline = GCE2d_MakeLine(p1, p2).Value() MW1.Add(BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)).Edge()) p1 = gp_Pnt2d(150., 200.) p2 = gp_Pnt2d(100., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW1.Add(BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)).Edge()) MKF1 = BRepBuilderAPI_MakeFace() MKF1.Init(surf, False, 1e-6) MKF1.Add(MW1.Wire()) FP = MKF1.Face() breplib_BuildCurves3d(FP) MKrev = BRepFeat_MakeRevol(S, FP, F1, D, 1, True) F2 = faces[4] MKrev.Perform(F2) display.EraseAll() display.DisplayShape(MKrev.Shape()) display.FitAll()
def read_step_file(filename, return_as_shapes=False, verbosity=False): """ read the STEP file and returns a compound filename: the file path return_as_shapes: optional, False by default. If True returns a list of shapes, else returns a single compound verbosity: optionl, False by default. """ assert os.path.isfile(filename) step_reader = STEPControl_Reader() status = step_reader.ReadFile(filename) if status == IFSelect_RetDone: # check status if verbosity: failsonly = False step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity) step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) _nbs = step_reader.NbShapes() shape_to_return = step_reader.Shape(1) # a compound assert not shape_to_return.IsNull() else: raise AssertionError("Error: can't read file.") if return_as_shapes: shape_to_return = TopologyExplorer(shape_to_return).solids() return shape_to_return
def extrusion(event=None): # Make a box Box = BRepPrimAPI_MakeBox(400., 250., 300.) S = Box.Shape() # Choose the first Face of the box F = next(TopologyExplorer(S).faces()) surf = BRep_Tool_Surface(F) # Make a plane from this face Pl = Handle_Geom_Plane_DownCast(surf) Pln = Pl.GetObject() # Get the normal of this plane. This will be the direction of extrusion. D = Pln.Axis().Direction() # Inverse normal D.Reverse() # Create the 2D planar sketch MW = BRepBuilderAPI_MakeWire() p1 = gp_Pnt2d(200., -100.) p2 = gp_Pnt2d(100., -100.) aline = GCE2d_MakeLine(p1, p2).Value() Edge1 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge1.Edge()) p1 = p2 p2 = gp_Pnt2d(100., -200.) aline = GCE2d_MakeLine(p1, p2).Value() Edge2 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge2.Edge()) p1 = p2 p2 = gp_Pnt2d(200., -200.) aline = GCE2d_MakeLine(p1, p2).Value() Edge3 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge3.Edge()) p1 = p2 p2 = gp_Pnt2d(200., -100.) aline = GCE2d_MakeLine(p1, p2).Value() Edge4 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge4.Edge()) # Build Face from Wire. NB: a face is required to generate a solid. MKF = BRepBuilderAPI_MakeFace() MKF.Init(surf, False, 1e-6) MKF.Add(MW.Wire()) FP = MKF.Face() breplib_BuildCurves3d(FP) MKP = BRepFeat_MakePrism(S, FP, F, D, False, True) MKP.Perform(200.) # TODO MKP completes, seeing a split operation but no extrusion assert MKP.IsDone() res1 = MKP.Shape() display.EraseAll() display.DisplayColoredShape(res1, 'BLUE') display.FitAll()
def create_offsets(face, nr_of_counters, distance_between_contours): offset = BRepOffsetAPI_MakeOffset() offset.Init(GeomAbs_Arc) for wi in TopologyExplorer(face).wires(): offset.AddWire(wi) for i in range(nr_of_counters): offset.Perform(-distance_between_contours * i) if offset.IsDone(): yield offset.Shape()
def build_curve_network(event=None): ''' mimic the curve network surfacing command from rhino ''' print('Importing IGES file...') pth = os.path.dirname(os.path.abspath(__file__)) pth = os.path.abspath( os.path.join(pth, 'models', 'curve_geom_plate.igs')) iges = read_iges_file(pth) print(iges) print('Building geomplate...') topo = TopologyExplorer(iges) edges_list = list(topo.edges()) face = build_geom_plate(edges_list) print('done.') display.EraseAll() display.DisplayShape(edges_list) display.DisplayShape(face) display.FitAll() print('Cutting out of edges...')
def geom_plate(event=None): display.EraseAll() p1 = gp_Pnt(0, 0, 0) p2 = gp_Pnt(0, 10, 0) p3 = gp_Pnt(0, 10, 10) p4 = gp_Pnt(0, 0, 10) p5 = gp_Pnt(5, 5, 5) poly = make_closed_polygon([p1, p2, p3, p4]) edges = [i for i in TopologyExplorer(poly).edges()] face = make_n_sided(edges, [p5]) display.DisplayShape(edges) display.DisplayShape(make_vertex(p5)) display.DisplayShape(face, update=True)
def split_shape(event=None): S = BRepPrimAPI_MakeBox(gp_Pnt(-100, -60, -80), 150, 200, 170).Shape() asect = BRepAlgoAPI_Section(S, gp_Pln(1, 2, 1, -15), False) asect.ComputePCurveOn1(True) asect.Approximation(True) asect.Build() R = asect.Shape() asplit = BRepFeat_SplitShape(S) for edg in TopologyExplorer(R).edges(): face = TopoDS_Face() if asect.HasAncestorFaceOn1(edg, face): asplit.Add(edg, face) asplit.Build() display.EraseAll() display.DisplayShape(asplit.Shape()) display.FitAll()
def DisplayShape(self, shp, # the TopoDS_Shape to be displayed shape_color=default_shape_color, # the default render_edges=False, edge_color=default_edge_color, compute_uv_coords=False, quality=1.0, transparency=False, opacity=1., topo_level='default', update=False): """ Displays a topods_shape in the renderer instance. shp: the TopoDS_Shape to render shape_color: the shape color, in html corm, eg '#abe000' render_edges: optional, False by default. If True, compute and dislay all edges as a linear interpolation of segments. edge_color: optional, black by default. The color used for edge rendering, in html form eg '#ff00ee' compute_uv_coords: optional, false by default. If True, compute texture coordinates (required if the shape has to be textured) quality: optional, 1.0 by default. If set to something lower than 1.0, mesh will be more precise. If set to something higher than 1.0, mesh will be less precise, i.e. lower numer of triangles. transparency: optional, False by default (opaque). opacity: optioanl, float, by default to 1 (opaque). if transparency is set to True, 0. is fully opque, 1. is fully transparent. detail_level: "default" by default. The value should be either "compound", "shape", "vertex". update: optional, False by default. If True, render all the shapes. """ if topo_level != "default": t = TopologyExplorer(shp) map_type_and_methods = {"Solid": t.solids, "Face": t.faces, "Shell": t.shells, "Compound": t.compounds, "Compsolid": t.comp_solids} for subshape in map_type_and_methods[topo_level](): self.AddShapeToScene(subshape, shape_color, render_edges, edge_color, compute_uv_coords, quality, transparency, opacity) else: self.AddShapeToScene(shp, shape_color, render_edges, edge_color, compute_uv_coords, quality, transparency, opacity) if update: self.Display()
def brepfeat_prism(event=None): box = BRepPrimAPI_MakeBox(400, 250, 300).Shape() faces = TopologyExplorer(box).faces() for i in range(5): face = next(faces) srf = BRep_Tool_Surface(face) c = gp_Circ2d(gp_Ax2d(gp_Pnt2d(200, 130), gp_Dir2d(1, 0)), 75) circle = Geom2d_Circle(c).GetHandle() wire = BRepBuilderAPI_MakeWire() wire.Add(BRepBuilderAPI_MakeEdge(circle, srf, 0., pi).Edge()) wire.Add(BRepBuilderAPI_MakeEdge(circle, srf, pi, 2. * pi).Edge()) wire.Build() display.DisplayShape(wire.Wire()) mkf = BRepBuilderAPI_MakeFace() mkf.Init(srf, False, 1e-6) mkf.Add(wire.Wire()) mkf.Build() new_face = mkf.Face() breplib_BuildCurves3d(new_face) display.DisplayShape(new_face) prism = BRepFeat_MakeDPrism(box, mkf.Face(), face, 100, True, True) prism.Perform(400) assert prism.IsDone() display.EraseAll() display.DisplayShape(prism.Shape()) display.DisplayColoredShape(wire.Wire(), 'RED') display.FitAll()
def variable_filleting(event=None): display.EraseAll() # Create Box Box = BRepPrimAPI_MakeBox(200, 200, 200).Shape() # Fillet Rake = BRepFilletAPI_MakeFillet(Box) ex = TopologyExplorer(Box).edges() next(ex) next(ex) next(ex) Rake.Add(8, 50, next(ex)) Rake.Build() if Rake.IsDone(): evolvedBox = Rake.Shape() display.DisplayShape(evolvedBox) else: print("Rake not done.") # Create Cylinder Cylinder = BRepPrimAPI_MakeCylinder( gp_Ax2(gp_Pnt(-300, 0, 0), gp_Dir(0, 0, 1)), 100, 200).Shape() fillet_ = BRepFilletAPI_MakeFillet(Cylinder) TabPoint2 = TColgp_Array1OfPnt2d(0, 20) for i in range(0, 20): Point2d = gp_Pnt2d(i * 2 * pi / 19, 60 * cos(i * pi / 19 - pi / 2) + 10) TabPoint2.SetValue(i, Point2d) exp2 = TopologyExplorer(Cylinder).edges() fillet_.Add(TabPoint2, next(exp2)) fillet_.Build() if fillet_.IsDone(): LawEvolvedCylinder = fillet_.Shape() display.DisplayShape(LawEvolvedCylinder) else: print("fillet not done.") ## TODO : fillet not done P = gp_Pnt(350, 0, 0) Box2 = BRepPrimAPI_MakeBox(P, 200, 200, 200).Shape() afillet = BRepFilletAPI_MakeFillet(Box2) TabPoint = TColgp_Array1OfPnt2d(1, 6) P1 = gp_Pnt2d(0., 8.) P2 = gp_Pnt2d(0.2, 16.) P3 = gp_Pnt2d(0.4, 25.) P4 = gp_Pnt2d(0.6, 55.) P5 = gp_Pnt2d(0.8, 28.) P6 = gp_Pnt2d(1., 20.) TabPoint.SetValue(1, P1) TabPoint.SetValue(2, P2) TabPoint.SetValue(3, P3) TabPoint.SetValue(4, P4) TabPoint.SetValue(5, P5) TabPoint.SetValue(6, P6) exp = TopologyExplorer(Box2).edges() next(exp) next(exp) next(exp) afillet.Add(TabPoint, next(exp)) afillet.Build() if afillet.IsDone(): LawEvolvedBox = afillet.Shape() else: print("aFillet not done.") display.DisplayShape(LawEvolvedBox) display.FitAll()
def brep_feat_extrusion_protrusion(event=None): # Extrusion S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = TopologyExplorer(S).faces() F = next(faces) surf1 = BRep_Tool_Surface(F) Pl1 = Handle_Geom_Plane_DownCast(surf1).GetObject() D1 = Pl1.Pln().Axis().Direction().Reversed() MW = BRepBuilderAPI_MakeWire() p1, p2 = gp_Pnt2d(200., -100.), gp_Pnt2d(100., -100.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(100., -100.), gp_Pnt2d(100., -200.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(100., -200.), gp_Pnt2d(200., -200.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(200., -200.), gp_Pnt2d(200., -100.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) MKF = BRepBuilderAPI_MakeFace() MKF.Init(surf1, False, 1e-6) MKF.Add(MW.Wire()) FP = MKF.Face() breplib_BuildCurves3d(FP) display.EraseAll() MKP = BRepFeat_MakePrism(S, FP, F, D1, 0, True) MKP.PerformThruAll() res1 = MKP.Shape() display.DisplayShape(res1) # Protrusion next(faces) F2 = next(faces) surf2 = BRep_Tool_Surface(F2) Pl2 = Handle_Geom_Plane_DownCast(surf2).GetObject() D2 = Pl2.Pln().Axis().Direction().Reversed() MW2 = BRepBuilderAPI_MakeWire() p1, p2 = gp_Pnt2d(100., 100.), gp_Pnt2d(200., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(200., 100.), gp_Pnt2d(150., 200.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(150., 200.), gp_Pnt2d(100., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) MKF2 = BRepBuilderAPI_MakeFace() MKF2.Init(surf2, False, 1e-6) MKF2.Add(MW2.Wire()) MKF2.Build() FP = MKF2.Face() breplib_BuildCurves3d(FP) MKP2 = BRepFeat_MakePrism(res1, FP, F2, D2, 0, True) MKP2.PerformThruAll() display.EraseAll() trf = gp_Trsf() trf.SetTranslation(gp_Vec(0, 0, 300)) gtrf = gp_GTrsf() gtrf.SetTrsf(trf) tr = BRepBuilderAPI_GTransform(MKP2.Shape(), gtrf, True) fused = BRepAlgoAPI_Fuse(tr.Shape(), MKP2.Shape()) fused.RefineEdges() fused.Build() print('Boolean operation error status:', fused.ErrorStatus()) display.DisplayShape(fused.Shape()) display.FitAll()
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. ## # You should have received a copy of the GNU Lesser General Public License # along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. from __future__ import print_function from random import random from OCC.AIS import AIS_ColoredShape from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Display.OCCViewer import rgb_color from OCC.Display.SimpleGui import init_display from OCC.TopologyUtils import TopologyExplorer display, start_display, add_menu, add_function_to_menu = init_display() my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape() ais = AIS_ColoredShape(my_box) for fc in TopologyExplorer(my_box).faces(): # set a custom color per-face ais.SetCustomColor(fc, rgb_color(random(), random(), random())) display.Context.Display(ais.GetHandle()) display.FitAll() start_display()
print("Test 1 Results:") print(" * single thread runtime: %.2fs" % delta_single) print(" * multi thread runtime: %.2fs" % delta_multi) print(" * muti/single=%.2f%%" % (delta_multi / delta_single * 100)) # TEST 2 : other step, with a loop over each subshape print("TEST 2 ===") shp3 = read_step_file( os.path.join('..', 'examples', 'models', 'RC_Buggy_2_front_suspension.stp')) shp4 = read_step_file( os.path.join('..', 'examples', 'models', 'RC_Buggy_2_front_suspension.stp')) topo1 = TopologyExplorer(shp3) t4 = time.monotonic() for solid in topo1.solids(): o = Tesselator(solid) o.Compute(parallel=False, mesh_quality=0.5) t5 = time.monotonic() delta_single = t5 - t4 topo2 = TopologyExplorer(shp4) t6 = time.monotonic() for solid in topo2.solids(): o = Tesselator(solid) o.Compute(parallel=True, mesh_quality=0.5) t7 = time.monotonic() delta_multi = t7 - t6
def build_curve_network(event=None, enforce_tangency=True): ''' mimic the curve network surfacing command from rhino ''' root_compound_shape = read_step_file("./models/splinecage.stp") topology_explorer = TopologyExplorer(root_compound_shape) # approximate the hell out of all surfaces and curves # I wanna see it in its full glory display.Context.SetDeviationAngle(0.00001) # 0.001 -> default display.Context.SetDeviationCoefficient(0.0001) # 0.001 -> default tangent_constraint_faces = [f for f in topology_explorer.faces()] # loop through the imported faces # associate the length of each of the faces edges to the corresponding face _edge_length_to_face, _edge_length_to_edge = hash_edge_lenght_to_face( tangent_constraint_faces) # loop through the imported curves, avoiding the imported faces # when we've got these filtered out, we retrieved the geometry to build the surface from filtered_edges = [ e for e in topology_explorer._loop_topo( TopAbs_EDGE, root_compound_shape, TopAbs_FACE) ] filtered_length = {} for e in filtered_edges: l = round(length_from_edge(e), 3) filtered_length[l] = e input_edge_face_pairs, edges_no_adjacent_face = [], [] for l, edg in filtered_length.items(): if l in _edge_length_to_edge: edge_face_pair = (_edge_length_to_edge[l], _edge_length_to_face[l]) input_edge_face_pairs.append(edge_face_pair) else: edges_no_adjacent_face.append(edg) brep_plate_builder = BRepOffsetAPI_MakeFilling() if enforce_tangency: print("going for surface quality") brep_plate_builder.SetConstrParam( 0.0001, 0.001, 0.01, 0.01) # ?!!! Tol2d=1.0, Tol3d=1.0, TolAng=1.0, TolCurv=1.0 brep_plate_builder.SetApproxParam(8, 240) # MaxDeg=8, MaxSegments=9 brep_plate_builder.SetResolParam( 3, 64, 3) # Degree=3, NbPtsOnCur=15, NbIter=2, Anisotropie=0 else: print("quick and dirty") # illegal instruction 4??? for i in input_edge_face_pairs: display.DisplayShape(i, color=random_color()) constraint_edg, support_face = i if constraint_edg.IsNull() or support_face.IsNull(): print("OMG null") brep_plate_builder.Add(constraint_edg, support_face, GeomAbs_G1) # not entirely sure why this fails... how is that different from adding from points? # for e in edges_no_adjacent_face: # brep_plate_builder.Add(e, GeomAbs_C0) # libc++abi.dylib: terminating with uncaught exception of type Standard_OutOfRange for e in edges_no_adjacent_face: display.DisplayShape(e) for pt in divide_edge_by_nr_of_points(e, 12)[2:-2]: brep_plate_builder.Add(pt[1]) brep_plate_builder.Build() if brep_plate_builder.IsDone(): face = brep_plate_builder.Shape() display.DisplayColoredShape(face, "ORANGE") else: print("constructing the surface failed")