def makeHeartWire(): "make a heart wire" e1 = Wrappers.edgeFromTwoPoints(gp.gp_Pnt(0,0,0), gp.gp_Pnt(4.0,4.0,0)); circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(2,4,0),gp.gp().DZ()),2); e2 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle, gp.gp_Pnt(4,4,0),gp.gp_Pnt(0,4,0)).Edge(); circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(-2,4,0),gp.gp().DZ()),2); e3 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle, gp.gp_Pnt(0,4,0),gp.gp_Pnt(-4,4,0)).Edge(); e4 = Wrappers.edgeFromTwoPoints(gp.gp_Pnt(-4,4,0), gp.gp_Pnt(0,0,0)); return Wrappers.wireFromEdges([e1,e2,e3,e4]);
def makeHeartWire(): "make a heart wire" e1 = Wrappers.edgeFromTwoPoints(gp.gp_Pnt(0, 0, 0), gp.gp_Pnt(4.0, 4.0, 0)) circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(2, 4, 0), gp.gp().DZ()), 2) e2 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle, gp.gp_Pnt(4, 4, 0), gp.gp_Pnt(0, 4, 0)).Edge() circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(-2, 4, 0), gp.gp().DZ()), 2) e3 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle, gp.gp_Pnt(0, 4, 0), gp.gp_Pnt(-4, 4, 0)).Edge() e4 = Wrappers.edgeFromTwoPoints(gp.gp_Pnt(-4, 4, 0), gp.gp_Pnt(0, 0, 0)) return Wrappers.wireFromEdges([e1, e2, e3, e4])
def through_sections(): #ruled circle_1 = gp_Circ(gp_Ax2(gp_Pnt(-100., 0., -100.), gp_Dir(0., 0., 1.)), 40.) wire_1 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_1).Edge()).Wire() circle_2 = gp_Circ(gp_Ax2(gp_Pnt(-10., 0., -0.), gp_Dir(0., 0., 1.)), 40.) wire_2 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_2).Edge()).Wire() circle_3 = gp_Circ(gp_Ax2(gp_Pnt(-75., 0., 100.), gp_Dir(0., 0., 1.)), 40.) wire_3 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_3).Edge()).Wire() circle_4 = gp_Circ(gp_Ax2(gp_Pnt(0., 0., 200.), gp_Dir(0., 0., 1.)), 40.) wire_4 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_4).Edge()).Wire() generatorA = BRepOffsetAPI_ThruSections(False, True) map(generatorA.AddWire, [wire_1, wire_2, wire_3, wire_4]) generatorA.Build() display.DisplayShape(generatorA.Shape()) #smooth circle_1b = gp_Circ(gp_Ax2(gp_Pnt(100., 0., -100.), gp_Dir(0., 0., 1.)), 40.) wire_1b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_1b).Edge()).Wire() circle_2b = gp_Circ(gp_Ax2(gp_Pnt(210., 0., -0.), gp_Dir(0., 0., 1.)), 40.) wire_2b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_2b).Edge()).Wire() circle_3b = gp_Circ(gp_Ax2(gp_Pnt(275., 0., 100.), gp_Dir(0., 0., 1.)), 40.) wire_3b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_3b).Edge()).Wire() circle_4b = gp_Circ(gp_Ax2(gp_Pnt(200., 0., 200.), gp_Dir(0., 0., 1.)), 40.) wire_4b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_4b).Edge()).Wire() generatorB = BRepOffsetAPI_ThruSections(True, False) map(generatorB.AddWire, [wire_1b, wire_2b, wire_3b, wire_4b]) generatorB.Build() display.DisplayShape(generatorB.Shape(), update=True)
def make_cylinder(position, direction, radius, length, offset, x_axis): cyl_ax = gp.gp_Ax2(gp.gp_Pnt(offset,0,0), gp.gp_Dir(0,0,1), gp.gp_Dir(1,0,0)) cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(cyl_ax, radius, length) ax = gp.gp_Ax2(gp.gp_Pnt(*position), gp.gp_Dir(*direction), gp.gp_Dir(*x_axis)) ax3 = gp.gp_Ax3() trans = gp.gp_Trsf() trans.SetTransformation(gp.gp_Ax3(ax), ax3) t_cyl = BRepBuilderAPI.BRepBuilderAPI_Transform(cyl.Shape(), trans) print(position, direction, radius, length) return toshape(t_cyl)
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 createEdges(self): edges = [] # Join points a,b edge = make_edge(getGpPt(self.a), getGpPt(self.b)) edges.append(edge) # # Join points b1 and b2 # cirl = gp_Circ(gp_Ax2(getGpPt(self.o1), getGpDir(self.wDir)), self.R1) # edge = make_edge(cirl,getGpPt(self.b2), getGpPt(self.b1)) # edges.append(edge) # Join points b and c2 edge = make_edge(getGpPt(self.b), getGpPt(self.c2)) edges.append(edge) # join points c2 and c1 cirl2 = gp_Circ(gp_Ax2(getGpPt(self.o2), getGpDir(self.wDir)), self.R1) edge = make_edge(cirl2, getGpPt(self.c1), getGpPt(self.c2)) edges.append(edge) # Join points c1 and d edge = make_edge(getGpPt(self.c1), getGpPt(self.d)) edges.append(edge) # Join points d and a edge = make_edge(getGpPt(self.d), getGpPt(self.a)) edges.append(edge) return edges
def execute(self): ax = gp.gp_Ax2(gp.gp_Pnt(*self.position), gp.gp_Dir(*self.z_axis), gp.gp_Dir(*self.x_axis)) m_box = BRepPrimAPI.BRepPrimAPI_MakeBox(ax, *self.dims) self.update_naming(m_box) return m_box.Shape()
def preview(self, inp, direction): if self.step == 0: self.previous_data = [inp] return [BRepBuilderAPI.BRepBuilderAPI_MakeVertex(inp).Vertex()] elif self.step == 1: point0 = self.previous_data[0] point1 = inp # checking if the previous points are identical: This is necessary # before continuing in order to avoid a crash on Windows if point0 == point1: raise InvalidInputException dirvec = vec(0, 0, 0) dirvec[direction] = 1 axis = gp.gp_Ax2(point0, dirvec.to_gp_Dir()) d = point0 - inp d[direction] = 0 dist = d.length() a = Geom.Geom_Circle(axis, dist).GetHandle() b = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(a).Edge() c = BRepBuilderAPI.BRepBuilderAPI_MakeWire(b).Wire() d = BRepBuilderAPI.BRepBuilderAPI_MakeFace(c).Face() self._final = [d] return self._final
def createModel(self): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = -self.T * self.shaftDir # extrudeDir is a numpy array boltHead = makePrismFromFace(aFace, extrudeDir) mkFillet = BRepFilletAPI_MakeFillet(boltHead) anEdgeExplorer = TopExp_Explorer(boltHead, TopAbs_EDGE) while anEdgeExplorer.More(): aEdge = topods.Edge(anEdgeExplorer.Current()) mkFillet.Add(self.T / 17. , aEdge) anEdgeExplorer.Next() boltHead = mkFillet.Shape() cylOrigin = self.origin boltCylinder = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.shaftDir)), self.r, self.H).Shape() whole_Bolt = BRepAlgoAPI_Fuse(boltHead,boltCylinder).Shape() mkFillet = BRepFilletAPI_MakeFillet(whole_Bolt) return whole_Bolt
def wxy_wire(pnt, wxy=[10, 20]): if wxy[0] >= wxy[1]: ax2 = gp_Ax2(pnt, gp_Dir(0, 0, 1), gp_Dir(1, 0, 0)) w_x = wxy[0] w_y = wxy[1] elif wxy[1] >= wxy[0]: ax2 = gp_Ax2(pnt, gp_Dir(0, 0, 1), gp_Dir(0, 1, 0)) w_x = wxy[1] w_y = wxy[0] else: ax2 = gp_Ax2(pnt, gp_Dir(0, 0, 1), gp_Dir(1, 0, 0)) w_x = wxy[0] w_y = wxy[1] obj = Geom_Ellipse(ax2, w_x, w_y).Elips() obj = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(obj).Edge()).Wire() return obj
def AddCone(BasePoint, Radius, height, direction=gp_Dir(1, 0, 0)): """Generates a cone shape originating at BasePoint with base Radius and height (points in the direction of input 'direction) Parameters ---------- BasePoint : OCC.gp.gp_Pnt or array length 3 The centre base point Radius : scalar Cone base radius height : scalar Cone height direction : OCC.gp.gp_Dir (default: positive x direction) the direction of the cones axis i.e. normal to the base: defaults to x axis Returns ------- shape : TopoDS_Shape The generated Cone """ try: BasePoint = gp_Pnt(*BasePoint) except: pass ax2 = gp_Ax2(BasePoint, direction) cone = BRepPrimAPI_MakeCone(ax2, Radius, 0, height) return cone.Shape()
def create_shape(self): attrs = self.element.attrib cx = parse_unit(attrs.get('cx', 0)) cy = parse_unit(attrs.get('cy', 0)) r = parse_unit(attrs.get('r', 0)) circle = gp_Circ(gp_Ax2(gp_Pnt(cx, cy, 0), Z_DIR), r) return BRepBuilderAPI_MakeEdge(circle).Edge()
def pipe(event=None): CurvePoles = TColgp_Array1OfPnt(1,6) pt1 = gp_Pnt(0.,0.,0.); pt2 = gp_Pnt(20.,50.,0.); pt3 = gp_Pnt(60.,100.,0.); pt4 = gp_Pnt(150.,0.,0.); CurvePoles.SetValue(1, pt1) CurvePoles.SetValue(2, pt2) CurvePoles.SetValue(3, pt3) CurvePoles.SetValue(4, pt4) curve = Geom_BezierCurve(CurvePoles) print type(curve) E = BRepBuilderAPI_MakeEdge(curve.GetHandle()).Edge() W = BRepBuilderAPI_MakeWire(E).Wire() #ais1 = AIS_Shape(W) #self.interactive_context.Display(ais1,1) c = gp_Circ(gp_Ax2(gp_Pnt(0.,0.,0.),gp_Dir(0.,1.,0.)),10.) Ec = BRepBuilderAPI_MakeEdge(c).Edge() Wc = BRepBuilderAPI_MakeWire(Ec).Wire() #ais3 = AIS_Shape(Wc) #self.interactive_context.Display(ais3,1) F = BRepBuilderAPI_MakeFace(gp_Pln(gp_Ax3(gp.gp().ZOX())),Wc,1).Face() MKPipe = BRepOffsetAPI_MakePipe(W,F) MKPipe.Build() display.DisplayShape(MKPipe.Shape())
def createEdges(self): edges= [] # Join points a,b edge = make_edge(getGpPt(self.a), getGpPt(self.b)) edges.append(edge) # # Join points b1 and b2 # cirl = gp_Circ(gp_Ax2(getGpPt(self.o1), getGpDir(self.wDir)), self.R1) # edge = make_edge(cirl,getGpPt(self.b2), getGpPt(self.b1)) # edges.append(edge) # Join points b and c2 edge = make_edge(getGpPt(self.b), getGpPt(self.c2)) edges.append(edge) # join points c2 and c1 cirl2 = gp_Circ(gp_Ax2(getGpPt(self.o2), getGpDir(self.wDir)), self.R1) edge = make_edge(cirl2, getGpPt(self.c1), getGpPt(self.c2)) edges.append(edge) # Join points c1 and d edge = make_edge(getGpPt(self.c1), getGpPt(self.d)) edges.append(edge) # Join points d and a edge = make_edge(getGpPt(self.d), getGpPt(self.a)) edges.append(edge) return edges
def create(self,center_Pnt,merged_arg=0): self.new_Pnt=center_Pnt self.new_gp_Pnt=gp_Pnt(center_Pnt[0],center_Pnt[1],center_Pnt[2]) self.shape=BRepPrimAPI_MakeSphere(self.new_gp_Pnt,2.1).Shape() display.DisplayShape(self.shape, update=True, color='YELLOW') self.attach_gp_dir=[] self.attach_gp_Ax2=[] self.magnet=[] self.attach_dir = [] for i in range(len(self.attach_pos)): if self.attach_pos[i]!=0: # self.attach_gp_dir.append(i) # self.attach_gp_Ax2.append(i) # self.magnet.append(i) self.dir=merged_arg merge_rotate=[math.cos(i * self.divide_arg)*math.cos(merged_arg)- math.sin(i*self.divide_arg)*math.sin(merged_arg), math.sin(i * self.divide_arg)*math.cos(merged_arg)+ math.cos(i * self.divide_arg)*math.sin(merged_arg),0] self.attach_dir.append([merge_rotate[0],merge_rotate[1],merge_rotate[2]]) self.attach_gp_dir.append(gp_Dir(self.attach_dir[i][0],self.attach_dir[i][1],self.attach_dir[i][2])) self.attach_gp_Ax2.append(gp_Ax2(self.new_gp_Pnt,self.attach_gp_dir[i])) self.magnet.append(BRepPrimAPI_MakeCylinder(self.attach_gp_Ax2[i],0.25,2.4).Shape()) if self.attach_pos[i]==1: display.DisplayShape(self.magnet[i], update=True, color='RED') elif self.attach_pos[i]==-1: display.DisplayShape(self.magnet[i], update=True, color=22)#22 is blue else: self.attach_gp_dir.append(i) self.attach_gp_Ax2.append(i) self.attach_dir.append(i) self.magnet.append(i)
def boolean_cut(base): # Create a cylinder cylinder_radius = 0.25 cylinder_height = 2.0 cylinder_origin = gp_Ax2(gp_Pnt(0.0, 0.0, -cylinder_height / 2.0), gp_Dir(0.0, 0.0, 1.0)) cylinder = BRepPrimAPI_MakeCylinder(cylinder_origin, cylinder_radius, cylinder_height) # Repeatedly move and subtract it from the input shape move = gp_Trsf() boolean_result = base clone_radius = 1.0 for clone in range(8): angle = clone * pi / 4.0 # Move the cylinder move.SetTranslation( gp_Vec(cos(angle) * clone_radius, sin(angle) * clone_radius, 0.0)) moved_cylinder = BRepBuilderAPI_Transform(cylinder.Shape(), move, True).Shape() # Subtract the moved cylinder from the drilled sphere boolean_result = BRepAlgoAPI_Cut(boolean_result, moved_cylinder).Shape() return boolean_result
def mirror(brep, plane='xz', axe2=None, copy=False): """Originally from pythonocc-utils : might add dependency on this? Mirrors object Parameters ---------- brep : OCC.TopoDS.TopoDS_Shape The shape to mirror plane : string (default = 'xz') The name of the plane in which to mirror objects. Acceptable inputs are any of 'xy', 'yx' , 'zy', 'yz', 'yz', 'zy'. Overwritten if axe2 is defined. axe2 : OCC.gp.gp_Ax2 The axes through which to mirror (overwrites input 'plane') copy : bool Returns ------- BRepBuilderAPI_Transform.Shape : TopoDS_Shape The reflected shape Notes ----- Pchambers: Added a functionality here to specify a plane using a string so that users could avoid interacting with core occ objects""" if axe2: plane = None else: Orig = gp_Pnt(0., 0., 0.) if plane in ['xz', 'zx']: ydir = gp_Dir(0, 1, 0) axe2 = gp_Ax2(Orig, ydir) elif plane in ['yz', 'zy']: xdir = gp_Dir(1, 0, 0) axe2 = gp_Ax2(Orig, xdir) elif plane in ['xy', 'yx']: zdir = gp_Dir(0, 0, 1) axe2 = gp_Ax2(Orig, zdir) else: raise (ValueError, "Unknown mirror plane string,", plane) trns = gp_Trsf() trns.SetMirror(axe2) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) return brep_trns.Shape()
def mirror(brep, plane='xz', axe2=None, copy=False): """Originally from pythonocc-utils : might add dependency on this? Mirrors object Parameters ---------- brep : OCC.TopoDS.TopoDS_Shape The shape to mirror plane : string (default = 'xz') The name of the plane in which to mirror objects. Acceptable inputs are any of 'xy', 'yx' , 'zy', 'yz', 'yz', 'zy'. Overwritten if axe2 is defined. axe2 : OCC.gp.gp_Ax2 The axes through which to mirror (overwrites input 'plane') copy : bool Returns ------- BRepBuilderAPI_Transform.Shape : TopoDS_Shape The reflected shape Notes ----- Pchambers: Added a functionality here to specify a plane using a string so that users could avoid interacting with core occ objects""" if axe2: plane = None else: Orig = gp_Pnt(0., 0., 0.) if plane in ['xz', 'zx']: ydir = gp_Dir(0, 1, 0) axe2 = gp_Ax2(Orig, ydir) elif plane in ['yz', 'zy']: xdir = gp_Dir(1, 0, 0) axe2 = gp_Ax2(Orig, xdir) elif plane in ['xy', 'yx']: zdir = gp_Dir(0, 0, 1) axe2 = gp_Ax2(Orig, zdir) else: raise(ValueError, "Unknown mirror plane string,", plane) trns = gp_Trsf() trns.SetMirror(axe2) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) return brep_trns.Shape()
def wire_sweep_circle(ct1, ct2): ''' input c1: gp_Pnt c2: gp_Pnt output w: TopoDS_Wire ''' center = DRAIN_RCS.Location() vec = DRAIN_RCS.Direction() radius = center.Distance(ct1) pt1 = gp_Pnt(ct1.XYZ()) pt2 = gp_Pnt(ct1.XYZ()) pt3 = gp_Pnt(ct2.XYZ()) pt4 = gp_Pnt(ct2.XYZ()) vec1 = gp_Vec(ct1, center) vec1.Normalize() vec2 = gp_Vec(ct2, center) vec2.Normalize() pt1.Translate(vec1 * DRAIN_S) pt2.Translate(-vec1 * DRAIN_S) pt3.Translate(vec2 * DRAIN_S) pt4.Translate(-vec2 * DRAIN_S) cir1 = gp_Circ(gp_Ax2(ct1, vec), DRAIN_S) ed1 = BRepBuilderAPI_MakeEdge( GC_MakeArcOfCircle(cir1, pt1, pt2, True).Value()).Edge() cir2 = gp_Circ(gp_Ax2(center, vec), radius + DRAIN_S) ed2 = BRepBuilderAPI_MakeEdge( GC_MakeArcOfCircle(cir2, pt2, pt4, False).Value()).Edge() cir3 = gp_Circ(gp_Ax2(ct2, vec), DRAIN_S) ed3 = BRepBuilderAPI_MakeEdge( GC_MakeArcOfCircle(cir3, pt4, pt3, True).Value()).Edge() cir4 = gp_Circ(gp_Ax2(center, vec), radius - DRAIN_S) ed4 = BRepBuilderAPI_MakeEdge( GC_MakeArcOfCircle(cir4, pt1, pt3, False).Value()).Edge() wire = BRepBuilderAPI_MakeWire(ed1, ed2, ed3, ed4).Wire() return wire
def generate_shape(): """Create a sphere with faces top and bottom""" sphere_radius = 1.0 sphere_angle = atan(0.5) sphere_origin = gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) sphere = BRepPrimAPI_MakeSphere(sphere_origin, sphere_radius, -sphere_angle, sphere_angle).Shape() return sphere
def SetCylDirection(self, val): a = (val - 50.) / 10. b = math.sin(a) c = math.cos(a) ax = gp.gp_Ax2(gp.gp_Pnt(0, 0, 0), gp.gp_Dir(b, 0, c)) cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(ax, 25, 50).Shape() self.shape_tool.SetShape(self.cyl_label, cyl) self.shape_tool.UpdateAssembly(self.top_label)
def makeCircleWire(): "designed to be include inside the square to simulate an island" circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(2,2,0),gp.gp().DZ()),1); e1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle).Edge(); mw = BRepBuilderAPI.BRepBuilderAPI_MakeWire(); mw.Add(e1); return mw.Wire();
def make_true_para(FL, rmin, rmax): """ makes a parabloid, with rotation axis along Z and focus at the origin """ ax = gp.gp_Ax2(gp.gp_Pnt(0.,0.,-FL), #origin gp.gp_Dir(1.,0.,0.), #main direction is Z gp.gp_Dir(0.,0.,1.)) #X Direction is X para = Geom.Geom_Parabola(ax, FL) h_para = Geom.Handle_Geom_Parabola(para) ax2 = gp.gp_Ax2(gp.gp_Pnt(0,0,0), #origin gp.gp_Dir(0.,0.,1.), #main direction is Z gp.gp_Dir(1.,0.,0.)) #X Direction is X pbl_shape = BRepPrimAPI.BRepPrimAPI_MakeRevolution(ax2, h_para, rmin, rmax) return pbl_shape.Shape()
def makeCircleWire(): "designed to be include inside the square to simulate an island" circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(2, 2, 0), gp.gp().DZ()), 1) e1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle).Edge() mw = BRepBuilderAPI.BRepBuilderAPI_MakeWire() mw.Add(e1) return mw.Wire()
def make_box(position, direction, x_axis, dx, dy, dz): box = BRepPrimAPI.BRepPrimAPI_MakeBox(gp.gp_Pnt(-dx/2., -dy/2., 0.0), dx, dy, -dz) ax = gp.gp_Ax2(gp.gp_Pnt(*position), gp.gp_Dir(*direction), gp.gp_Dir(*x_axis)) ax3 = gp.gp_Ax3() trans = gp.gp_Trsf() trans.SetTransformation(gp.gp_Ax3(ax), ax3) t_box = BRepBuilderAPI.BRepBuilderAPI_Transform(box.Shape(), trans) return toshape(t_box)
def reflect(p0, v0, ax): ray = gp_Lin(p0, vec_to_dir(v0)) intersection = IntAna_IntConicQuad(ray, gp_Pln(ax), precision_Angular(), precision_Confusion()) p1 = intersection.Point(1) vx, vy = gp_Vec(1, 0, 0), gp_Vec(0, 1, 0) handle = Geom_Plane(ax) handle.D1(0.5, 0.5, gp_Pnt(), vx, vy) vz = vx.Crossed(vy) v1 = v0.Mirrored(gp_Ax2(p1, vec_to_dir(vz))) return p1, v1
def do_cone(): axe = gp_Ax2() axe.SetLocation(gp_Pnt((random_vec() * scope).XYZ())) axe.SetDirection(gp_Dir(random_vec())) cone = BRepPrimAPI_MakeCone( axe, 0, #random.uniform(0,), # r1 random.uniform(10, 30), # r2 random.uniform(30, 1500), # h ) return cone.Shape()
def make_spherical_lens2(CT1, CT2, diameter, curvature1, curvature2, centre, direction, x_axis): cax = gp.gp_Ax2(gp.gp_Pnt(0,0,CT1-curvature1), gp.gp_Dir(0,sign(curvature1),0), gp.gp_Dir(1,0,0)) circ = Geom.Geom_Circle(cax, abs(curvature1)) h_circ = Geom.Handle_Geom_Circle(circ) cax2 = gp.gp_Ax2(gp.gp_Pnt(0,0,CT2-curvature2), gp.gp_Dir(0,-sign(curvature2),0), gp.gp_Dir(1,0,0)) circ2 = Geom.Geom_Circle(cax2, abs(curvature2)) h_circ2 = Geom.Handle_Geom_Circle(circ2) r = diameter/2. h2 = CT1 - curvature1 + numpy.sqrt(curvature1**2 - r**2)*sign(curvature1) h3 = CT2 - curvature2 + numpy.sqrt(curvature2**2 - r**2)*sign(curvature2) p1 = gp.gp_Pnt(0,0,CT1) p2 = gp.gp_Pnt(r,0,h2) p3 = gp.gp_Pnt(r,0,h3) p4 = gp.gp_Pnt(0,0,CT2) e1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(h_circ, p1, p2) e2 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(p2,p3) e3 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(h_circ2, p3,p4) e4 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(p4,p1) wire = BRepBuilderAPI.BRepBuilderAPI_MakeWire() for e in (e1,e2,e3,e4): print(e) wire.Add(e.Edge()) face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(wire.Wire()) ax = gp.gp_Ax1(gp.gp_Pnt(0,0,0), gp.gp_Dir(0,0,1)) solid = BRepPrimAPI.BRepPrimAPI_MakeRevol(face.Shape(), ax) return position_shape(toshape(solid), centre, direction, x_axis)
def make_ellipse(p, rx, ry, rotate=0, direction=Z_DIR): """ gp_Elips doesn't allow minor > major so swap and rotate instead if that's the case. """ c = gp_Pnt(*p) if ry > rx: rx, ry = ry, rx # Swap rotate += pi/2 # This only works when rotate == 0 ellipse = gp_Elips(gp_Ax2(c, direction), rx, ry) ellipse.Rotate(gp_Ax1(c, direction), rotate) return ellipse
def preview(self, input, direction): assert self.step == 0 vec_ = vec(0, 0, 0) vec_[direction] = 1 tr = gp.gp_Trsf() tr.SetMirror(gp.gp_Ax2(gp_.gp_Pnt(0, 0, 0), vec_.to_gp_Dir())) # object.Moved() cannot be used here because it does not adjust # the surface orientation and will result in incorrect models t = BRepBuilderAPI.BRepBuilderAPI_Transform(input, tr).Shape() t = copy_geom.copy(t) self._final = [t] self.remove = [input] return self._final
def makeSquareWithRoundHole(): points = [ (0,0),(0.05,-1.0),(1.0,0),(2.0,0),(2.0,6.0),(0.0,6.0) ]; ow = makeWireFromPointList(points); circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(1.0,2,0),gp.gp().DZ()),0.75); e1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle).Edge(); mw = BRepBuilderAPI.BRepBuilderAPI_MakeWire(); mw.Add(e1); circle = mw.Wire(); builder = BRepBuilderAPI.BRepBuilderAPI_MakeFace(ow,True); builder.Add(circle); return builder.Face();
def through_sections(): #ruled circle_1 = gp_Circ(gp_Ax2(gp_Pnt(-100., 0., -100.), gp_Dir(0., 0., 1.)), 40.) wire_1 = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_1).Edge()).Wire() circle_2 = gp_Circ(gp_Ax2(gp_Pnt(-10., 0., -0.), gp_Dir(0., 0., 1.)), 40.) wire_2 = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_2).Edge()).Wire() circle_3 = gp_Circ(gp_Ax2(gp_Pnt(-75., 0., 100.), gp_Dir(0., 0., 1.)), 40.) wire_3 = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_3).Edge()).Wire() circle_4 = gp_Circ(gp_Ax2(gp_Pnt(0., 0., 200.), gp_Dir(0., 0., 1.)), 40.) wire_4 = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_4).Edge()).Wire() generatorA = BRepOffsetAPI_ThruSections(False, True) # the use of the map function fails at producing the ThruSection # on py3k. Why ? # map(generatorA.AddWire, [wire_1, wire_2, wire_3, wire_4]) # we have to use a loop for wir in [wire_1, wire_2, wire_3, wire_4]: generatorA.AddWire(wir) generatorA.Build() display.DisplayShape(generatorA.Shape()) #smooth circle_1b = gp_Circ(gp_Ax2(gp_Pnt(100., 0., -100.), gp_Dir(0., 0., 1.)), 40.) wire_1b = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_1b).Edge()).Wire() circle_2b = gp_Circ(gp_Ax2(gp_Pnt(210., 0., -0.), gp_Dir(0., 0., 1.)), 40.) wire_2b = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_2b).Edge()).Wire() circle_3b = gp_Circ(gp_Ax2(gp_Pnt(275., 0., 100.), gp_Dir(0., 0., 1.)), 40.) wire_3b = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_3b).Edge()).Wire() circle_4b = gp_Circ(gp_Ax2(gp_Pnt(200., 0., 200.), gp_Dir(0., 0., 1.)), 40.) wire_4b = BRepBuilderAPI_MakeWire( BRepBuilderAPI_MakeEdge(circle_4b).Edge()).Wire() generatorB = BRepOffsetAPI_ThruSections(True, False) # same here, the following line fails # map(generatorB.AddWire, [wire_1b, wire_2b, wire_3b, wire_4b]) for wir in [wire_1b, wire_2b, wire_3b, wire_4b]: generatorB.AddWire(wir) generatorB.Build() display.DisplayShape(generatorB.Shape(), update=True)
def makeBox(cls, length, width, height, pnt=Vector(0, 0, 0), dir=Vector(0, 0, 1)): """ makeBox(length,width,height,[pnt,dir]) -- Make a box located in pnt with the dimensions (length,width,height) By default pnt=Vector(0,0,0) and dir=Vector(0,0,1)' """ return cls( BRepPrimAPI_MakeBox(gp_Ax2(pnt.toPnt(), dir.toDir()), length, width, height).Shape())
def compute_minimal_distance_between_circles(): """ compute the minimal distance between 2 circles here the minimal distance overlaps the intersection of the circles the points are rendered to indicate the locations """ # required for precise rendering of the circles display.Context.SetDeviationCoefficient(0.0001) L = gp_Pnt(4, 10, 0) M = gp_Pnt(10, 16, 0) Laxis = gp_Ax2() Maxis = gp_Ax2() Laxis.SetLocation(L) Maxis.SetLocation(M) r1 = 12.0 r2 = 15.0 Lcircle = gp_Circ(Laxis, r1) Mcircle = gp_Circ(Maxis, r2) l_circle, m_circle = make_edge(Lcircle), make_edge(Mcircle) display.DisplayShape((l_circle, m_circle)) # compute the minimal distance between 2 circles # the minimal distance here matches the intersection of the circles dss = BRepExtrema_DistShapeShape(l_circle, m_circle) print("intersection parameters on l_circle:", [dss.ParOnEdgeS1(i) for i in range(1, dss.NbSolution() + 1)]) print("intersection parameters on m_circle:", [dss.ParOnEdgeS2(i) for i in range(1, dss.NbSolution() + 1)]) for i in range(1, dss.NbSolution() + 1): pnt = dss.PointOnShape1(i) display.DisplayShape(make_vertex(pnt))
def createModel(self): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = -self.T * self.shaftDir # extrudeDir is a numpy array boltHead = makePrismFromFace(aFace, extrudeDir) cylOrigin = self.origin boltCylinder = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.shaftDir)), self.r, self.H).Shape() whole_Bolt = BRepAlgoAPI_Fuse(boltHead, boltCylinder).Shape() return whole_Bolt
def makeCylinder(cls, radius, height, pnt=Vector(0, 0, 0), dir=Vector(0, 0, 1), angleDegrees=360): """ makeCylinder(radius,height,[pnt,dir,angle]) -- Make a cylinder with a given radius and height By default pnt=Vector(0,0,0),dir=Vector(0,0,1) and angle=360' """ return cls( BRepPrimAPI_MakeCylinder(gp_Ax2(pnt.toPnt(), dir.toDir()), radius, height, angleDegrees * DEG2RAD).Shape())
def makeSquareWithRoundHole(): points = [(0, 0), (0.05, -1.0), (1.0, 0), (2.0, 0), (2.0, 6.0), (0.0, 6.0)] ow = makeWireFromPointList(points) circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(1.0, 2, 0), gp.gp().DZ()), 0.75) e1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle).Edge() mw = BRepBuilderAPI.BRepBuilderAPI_MakeWire() mw.Add(e1) circle = mw.Wire() builder = BRepBuilderAPI.BRepBuilderAPI_MakeFace(ow, True) builder.Add(circle) return builder.Face()
def create_model(self): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = self.T * self.wDir # extrudeDir is a numpy array prism = makePrismFromFace(aFace, extrudeDir) cylOrigin = self.sec_origin innerCyl = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.wDir)), self.r1, self.H).Shape() result_shape = BRepAlgoAPI_Cut(prism, innerCyl).Shape() return result_shape
def makeKeyHoleWire(): circle2 = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(40,40,2),gp.gp_Dir(0,0,1)),10) Edge4 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle2,gp.gp_Pnt(40,50,2),gp.gp_Pnt(50,40,2)).Edge() ExistingWire2 = BRepBuilderAPI.BRepBuilderAPI_MakeWire(Edge4).Wire() P1 = gp.gp_Pnt(50,40,2) P2 = gp.gp_Pnt(80,40,2) #5,204,0 Edge5 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(P1,P2).Edge() MW = BRepBuilderAPI.BRepBuilderAPI_MakeWire() MW.Add(Edge5) MW.Add(ExistingWire2) if MW.IsDone(): WhiteWire = MW.Wire() return [WhiteWire,Edge5,ExistingWire2];
def edge(event=None): # The blud edge BlueEdge = BRepBuilderAPI_MakeEdge(gp_Pnt(-80, -50, -20), gp_Pnt(-30, -60, -60)) V1 = BRepBuilderAPI_MakeVertex(gp_Pnt(-20, 10, -30)) V2 = BRepBuilderAPI_MakeVertex(gp_Pnt(10, 7, -25)) YellowEdge = BRepBuilderAPI_MakeEdge(V1.Vertex(), V2.Vertex()) #The white edge line = gp_Lin(gp_Ax1(gp_Pnt(10, 10, 10), gp_Dir(1, 0, 0))) WhiteEdge = BRepBuilderAPI_MakeEdge(line, -20, 10) #The red edge Elips = gp_Elips(gp_Ax2(gp_Pnt(10, 0, 0), gp_Dir(1, 1, 1)), 60, 30) RedEdge = BRepBuilderAPI_MakeEdge(Elips, 0, math.pi/2) # The green edge and the both extreme vertex P1 = gp_Pnt(-15, 200, 10) P2 = gp_Pnt(5, 204, 0) P3 = gp_Pnt(15, 200, 0) P4 = gp_Pnt(-15, 20, 15) P5 = gp_Pnt(-5, 20, 0) P6 = gp_Pnt(15, 20, 0) P7 = gp_Pnt(24, 120, 0) P8 = gp_Pnt(-24, 120, 12.5) array = TColgp_Array1OfPnt(1, 8) array.SetValue(1, P1) array.SetValue(2, P2) array.SetValue(3, P3) array.SetValue(4, P4) array.SetValue(5, P5) array.SetValue(6, P6) array.SetValue(7, P7) array.SetValue(8, P8) curve = Geom_BezierCurve(array) ME = BRepBuilderAPI_MakeEdge(curve.GetHandle()) GreenEdge = ME V3 = ME.Vertex1() V4 = ME.Vertex2() display.DisplayColoredShape(BlueEdge.Edge(), 'BLUE') display.DisplayShape(V1.Vertex()) display.DisplayShape(V2.Vertex()) display.DisplayColoredShape(WhiteEdge.Edge(), 'WHITE') display.DisplayColoredShape(YellowEdge.Edge(), 'YELLOW') display.DisplayColoredShape(RedEdge.Edge(), 'RED') display.DisplayColoredShape(GreenEdge.Edge(), 'GREEN') display.DisplayShape(V3) display.DisplayShape(V4, update=True)
def common(event=None): # Create Box axe = gp_Ax2(gp_Pnt(10, 10, 10), gp_Dir(1, 2, 1)) Box = BRepPrimAPI_MakeBox(axe, 60, 80, 100).Shape() # Create wedge Wedge = BRepPrimAPI_MakeWedge(60., 100., 80., 20.).Shape() # Common surface CommonSurface = BRepAlgoAPI_Common(Box, Wedge).Shape() display.EraseAll() ais_box = display.DisplayShape(Box) ais_wedge = display.DisplayShape(Wedge) display.Context.SetTransparency(ais_box, 0.8) display.Context.SetTransparency(ais_wedge, 0.8) display.DisplayShape(CommonSurface) display.FitAll()
def createModel(self): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = self.T * self.wDir # extrudeDir is a numpy array prism = makePrismFromFace(aFace, extrudeDir) cylOrigin = self.secOrigin innerCyl = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.wDir)), self.r1, self.H).Shape() result_shape = BRepAlgoAPI_Cut(prism, innerCyl).Shape() return result_shape
def createModel(self): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = -self.T * self.shaftDir # extrudeDir is a numpy array boltHead = makePrismFromFace(aFace, extrudeDir) cylOrigin = self.origin boltCylinder = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.shaftDir)), self.r, self.H).Shape() whole_Bolt = BRepAlgoAPI_Fuse(boltHead,boltCylinder).Shape() return whole_Bolt
def make_cut_cylinder(solid, points): """ :param solid: тело в котором вырежаем цилиндр :param points: массив точек x, y, z, R, H :return: """ for p in points: x = p[0] y = p[1] z = p[2] R = p[3] H = p[4] pnt = gp_Pnt(x, y, z) dr = gp_Dir(1.0, 0.0, 0.0) ax = gp_Ax2(pnt, dr) my_cyl = BRepPrimAPI_MakeCylinder(ax, R, H).Shape() solid = BRepAlgo_Cut(solid.Shape(), my_cyl) return solid
def boolean_cut(base): # Create a cylinder cylinder_radius = 0.25 cylinder_height = 2.0 cylinder_origin = gp_Ax2(gp_Pnt(0.0, 0.0, - cylinder_height / 2.0), gp_Dir(0.0, 0.0, 1.0)) cylinder = BRepPrimAPI_MakeCylinder(cylinder_origin, cylinder_radius, cylinder_height) # Repeatedly move and subtract it from the input shape move = gp_Trsf() boolean_result = base clone_radius = 1.0 for clone in range(8): angle = clone * pi / 4.0 # Move the cylinder move.SetTranslation(gp_Vec(cos(angle) * clone_radius, sin(angle) * clone_radius, 0.0)) moved_cylinder = BRepBuilderAPI_Transform(cylinder.Shape(), move, True).Shape() # Subtract the moved cylinder from the drilled sphere boolean_result = BRepAlgoAPI_Cut(boolean_result, moved_cylinder).Shape() return boolean_result
def mode_drawing(self, widget=None): """ This is a stand-alone call to make a drafting-like drawing of the shape. It's better than HLR, because HLR shows creases at edges where shapes are tangent. If this must be a menu call, pop up a separate window for it. """ self.saved_projection = self.glarea.occ_view.ViewOrientation() # Graphic3d_Vertex vcenter = self.saved_projection.ViewReferencePoint() vout = self.saved_projection.ViewReferencePlane() # Graphic3d_Vector vup = self.saved_projection.ViewReferenceUp() # Graphic3d_Vector vout_gp = _gp.gp_Vec(vout.X(), vout.Y(), vout.Z()) vright = _gp.gp_Vec(vup.X(), vup.Y(), vup.Z()) vright.Cross(vout_gp) projection = _HLRAlgo_Projector( _gp.gp_Ax2(_gp.gp_Pnt(vcenter.X(), vcenter.Y(), vcenter.Z()), _gp.gp_Dir(vout.X(), vout.Y(), vout.Z()), _gp.gp_Dir(vright.X(), vright.Y(), vright.Z()))) hlr_algo = _HLRBRep_Algo() handle_hlr_algo = hlr_algo.GetHandle() for display_shape in self.display_shapes: hlr_algo.Add(display_shape['shape']) hlr_algo.Projector(projection) hlr_algo.Update() hlr_algo.Hide() hlr_toshape = _HLRBRep_HLRToShape(handle_hlr_algo) vcompound = hlr_toshape.VCompound() outlinevcompound = hlr_toshape.OutLineVCompound() self.clear(0) self.display(vcompound, color=display_shape['color'], line_type=display_shape['line_type'], line_width=display_shape['line_width'], logging=False) self.display(outlinevcompound, color=display_shape['color'], line_type=display_shape['line_type'], line_width=display_shape['line_width'], logging=False) self.viewstandard(viewtype='top')
def through_sections(): #ruled circle_1 = gp_Circ(gp_Ax2(gp_Pnt(-100., 0., -100.), gp_Dir(0., 0., 1.)), 40.) wire_1 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_1).Edge()).Wire() circle_2 = gp_Circ(gp_Ax2(gp_Pnt(-10., 0., -0.), gp_Dir(0., 0., 1.)), 40.) wire_2 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_2).Edge()).Wire() circle_3 = gp_Circ(gp_Ax2(gp_Pnt(-75., 0., 100.), gp_Dir(0., 0., 1.)), 40.) wire_3 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_3).Edge()).Wire() circle_4 = gp_Circ(gp_Ax2(gp_Pnt(0., 0., 200.), gp_Dir(0., 0., 1.)), 40.) wire_4 = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_4).Edge()).Wire() generatorA = BRepOffsetAPI_ThruSections(False, True) # the use of the map function fails at producing the ThruSection # on py3k. Why ? # map(generatorA.AddWire, [wire_1, wire_2, wire_3, wire_4]) # we have to use a loop for wir in [wire_1, wire_2, wire_3, wire_4]: generatorA.AddWire(wir) generatorA.Build() display.DisplayShape(generatorA.Shape()) #smooth circle_1b = gp_Circ(gp_Ax2(gp_Pnt(100., 0., -100.), gp_Dir(0., 0., 1.)), 40.) wire_1b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_1b).Edge()).Wire() circle_2b = gp_Circ(gp_Ax2(gp_Pnt(210., 0., -0.), gp_Dir(0., 0., 1.)), 40.) wire_2b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_2b).Edge()).Wire() circle_3b = gp_Circ(gp_Ax2(gp_Pnt(275., 0., 100.), gp_Dir(0., 0., 1.)), 40.) wire_3b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_3b).Edge()).Wire() circle_4b = gp_Circ(gp_Ax2(gp_Pnt(200., 0., 200.), gp_Dir(0., 0., 1.)), 40.) wire_4b = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(circle_4b).Edge()).Wire() generatorB = BRepOffsetAPI_ThruSections(True, False) # same here, the following line fails # map(generatorB.AddWire, [wire_1b, wire_2b, wire_3b, wire_4b]) for wir in [wire_1b, wire_2b, wire_3b, wire_4b]: generatorB.AddWire(wir) generatorB.Build() display.DisplayShape(generatorB.Shape(), update=True)
def mirror(self, **kwargs): """ params (one of): point def. mirror about point axis plane """ inValue=None if 'point' in kwargs: inValue = gp_Pnt(kwargs['point']) elif 'axis' in kwargs: inValue = gp_Ax1(gp_Pnt(*kwargs['axis'][:3]), gp_Dir(*kwargs['axis'][3:])) elif 'plane' in kwargs: inValue = gp_Ax2(gp_Pnt(*kwargs['axis'][:3]), gp_Dir(*kwargs['axis'][3:])) else: print "Warning: mirror could not be done becase there was no parameters specified" return self tr = gp_Trsf() tr.SetMirror(inValue) self.shape = BRepBuilderAPI_Transform(self.shape, tr).Shape() self.__extract_curves() return self
from OCC import TopoDS, BRep, STEPControl, BRepBuilderAPI app = wx.App() frame = wx.Frame(None, -1, "OCC frame", size=(600,700)) canvas = GraphicsCanva3D(frame) frame.Show() wx.SafeYield() canvas.Init3dViewer() viewer = canvas._3dDisplay print viewer box = BRepPrimAPI.BRepPrimAPI_MakeBox(20,30,40) ax = gp_Ax2() ax.Translate(gp_Vec(50,50,50)) cyl_len = 40 radius = 10 angle = pi*1.5 cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(radius, cyl_len) ax = gp_Ax3() ax.SetLocation(gp_Pnt(50,60,70)) ax.SetDirection(gp_Dir(gp_Vec(1,1,1))) trans = gp_Trsf() trans.SetTransformation(ax) t_cyl = BRepBuilderAPI.BRepBuilderAPI_Transform(cyl.Shape(), trans)
from OCC.gp import gp_Ax2, gp_Pnt, gp_Dir try: display.EraseAll() except Exception: print "Not interactive Viewer mode" # # Make the 1st cylinder # cyl1 = BRepPrimAPI_MakeCylinder(50, 200) cyl1_shape = cyl1.Shape() # # Make the 2nd cylinder # cyl2 = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(200, 200, 0), gp_Dir(0, 0, 1)), 40, 110, 210*math.pi/180) cyl2_shape = cyl2.Shape() try: display.DisplayShape(cyl2_shape) display.View_Iso() except Exception: print "Not interactive Viewer mode" from OCC.Display.SimpleGui import init_display display.DisplayShape(cyl2_shape, update=True) start_display() # # Export result to IGES file # # i = OCC.IGESControl_Controller()
def makeCircleWire(): circle = gp.gp_Circ(gp.gp_Ax2(gp.gp_Pnt(0,2,0),gp.gp().DZ()),.75); e2 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(circle, gp.gp_Pnt(0,1.25,0),gp.gp_Pnt(0,1.25,0)).Edge(); return Wrappers.wireFromEdges([e2]);
##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##pythonOCC is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##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 OCC.gp import gp_Dir, gp_Ax2, gp_Circ, gp_Pnt from OCC.AIS import AIS_Shape, AIS_RadiusDimension from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge from OCC.TCollection import TCollection_ExtendedString from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() c = gp_Circ(gp_Ax2(gp_Pnt(200., 200., 0.), gp_Dir(0., 0., 1.)), 80) ec = BRepBuilderAPI_MakeEdge(c).Edge() ais_shp = AIS_Shape(ec) display.Context.Display(ais_shp.GetHandle()) rd = AIS_RadiusDimension(ec) #rd.SetArrowSize(12) display.Context.Display(rd.GetHandle()) display.FitAll() start_display()
def variable_filleting(event=None): # Create Box Box = BRepPrimAPI_MakeBox(200, 200, 200).Shape() # Fillet Rake = BRepFilletAPI_MakeFillet(Box) ex = Topo(Box).edges() next(ex) next(ex) next(ex) Rake.Add(8, 50, next(ex)) Rake.Build() if Rake.IsDone(): evolvedBox = Rake.Shape() # 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 = Topo(Cylinder).edges() fillet.Add(TabPoint2, next(exp2)) fillet.Build() if fillet.IsDone(): LawEvolvedCylinder = fillet.Shape() display.DisplayShape(LawEvolvedCylinder) 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.0, 8.0) P2 = gp_Pnt2d(0.2, 16.0) P3 = gp_Pnt2d(0.4, 25.0) P4 = gp_Pnt2d(0.6, 55.0) P5 = gp_Pnt2d(0.8, 28.0) P6 = gp_Pnt2d(1.0, 20.0) 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 = Topo(Box2).edges() next(exp) next(exp) next(exp) afillet.Add(TabPoint, next(exp)) afillet.Build() if afillet.IsDone(): LawEvolvedBox = afillet.Shape() display.EraseAll() display.DisplayShape(Box) display.EraseAll() display.DisplayShape(evolvedBox) display.DisplayShape(LawEvolvedBox) display.FitAll()
def face(): p1 = gp_Pnt() p2 = gp_Pnt() p3 = gp_Pnt() p4 = gp_Pnt() p5 = gp_Pnt() p6 = gp_Pnt() # The white Face sphere = gp_Sphere(gp_Ax3(gp_Pnt(0, 0, 0), gp_Dir(1, 0, 0)), 150) green_face = BRepBuilderAPI_MakeFace(sphere, 0.1, 0.7, 0.2, 0.9) # The red face p1.SetCoord(-15, 200, 10) p2.SetCoord(5, 204, 0) p3.SetCoord(15, 200, 0) p4.SetCoord(-15, 20, 15) p5.SetCoord(-5, 20, 0) p6.SetCoord(15, 20, 35) array = TColgp_Array2OfPnt(1, 3, 1, 2) array.SetValue(1, 1, p1) array.SetValue(2, 1, p2) array.SetValue(3, 1, p3) array.SetValue(1, 2, p4) array.SetValue(2, 2, p5) array.SetValue(3, 2, p6) curve = GeomAPI_PointsToBSplineSurface(array, 3, 8, GeomAbs_C2, 0.001).Surface() red_face = BRepBuilderAPI_MakeFace(curve, 1e-6) #The brown face circle = gp_Circ(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(1, 0, 0)), 80) Edge1 = BRepBuilderAPI_MakeEdge(circle, 0, math.pi) Edge2 = BRepBuilderAPI_MakeEdge(gp_Pnt(0, 0, -80), gp_Pnt(0, -10, 40)) Edge3 = BRepBuilderAPI_MakeEdge(gp_Pnt(0, -10, 40), gp_Pnt(0, 0, 80)) ##TopoDS_Wire YellowWire MW1 = BRepBuilderAPI_MakeWire(Edge1.Edge(), Edge2.Edge(), Edge3.Edge()) if MW1.IsDone(): yellow_wire = MW1.Wire() brown_face = BRepBuilderAPI_MakeFace(yellow_wire) #The pink face p1.SetCoord(35, -200, 40) p2.SetCoord(50, -204, 30) p3.SetCoord(65, -200, 30) p4.SetCoord(35, -20, 45) p5.SetCoord(45, -20, 30) p6.SetCoord(65, -20, 65) array2 = TColgp_Array2OfPnt(1, 3, 1, 2) array2.SetValue(1, 1, p1) array2.SetValue(2, 1, p2) array2.SetValue(3, 1, p3) array2.SetValue(1, 2, p4) array2.SetValue(2, 2, p5) array2.SetValue(3, 2, p6) BSplineSurf = GeomAPI_PointsToBSplineSurface(array2, 3, 8, GeomAbs_C2, 0.001) aFace = BRepBuilderAPI_MakeFace(BSplineSurf.Surface(), 1e-6).Face() ## ##//2d lines P12d = gp_Pnt2d(0.9, 0.1) P22d = gp_Pnt2d(0.2, 0.7) P32d = gp_Pnt2d(0.02, 0.1) ## line1 = Geom2d_Line(P12d, gp_Dir2d((0.2-0.9), (0.7-0.1))) line2 = Geom2d_Line(P22d, gp_Dir2d((0.02-0.2), (0.1-0.7))) line3 = Geom2d_Line(P32d, gp_Dir2d((0.9-0.02), (0.1-0.1))) ## ##//Edges are on the BSpline surface Edge1 = BRepBuilderAPI_MakeEdge(line1.GetHandle(), BSplineSurf.Surface(), 0, P12d.Distance(P22d)).Edge() Edge2 = BRepBuilderAPI_MakeEdge(line2.GetHandle(), BSplineSurf.Surface(), 0, P22d.Distance(P32d)).Edge() Edge3 = BRepBuilderAPI_MakeEdge(line3.GetHandle(), BSplineSurf.Surface(), 0, P32d.Distance(P12d)).Edge() ## Wire1 = BRepBuilderAPI_MakeWire(Edge1, Edge2, Edge3).Wire() Wire1.Reverse() pink_face = BRepBuilderAPI_MakeFace(aFace, Wire1).Face() breplib_BuildCurves3d(pink_face) display.DisplayColoredShape(green_face.Face(), 'GREEN') display.DisplayColoredShape(red_face.Face(), 'RED') display.DisplayColoredShape(pink_face, Quantity_Color(Quantity_NOC_PINK)) display.DisplayColoredShape(brown_face.Face(), 'BLUE') display.DisplayColoredShape(yellow_wire, 'YELLOW', update=True)
def FuselageLongitudinalGuideCurves(self, NoseLengthRatio, TailLengthRatio): """Internal function. Defines the four longitudinal curves that outline the fuselage (outer mould line).""" FSVU, FSVL = self.AirlinerFuselageSideView(NoseLengthRatio, TailLengthRatio) FSVUCurve = act.points_to_BezierCurve(FSVU) FSVLCurve = act.points_to_BezierCurve(FSVL) AFPVPort, NoseEndX, TailStartX = \ self.AirlinerFuselagePlanView(NoseLengthRatio, TailLengthRatio) # Generate plan view PlanPortCurve = act.points_to_BezierCurve(AFPVPort).GetHandle() # TODO: How wide is the fuselage (use bounding box) # Note: THIS DOESNT WORK AS OCC BOUNDING BOX ROUTINES INCLUDE CURVE # POLES. MAY BE ABLE TO WORKAROUND WITH TRIANGULATION # H_PlanPortCurve = PlanPortCurve.GetHandle() # Get handle of curve # PP_Edge = act.make_edge(H_PlanPortCurve) # (Xmin,Ymin,Zmin,Xmax,Ymax,Zmax) = act.ObjectsExtents([PP_Edge]) (Xmin, Ymin, Zmin) = np.min(AFPVPort, axis=0) (Xmax, Ymax, Zmax) = np.max(AFPVPort, axis=0) # # TODO: Generate a (slightly wider) projection surface # > This is legacy from Rhino # FSVMeanCurve = rs.MeanCurve(FSVUCurve, FSVLCurve) # RuleLinePort = rs.AddLine((0,0,0),(0,-1.1*abs(Ymax-Ymin),0)) # FSVMCEP = rs.CurveEndPoint(FSVMeanCurve) # AftLoftEdgePort = rs.CopyObject(RuleLinePort, FSVMCEP) # ParallelLoftEdgePort = rs.CopyObject(FSVMeanCurve,(0,-1.1*abs(Ymax-Ymin),0)) # LSPort = rs.AddSweep2((FSVMeanCurve,ParallelLoftEdgePort ),(RuleLinePort, AftLoftEdgePort )) # # Mean Curve: This is wrong! But no mean curve found in OCC FSVMean = (FSVU+FSVL)/2. FSVMeanCurve = act.points_to_BezierCurve(FSVMean) FSVMeanEdge = act.make_edge(FSVMeanCurve.GetHandle()) self._MeanEdge = FSVMeanEdge RuleLinePort = act.make_edge(gp_Pnt(0., 0., 0.), gp_Pnt(0., -1.1*abs(Ymax-Ymin), 0.)) FSVMCEP = FSVMeanCurve.EndPoint() MoveVec = gp_Vec(gp_Pnt(0, 0, 0), FSVMCEP) AftLoftEdgePort = topods.Edge( act.translate_topods_from_vector(RuleLinePort, MoveVec, copy=True)) # Make copy of the mean curve MoveVec = gp_Vec(gp_Pnt(0, 0, 0), gp_Pnt(0, -1.1*abs(Ymax-Ymin), 0)) # Didnt need this to construct Port Edge of the loft surface in OCC # (Legacy from Rhino) - may experiment with this behaviour later: # ParallelLoftEdgePort = topods.Edge(act.translate_topods_from_vector( # FSVMeanEdge, # MoveVec, # copy=True)) # Mean Surface: # LSPort_edges = [RuleLinePort, FSVMeanEdge, AftLoftEdgePort, # ParallelLoftEdgePort] spine = act.make_wire(FSVMeanEdge) section1 = act.make_wire(RuleLinePort) section2 = act.make_wire(AftLoftEdgePort) # support = act.make_wire(ParallelLoftEdgePort) LSPort = act.make_pipe_shell(spine, [section1, section2]) self._LSPort = LSPort # # Project the plan view onto the mean surface HPortCurve = act.project_curve_to_surface(PlanPortCurve, LSPort, gp_Dir(0, 0, 100)) PortCurve = HPortCurve.GetObject() # # TODO: House-keeping # DeleteObjects([LSPort,PlanPortCurve,ParallelLoftEdgePort,RuleLinePort, # AftLoftEdgePort]) # TODO: Tidy up the mean curve # Tidy up the mean curve. This is necessary for a smooth result and # removing it can render the algorithm unstable. However, FitCurve # itself may sometimes be slightly unstable. # FLength = abs(Xmax-Xmin) # establish a reference length # PortCurveSimplified = rs.FitCurve(PortCurve, # distance_tolerance = FLength*0.001) # StarboardCurveSimplified = act.MirrorObjectXZ(PortCurveSimplified) # # rs.DeleteObject(PortCurve) # # # Compute the actual end points of the longitudinal curves # TODO: Compute end points of curves (Needed when mean curve has been # Changed by fitting curve) # (Xmin,Ymin,Zmin,Xmax1,Ymax,Zmax) =\ # act.ObjectsExtents(StarboardCurveSimplified) # (Xmin,Ymin,Zmin,Xmax2,Ymax,Zmax) =\ # act.ObjectsExtents(PortCurveSimplified) # (Xmin,Ymin,Zmin,Xmax3,Ymax,Zmax) = act.ObjectsExtents(FSVUCurve) # (Xmin,Ymin,Zmin,Xmax4,Ymax,Zmax) = act.ObjectsExtents(FSVLCurve) # EndX = min([Xmax1,Xmax2,Xmax3,Xmax4]) # PortCurveSimplified = PlanPortCurve # Seems easiest to mirror portcurve with handles? h = Handle_Geom_BSplineCurve() mirror_ax2 = gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 1, 0)) c = PortCurve.Copy() c.GetObject().Mirror(mirror_ax2) HStarboardCurve = h.DownCast(c) # StarboardCurve = hSt.GetObject() EndX = Xmax # This is not correct: just trying to get it working return (HStarboardCurve, HPortCurve, FSVUCurve, FSVLCurve, FSVMeanCurve, NoseEndX, TailStartX, EndX)
def BuildFuselageOML(self, Max_attempt=5): """Builds the Fuselage outer mould line Notes ----- It is not expected that users will interact with this directly. Use the Fuslage class initialisation fuction instead """ NetworkSrfSettings = np.array([[35, 20, 15, 15, 20], [35, 30, 15, 5, 20], [35, 20, 15, 2, 20], [30, 30, 15, 2, 20], [30, 20, 15, 2, 20], [25, 20, 15, 2, 20], [20, 20, 15, 2, 20], [15, 20, 15, 2, 20]]) HStarboardCurve, HPortCurve, FSVUCurve, FSVLCurve, FSVMeanCurve, \ NoseEndX, TailStartX, EndX = \ self.FuselageLongitudinalGuideCurves(self.NoseLengthRatio, self.TailLengthRatio) # Returning the PortCurve and StarboardCurve as Geom_BSplineCurve # makes kernel freeze in pythonocc 0.16.5, so needed to carry around a # handle instead - very strange bug! PortCurve = HPortCurve .GetObject() StarboardCurve = HStarboardCurve.GetObject() # Compute the stern point coordinates of the fuselage Pu = FSVUCurve.EndPoint() Pl = FSVLCurve.EndPoint() self.SternPoint = gp_Pnt(Pu.X(), Pu.Y(), 0.5*(Pu.Z()+Pl.Z())) Pu = FSVUCurve.StartPoint() Pl = FSVLCurve.StartPoint() self.BowPoint = gp_Pnt(Pu.X(), Pu.Y(), 0.5*(Pu.Z()+Pl.Z())) i_attempt = 0 while i_attempt < Max_attempt: i_attempt = i_attempt + 1 print("Surface fit attempt {}".format(i_attempt)) # Construct array of cross section definition frames SX0 = 0 SX1 = 0.04 * NoseEndX SX2 = SX1 + 0.25 * NoseEndX SX3 = NoseEndX SX4 = TailStartX SX5 = EndX Step01, Step12, Step23, Step34, Step45 = \ NetworkSrfSettings[i_attempt - 1] print("""Attempting loft surface fit with network density setup {}""".format(NetworkSrfSettings[i_attempt][:])) Stations01 = np.linspace(SX0, SX1, max([Step01, 2])) Stations12 = np.linspace(SX1, SX2, max([Step12, 2])) Stations23 = np.linspace(SX2, SX3, max([Step23, 2])) Stations34 = np.linspace(SX3, SX4, max([Step34, 2])) Stations45 = np.linspace(SX4, SX5, max([Step45, 2])) StationRange = np.hstack([Stations01[:-1], Stations12[:-1], Stations23[:-1], Stations34[:-1], Stations45]) C = [] FirstTime = True for i, XStation in enumerate(StationRange[1:]): # Create plane normal to x direction P = Geom_Plane(gp_Pln(gp_Pnt(XStation, 0, 0), gp_Dir(gp_Vec(1, 0, 0)))) # Make into a face for visualisation/debugging try: IPoint2 = act.points_from_intersection(P, FSVUCurve) IPoint3 = act.points_from_intersection(P, PortCurve) IPoint4 = act.points_from_intersection(P, FSVLCurve) IPoint1 = act.points_from_intersection(P, StarboardCurve) # IPointCentre = act.points_from_intersection(P, FSVMeanCurve) except RuntimeError: print("Intersection Points at Section X={} Not Found" .format(XStation)) print("Skipping this plane location") continue PseudoDiameter = abs(IPoint4.Z()-IPoint2.Z()) if self.CylindricalMidSection and\ NoseEndX < XStation < TailStartX: print("Enforcing circularity in the central section...") if FirstTime: PseudoRadius = PseudoDiameter / 2. FirstTime = False PseudoRadius = PseudoDiameter / 2. # Note: Add Circle with radius PseudoRadius at Pc from OCC.GC import GC_MakeCircle c = GC_MakeCircle(gp_Ax2(IPointCentre, gp_Dir(1, 0, 0)), PseudoRadius).Value() else: # Set the tangents at each point for interpolation: # assume that these are solely in 1 axis as points lie # extremities of an elliptical shape tangents = np.array([[0, -1, 0], [0, 0, -1], [0, 1, 0], [0, 0, 1]]) c = act.points_to_bspline( [IPoint2, IPoint3, IPoint4, IPoint1], periodic=True, scale=False, tangents=tangents) C.append(c) # Fit fuselage external surface sections = [act.make_wire(act.make_edge(curve)) for curve in C] guides = ([FSVUCurve.GetHandle(), PortCurve.GetHandle(), FSVLCurve.GetHandle(), StarboardCurve.GetHandle()]) guides = [act.make_wire(act.make_edge(guide)) for guide in guides] self._Lguides = guides self._Csections = sections self._NoseVertex = act.make_vertex(self.BowPoint) try: OMLSurf = \ act.AddSurfaceLoft(C, first_vertex=self._NoseVertex, continuity=GeomAbs_C2, solid=False) except: OMLSurf = None if OMLSurf is not None: print("Network surface fit succesful on attempt {}\n" .format(i_attempt)) self.AddComponent(OMLSurf, 'OML') return None # If all attempts at fitting a network surface failed, we attempt a # Pipe Shell: if OMLSurf is None: print("""Failed to fit network surface to the external shape of the fuselage""") print("""Attempting alternative fitting method, quality likely to be low...""") try: OMLSurf = act.make_pipe_shell(C) self.AddComponent(OMLSurf, 'OML') except: raise(RuntimeError, "Could not produce a valid OML surface") # Note: The following is the last resort surface fit from Rhino Airconics # And currently is not available in OCC Airconics: # SimplificationReqd = True # Enforce simplification # if not(FuselageOMLSurf): # print "Alternative fitting method failed too. Out of ideas." # # if FuselageOMLSurf and SimplificationReqd: # rs.UnselectAllObjects() # rs.SelectObject(FuselageOMLSurf) # ToleranceStr = str(0.0005*EndX) # print "Smoothing..." # rs.Command("FitSrf " + ToleranceStr) # rs.UnselectAllObjects() # # Shouldnt get here return None