def get_booleaned_edges(shape): edges = [] exp = TopExp.TopExp_Explorer(shape, TopAbs.TopAbs_EDGE) while exp.More(): edges.append(topods.Edge(exp.Current())) exp.Next() return edges
def get_split_edges(self, edge_face_map, face, zmax, product): exp2 = TopExp.TopExp_Explorer(face, TopAbs.TopAbs_EDGE) while exp2.More(): edge = topods.Edge(exp2.Current()) adjface = TopoDS.TopoDS_Face() getadj = TopOpeBRepBuild.TopOpeBRepBuild_Tools.GetAdjacentFace( face, edge, edge_face_map, adjface) if getadj: try: edge_angle = math.degrees( self.get_angle_between_faces(face, adjface)) except: # TODO: Figure out when a math domain error might occur, # because it does, sometimes. edge_angle = 0 if edge_angle > 30 and edge_angle < 160: newedge = self.build_new_edge(edge, zmax + 0.01) if newedge: self.background_elements.append({ 'raw': product, 'geometry': newedge, 'type': 'line', 'z': zmax + 0.01 }) exp2.Next()
def build_new_face(self, face, zpos, product): exp = TopExp.TopExp_Explorer(face, TopAbs.TopAbs_WIRE) while exp.More(): wireexp = BRepTools.BRepTools_WireExplorer( topods.Wire(exp.Current())) new_wire_builder = BRepBuilderAPI.BRepBuilderAPI_MakeWire() first_vertex = None previous_vertex = None while wireexp.More(): current_vertex = wireexp.CurrentVertex() current_point = BRep.BRep_Tool.Pnt(current_vertex) # Dodgy technique to squash in Z axis current_point.SetZ(zpos) current_vertex = BRepBuilderAPI.BRepBuilderAPI_MakeVertex( current_point).Vertex() if not first_vertex: first_vertex = current_vertex if not previous_vertex: previous_vertex = current_vertex else: try: new_wire_builder.Add( topods.Edge( BRepBuilderAPI.BRepBuilderAPI_MakeEdge( previous_vertex, current_vertex).Edge())) previous_vertex = current_vertex except: pass wireexp.Next() # make last edge if not wireexp.More(): try: new_wire_builder.Add( topods.Edge( BRepBuilderAPI.BRepBuilderAPI_MakeEdge( current_vertex, first_vertex).Edge())) except: pass try: new_wire = new_wire_builder.Wire() new_face = BRepBuilderAPI.BRepBuilderAPI_MakeFace( new_wire).Face() self.background_elements.append({ 'raw': product, 'geometry': new_wire, 'geometry_face': new_face, 'type': 'polygon', 'z': zpos }) except: #print('Could not build face') pass exp.Next()
def draw_line(self, element, position): classes = self.get_classes(element["raw"], position) exp = TopExp.TopExp_Explorer(element["geometry"], TopAbs.TopAbs_VERTEX) points = [] while exp.More(): point = BRep.BRep_Tool.Pnt(topods.Vertex(exp.Current())) points.append((point.X() * self.scale, -point.Y() * self.scale)) exp.Next() self.svg.add( self.svg.line(start=points[0], end=points[1], class_=" ".join(classes)))
def build_new_edge(self, edge, zpos): exp = TopExp.TopExp_Explorer(edge, TopAbs.TopAbs_VERTEX) new_vertices = [] while exp.More(): current_vertex = topods.Vertex(exp.Current()) current_point = BRep.BRep_Tool.Pnt(current_vertex) current_point.SetZ(zpos) new_vertices.append(BRepBuilderAPI.BRepBuilderAPI_MakeVertex(current_point).Vertex()) exp.Next() try: return BRepBuilderAPI.BRepBuilderAPI_MakeEdge(new_vertices[0], new_vertices[1]).Edge() except: return None
def get_background_elements(self): total_product_shapes = len(self.product_shapes) n = 0 intersections = [] compound = TopoDS.TopoDS_Compound() builder = BRep.BRep_Builder() builder.MakeCompound(compound) for product, shape in self.product_shapes: builder.Add(compound, shape) print('{}/{} background elements processed ...'.format( n, total_product_shapes), end='\r', flush=True) #print('Processing product {} '.format(product.Name)) n += 1 intersection = BRepAlgoAPI.BRepAlgoAPI_Common( self.section_box['shape'], shape).Shape() intersection_edges = self.get_booleaned_edges(intersection) if len(intersection_edges) <= 0: continue intersections.append(intersection) transformed_intersection = BRepBuilderAPI.BRepBuilderAPI_Transform( intersection, self.transformation) intersection = transformed_intersection.Shape() edge_face_map = TopTools.TopTools_IndexedDataMapOfShapeListOfShape( ) TopExp.topexp.MapShapesAndAncestors(intersection, TopAbs.TopAbs_EDGE, TopAbs.TopAbs_FACE, edge_face_map) exp = TopExp.TopExp_Explorer(intersection, TopAbs.TopAbs_FACE) while exp.More(): face = topods.Face(exp.Current()) normal = self.get_normal(face) # Cull back-faces if normal.Z() <= 0: exp.Next() continue zpos, zmax = self.calculate_face_zpos(face) self.build_new_face(face, zpos, product) self.get_split_edges(edge_face_map, face, zmax, product) exp.Next()
def getPolygonesFromShape(shape): from Geometry.Geom3D import Pnt3D,Poly3D exp = TopExp.TopExp_Explorer(shape, TopAbs.TopAbs_WIRE) # .TopAbs_FACE) polygons = [] while exp.More(): wire = TopoDS.topods.Wire(TopoDS.topods.Wire(exp.Current())) exp.Next() points = [] from OCCUtils import Topology explorer = Topology.WireExplorer(wire) vertices = explorer.ordered_vertices() for vertex in vertices: pnt = BRep.BRep_Tool_Pnt(vertex) points.append(Pnt3D(pnt.X(),pnt.Y(),pnt.Z())) polygons.append(Poly3D(points)) return polygons