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_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 drillWithHoles(shape): "returns a shape with a bunch of spheres removed from it" box = Bnd.Bnd_Box() b = BRepBndLib.BRepBndLib() b.Add(shape, box) cShape = shape (xMin, yMin, zMin, xMax, yMax, zMax) = box.Get() d = 0.05 * ((xMax - xMin)) di = 4.0 * d c = 0 #drill holes in a rectangular grid for x in frange6(xMin, xMax, di): for y in frange6(yMin, yMax, di): for z in frange6(zMin, zMax, di): c += 1 print "Inter %d " % c #make a sphere center = gp.gp_Pnt(x, y, z) hole = BRepPrimAPI.BRepPrimAPI_MakeSphere(center, d).Shape() cut = BRepAlgoAPI.BRepAlgoAPI_Cut(cShape, hole) cut.SetOperation(3) #3 is cut21 tmp = cut.Shape() #store the newly cut shape if cut.ErrorStatus() != 0: print "Error %d cutting" % cut.ErrorStatus() else: print "Success!" cShape = tmp return cShape
def doc_demo_(): from doc import doc_ctrl from OCC import BRepPrimAPI quader = BRepPrimAPI.BRepPrimAPI_MakeBox(3, 4, 5).Solid() kugel = BRepPrimAPI.BRepPrimAPI_MakeSphere(2).Solid() with doc_ctrl.open_command(): doc_ctrl.add(quader) doc_ctrl.set_color(quader, [0, 1, 0]) with doc_ctrl.open_command(): doc_ctrl.add(kugel) doc_ctrl.set_color(kugel, [1, 0, 0])
def test_step_exporter_overwrite(box_shape): r"""Happy path with a subclass of TopoDS_Shape""" filename = path_from_file(__file__, "./models_out/box.stp") exporter = StepExporter(filename) solid = shape_to_topology(box_shape) assert isinstance(solid, TopoDS.TopoDS_Solid) exporter.add_shape(solid) exporter.write_file() initial_timestamp = os.path.getmtime(filename) assert os.path.isfile(filename) # read the written box.stp importer = StepImporter(filename) topo_compound = Topo(importer.compound) assert topo_compound.number_of_faces() == 6 assert len([i for i in topo_compound.faces()]) == 6 assert topo_compound.number_of_edges() == 12 # add a sphere and write again with same exporter sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(10) exporter.add_shape(sphere.Shape()) exporter.write_file() # this creates a file with a box and a sphere intermediate_timestamp = os.path.getmtime(filename) assert intermediate_timestamp >= initial_timestamp # check that the file contains the box and the sphere importer = StepImporter(filename) assert len([i for i in Topo(importer.compound).faces()]) == 7 # 6 from box + 1 from sphere assert len([i for i in Topo(importer.compound).solids()]) == 2 # create a new exporter and overwrite with a box only filename = path_from_file(__file__, "./models_out/box.stp") exporter = StepExporter(filename) solid = shape_to_topology(box_shape) exporter.add_shape(solid) exporter.write_file() assert os.path.isfile(filename) last_timestamp = os.path.getmtime(filename) assert last_timestamp >= intermediate_timestamp # check the file only contains a box importer = StepImporter(filename) assert len([i for i in Topo(importer.compound).faces()]) == 6 # 6 from box assert len([i for i in Topo(importer.compound).solids()]) == 1
def test_iges_exporter_overwrite(box_shape): r"""Happy path with a subclass of TopoDS_Shape""" filename = path_from_file(__file__, "./models_out/box.igs") exporter = IgesExporter(filename) solid = shape_to_topology(box_shape) assert isinstance(solid, TopoDS.TopoDS_Solid) exporter.add_shape(solid) exporter.write_file() assert os.path.isfile(filename) # read the written box.igs importer = IgesImporter(filename) topo_compound = Topo(importer.compound) assert topo_compound.number_of_faces() == 6 assert topo_compound.number_of_edges() == 24 # add a sphere and write again with same exporter sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(10) exporter.add_shape(sphere.Shape()) exporter.write_file() # this creates a file with a box and a sphere # check that the file contains the box and the sphere importer = IgesImporter(filename) topo_compound = Topo(importer.compound) assert topo_compound.number_of_faces() == 7 # 6 from box + 1 from sphere # create a new exporter and overwrite with a box only filename = path_from_file(__file__, "./models_out/box.igs") exporter = IgesExporter(filename) solid = shape_to_topology(box_shape) exporter.add_shape(solid) exporter.write_file() assert os.path.isfile(filename) # check the file only contains a box importer = IgesImporter(filename) topo_compound = Topo(importer.compound) assert topo_compound.number_of_faces() == 6 # 6 from box
def drillWithHolesFaster(shape): "returns a shape with a bunch of spheres removed from it" box = Bnd.Bnd_Box() b = BRepBndLib.BRepBndLib() b.Add(shape, box) (xMin, yMin, zMin, xMax, yMax, zMax) = box.Get() d = 0.05 * ((xMax - xMin)) di = 3.0 * d vec = gp.gp_Vec(gp.gp_Pnt(0, 0, 0), gp.gp_Pnt(0, 0, d)) cp = None compound = TopoDS.TopoDS_Compound() builder = BRep.BRep_Builder() builder.MakeCompound(compound) #drill holes in a rectangular grid for x in frange6(xMin, xMax, di): for y in frange6(yMin, yMax, di): for z in frange6(zMin, zMax, di): #make a sphere center = gp.gp_Pnt(x, y, z) hole = BRepPrimAPI.BRepPrimAPI_MakeSphere(center, d).Shape() #lets see if a square hole is faster! #w = squareWire(center,d ); #hb = BRepPrimAPI.BRepPrimAPI_MakePrism(w,vec,False,True); #hb.Build(); #hole = hb.Shape(); builder.Add(compound, hole) display.DisplayShape(compound) q = time.clock() cut = BRepAlgoAPI.BRepAlgoAPI_Cut(shape, compound) if cut.ErrorStatus() == 0: print "Cut Took %0.3f sec." % (time.clock() - q) return cut.Shape() else: print "Error Cutting: %d" % cut.ErrorStatus() return shape
def test_stl_exporter_overwrite(box_shape): r"""Happy path with a subclass of TopoDS_Shape""" filename = path_from_file(__file__, "./models_out/box.stl") exporter = StlExporter(filename) solid = shape_to_topology(box_shape) assert isinstance(solid, TopoDS.TopoDS_Solid) exporter.set_shape(solid) exporter.write_file() assert os.path.isfile(filename) # read the written box.stl importer = StlImporter(filename) topo = Topo(importer.shape) assert topo.number_of_shells() == 1 # set a sphere and write again with same exporter sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(10) exporter.set_shape(sphere.Shape()) exporter.write_file( ) # this creates a file with a sphere only, this is STL specific # check that the file contains the sphere only importer = StlImporter(filename) topo = Topo(importer.shape) assert topo.number_of_shells() == 1 # create a new exporter and overwrite with a box only filename = path_from_file(__file__, "./models_out/box.stl") exporter = StlExporter(filename) solid = shape_to_topology(box_shape) exporter.set_shape(solid) exporter.write_file() assert os.path.isfile(filename) # check the file only contains a box importer = StlImporter(filename) topo = Topo(importer.shape) assert topo.number_of_shells() == 1
def execute(self): pt = gp.gp_Pnt(*self.position) R = self.radius sph = BRepPrimAPI.BRepPrimAPI_MakeSphere(pt, R) return sph.Shape()
def CreateShape(self): self.occ_shape = occprim.BRepPrimAPI_MakeSphere( gp_Pnt(0, 0, 0), self.radius).Shape() self.OrientShape()
from viewer import view from OCC import BRepPrimAPI, BRepBuilderAPI, gp, Geom, STEPControl R = 50. sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(R) def scale(sx, sy, sz): t = gp.gp_GTrsf() t.SetValue(1, 1, sx) t.SetValue(2, 2, sy) t.SetValue(3, 3, sz) return t trans = BRepBuilderAPI.BRepBuilderAPI_GTransform(sphere.Shape(), scale(1, 1, 2)) step_export = STEPControl.STEPControl_Writer() step_export.Transfer(trans.Shape(), STEPControl.STEPControl_AsIs) step_export.Write("/home/bryan/test_ellipse.stp") #el = gp.gp_Elips(gp.gp_Ax2(gp.gp_Pnt(0,0,0), gp.gp_Dir(1,0,0)), 10, 5) #EL = Geom.Geom_Ellipse(el) #handle = Geom.Handle_Geom_Ellipse(EL) #revolve = BRepPrimAPI.BRepPrimAPI_MakeRevolution(handle, 180) #nurbs = #view(revolve.Shape())
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)
#!/usr/bin/env python # coding: utf-8 r"""Exporting multiple shapes to IGES""" import logging from OCC import BRepPrimAPI from OCCDataExchange.iges import IgesExporter from OCCDataExchange.utils import path_from_file logging.basicConfig( level=logging.DEBUG, format= '%(asctime)s :: %(levelname)6s :: %(module)20s :: %(lineno)3d :: %(message)s' ) # First create a simple shape to export box_shape = BRepPrimAPI.BRepPrimAPI_MakeBox(50, 50, 50).Shape() sphere_shape = BRepPrimAPI.BRepPrimAPI_MakeSphere(20).Shape() # Export to IGES filename = path_from_file(__file__, "./models_out/result_export_multi.iges") my_iges_exporter = IgesExporter(filename, format="5.3") my_iges_exporter.add_shape(box_shape) my_iges_exporter.add_shape(sphere_shape) my_iges_exporter.write_file()
def sphere1_(): import random shape = BRepPrimAPI.BRepPrimAPI_MakeSphere( gp_.gp_Pnt_([0, 0, random.random() * 7]), 1).Shape() doc.doc_ctrl.add(shape)