def pipe_fillet(radius): # the points p1 = gp_Pnt(0, 0, 0) p2 = gp_Pnt(0, 1, 0) p3 = gp_Pnt(1, 2, 0) p4 = gp_Pnt(2, 2, 0) # the edges ed1 = BRepBuilderAPI_MakeEdge(p1, p2).Edge() ed2 = BRepBuilderAPI_MakeEdge(p2, p3).Edge() ed3 = BRepBuilderAPI_MakeEdge(p3, p4).Edge() # inbetween fillet12 = filletEdges(ed1, ed2) fillet23 = filletEdges(ed2, ed3) # the wire makeWire = BRepBuilderAPI_MakeWire() makeWire.Add(ed1) makeWire.Add(fillet12) makeWire.Add(ed2) makeWire.Add(fillet23) makeWire.Add(ed3) makeWire.Build() wire = makeWire.Wire() # the pipe dir = gp_Dir(0, 1, 0) circle = gp_Circ(gp_Ax2(p1, dir), radius) profile_edge = BRepBuilderAPI_MakeEdge(circle).Edge() profile_wire = BRepBuilderAPI_MakeWire(profile_edge).Wire() profile_face = BRepBuilderAPI_MakeFace(profile_wire).Face() pipe = BRepOffsetAPI_MakePipe(wire, profile_face).Shape() #display.DisplayShape(pipe, update=True) return (pipe)
def brep_feat_rib(event=None): mkw = BRepBuilderAPI_MakeWire() mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(0., 0., 0.), gp_Pnt(200., 0., 0.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(200., 0., 0.), gp_Pnt(200., 0., 50.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(200., 0., 50.), gp_Pnt(50., 0., 50.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(50., 0., 50.), gp_Pnt(50., 0., 200.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(50., 0., 200.), gp_Pnt(0., 0., 200.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(0., 0., 200.), gp_Pnt(0., 0., 0.)).Edge()) S = BRepPrimAPI_MakePrism(BRepBuilderAPI_MakeFace(mkw.Wire()).Face(), gp_Vec(gp_Pnt(0., 0., 0.), gp_Pnt(0., 100., 0.))) display.EraseAll() # display.DisplayShape(S.Shape()) W = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(gp_Pnt(50., 45., 100.), gp_Pnt(100., 45., 50.)).Edge()) aplane = Geom_Plane(0., 1., 0., -45.) aform = BRepFeat_MakeLinearForm(S.Shape(), W.Wire(), aplane.GetHandle(), gp_Vec(0., 10., 0.), gp_Vec(0., 0., 0.), 1, True) aform.Perform() display.DisplayShape(aform.Shape()) display.FitAll()
def brep_feat_local_revolution(event=None): S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = list(Topo(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 extrusion(event=None): # Make a box Box = BRepPrimAPI_MakeBox(400., 250., 300.) S = Box.Shape() # Choose the first Face of the box F = next(Topo(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.DisplayColoredShape(FP, 'YELLOW') display.FitAll()
def stitch(self, other): """Attempt to stich wires""" wire_builder = BRepBuilderAPI_MakeWire() wire_builder.Add(topods_Wire(self.wrapped)) wire_builder.Add(topods_Wire(other.wrapped)) wire_builder.Build() return self.__class__(wire_builder.Wire())
def create_shape(self): data = self.element.attrib.get('d') shapes = [] path = None for cmd, params in self.parse_path(data): if cmd == 'M': if path is not None: shapes.append(path.Wire()) path = BRepBuilderAPI_MakeWire() last_pnt = gp_Pnt(params[0], params[1], 0) start_pnt = last_pnt elif cmd in ['L', 'H', 'V']: pnt = gp_Pnt(params[0], params[1], 0) path.Add(BRepBuilderAPI_MakeEdge(last_pnt, pnt).Edge()) last_pnt = pnt elif cmd == 'Q': # Quadratic Bezier pts = TColgp_Array1OfPnt(1, 3) pts.SetValue(1, last_pnt) pts.SetValue(2, gp_Pnt(params[0], params[1], 0)) last_pnt = gp_Pnt(params[2], params[3], 0) pts.SetValue(3, last_pnt) curve = Geom_BezierCurve(pts) path.Add(BRepBuilderAPI_MakeEdge(curve.GetHandle()).Edge()) elif cmd == 'C': # Cubic Bezier pts = TColgp_Array1OfPnt(1, 4) pts.SetValue(1, last_pnt) pts.SetValue(2, gp_Pnt(params[0], params[1], 0)) pts.SetValue(3, gp_Pnt(params[2], params[3], 0)) last_pnt = gp_Pnt(params[4], params[5], 0) pts.SetValue(4, last_pnt) curve = Geom_BezierCurve(pts) path.Add(BRepBuilderAPI_MakeEdge(curve.GetHandle()).Edge()) elif cmd == 'A': # Warning: Play at your own risk! x1, y1 = last_pnt.X(), last_pnt.Y() rx, ry, phi, large_arc_flag, sweep_flag, x2, y2 = params phi = radians(phi) pnt = gp_Pnt(x2, y2, 0) cx, cy, rx, ry = compute_arc_center( x1, y1, rx, ry, phi, large_arc_flag, sweep_flag, x2, y2) z_dir = Z_DIR if sweep_flag else NEG_Z_DIR # sweep_flag c = make_ellipse((cx, cy, 0), rx, ry, phi, z_dir) curve = GC_MakeArcOfEllipse(c, last_pnt, pnt, True).Value() path.Add(BRepBuilderAPI_MakeEdge(curve).Edge()) last_pnt = pnt elif cmd == 'Z': path.Add(BRepBuilderAPI_MakeEdge(last_pnt, start_pnt).Edge()) shapes.append(path.Wire()) path = None # Close path last_pnt = start_pnt if path is not None: shapes.append(path.Wire()) return shapes
def update_shape(self, change): d = self.declaration shape = BRepBuilderAPI_MakeWire() for c in self.children(): convert = self.shape_to_wire if isinstance(c.shape, (list, tuple)): #: Assume it's a list of drawn objects... for item in c.shape: shape.Add(convert(item)) else: shape.Add(convert(c.shape)) assert shape.IsDone(), 'Edges must be connected' self.shape = shape
def Solid(self): mw = BRepBuilderAPI_MakeWire() points = [] x = -self.length / 2.0 y = -self.width / 2.0 z = 0.0 points.append(gp_Pnt(x, y, z)) x = self.length / 2.0 points.append(gp_Pnt(x, y, z)) me = BRepBuilderAPI_MakeEdge(points[0], points[1]) mw.Add(me.Edge()) # bottom edge ax = gp_Ax2(gp_Pnt(x, 0, 0), gp_Dir(0, 0, 1), gp_Dir(0, -1, 0)) circ = gp_Circ(ax, self.width / 2.0) me = BRepBuilderAPI_MakeEdge(circ, 0, pi) mw.Add(me.Edge()) points = [] y = self.width / 2.0 points.append(gp_Pnt(x, y, z)) x = -self.length / 2.0 points.append(gp_Pnt(x, y, z)) me = BRepBuilderAPI_MakeEdge(points[0], points[1]) mw.Add(me.Edge()) # top edge ax = gp_Ax2(gp_Pnt(x, 0, 0), gp_Dir(0, 0, 1), gp_Dir(0, 1, 0)) circ = gp_Circ(ax, self.width / 2.0) me = BRepBuilderAPI_MakeEdge(circ, 0, pi) mw.Add(me.Edge()) mf = BRepBuilderAPI_MakeFace(mw.Wire()) mp = BRepPrimAPI_MakePrism(mf.Face(), gp_Vec(0, 0, self.thickness)) shape = mp.Shape() #v_trans = gp_Vec(self.ax2.Location().XYZ()) ax = gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1), gp_Dir(1, 0, 0)) #mainRotationAngle = ax.Angle(self.ax2) trsf = gp_Trsf() trsf.SetTransformation(gp_Ax3(self.ax2), gp_Ax3(ax)) mt = BRepBuilderAPI_Transform(shape, trsf) return mt.Shape()
def makePieSlice(r, theta0, theta1, z_min, z_max): p0 = gp_Pnt(0, 0, z_min) p1 = gp_Pnt(r * cos(theta0), r * sin(theta0), z_min) p2 = gp_Pnt(r * cos(theta1), r * sin(theta1), z_min) edges = [] los = TopTools_ListOfShape() me = BRepBuilderAPI_MakeEdge(p0, p1) edges.append(me.Edge()) los.Append(me.Edge()) ax = gp_Ax2(gp_Pnt(0, 0, z_min), gp_Dir(0, 0, 1), gp_Dir(1, 0, 0)) circ = gp_Circ(ax, r) me = BRepBuilderAPI_MakeEdge(circ, theta0, theta1) edges.append(me.Edge()) los.Append(me.Edge()) me = BRepBuilderAPI_MakeEdge(p2, p0) edges.append(me.Edge()) los.Append(me.Edge()) """ mw = BRepBuilderAPI_MakeWire() for i in edges: mw.Add(i) """ mw = BRepBuilderAPI_MakeWire() mw.Add(los) pln = gp_Pln(gp_Pnt(0, 0, z_min), gp_Dir(0, 0, 1)) mf = BRepBuilderAPI_MakeFace(pln, mw.Wire()) face = mf.Face() mp = BRepPrimAPI_MakePrism(face, gp_Vec(0, 0, z_max - z_min)) return mp.Shape()
def make_wire(*args): # if we get an iterable, than add all edges to wire builder if isinstance(args[0], list) or isinstance(args[0], tuple): wire = BRepBuilderAPI_MakeWire() for i in args[0]: wire.Add(i) wire.Build() return wire.Wire() wire = BRepBuilderAPI_MakeWire(*args) return wire.Wire()
def create(cls, *args, close=True): builder = BRepBuilderAPI_MakeWire() if isinstance(args, (list, tuple)): if isinstance(args[0], (gp.gp_Pnt, TopoDS_Vertex)): s = 0 if close is True else 1 for i in range(s, len(args)): edge = make_edge(args[i - 1], args[i]) builder.Add(edge) elif isinstance(args[0], TopoDS_Edge): for e in args: builder.Add(e) # while not builder.IsDone(): with assert_isdone(builder, 'f'): builder.Build() shp = builder.Wire() builder.Delete() return shp
def update_shape(self, change): d = self.declaration shape = BRepBuilderAPI_MakeWire() for c in self.children(): if hasattr(c.shape, 'Wire'): #: No conversion needed shape.Add(c.shape.Wire()) elif hasattr(c.shape, 'Edge'): #: No conversion needed shape.Add(c.shape.Edge()) elif isinstance(c.shape, (list, tuple)): #: Assume it's a list of drawn objects... for e in c.shape: shape.Add(e.Edge()) else: #: Attempt to convert the shape into a wire shape.Add(topods.Wire(c.shape.Shape())) assert shape.IsDone(), 'Edges must be connected' self.shape = shape
def make_wirex(*args): """ list of Edge """ # if we get an iterable, than add all edges to wire builder wire = BRepBuilderAPI_MakeWire() for a in args: wire.Add(a) wire.Build() with assert_isdone(wire, 'failed to produce wire'): result = wire.Wire() return result
def assembleEdges(cls, listOfEdges): """ Attempts to build a wire that consists of the edges in the provided list :param cls: :param listOfEdges: a list of Edge objects :return: a wire with the edges assembled """ wire_builder = BRepBuilderAPI_MakeWire() for edge in listOfEdges: wire_builder.Add(edge.wrapped) return cls(wire_builder.Wire())
def brepfeat_prism(event=None): box = BRepPrimAPI_MakeBox(400, 250, 300).Shape() faces = Topo(box).faces() for i in range(3): 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.DisplayColoredShape(box, 'GREEN') 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 combine(cls, listOfWires): """ Attempt to combine a list of wires into a new wire. the wires are returned in a list. :param cls: :param listOfWires: :return: """ wire_builder = BRepBuilderAPI_MakeWire() for wire in listOfWires: wire_builder.Add(wire.wrapped) return cls(wire_builder.Wire())
def make_wire(*args): # if we get an iterable, than add all edges to wire builder if isinstance(args[0], list) or isinstance(args[0], tuple): wire = BRepBuilderAPI_MakeWire() for i in args[0]: wire.Add(i) wire.Build() return wire.Wire() wire = BRepBuilderAPI_MakeWire(*args) wire.Build() with assert_isdone(wire, 'failed to produce wire'): result = wire.Wire() return result
def makeRect(self,r,h) : from OCC.Core.gp import gp_Pnt from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeWire from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeFace print("Make Rect") wire = BRepBuilderAPI_MakeWire() print("Make Points") p1 = gp_Pnt(0,0,0) p2 = gp_Pnt(r,0,0) p3 = gp_Pnt(r,0,h) p4 = gp_Pnt(0,0,h) print("make Edges") wire.Add(BRepBuilderAPI_MakeEdge(p1,p2).Edge()) wire.Add(BRepBuilderAPI_MakeEdge(p2,p3).Edge()) wire.Add(BRepBuilderAPI_MakeEdge(p3,p4).Edge()) wire.Add(BRepBuilderAPI_MakeEdge(p4,p1).Edge()) print("Make Face") face = BRepBuilderAPI_MakeFace(wire.Wire()).Face() print("Return Face") return face
def makeWire(self, edges): """ Topo_DS_Wire makeWire([Topo_DS_Edge, Topo_DS_Edge,...]) Returns a pythonOCC Wire object constructed from the delivered list of Topo_DS_Edges. Returns None on failure. """ try: wire = BRepBuilderAPI_MakeWire(edges[0]) del edges[0] for edge in edges: wire.Add(edge) return wire except Exception: traceback.print_exc() return None
def makeWire(edges): """ Creating Wire Note the edges have to be added in an order than default. I chose the keep the edge numbering in a sensible format (the format shown in the diffuser visualization). However, wire.Add() requires that each edge be connected to the current wire. The current order goes around the physical loop starting at edge0 """ wire = BRepBuilderAPI_MakeWire() edgeorder = (0, 1, 6, 2, 3, 4, 5) for edgen in edgeorder: wire.Add(edges[edgen].Edge()) if args.v >= 1: print('Wire are created') return wire
def pipe(point1, point2, radius): makeWire = BRepBuilderAPI_MakeWire() edge = BRepBuilderAPI_MakeEdge(point1, point2).Edge() makeWire.Add(edge) makeWire.Build() wire = makeWire.Wire() dir = gp_Dir(point2.X() - point1.X(), point2.Y() - point1.Y(), point2.Z() - point1.Z()) circle = gp_Circ(gp_Ax2(point1, dir), radius) profile_edge = BRepBuilderAPI_MakeEdge(circle).Edge() profile_wire = BRepBuilderAPI_MakeWire(profile_edge).Wire() profile_face = BRepBuilderAPI_MakeFace(profile_wire).Face() pipe = BRepOffsetAPI_MakePipe(wire, profile_face).Shape() return (pipe)
def region_as_splinegon(boundary_splines): """Represents a region as a splinegon. Based on the combined topological-geometrical (intermediate) representation of the regions, (done by :func:`skeleton2regions`), this function provides a fully geometrical description of a region. Parameters ---------- boundary_splines : list Each element of the list is a `Handle_Geom_BSplineCurve` object, giving a reference to the B-splines bounding the region. The splines must either be ordered (see :func:`branches2boundary`) or they must appear in an order such that the n-th spline in the list can be connected to one of the first n-1 splines in the list. Returns ------- splinegon : TopoDS_Face The resulting splinegon (surface). For details on the object, see the OpenCASCADE API: https://www.opencascade.com/doc/occt-7.4.0/refman/html/class_topo_d_s___face.html boundary : TopoDS_Wire The boundary of the splinegon. For details on the resulting object, see the OpenCASCADE API: https://www.opencascade.com/doc/occt-7.4.0/refman/html/class_topo_d_s___wire.html See Also -------- skeleton2regions fit_spline Notes ----- The syntax `Handle_classname` in PythonOCC corresponds to wrapping the object of class `classname` with a smart pointer. In the C++ interface, it is done by a template: `Handle<classname>`. """ # Combine the splines so that they form the boundary (a collection of joining splines) boundary = BRepBuilderAPI_MakeWire() for spline in boundary_splines: edge = BRepBuilderAPI_MakeEdge(spline).Edge() boundary.Add(edge) _wire_error(boundary.Error()) # The planar surface (face) is stretched by the wire splinegon = BRepBuilderAPI_MakeFace(boundary.Wire()) _face_error(splinegon.Error()) return splinegon.Face(), boundary.Wire()
def build(input, output): #sample xml for testing xml = "<eagle version=\"7\"><drawing><board><plain><wire x1=\"0\" y1=\"0\" x2=\"0\" y2=\"2\" width=\"0.254\" layer=\"20\"/><wire x1=\"0\" y1=\"2\" x2=\"1.5\" y2=\"2\" width=\"0.254\" layer=\"20\"/><wire x1=\"1.5\" y1=\"2\" x2=\"1\" y2=\"0\" width=\"0.254\" layer=\"20\"/><wire x1=\"1\" y1=\"0\" x2=\"0\" y2=\"0\" width=\"0.254\" layer=\"20\"/></plain></board></drawing></eagle>" #xmldoc = minidom.parseString( xml ) xmldoc = minidom.parse(input) wires = xmldoc.getElementsByTagName('wire') makeWire = BRepBuilderAPI_MakeWire() for wire in wires: if wire.attributes['layer'].value == '20': x1 = float(wire.attributes['x1'].value) y1 = float(wire.attributes['y1'].value) x2 = float(wire.attributes['x2'].value) y2 = float(wire.attributes['y2'].value) #print('Building edge from {}, {} to {}, {}'.format( x1,y1,x2,y2)) edge = BRepBuilderAPI_MakeEdge( gp_Pnt( x1, y1, 0.0 ), \ gp_Pnt( x2, y2, 0.0 ) \ ) makeWire.Add(edge.Edge()) face = BRepBuilderAPI_MakeFace(makeWire.Wire()) #vector & height vector = gp_Vec(0, 0, .1) body = BRepPrimAPI_MakePrism(face.Face(), vector) # initialize the STEP exporter step_writer = STEPControl_Writer() Interface_Static_SetCVal("write.step.schema", "AP203") # transfer shapes and write file step_writer.Transfer(body.Shape(), STEPControl_AsIs) status = step_writer.Write(output) if status != IFSelect_RetDone: raise AssertionError("load failed")
def gen_boxSolidAsTable(width=1000, depth=1000, height=1215): pntList = [ gp_Pnt(width / 2., depth / 2., 0), gp_Pnt(-width / 2., depth / 2., 0), gp_Pnt(-width / 2., -depth / 2., 0), gp_Pnt(width / 2., -depth / 2., 0) ] edgList = [] for i in range(0, len(pntList) - 1): edgList.append(BRepBuilderAPI_MakeEdge(pntList[i], pntList[i + 1])) edgList.append(BRepBuilderAPI_MakeEdge(pntList[3], pntList[0])) wire = BRepBuilderAPI_MakeWire() for i in edgList: wire.Add(i.Edge()) wireProfile = wire.Wire() faceProfile = BRepBuilderAPI_MakeFace(wireProfile).Shape() aPrismVec = gp_Vec(0, 0, height) return BRepPrimAPI_MakePrism(faceProfile, aPrismVec).Shape()
def revolved_cut(base): # Define 7 points face_points = TColgp_Array1OfPnt(1, 7) face_inner_radius = 0.6 pts = [ gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius - 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius + 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), ] for n, i in enumerate(pts): face_points.SetValue(n + 1, i) # Use these points to create edges and add these edges to a wire hexwire = BRepBuilderAPI_MakeWire() for i in range(1, 7): hexedge = BRepBuilderAPI_MakeEdge(face_points.Value(i), face_points.Value(i + 1)).Edge() hexwire.Add(hexedge) # Turn the wire into a 6 sided face hexface = BRepBuilderAPI_MakeFace(hexwire.Wire()).Face() # Revolve the face around an axis revolve_axis = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) revolved_shape = BRepPrimAPI_MakeRevol(hexface, revolve_axis).Shape() # Move the generated shape move = gp_Trsf() move.SetTranslation(gp_Pnt(0, 0, 0), gp_Pnt(0, 0, sin(0.5))) moved_shape = BRepBuilderAPI_Transform(revolved_shape, move, False).Shape() # Remove the revolved shape cut = BRepAlgoAPI_Cut(base, moved_shape).Shape() return cut
def revolved_shape(): """ demonstrate how to create a revolved shape from an edge adapted from algotopia.com's opencascade_basic tutorial: http://www.algotopia.com/contents/opencascade/opencascade_basic """ face_inner_radius = 0.6 # point to create an edge from edg_points = [ gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius - 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius + 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), ] # aggregate edges in wire hexwire = BRepBuilderAPI_MakeWire() for i in range(6): hexedge = BRepBuilderAPI_MakeEdge(edg_points[i], edg_points[i + 1]).Edge() hexwire.Add(hexedge) hexwire_wire = hexwire.Wire() # face from wire hexface = BRepBuilderAPI_MakeFace(hexwire_wire).Face() revolve_axis = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) # create revolved shape revolved_shape_ = BRepPrimAPI_MakeRevol(hexface, revolve_axis, math.radians(90.)).Shape() # render wire & revolved shape display.DisplayShape([revolved_shape_, hexwire_wire]) display.FitAll() start_display()
def __init__(self, knotVector=[], controlPoints=[], degree=3, thickness=10, axis=0, center=False): self.thickness = thickness wire = BRepBuilderAPI_MakeWire() uniqueKnots = unique(knotVector) frequency = [knotVector.count(knot) for knot in uniqueKnots] knots = TColStd_Array1OfReal(0, len(uniqueKnots) - 1) for i in range(len(uniqueKnots)): knots.SetValue(i, uniqueKnots[i]) mults = TColStd_Array1OfInteger(0, len(frequency) - 1) for i in range(len(frequency)): mults.SetValue(i, frequency[i]) poles = TColgp_Array1OfPnt(0, len(controlPoints) - 1) for i in range(len(controlPoints)): p = controlPoints[i] poles.SetValue(i, gp_Pnt(p[0], p[1], p[2])) poles2d = TColgp_Array1OfPnt2d(0, len(controlPoints) - 1) plane = get_principal_plane(controlPoints) for i in range(len(controlPoints)): p = controlPoints[i] if plane == 0: poles2d.SetValue(i, gp_Pnt2d(p[1], p[2])) elif plane == 1: poles2d.SetValue(i, gp_Pnt2d(p[0], p[2])) elif plane == 2: poles2d.SetValue(i, gp_Pnt2d(p[0], p[1])) curve2d = Geom2d_BSplineCurve(poles2d, knots, mults, degree) if is_self_intersecting(curve2d.GetHandle()): from cadmium import CadmiumException raise CadmiumException('Self intersecting BSpline not allowed') curve = Geom_BSplineCurve(poles, knots, mults, degree) me = BRepBuilderAPI_MakeEdge(curve.GetHandle()) wire.Add(me.Edge()) first = controlPoints[0] first = gp_Pnt(first[0], first[1], first[2]) last = controlPoints[-1] last = gp_Pnt(last[0], last[1], last[2]) if not first.IsEqual(last, 1.0e-9): closer = BRepBuilderAPI_MakeEdge( gp_Lin(first, gp_Dir(gp_Vec(first, last))), first, last) wire.Add(closer.Edge()) face = BRepBuilderAPI_MakeFace(wire.Wire()) if axis == 0: extrusion_vector = gp_Vec(self.thickness, 0, 0) elif axis == 1: extrusion_vector = gp_Vec(0, self.thickness, 0) elif axis == 2: extrusion_vector = gp_Vec(0, 0, self.thickness) self.instance = BRepPrimAPI_MakePrism(face.Shape(), extrusion_vector) Solid.__init__(self, self.instance.Shape(), center=center)
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 write(self, mesh_points, filename, tolerance=None): """ Writes a output file, called filename, copying all the structures from self.filename but the coordinates. mesh_points is a matrix that contains the new coordinates to write in the output file. :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of the points of the mesh :param string filename: name of the output file. :param float tolerance: tolerance for the construction of the faces and wires in the write function. If not given it uses `self.tolerance`. """ self._check_filename_type(filename) self._check_extension(filename) self._check_infile_instantiation() self.outfile = filename if tolerance is not None: self.tolerance = tolerance # cycle on the faces to update the control points position # init some quantities faces_explorer = TopExp_Explorer(self.shape, TopAbs_FACE) n_faces = 0 control_point_position = self._control_point_position compound_builder = BRep_Builder() compound = OCC.TopoDS.TopoDS_Compound() compound_builder.MakeCompound(compound) while faces_explorer.More(): # similar to the parser method face = OCC.TopoDS.topods_Face(faces_explorer.Current()) nurbs_converter = BRepBuilderAPI_NurbsConvert(face) nurbs_converter.Perform(face) nurbs_face = nurbs_converter.Shape() face_aux = OCC.TopoDS.topods_Face(nurbs_face) brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) occ_face = bspline_face.GetObject() n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() i = 0 for pole_u_direction in range(n_poles_u): for pole_v_direction in range(n_poles_v): control_point_coordinates = mesh_points[ i + control_point_position[n_faces], :] point_xyz = gp_XYZ(*control_point_coordinates) gp_point = gp_Pnt(point_xyz) occ_face.SetPole(pole_u_direction + 1, pole_v_direction + 1, gp_point) i += 1 # construct the deformed wire for the trimmed surfaces wire_maker = BRepBuilderAPI_MakeWire() tol = ShapeFix_ShapeTolerance() brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), self.tolerance).Face() brep_face = BRep_Tool.Surface(brep) # cycle on the edges edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE) while edge_explorer.More(): edge = OCC.TopoDS.topods_Edge(edge_explorer.Current()) # edge in the (u,v) coordinates edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux) # evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(\ edge_uv_coordinates[0], brep_face) edge_phis_coordinates = edge_phis_coordinates_aux.Edge() tol.SetTolerance(edge_phis_coordinates, self.tolerance) wire_maker.Add(edge_phis_coordinates) edge_explorer.Next() # grouping the edges in a wire wire = wire_maker.Wire() # trimming the surfaces brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), wire).Shape() compound_builder.Add(compound, brep_surf) n_faces += 1 faces_explorer.Next() self.write_shape_to_file(compound, self.outfile)
def build_tooth(): base_center = gp_Pnt2d( pitch_circle_radius + (tooth_radius - roller_radius), 0) base_circle = gp_Circ2d(gp_Ax2d(base_center, gp_Dir2d()), tooth_radius) trimmed_base = GCE2d_MakeArcOfCircle(base_circle, M_PI - (roller_contact_angle / 2.), M_PI).Value() Proxy(trimmed_base).Reverse() # just a trick p0 = Proxy(trimmed_base).StartPoint() p1 = Proxy(trimmed_base).EndPoint() # Determine the center of the profile circle x_distance = cos( roller_contact_angle / 2.) * (profile_radius + tooth_radius) y_distance = sin( roller_contact_angle / 2.) * (profile_radius + tooth_radius) profile_center = gp_Pnt2d(pitch_circle_radius - x_distance, y_distance) # Construct the profile circle gp_Circ2d profile_circle = gp_Circ2d(gp_Ax2d(profile_center, gp_Dir2d()), profile_center.Distance(p1)) geom_profile_circle = GCE2d_MakeCircle(profile_circle).Value() # Construct the outer circle gp_Circ2d outer_circle = gp_Circ2d(gp_Ax2d(gp_Pnt2d(0, 0), gp_Dir2d()), top_radius) geom_outer_circle = GCE2d_MakeCircle(outer_circle).Value() inter = Geom2dAPI_InterCurveCurve(geom_profile_circle, geom_outer_circle) num_points = inter.NbPoints() assert isinstance(p1, gp_Pnt2d) if num_points == 2: if p1.Distance(inter.Point(1)) < p1.Distance(inter.Point(2)): p2 = inter.Point(1) else: p2 = inter.Point(2) elif num_points == 1: p2 = inter.Point(1) else: exit(-1) # Trim the profile circle and mirror trimmed_profile = GCE2d_MakeArcOfCircle(profile_circle, p1, p2).Value() # Calculate the outermost point p3 = gp_Pnt2d( cos(tooth_angle / 2.) * top_radius, sin(tooth_angle / 2.) * top_radius) # and use it to create the third arc trimmed_outer = GCE2d_MakeArcOfCircle(outer_circle, p2, p3).Value() # Mirror and reverse the three arcs mirror_axis = gp_Ax2d(gp_Origin2d(), gp_DX2d().Rotated(tooth_angle / 2.)) mirror_base = Handle_Geom2d_TrimmedCurve.DownCast( Proxy(trimmed_base).Copy()) mirror_profile = Handle_Geom2d_TrimmedCurve.DownCast( Proxy(trimmed_profile).Copy()) mirror_outer = Handle_Geom2d_TrimmedCurve.DownCast( Proxy(trimmed_outer).Copy()) Proxy(mirror_base).Mirror(mirror_axis) Proxy(mirror_profile).Mirror(mirror_axis) Proxy(mirror_outer).Mirror(mirror_axis) Proxy(mirror_base).Reverse() Proxy(mirror_profile).Reverse() Proxy(mirror_outer).Reverse() # Replace the two outer arcs with a single one outer_start = Proxy(trimmed_outer).StartPoint() outer_mid = Proxy(trimmed_outer).EndPoint() outer_end = Proxy(mirror_outer).EndPoint() outer_arc = GCE2d_MakeArcOfCircle(outer_start, outer_mid, outer_end).Value() # Create an arc for the inside of the wedge inner_circle = gp_Circ2d(gp_Ax2d(gp_Pnt2d(0, 0), gp_Dir2d()), top_radius - roller_diameter) inner_start = gp_Pnt2d(top_radius - roller_diameter, 0) inner_arc = GCE2d_MakeArcOfCircle(inner_circle, inner_start, tooth_angle).Value() Proxy(inner_arc).Reverse() # Convert the 2D arcs and two extra lines to 3D edges plane = gp_Pln(gp_Origin(), gp_DZ()) arc1 = BRepBuilderAPI_MakeEdge(geomapi_To3d(trimmed_base, plane)).Edge() arc2 = BRepBuilderAPI_MakeEdge(geomapi_To3d(trimmed_profile, plane)).Edge() arc3 = BRepBuilderAPI_MakeEdge(geomapi_To3d(outer_arc, plane)).Edge() arc4 = BRepBuilderAPI_MakeEdge(geomapi_To3d(mirror_profile, plane)).Edge() arc5 = BRepBuilderAPI_MakeEdge(geomapi_To3d(mirror_base, plane)).Edge() p4 = Proxy(mirror_base).EndPoint() p5 = Proxy(inner_arc).StartPoint() lin1 = BRepBuilderAPI_MakeEdge(gp_Pnt(p4.X(), p4.Y(), 0), gp_Pnt(p5.X(), p5.Y(), 0)).Edge() arc6 = BRepBuilderAPI_MakeEdge(geomapi_To3d(inner_arc, plane)).Edge() p6 = Proxy(inner_arc).EndPoint() lin2 = BRepBuilderAPI_MakeEdge(gp_Pnt(p6.X(), p6.Y(), 0), gp_Pnt(p0.X(), p0.Y(), 0)).Edge() wire = BRepBuilderAPI_MakeWire(arc1) wire.Add(arc2) wire.Add(arc3) wire.Add(arc4) wire.Add(arc5) wire.Add(lin1) wire.Add(arc6) wire.Add(lin2) face = BRepBuilderAPI_MakeFace(wire.Wire()) wedge = BRepPrimAPI_MakePrism(face.Shape(), gp_Vec(0.0, 0.0, thickness)) return wedge.Shape()