def fix_collinear(cls, wire, tol): """ :return: """ if wire.Reversed() is 1: wire.Reverse() explorer = BRepTools_WireExplorer(wire) prev = None while explorer.More(): curr = explorer.Current() if prev is None: prev = curr explorer.Next() continue v1 = gp.gp_Vec(topexp.FirstVertex(curr), topexp.LastVertex(curr)) v2 = gp.gp_Vec(topexp.FirstVertex(prev), topexp.LastVertex(prev)) if v1.Angle(v2) < tol: # remove common vertex comm = topexp.CommonVertex(curr, prev) # if angle between these is close to 0 or pi, remove the vertex ... return
def process_wire(wire): '''takes a wire and extracts points returns a list of points''' vertices = [] explorer = BRepTools_WireExplorer(wire) while explorer.More(): vertex = TopoDS().Vertex(explorer.CurrentVertex()) xyz = Point(BRep_Tool().Pnt(vertex)) vertices.append(xyz) explorer.Next() return vertices
def iter_edges_from_wire(wire): """ Generator / Iterator over the edges of a wire Compared to iter_edges, it preserves the order of the edges. """ exp = BRepTools_WireExplorer(wire) while exp.More(): yield topods_Edge(exp.Current()) exp.Next()
def copy_reversed(wire, edge_dict=None): if edge_dict is not None: return copy_reversed_params(wire, edge_dict) itr = BRepTools_WireExplorer(wire) verts = [] while itr.More(): current_item = itr.Current() out_fst = topexp.FirstVertex(current_item) # out_lst = topexp.LastVertex(current_item) verts.append(out_fst) itr.Next() verts.reverse() return Wire.create(*verts)
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 = set() 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 current_item_hash not in hashes: hashes.add(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 copy_reversed_params(wire, edge_dict): itr = BRepTools_WireExplorer(wire) verts = [] while itr.More(): current_item = itr.Current() out_fst = topexp.FirstVertex(current_item) out_lst = topexp.LastVertex(current_item) new_edge = Construct.make_edge(Construct.copy_vertex(out_lst), Construct.copy_vertex(out_fst)) verts.append(new_edge) edge_dict[hash(new_edge)] = edge_dict[hash(current_item)] itr.Next() # verts.reverse() return Construct.make_wirex(*verts)
def follow(wire, face=None): if face: itr = BRepTools_WireExplorer(wire, face) else: itr = BRepTools_WireExplorer(wire) verts = [] print('------------') while itr.More(): current_item = itr.Current() out_fst = topexp.FirstVertex(current_item) out_lst = topexp.LastVertex(current_item) v1 = Common.vertex2pnt(out_fst) v2 = Common.vertex2pnt(out_lst) print(gp.repr3(v1), gp.repr3(v2), current_item.Orientation()) # verts.append(out_fst) itr.Next() print('------------') return
def _reinitialize(self): self.wire_explorer = BRepTools_WireExplorer(self.wire) self.done = False
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 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() Proxy(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
def __call__( self, outer_wire: TopoDS_Shape, support_outer: TopoDS_Edge, new_point: gp.gp_Pnt, inner_wire: TopoDS_Wire, support_inner: TopoDS_Vertex, ) -> TopoDS_Shape: """ Im sure theres a builtin command for this, but i cant find it... 'outer_wire' is a wire which contains a hole defined by the reverse of 'inner_wire' Remove the hole by replacing the edge 'support_outer' with a loop that includes inner_wire Produces a single Wire oriented to its interior face. :param outer_wire: :param support_outer: Edge on the outer wire :param new_point: :param inner_wire: :param support_inner: :return: """ new_vert_out2in = Construct.make_vertex(new_point) new_vert_in2out = Construct.make_vertex(new_point) v1 = topexp.FirstVertex(support_outer) v2 = topexp.LastVertex(support_outer) new_edge_a = Construct.make_edge(v1, new_vert_in2out) new_edge_d = Construct.make_edge(new_vert_out2in, v2) self.replaced.append([support_outer, [new_edge_a, new_edge_d]]) pt = Common.vertex2pnt(support_inner) vert_inner = Construct.make_vertex(pt) new_edge_b = Construct.make_edge(new_vert_in2out, support_inner) new_edge_c = Construct.make_edge(vert_inner, new_vert_out2in) self.added.extend([new_edge_b, new_edge_c]) # get the edges which are on inner support edges_before = [] edges_after = [] found = False itr = BRepTools_WireExplorer(inner_wire) while itr.More(): if itr.CurrentVertex() == support_inner: # the edge leaving the selected support found = True if found is True: edges_after.append(itr.Current()) else: edges_before.append(itr.Current()) itr.Next() new_order = edges_after + edges_before last = new_order.pop() # ------------------------------------------------- builder = brep.BRepBuilderAPI_MakeWire() builder.Add(new_edge_a) builder.Add(new_edge_b) for edge in new_order: builder.Add(edge) last_vert = topexp.FirstVertex(last) new_last = Construct.make_edge(last_vert, vert_inner) self.replaced.append([last, [new_last]]) builder.Add(new_last) builder.Add(new_edge_c) builder.Add(new_edge_d) builder.Build() replacement = builder.Wire() reshaper = brep.BRepTools_ReShape() reshaper.Replace(support_outer, replacement) return reshaper.Apply(outer_wire)