def test_export_to_x3d(self): """ 3rd test : export a sphere to X3D file format """ a_sphere = BRepPrimAPI_MakeSphere(10.).Shape() tess = Tesselator(a_sphere) tess.Compute() tess.ExportShapeToX3D(os.path.join("test_io", "sphere.x3d")) self.assert_(os.path.exists(os.path.join("test_io", "sphere.x3d")))
def create_AirconicsShape(): """Populates a basic airconics shape with a unit cube striding by 1 in x y and z from the origin, and a unit sphere centered at the origin""" cube = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 1, 1, 1).Shape() sphere = BRepPrimAPI_MakeSphere(gp_Pnt(0, 0, 0), 1).Shape() shape = AirconicsShape(components={'cube': cube, 'sphere': sphere}) return shape
def test_creat_face(self): # create a box my_face = Face(BRepPrimAPI_MakeSphere(1., 1.).Face()) assert not my_face.IsNull() assert my_face.tolerance == 1e-06 assert not my_face.is_planar() assert my_face.is_trimmed()
def dbgSphere(self, pt): """ TODO : know why sphere is appended to the model, if no reason than remove sphere from all the cad files (by Anand Swaroop) :param pt: pt of origin for the nut bol placement :return: returns the sphere """ return BRepPrimAPI_MakeSphere(getGpPt(pt), 0.1).Shape()
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 slicer(event=None): # Param Zmin, Zmax, deltaZ = -100, 100, 5 # Note: the shape can also come from a shape selected from InteractiveViewer if 'display' in dir(): shape = display.GetSelectedShape() else: # Create the shape to slice shape = BRepPrimAPI_MakeSphere(60.).Shape() # Define the direction D = gp_Dir(0., 0., 1.) # the z direction # Perform slice sections = [] init_time = time.time() # for total time computation for z in range(Zmin, Zmax, deltaZ): # Create Plane defined by a point and the perpendicular direction P = gp_Pnt(0, 0, z) Pln = gp_Pln(P, D) face = BRepBuilderAPI_MakeFace(Pln).Shape() # Computes Shape/Plane intersection section_shp = BRepAlgoAPI_Section(shape, face) if section_shp.IsDone(): sections.append(section_shp) total_time = time.time() - init_time print("%.3fs necessary to perform slice." % total_time) display.EraseAll() display.DisplayShape(shape) for section_ in sections: display.DisplayShape(section_.Shape()) display.FitAll()
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 create_shape(self): d = self.declaration args = [d.axis, d.radius] #: Ugly... if d.angle: args.append(d.angle) if d.angle2: args.append(d.angle2) if d.angle3: args.append(d.angle3) self.shape = BRepPrimAPI_MakeSphere(*args)
def preview(self, inp, direction): if self.step == 0: self.previous_data = [inp] return [BRepBuilderAPI.BRepBuilderAPI_MakeVertex(inp).Vertex()] elif self.step == 1: center = self.previous_data[0] radius = (center - inp).length() if not radius: raise InvalidInputException self._final = [BRepPrimAPI_MakeSphere(center, radius).Solid()] return self._final
def cut(event=None): # Create Box Box = BRepPrimAPI_MakeBox(200, 60, 60).Shape() # Create Sphere Sphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape() # Cut: the shere is cut 'by' the box Cut = BRepAlgoAPI_Cut(Sphere, Box).Shape() display.EraseAll() ais_box = display.DisplayShape(Box) display.Context.SetTransparency(ais_box, 0.8) display.DisplayShape(Cut) display.FitAll()
def testTopoDS_ShapeOperators(self): shape_1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() shape_2 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() shape_3 = BRepPrimAPI_MakeSphere(20).Shape() self.assertFalse(shape_1 == shape_2) self.assertFalse(shape_2 == shape_3) self.assertFalse(shape_3 == shape_1) self.assertFalse(shape_2 == "some_string") self.assertTrue(shape_1 != shape_2) self.assertTrue(shape_2 != shape_3) self.assertTrue(shape_3 != shape_1) self.assertTrue(shape_2 != "some_string")
def test_calculate_bb_dimensions_triangulate(self): a = gp_Pnt(-1, -1, -1) b = gp_Pnt(3, 3, 3) box = BRepPrimAPI_MakeBox(a, b).Shape() sphere = BRepPrimAPI_MakeSphere(3).Shape() section = BRepAlgoAPI_Cut(box, sphere).Shape() params = ffdp.FFDParameters() xyz_min, xyz_max = params._calculate_bb_dimension(section, triangulate=True) correct_min = -1 * np.ones(3) correct_max = 3 * np.ones(3) np.testing.assert_almost_equal(xyz_min, correct_min, decimal=1) np.testing.assert_almost_equal(xyz_max, correct_max, decimal=1)
def makeSphere(cls, radius, pnt=Vector(0, 0, 0), dir=Vector(0, 0, 1), angleDegrees1=0, angleDegrees2=90, angleDegrees3=360): """ Make a sphere with a given radius By default pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=0, angle2=90 and angle3=360 """ return cls( BRepPrimAPI_MakeSphere(gp_Ax2(pnt.toPnt(), dir.toDir()), radius, angleDegrees1 * DEG2RAD, angleDegrees2 * DEG2RAD, angleDegrees3 * DEG2RAD).Shape())
def section(event=None): Torus = BRepPrimAPI_MakeTorus(120, 20).Shape() radius = 120.0 sections = [] for i in range(-3, 4): # Create Sphere Sphere = BRepPrimAPI_MakeSphere(gp_Pnt(26 * 3 * i, 0, 0), radius).Shape() # Computes Torus/Sphere section section = BRepAlgoAPI_Section(Torus, Sphere, False) section.ComputePCurveOn1(True) section.Approximation(True) section.Build() sections.append(section) display.EraseAll() display.DisplayShape(Torus) for section in sections: display.DisplayShape(section.Shape()) display.FitAll()
def test_with_sphere(): r"""Test the bounding box workaround with a sphere""" radius = 10.0 sphere = BRepPrimAPI_MakeSphere(radius).Shape() bb = BoundingBox(sphere) increment = 0.01 bbw_x_min = real_bb_position("X", "MIN", bb.x_min, sphere, increment=increment) bbw_x_max = real_bb_position("X", "MAX", bb.x_max, sphere, increment=increment) bbw_y_min = real_bb_position("Y", "MIN", bb.y_min, sphere, increment=increment) bbw_y_max = real_bb_position("Y", "MAX", bb.y_max, sphere, increment=increment) bbw_z_min = real_bb_position("Z", "MIN", bb.z_min, sphere, increment=increment) bbw_z_max = real_bb_position("Z", "MAX", bb.z_max, sphere, increment=increment) assert bb.x_min < -radius assert bb.x_max > radius assert bb.y_min < -radius assert bb.y_max > radius assert bb.z_min < -radius assert bb.z_max > radius assert bbw_x_min <= - radius assert abs(bbw_x_min - (-radius)) <= increment assert bbw_x_max >= radius assert abs(bbw_x_max - radius) <= increment assert bbw_y_min <= - radius assert abs(bbw_y_min - (-radius)) <= increment assert bbw_y_max >= radius assert abs(bbw_y_max - radius) <= increment assert bbw_z_min <= - radius assert abs(bbw_z_min - (-radius)) <= increment assert bbw_z_max >= radius assert abs(bbw_z_max - radius) <= increment
def simple_mesh(): # # Create the shape # shape = BRepPrimAPI_MakeBox(200, 200, 200).Shape() theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape() theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape() shape = BRepAlgoAPI_Fuse(theSphere, theBox).Shape() # # Mesh the shape # BRepMesh_IncrementalMesh(shape, 0.8) builder = BRep_Builder() comp = TopoDS_Compound() builder.MakeCompound(comp) bt = BRep_Tool() ex = TopExp_Explorer(shape, TopAbs_FACE) while ex.More(): face = topods_Face(ex.Current()) location = TopLoc_Location() facing = (bt.Triangulation(face, location)).GetObject() tab = facing.Nodes() tri = facing.Triangles() for i in range(1, facing.NbTriangles()+1): trian = tri.Value(i) index1, index2, index3 = trian.Get() for j in range(1, 4): if j == 1: m = index1 n = index2 elif j == 2: n = index3 elif j == 3: m = index2 me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n)) if me.IsDone(): builder.Add(comp, me.Edge()) ex.Next() display.EraseAll() display.DisplayShape(shape) display.DisplayShape(comp, update=True)
def test_mesh_sphere_quadrangle(self): aShape = BRepPrimAPI_MakeSphere(10.).Shape() # Create the Mesh aMeshGen = SMESH_Gen() aMesh = aMeshGen.CreateMesh(0, True) # 1D an1DHypothesis = StdMeshers_Arithmetic1D(0, 0, aMeshGen)#discretization of the wire an1DHypothesis.SetLength(0.1, False) #the smallest distance between 2 points an1DHypothesis.SetLength(0.5, True) # the longest distance between 2 points an1DAlgo = StdMeshers_Regular_1D(1, 0, aMeshGen) # interpolation # 2D a2dHypothseis = StdMeshers_TrianglePreference(2, 0, aMeshGen) #define the boundary a2dAlgo = StdMeshers_Quadrangle_2D(3, 0, aMeshGen) # the 2D mesh #Calculate mesh aMesh.ShapeToMesh(aShape) #Assign hyptothesis to mesh aMesh.AddHypothesis(aShape, 0) aMesh.AddHypothesis(aShape, 1) aMesh.AddHypothesis(aShape, 2) aMesh.AddHypothesis(aShape, 3) #Compute the data aMeshGen.Compute(aMesh, aMesh.GetShapeToMesh())
def testRemovedByRefFeature(self): ''' test that arguments returned by ref transormation is ok ''' from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere from OCC.BRep import BRep_Tool_Surface from OCC.GeomLProp import GeomLProp_SLProps from OCC.gp import gp_Pnt sphere_shape = BRepPrimAPI_MakeSphere(40.).Shape() # build a surface from this sphere from OCC.Utils.Topology import Topo t = Topo(sphere_shape) for f in t.faces(): face = f surf = BRep_Tool_Surface(face) lprop = GeomLProp_SLProps(0, 1e-12) lprop.SetSurface(surf) # evaluate_uv_coordinates coords = [] p = 0.0 # first point u, v = [0, 0] lprop.SetParameters(u, v) pnt = lprop.Value() print 'First point coords : ', pnt.Coord() print surf.GetObject().Value(u, v).Coord() # This one is [40.0,0.,0.] self.assertEqual(str(pnt.Coord()), '(40.0, 0.0, 0.0)') coords.append(pnt) #second point u, v = [0.5, 0.5] lprop.SetParameters(u, v) pnt2 = lprop.Value() # check then that the value has not changed (it does if returned by ref) self.assertEqual(str(pnt.Coord()), '(40.0, 0.0, 0.0)')
def also_on_select(shapes): for shape in shapes: if shape.ShapeType() == TopAbs_SOLID: print("solid selected") if shape.ShapeType() == TopAbs_EDGE: print("edge selected") if shape.ShapeType() == TopAbs_FACE: print("face selected") def location_from_vector(x, y, z): trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(500, 0, 0)) loc = TopLoc_Location(trsf) return loc cube = BRepPrimAPI_MakeBox(100, 100, 100).Shape() sphere = BRepPrimAPI_MakeSphere(100).Shape() sphere.Move(location_from_vector(500,0,0)) display.DisplayShape(cube) display.DisplayShape(sphere) viewer = get_occ_viewer() viewer.sig_topods_selected.connect(on_select) viewer.sig_topods_selected.connect(also_on_select) display.FitAll() start_display()
def dbgSphere(self, pt): return BRepPrimAPI_MakeSphere(getGpPt(pt), 0.1).Shape()
def sphere(event=None): display.DisplayShape(BRepPrimAPI_MakeSphere(100).Shape(), update=True)
from OCC.BRepAlgoAPI import BRepAlgoAPI_Cut from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere from OCC.gp import gp_Pnt, gp_Ax1, gp_Dir from OCC.GProp import GProp_GProps from OCC.BRepGProp import brepgprop_VolumeProperties from math import pi # original implementation with occ backend import siconos.io.mechanics_io siconos.io.mechanics_io.set_implementation('original') siconos.io.mechanics_io.set_backend('occ') # ball shape ball = BRepPrimAPI_MakeSphere(.15).Shape() ball_props = GProp_GProps() brepgprop_VolumeProperties(ball, ball_props) ball_mass = ball_props.Mass() ball_com = ball_props.CentreOfMass() ball_inertia = ball_props.MatrixOfInertia() ball_I1 = ball_props.MomentOfInertia(gp_Ax1(ball_com, gp_Dir(1, 0, 0))) ball_I2 = ball_props.MomentOfInertia(gp_Ax1(ball_com, gp_Dir(0, 1, 0))) ball_I3 = ball_props.MomentOfInertia(gp_Ax1(ball_com, gp_Dir(0, 0, 1))) print 'ball mass:', ball_mass print 'ball center of mass:', (ball_com.Coord(1), ball_com.Coord(2), ball_com.Coord(3))
def dbg_sphere(self, pt): return BRepPrimAPI_MakeSphere(get_gp_pt(pt), 0.1).Shape()
# radisu of second ball r2 = 1 # gap gap = .47 # hgap hgap = 0. # size of a brick bx = 5 by = 2 bz = 2 sphere1 = BRepPrimAPI_MakeSphere(r1).Shape() sphere2 = BRepPrimAPI_MakeSphere(r2).Shape() cylinder1 = BRepPrimAPI_MakeCylinder(.3, l1).Shape() cylinder2 = BRepPrimAPI_MakeCylinder(.3, l2).Shape() ground = BRepPrimAPI_MakeBox(gp_Pnt(-50, -50, 0), 100., 100., .5).Shape() brick = BRepPrimAPI_MakeBox(gp_Pnt(-bx / 2., -by / 2., -bz / 2.), bx, by, bz).Shape() # Creation of the hdf5 file for input/output with MechanicsHdf5Runner() as io: # io.add_occ_shape('Mass1', sphere1) io.add_occ_shape('Mass2', sphere2) io.add_occ_shape('Arm1', cylinder1) io.add_occ_shape('Arm2', cylinder2)
def iso_curve(self, u_or_v, param): """ get the iso curve from a u,v + parameter :param u_or_v: :param param: :return: """ uv = 0 if u_or_v == 'u' else 1 iso = Adaptor3d_IsoCurve(self.adaptor_handle.GetHandle(), uv, param) return iso def edges(self): return [ Edge(i) for i in WireExplorer(next(self.topo.wires())).ordered_edges() ] def __repr__(self): return self.name def __str__(self): return self.__repr__() if __name__ == "__main__": from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere sph = BRepPrimAPI_MakeSphere(1, 1).Face() fc = Face(sph) print(fc.is_trimmed()) print(fc.is_planar())
##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_Pnt, gp_XYZ, gp_Mat, gp_GTrsf from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox from OCC.BRepBuilderAPI import BRepBuilderAPI_GTransform from OCC.BRepAlgoAPI import BRepAlgoAPI_Common from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() orig = gp_Pnt(0., 0., 0.) sphere = BRepPrimAPI_MakeSphere(orig, 50.).Solid() # be careful that the following scale numbers are "not too big", # otherwise boolean operations can be buggy # see isue a1 = 17.1 a2 = 17.1 a3 = 3.5 gTrsf = gp_GTrsf(gp_Mat(a1, 0, 0, 0, a2, 0, 0, 0, a3), gp_XYZ(0., 112.2, 0.)) ellipsoid = BRepBuilderAPI_GTransform(sphere, gTrsf).Shape() # then perform a boolean intersection with a box box = BRepPrimAPI_MakeBox(gp_Pnt(-1000, -1000, -1000), gp_Pnt(1000, 112.2, 1000)).Shape() common = BRepAlgoAPI_Common(box, ellipsoid).Shape() assert not common.IsNull()
#mechanics_run.set_backend('occ') import siconos.io.mechanics_run siconos.io.mechanics_run.set_backend('occ') import numpy as np import math # create sphere radius = 1.0 # The sphere center X1 = 0.0 Y1 = 0.0 Z1 = 0.0 # create OCC.gp.gp_Pnt-Point from vector point = gp_Pnt(X1, Y1, Z1) sphere_r1 = BRepPrimAPI_MakeSphere(point, radius) sphere_r1_shape = sphere_r1.Shape() radius = 0.01 sphere_r01 = BRepPrimAPI_MakeSphere(point, radius) sphere_r01_shape = sphere_r01.Shape() radius = 0.001 sphere_r001 = BRepPrimAPI_MakeSphere(point, radius) sphere_r001_shape = sphere_r001.Shape() from OCC.BRep import BRep_Builder from OCC.TopoDS import TopoDS_Compound builder = BRep_Builder() comp = TopoDS_Compound()
#!/usr/bin/env python # # Example of a bouncing ball with OpenCascade contactors and occ distance # from siconos.mechanics.collision.tools import Volume, Contactor from siconos.io.mechanics_run import MechanicsHdf5Runner from siconos import numerics import siconos.io.mechanics_run from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere from OCC.gp import gp_Pnt siconos.io.mechanics_run.set_backend('occ') sphere = BRepPrimAPI_MakeSphere(1.).Shape() ground = BRepPrimAPI_MakeBox(gp_Pnt(-50, -50, 0), 100., 100., .5).Shape() # Creation of the hdf5 file for input/output with MechanicsHdf5Runner() as io: io.add_occ_shape('Sphere', sphere) io.add_occ_shape('Ground', ground) io.add_object('sphere', [Volume('Sphere'), Contactor('Sphere', contact_type='Face', contact_index=0)], mass=1, translation=[0, 0, 10], velocity=[0, 0, 0, 0, 0, 0]) io.add_object('ground', [Contactor('Ground', contact_type='Face', contact_index=5)],
##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.AIS import AIS_Shape from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere from OCC.Display.SimpleGui import init_display from OCC.Graphic3d import (Graphic3d_ShaderProgram, Graphic3d_TOS_VERTEX, Graphic3d_TOS_FRAGMENT, Graphic3d_ShaderObject) from OCC.TCollection import TCollection_AsciiString display, start_display, add_menu, add_function_to_menu = init_display() shape = BRepPrimAPI_MakeSphere(100).Shape() anIO = AIS_Shape(shape) display.Context.Display(anIO.GetHandle()) # gragment shader fs = """ void main(void) { gl_FragColor=vec4(0.0, 1.0, 0, 1.0); } """ # vertex shader vs = """ void main() {
def get_test_sphere_shape(): return BRepPrimAPI_MakeSphere(10.).Shape()