def make_interp_parabola(FL, rmin, rmax, segments=50): A = 1./(4*FL) x = numpy.linspace(rmin, rmax, segments) y = (A * x**2) - FL points = [(X,0,Z) for X,Z in zip(x,y)] points.append((x[0],0,y[-1])) def pairs(itr): a,b = itertools.tee(itr) next(b) return zip(a,b) edges = (BRepBuilderAPI.BRepBuilderAPI_MakeEdge( gp.gp_Pnt(*p1), gp.gp_Pnt(*p2)) for p1, p2 in pairs(points)) last_edge = BRepBuilderAPI.BRepBuilderAPI_MakeEdge( gp.gp_Pnt(*points[-1]), gp.gp_Pnt(*points[0])) wire = BRepBuilderAPI.BRepBuilderAPI_MakeWire() for e in edges: wire.Add(e.Edge()) wire.Add(last_edge.Edge()) face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(wire.Wire()) ax = gp.gp_Ax1(gp.gp_Pnt(0,0,0), gp.gp_Dir(0,0,1)) revol = BRepPrimAPI.BRepPrimAPI_MakeRevol(face.Shape(), ax) return revol.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 point1[direction] = point0[direction] if (point1[direction-1] == point0[direction-1] or point1[direction-2] == point0[direction-2]): raise InvalidInputException points = [] # add two other corners: for i in range(3): if i != direction: point = point0[:] point[i] = point1[i] points.append(point) points.insert(1, point0) points.insert(3, point1) builder = BRepBuilderAPI.BRepBuilderAPI_MakePolygon() for pnt in points: builder.Add(gp.gp_Pnt(*pnt)) builder.Build() builder.Close() polygon = builder.Wire() face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(polygon).Face() self._final = [face] return self._final
def makeSection2(self, cuttingPlane, shapeToSection, zLevel): """ Uses BrepSection Algo. this generally returns a list of wires, not a face """ #section is certainly faster, but produces only edges. #those have to be re-organized into wires, probably #using ShapeAnalysis_WireOrder face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(cuttingPlane).Shape() # Computes Shape/Plane intersection section = BRepAlgoAPI.BRepAlgoAPI_Section(self.solid.shape, face) #section = BRepAlgo.BRepAlgo_Section(self.solid.shape,face); section.Build() if section.IsDone(): #Topology.dumpTopology(section.Shape()); #what we got back was a compound of edges t = Topo(section.Shape()) wb = OCCUtil.MultiWireBuilder() for e in t.edges(): wb.addEdge(e) wires = wb.getWires() print wires for w in wires: Topology.dumpTopology(w) return wires else: raise Exception("Could not compute Section!")
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 _makeSlice(self, shapeToSlice, zLevel): s = Slice() #used to determine if a slice is identical to others. s.hatchDir = self.hatchReversed s.fillWidth = self.options.filling.fillWidth #change if layers are variable thickness s.sliceHeight = self.options.layerHeight s.zLevel = zLevel #make a cutting plane p = gp.gp_Pnt(0, 0, zLevel) origin = gp.gp_Pnt(0, 0, zLevel - 1) csys = gp.gp_Ax3(p, gp.gp().DZ()) cuttingPlane = gp.gp_Pln(csys) bff = BRepBuilderAPI.BRepBuilderAPI_MakeFace(cuttingPlane) face = bff.Face() #odd, a halfspace is faster than a box? hs = BRepPrimAPI.BRepPrimAPI_MakeHalfSpace(face, origin) hs.Build() halfspace = hs.Solid() #make the cut bc = BRepAlgoAPI.BRepAlgoAPI_Cut(shapeToSlice, halfspace) cutShape = bc.Shape() foundFace = False for face in Topo(cutShape).faces(): if self._isAtZLevel(zLevel, face): foundFace = True log.debug("Face is at zlevel" + str(zLevel)) s.addFace(face) #TestDisplay.display.showShape(face); log.debug("Face" + str(face)) if self.options.useSliceFactoring: mySum = s.getCheckSum() #print 'Slice Created, Checksum is',mySum; for otherSlice in self.slices: #print "Slice Checksum=",otherSlice.getCheckSum(); if mySum == otherSlice.getCheckSum(): log.info( "This slice matches another one exactly. using that so we can save time." ) return otherSlice.copyToZ(zLevel) if not foundFace: log.warn("No faces found after slicing at zLevel " + str(zLevel) + " !. Skipping This layer completely") return None else: return s
def preview(self, input, direction): try: wire = TopoDS.TopoDS_wire(input) except RuntimeError: raise InvalidInputException face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(wire).Face() self.remove = [input] self._final = [face] return self._final
def faceFromWires(outer, innerWireList): "make a face from a set of wires" builder = BRepBuilderAPI.BRepBuilderAPI_MakeFace(outer) for w in innerWireList: builder.Add(w) builder.Build() f = builder.Face() #TestDisplay.display.showShape(f); return f
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 _makeSlice(self, shapeToSlice, zLevel): s = Slice() #change if layers are variable thickness s.sliceHeight = self.sliceHeight s.zLevel = zLevel #make a cutting plane p = gp.gp_Pnt(0, 0, zLevel) origin = gp.gp_Pnt(0, 0, zLevel - 1) csys = gp.gp_Ax3(p, gp.gp().DZ()) cuttingPlane = gp.gp_Pln(csys) bff = BRepBuilderAPI.BRepBuilderAPI_MakeFace(cuttingPlane) face = bff.Face() #odd, a halfspace is faster than a box? hs = BRepPrimAPI.BRepPrimAPI_MakeHalfSpace(face, origin) hs.Build() halfspace = hs.Solid() #make the cut bc = BRepAlgoAPI.BRepAlgoAPI_Cut(shapeToSlice, halfspace) cutShape = bc.Shape() #search the shape for faces at the specified zlevel texp = TopExp.TopExp_Explorer() texp.Init(cutShape, TopAbs.TopAbs_FACE) foundFace = False while (texp.More()): face = ts.Face(texp.Current()) if self._isAtZLevel(zLevel, face): foundFace = True logging.debug("Face is at zlevel" + str(zLevel)) s.addFace(face, self.saveSliceFaces) texp.Next() #free memory face.Nullify() bc.Destroy() texp.Clear() texp.Destroy() if not foundFace: logging.warn("No faces found after slicing at zLevel " + str(zLevel) + " !. Skipping This layer completely") return None else: return s
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 makeSection(self, cuttingPlane, shapeToSection, zLevel): """ Uses halfspaces to make a cut. """ bff = BRepBuilderAPI.BRepBuilderAPI_MakeFace(cuttingPlane) face = bff.Face() origin = gp.gp_Pnt(0, 0, zLevel - 1) #odd, a halfspace is faster than a box? hs = BRepPrimAPI.BRepPrimAPI_MakeHalfSpace(face, origin) hs.Build() halfspace = hs.Solid() #make the cut bc = BRepAlgoAPI.BRepAlgoAPI_Cut(self.solid.shape, halfspace) cutShape = bc.Shape() ff = [] for face in Topo(cutShape).faces(): if OCCUtil.isFaceAtZLevel(zLevel, face): ff.append(face) return ff
def make_ellipsoid_2(focus1, focus2, major_axis): f1 = numpy.asarray(focus1) f2 = numpy.asarray(focus2) direction = -(f1 - f2) centre = (f1 + f2)/2. sep = numpy.sqrt((direction**2).sum()) minor_axis = numpy.sqrt( major_axis**2 - (sep/2.)**2 ) el = gp.gp_Elips(gp.gp_Ax2(gp.gp_Pnt(0,0,0), gp.gp_Dir(1,0,0)), major_axis, minor_axis) edge1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(el, gp.gp_Pnt(0,0,major_axis), gp.gp_Pnt(0,0,-major_axis)) edge2 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(gp.gp_Pnt(0,0,-major_axis), gp.gp_Pnt(0,0,major_axis)) wire = BRepBuilderAPI.BRepBuilderAPI_MakeWire() wire.Add(edge1.Edge()) wire.Add(edge2.Edge()) face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(wire.Wire()) el = BRepPrimAPI.BRepPrimAPI_MakeRevol(face.Shape(), gp.gp_Ax1(gp.gp_Pnt(0,0,0), gp.gp_Dir(0,0,1)), numpy.pi*2) loc = gp.gp_Ax3() loc.SetLocation(gp.gp_Pnt(*centre)) loc.SetDirection(gp.gp_Dir(*direction)) tr = gp.gp_Trsf() tr.SetTransformation(loc, gp.gp_Ax3()) trans = BRepBuilderAPI.BRepBuilderAPI_Transform(el.Shape(), tr) shape = toshape(trans) return shape
def make_spherical_lens(CT, diameter, curvature, centre, direction, x_axis): cax = gp.gp_Ax2(gp.gp_Pnt(0,0,CT-curvature), gp.gp_Dir(0,1,0), gp.gp_Dir(1,0,0)) circ = Geom.Geom_Circle(cax, curvature) h_circ = Geom.Handle_Geom_Circle(circ) r = diameter/2. h2 = CT - curvature + numpy.sqrt(curvature**2 - r**2) p1 = gp.gp_Pnt(0,0,CT) p2 = gp.gp_Pnt(r,0,h2) p3 = gp.gp_Pnt(r,0,0) p4 = gp.gp_Pnt(0,0,0) #ps = p1,p2,p3,p4 #vs = [BRepBuilderAPI.BRepBuilderAPI_MakeVertex(p) for p in ps] e1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(h_circ, p1, p2) e2 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(p2,p3) e3 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(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)
#!/usr/bin/python # coding: utf-8 r"""test_revolve.py" """ from math import pi from OCC import BRepPrimAPI, gp, BRepBuilderAPI from viewer import view el = gp.gp_Elips(gp.gp_Ax2(gp.gp_Pnt(0, 0, 0), gp.gp_Dir(1, 0, 0)), 20.0, 10.0) edge1 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(el, gp.gp_Pnt(0, 0, 20), gp.gp_Pnt(0, 0, -20)) edge2 = BRepBuilderAPI.BRepBuilderAPI_MakeEdge(gp.gp_Pnt(0, 0, -20), gp.gp_Pnt(0, 0, 20)) wire = BRepBuilderAPI.BRepBuilderAPI_MakeWire() wire.Add(edge1.Edge()) wire.Add(edge2.Edge()) face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(wire.Wire()) el = BRepPrimAPI.BRepPrimAPI_MakeRevol( face.Shape(), gp.gp_Ax1(gp.gp_Pnt(0, 0, 0), gp.gp_Dir(0, 0, 1)), pi * 2) # h_curve = Geom.Handle_Geom_Ellipse(curve) # rev = BRepPrimAPI.BRepPrimAPI_MakeRevolution(h_curve, 120.1) # #nurbs = BRepBuilderAPI.BRepBuilderAPI_NurbsConvert(rev.Shape()) view(el.Shape())
def makeHeartFaceNoHole(): hw = makeHeartWire() builder = BRepBuilderAPI.BRepBuilderAPI_MakeFace(hw, True) return builder.Face()
h_sph = Geom.Handle_Geom_SphericalSurface(sph) c_rad = 12.0 #cylinder radius cyl = Geom.Geom_CylindricalSurface(gp.gp_Ax3(), c_rad) h_cyl = Geom.Handle_Geom_CylindricalSurface(cyl) intersect = GeomAPI.GeomAPI_IntSS(h_sph, h_cyl, 1e-7) edges = (BRepBuilderAPI.BRepBuilderAPI_MakeEdge(intersect.Line(i)) for i in xrange(1, intersect.NbLines() + 1)) wires = [BRepBuilderAPI.BRepBuilderAPI_MakeWire(e.Edge()) for e in edges] g_sph = gp.gp_Sphere(gp.gp_Ax3(), radius) faces = [BRepBuilderAPI.BRepBuilderAPI_MakeFace(w.Wire()) for w in wires] cyl_face = BRepBuilderAPI.BRepBuilderAPI_MakeFace(h_cyl) #cyl_face.Add(wires[0].Wire()) #cyl_face.Add(wires[1].Wire()) #shell = TopoDS.TopoDS_Shell() shell_builder = BRep.BRep_Builder() #shell_builder.MakeShell(shell) #shell_builder.Add(shell, cyl_face.Shape()) #for f in faces: # shell_builder.Add(shell, f.Shape()) #sewing = BRepBuilderAPI.BRepBuilderAPI_Sewing() #sewing.Add(shell) #sewing.Perform()
def makeHeartFace(): hw = makeHeartWire() circle = makeCircleWire2() builder = BRepBuilderAPI.BRepBuilderAPI_MakeFace(hw, True) builder.Add(circle) return builder.Face()