def vertices(self) -> List[BRepVertex]: vertices = [] explorer = BRepTools_WireExplorer(self.loop) while explorer.More(): vertex = explorer.CurrentVertex() vertices.append(BRepVertex(vertex)) explorer.Next() return vertices
class WireExplorer(object): ''' Wire traversal ''' def __init__(self, wire): assert isinstance(wire, TopoDS_Wire), 'not a TopoDS_Wire' self.wire = wire self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _reinitialize(self): self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False 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 ordered_edges(self): return self._loop_topo(edges=True) def ordered_vertices(self): return self._loop_topo(edges=False)
def __init__(self, wire, face=None): if face is None: explorer = BRepTools_WireExplorer(wire.object) else: explorer = BRepTools_WireExplorer(wire.object, face.object) edges = [] current_verts = [] while explorer.More(): ei = Edge(explorer.Current()) vi = Vertex(explorer.CurrentVertex()) edges.append(ei) current_verts.append(vi) explorer.Next() # CurrentVertex doesn't get the last vertex. Try to get it. ordered_verts = list(current_verts) if edges: vi = Vertex(explorer.CurrentVertex()) ordered_verts.append(vi) self._edges = edges self._current_verts = current_verts self._ordered_verts = ordered_verts
class WireExplorer: ''' Wire traversal ''' def __init__(self, wire): if not isinstance(wire, TopoDS_Wire): raise AsssertionError('not a TopoDS_Wire') self.wire = wire self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _reinitialize(self): self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _loop_topo(self, edges=True): if self.done: self._reinitialize() topologyType = topods_Edge if edges else topods_Vertex seq = [] occ_seq = [] while self.wire_explorer.More(): # loop edges if edges: current_item = self.wire_explorer.Current() # loop vertices else: current_item = self.wire_explorer.CurrentVertex() occ_seq.append(current_item) self.wire_explorer.Next() # Convert occ_seq to python list for elem in occ_seq: topo_to_add = topologyType(elem) seq.append(topo_to_add) self.done = True return iter(seq) def ordered_edges(self): return self._loop_topo(edges=True) def ordered_vertices(self): return self._loop_topo(edges=False)
class WireExplorer: """ Wire traversal """ def __init__(self, wire: TopoDS_Wire) -> None: if not isinstance(wire, TopoDS_Wire): raise AssertionError("not a TopoDS_Wire") self.wire = wire self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _reinitialize(self) -> None: self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False def _loop_topo(self, edges: Optional[bool] = True) -> Iterator[Any]: if self.done: self._reinitialize() topology_type = topods_Edge if edges else topods_Vertex seq = [] while self.wire_explorer.More(): # loop edges if edges: current_item = self.wire_explorer.Current() # loop vertices else: current_item = self.wire_explorer.CurrentVertex() seq.append(topology_type(current_item)) self.wire_explorer.Next() self.done = True return iter(seq) def ordered_edges(self) -> Iterator[TopoDS_Edge]: return self._loop_topo(edges=True) def ordered_vertices(self) -> Iterator[TopoDS_Vertex]: return self._loop_topo(edges=False)
def getWireStartPointAndTangentDir(aWire): ex = BRepTools_WireExplorer(aWire) edge = ex.Current() vertex = ex.CurrentVertex() v = getVectorTangentToCurveAtPoint(edge, 0) return BRep_Tool.Pnt(vertex), gp_Dir(v)
def cut_out(base): outer = gp_Circ2d(gp_OX2d(), top_radius - 1.75 * roller_diameter) inner = gp_Circ2d(gp_OX2d(), center_radius + 0.75 * roller_diameter) geom_outer = GCE2d_MakeCircle(outer).Value() geom_inner = GCE2d_MakeCircle(inner).Value() geom_inner.Reverse() base_angle = (2. * M_PI) / mounting_hole_count hole_angle = atan(hole_radius / mounting_radius) correction_angle = 3 * hole_angle left = gp_Lin2d(gp_Origin2d(), gp_DX2d()) right = gp_Lin2d(gp_Origin2d(), gp_DX2d()) left.Rotate(gp_Origin2d(), correction_angle) right.Rotate(gp_Origin2d(), base_angle - correction_angle) geom_left = GCE2d_MakeLine(left).Value() geom_right = GCE2d_MakeLine(right).Value() inter_1 = Geom2dAPI_InterCurveCurve(geom_outer, geom_left) inter_2 = Geom2dAPI_InterCurveCurve(geom_outer, geom_right) inter_3 = Geom2dAPI_InterCurveCurve(geom_inner, geom_right) inter_4 = Geom2dAPI_InterCurveCurve(geom_inner, geom_left) if inter_1.Point(1).X() > 0: p1 = inter_1.Point(1) else: p1 = inter_1.Point(2) if inter_2.Point(1).X() > 0: p2 = inter_2.Point(1) else: p2 = inter_2.Point(2) if inter_3.Point(1).X() > 0: p3 = inter_3.Point(1) else: p3 = inter_3.Point(2) if inter_4.Point(1).X() > 0: p4 = inter_4.Point(1) else: p4 = inter_4.Point(2) trimmed_outer = GCE2d_MakeArcOfCircle(outer, p1, p2).Value() trimmed_inner = GCE2d_MakeArcOfCircle(inner, p4, p3).Value() plane = gp_Pln(gp_Origin(), gp_DZ()) arc1 = BRepBuilderAPI_MakeEdge(geomapi_To3d(trimmed_outer, plane)).Edge() lin1 = BRepBuilderAPI_MakeEdge(gp_Pnt(p2.X(), p2.Y(), 0), gp_Pnt(p3.X(), p3.Y(), 0)).Edge() arc2 = BRepBuilderAPI_MakeEdge(geomapi_To3d(trimmed_inner, plane)).Edge() lin2 = BRepBuilderAPI_MakeEdge(gp_Pnt(p4.X(), p4.Y(), 0), gp_Pnt(p1.X(), p1.Y(), 0)).Edge() cutout_wire = BRepBuilderAPI_MakeWire(arc1) cutout_wire.Add(lin1) cutout_wire.Add(arc2) cutout_wire.Add(lin2) # Turn the wire into a face cutout_face = BRepBuilderAPI_MakeFace(cutout_wire.Wire()) filleted_face = BRepFilletAPI_MakeFillet2d(cutout_face.Face()) explorer = BRepTools_WireExplorer(cutout_wire.Wire()) while explorer.More(): vertex = explorer.CurrentVertex() filleted_face.AddFillet(vertex, roller_radius) explorer.Next() cutout = BRepPrimAPI_MakePrism(filleted_face.Shape(), gp_Vec(0.0, 0.0, thickness)).Shape() result = base rotate = gp_Trsf() for i in range(0, mounting_hole_count): rotate.SetRotation(gp_OZ(), i * 2. * M_PI / mounting_hole_count) rotated_cutout = BRepBuilderAPI_Transform(cutout, rotate, True) result = BRepAlgoAPI_Cut(result, rotated_cutout.Shape()).Shape() return result