def OrientShape(self): # Intended to be called at end of CreateShape # Shape is expected to be in default position and orientation T = gp_Trsf() u = self.orientation.u v = self.orientation.v w = self.orientation.w #T.SetValues(u[0],u[1],u[2],0,v[0],v[1],v[2],0,w[0],w[1],w[2],0) T.SetValues(u[0],v[0],w[0],0,u[1],v[1],w[1],0,u[2],v[2],w[2],0) self.occ_shape = occbuild.BRepBuilderAPI_Transform(self.occ_shape,T,False).Shape() T.SetTranslation(gp_Vec(self.center[0],self.center[1],self.center[2])) self.occ_shape = occbuild.BRepBuilderAPI_Transform(self.occ_shape,T,False).Shape()
def copyToZ(self, z, layerNo): "makes a copy of this slice, transformed to the specified z height" theCopy = Slice() theCopy.zLevel = z theCopy.fillAngle = self.fillAngle theCopy.layerNo = layerNo theCopy.thickness = self.thickness #make transformation p1 = gp.gp_Pnt(0, 0, 0) p2 = gp.gp_Pnt(0, 0, z) xform = gp.gp_Trsf() xform.SetTranslation(p1, p2) bt = BRepBuilderAPI.BRepBuilderAPI_Transform(xform) #copy all of the faces for f in self.faces: bt.Perform(f.face, True) newFace = Face(OCCUtil.cast(bt.Shape())) #copy shells for shell in f.shellWires: #print shell bt.Perform(shell, True) newFace.shellWires.append(OCCUtil.cast(bt.Shape())) #copy fillWires for fill in f.fillWires: bt.Perform(fill, True) newFace.shellWires.append(OCCUtil.cast(bt.Shape())) theCopy.addFace(newFace) return theCopy
def copyToZ(self, z): "makes a copy of this slice, transformed to the specified z height" theCopy = Slice() theCopy.zLevel = z theCopy.zHeight = self.zHeight theCopy.sliceHeight = self.sliceHeight theCopy.fillWidth = self.fillWidth theCopy.hatchDir = self.hatchDir theCopy.checkSum = self.checkSum #make transformation p1 = gp.gp_Pnt(0, 0, 0) p2 = gp.gp_Pnt(0, 0, z) xform = gp.gp_Trsf() xform.SetTranslation(p1, p2) bt = BRepBuilderAPI.BRepBuilderAPI_Transform(xform) #copy all of the faces for f in hSeqIterator(self.faces): bt.Perform(f, True) theCopy.addFace(Wrappers.cast(bt.Shape())) #copy all of the fillWires for w in hSeqIterator(self.fillWires): bt.Perform(w, True) #TestDisplay.display.showShape(bt.Shape() ); theCopy.fillWires.Append(Wrappers.cast(bt.Shape())) #copy all of the fillEdges for e in hSeqIterator(self.fillEdges): bt.Perform(e, True) #TestDisplay.display.showShape(bt.Shape() ); theCopy.fillEdges.Append(Wrappers.cast(bt.Shape())) return theCopy
def make_ellipsoid(focus1, focus2, major_axis): """ @param focus1: length 3 sequence giving first focus location @param focus2: length 3 sequence giving second focus location @param path_length: major axis length """ 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 ) sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(minor_axis) scale = gp.gp_GTrsf() scale.SetValue(3,3, major_axis/minor_axis) ellipse = BRepBuilderAPI.BRepBuilderAPI_GTransform(sphere.Shape(), scale) 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(ellipse.Shape(), tr) shape = toshape(trans) return shape
def CreateShape(self): outer = occprim.BRepPrimAPI_MakeCylinder(self.outer_radius,self.length).Shape() inner = occprim.BRepPrimAPI_MakeCylinder(self.inner_radius,self.length).Shape() self.occ_shape = occalgo.BRepAlgoAPI_Cut(outer,inner).Shape() T = gp_Trsf() T.SetTranslation(gp_Vec(0,0,-self.length/2)) self.occ_shape = occbuild.BRepBuilderAPI_Transform(self.occ_shape,T,False).Shape() self.OrientShape()
def CreateShape(self): self.occ_shape = occprim.BRepPrimAPI_MakeCylinder( self.radius, self.length).Shape() T = gp_Trsf() T.SetTranslation(gp_Vec(0, 0, -self.length / 2)) self.occ_shape = occbuild.BRepBuilderAPI_Transform( self.occ_shape, T, False).Shape() self.OrientShape()
def CreateShape(self): endcap1 = occprim.BRepPrimAPI_MakeSphere(gp_Pnt(0,0,0),self.radius).Shape() endcap2 = occprim.BRepPrimAPI_MakeSphere(gp_Pnt(0,0,self.length),self.radius).Shape() body = occprim.BRepPrimAPI_MakeCylinder(self.radius,self.length).Shape() self.occ_shape = occalgo.BRepAlgoAPI_Fuse(endcap1,body).Shape() self.occ_shape = occalgo.BRepAlgoAPI_Fuse(self.occ_shape,endcap2).Shape() T = gp_Trsf() T.SetTranslation(gp_Vec(0,0,-self.length/2)) self.occ_shape = occbuild.BRepBuilderAPI_Transform(self.occ_shape,T,False).Shape() self.OrientShape()
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 make_cylinder_2(start, end, radius): start = numpy.asarray(start) end = numpy.asarray(end) direction = end-start length = numpy.sqrt((direction**2).sum()) cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(radius, length) ax = gp.gp_Ax3(gp.gp_Pnt(*start), gp.gp_Dir(*direction)) trans = gp.gp_Trsf() trans.SetTransformation(ax, gp.gp_Ax3()) t_cyl = BRepBuilderAPI.BRepBuilderAPI_Transform(cyl.Shape(), trans) return toshape(t_cyl)
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 makeHexArray(self, bottomLeftCenter, countX, countY): """ makes an array of hexagons bottomLeftCenter is the center of the top left hex, as a three-element tuple countX is the number of hexes in the x direction countY is the number of hexes in the y direction returns a list of wires representing a hexagon fill pattern """ pattern = self.makePeriodic(bottomLeftCenter) wireBuilder = BRepBuilderAPI.BRepBuilderAPI_MakeWire(pattern) #make horizontal array tsf = gp.gp_Trsf() pDist = 2.0 * self.cartesianSpacing()[0] tsf.SetTranslation(gp.gp_Pnt(0, 0, 0), gp.gp_Pnt(pDist, 0, 0)) tx = BRepBuilderAPI.BRepBuilderAPI_Transform(tsf) currentShape = pattern for i in range(1, int((countX / 2) + 1)): tx.Perform(currentShape, False) currentShape = tx.Shape() #display.DisplayShape(currentShape); wireBuilder.Add(Wrappers.cast(currentShape)) #create an array by alternately offsetting one cell right and #moving down topHalf = wireBuilder.Wire() #topHalf= approximatedWire(topHalf); wires = [] wires.append(topHalf) dY = self.cartesianSpacing()[1] / 2.0 dX = self.cartesianSpacing()[0] ####TODO// performance note. This method takes about 31ms to compute 1000x1000 hex. # pretty good, except that nearly 50% of the time is spent in makeTransform!!! # a much better method would be to use the same transform object somehow for i in range(1, int(countY * 2)): if i % 2 == 0: t = makeTransform(0, dY * i, 0) else: t = makeTransform(dX, dY * i, 0) t.Perform(topHalf, False) w = Wrappers.cast(t.Shape()) #approximate the wire #wires.append ( approximatedWire(w)); wires.append(w) #display.DisplayShape(wires); return wires
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 translateToPositiveCenter(self, xCenter, yCenter): xT = xCenter - self.xMin + (self.xDim / 2.0) yT = yCenter - self.yMin + (self.yDim / 2.0) zT = self.zMin * (-1.0) xCenter = abs(self.xMin) y = abs(self.yMin) z = abs(self.zMin) p1 = gp.gp_Pnt(0, 0, 0) p2 = gp.gp_Pnt(xT, yT, zT) xform = gp.gp_Trsf() xform.SetTranslation(p1, p2) bt = BRepBuilderAPI.BRepBuilderAPI_Transform(xform) bt.Perform(self.shape, False) return Solid(bt.Shape())
def translateToPositiveSpace(self): if self.xMin < 0 or self.yMin < 0 or self.zMin < 0: log.debug("Shape appears to be in negative space. Translating...") x = abs(self.xMin) y = abs(self.yMin) z = abs(self.zMin) p1 = gp.gp_Pnt(0, 0, 0) p2 = gp.gp_Pnt(x, y, z) xform = gp.gp_Trsf() xform.SetTranslation(p1, p2) log.info("Translating shape by x=%0.3f,y=%0.3f,z=%0.3f" % (x, y, z)) bt = BRepBuilderAPI.BRepBuilderAPI_Transform(xform) bt.Perform(self.shape, False) return SolidAnalyzer(bt.Shape()) else: log.debug("Translation is not required. Returning existing shape") return self
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
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) viewer.DisplayShape(box.Shape()) viewer.DisplayShape(t_cyl.Shape()) app.MainLoop() ##Building the resulting compund #aRes = TopoDS.TopoDS_Compound() #aBuilder = BRep.BRep_Builder() #aBuilder.MakeCompound(aRes) #aBuilder.Add(aRes, box.Shape()) #aBuilder.Add(aRes, cyl.Shape()) # Export to STEP step_export = STEPControl.STEPControl_Writer()
def makeTransform(x=0, y=0, z=0): "make a simple translation" t = gp.gp_Trsf() t.SetTranslation(gp.gp_Pnt(0, 0, 0), gp.gp_Pnt(x, y, z)) tt = BRepBuilderAPI.BRepBuilderAPI_Transform(t) return tt
def make_sphere(position, radius): sph = BRepPrimAPI.BRepPrimAPI_MakeSphere(radius) trans = gp.gp_Trsf() trans.SetTranslation(gp.gp_Vec(*position)) t_sph = BRepBuilderAPI.BRepBuilderAPI_Transform(sph.Shape(), trans) return toshape(t_sph)