def test_tesselate_torus_with_edges(self): """ 2st test : tesselation of a torus """ a_torus = BRepPrimAPI_MakeTorus(10, 4).Shape() tess = Tesselator(a_torus) tess.Compute(compute_edges=True) self.assertGreater(tess.ObjGetTriangleCount(), 100) self.assertGreater(tess.ObjGetNormalCount(), 100)
def test_tesselate_twice(self): """ calling Compte() many times should no raise an exception """ another_torus = BRepPrimAPI_MakeTorus(10, 4).Shape() torus_tess = ShapeTesselator(another_torus) torus_tess.Compute() torus_tess.Compute()
def test_tesselate_torus(self): """ 2st test : tesselation of a torus """ a_torus = BRepPrimAPI_MakeTorus(10, 4).Shape() tess = Tesselator(a_torus, atNormal, 1.0, 1, 0.01, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.) tess.Compute() self.assertGreater(tess.ObjGetTriangleCount(), 100) self.assertGreater(tess.ObjGetNormalCount(), 100)
def test_x3dom_random_mesh_quality(self): """ Test: threejs 10 random boxes """ my_x3dom_renderer = x3dom_renderer.X3DomRenderer() torus_shp = BRepPrimAPI_MakeTorus(20., 10.).Shape() my_x3dom_renderer.DisplayShape(torus_shp, mesh_quality=1.0) my_x3dom_renderer.DisplayShape(torus_shp, mesh_quality=0.8) my_x3dom_renderer.DisplayShape(torus_shp, mesh_quality=2.0)
def test_x3d_file_is_valid_xml(self): """use ElementTree to parse X3D output""" another_torus = BRepPrimAPI_MakeTorus(10, 4).Shape() torus_tess = ShapeTesselator(another_torus) torus_tess.Compute() output_x3d_filename = os.path.join("test_io", "torus.x3d") torus_tess.ExportShapeToX3D(output_x3d_filename) self.assertTrue(os.path.exists(output_x3d_filename)) with open(output_x3d_filename, "r") as x3d_file: x3d_content = x3d_file.read() ET.fromstring(x3d_content) # raise an exception if not valid xml
def test_edge_to_bezier(self): b = BRepPrimAPI_MakeTorus(30, 10).Shape() t = TopologyExplorer(b) for ed in t.edges(): is_bezier, bezier_curve, degree = edge_to_bezier(ed) self.assertTrue(isinstance(is_bezier, bool)) if not is_bezier: self.assertTrue(degree is None) self.assertTrue(bezier_curve is None) else: self.assertTrue(isinstance(degree, int))
def test_tesselate_torus_with_bad_quality(self): """ 2st test : tesselation of a torus """ a_torus = BRepPrimAPI_MakeTorus(10, 4).Shape() tess = Tesselator(a_torus) tess.Compute(mesh_quality=40.) # since mesh quality is much lower, we should count less vertices and # triangles self.assertGreater(tess.ObjGetTriangleCount(), 10) self.assertLess(tess.ObjGetTriangleCount(), 100) self.assertGreater(tess.ObjGetNormalCount(), 10) self.assertLess(tess.ObjGetNormalCount(), 100)
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_shp = BRepAlgoAPI_Section(torus, sphere, False) section_shp.ComputePCurveOn1(True) section_shp.Approximation(True) section_shp.Build() sections.append(section_shp) rnd = JupyterRenderer() rnd.DisplayShape(torus) rnd.Display()
def index(): """PythonOCC Demo Page""" # remove shapes from previous (avoid duplicate shape after F5 refresh) my_ren._3js_shapes = {} my_ren._3js_edges = {} my_ren._3js_vertex = {} # import additional modules for building a box and a torus. from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeTorus from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform from OCC.Core.gp import gp_Trsf import time def translate_shp(shp, vec, copy=False): trns = gp_Trsf() trns.SetTranslation(vec) brep_trns = BRepBuilderAPI_Transform(shp, trns, copy) brep_trns.Build() return brep_trns.Shape() box = BRepPrimAPI_MakeBox(100.0, 200.0, 300.0).Shape() torus = BRepPrimAPI_MakeTorus(300.0, 105).Shape() t_torus = translate_shp(torus, gp_Vec(700, 0, 0)) init_time = time.time() my_ren.ConvertShape(box, export_edges=True) my_ren.ConvertShape(t_torus, export_edges=True) final_time = time.time() print("\nTotal meshing time : ", final_time - init_time) return render_template( "index.html", occ_version=OCC_VERSION, threejs_version=THREEJS_RELEASE, render_cfg=render_cfg, occ_shapes=my_ren._3js_shapes, occ_edges=my_ren._3js_edges, occ_vertex=my_ren._3js_vertex, )
def test_discretize_wire(self): tor = BRepPrimAPI_MakeTorus(50, 20).Shape() topo = TopologyExplorer(tor) for wire in topo.wires(): pnts = discretize_wire(wire) self.assertTrue(pnts)
def test_threejs_render_torus(self): """ Render a simple torus in threejs """ torus_shp = BRepPrimAPI_MakeTorus(20., 10.).Shape() my_threejs_renderer = threejs_renderer.ThreejsRenderer() my_threejs_renderer.DisplayShape(torus_shp)
def compute_bbox(shp, *kwargs): print("Compute bbox for %s " % shp) for shape in shp: bbox = Bnd_Box() brepbndlib_Add(shape, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() dx = xmax - xmin dy = ymax - ymin dz = zmax - zmin print("Selected shape bounding box : dx=%f, dy=%f, dz=%f." % (dx, dy, dz)) print(" bounding box center: x=%f, y=%f, z=%f" % (xmin + dx / 2., ymin + dy / 2., zmin + dz / 2.)) print(shape.text) display, start_display, add_menu, add_function_to_menu = init_display() # register callbacks display.register_select_callback(print_xy_click) display.register_select_callback(compute_bbox) # creating geometry my_torus = BRepPrimAPI_MakeBox(10., 20., 30.).Shape() my_box = BRepPrimAPI_MakeTorus(30., 5.).Shape() my_torus.text = "Hello from Torus" my_box.text = "Hello from Box" # and finally display geometry display.DisplayShape(my_torus) display.DisplayShape(my_box, update=True) start_display()
def boolean_fuse(base): ring_radius = 0.25 torus_radius = 1.0 - ring_radius torus = BRepPrimAPI_MakeTorus(torus_radius, ring_radius).Shape() fuse = BRepAlgoAPI_Fuse(base, torus).Shape() return fuse
## ##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/>. import os from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus from OCC.Extend.DataExchange import write_stl_file # first, create the shape my_torus = BRepPrimAPI_MakeTorus(20., 10.).Shape() # set the directory where to output the stl_output_dir = os.path.abspath(os.path.join("models")) # make sure the path exists otherwise OCE get confused if not os.path.isdir(stl_output_dir): raise AssertionError("wrong path provided") stl_low_resolution_file = os.path.join(stl_output_dir, "torus_default_resolution.stl") write_stl_file(my_torus, stl_low_resolution_file) # then we change the mesh resolution, and export as binary stl_high_resolution_file = os.path.join(stl_output_dir, "torus_high_resolution.stl") # we set the format to binary
def test_x3dom_render_torus(self): """ Render a simple torus using x3dom """ torus_shp = BRepPrimAPI_MakeTorus(20., 10.).Shape() my_x3dom_renderer = x3dom_renderer.X3DomRenderer() my_x3dom_renderer.DisplayShape(torus_shp)
import sys from OCC.Display.WebGl import threejs_renderer from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus from OCC.Core.gp import gp_Vec from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, NORMAL from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox sys.path.append('..') from OCC.Extend.ShapeFactory import translate_shp # In[ ]: # create 3 toruses # be careful to set copy to True or all the shapes will share the same mesh torus_shp1 = BRepPrimAPI_MakeTorus(20, 5).Shape() torus_shp2 = translate_shp(torus_shp1, gp_Vec(60, 0, 0), copy=True) torus_shp3 = translate_shp(torus_shp1, gp_Vec(-60, 0, 0), copy=True) # In[ ]: # use the NORMAL.CLIENT_SIDE in order to clearly see faces # in case the NORMAL.SERVER_SIDE option is used, vertex normals lead to # a smooth rendering my_renderer = JupyterRenderer(compute_normals_mode=NORMAL.CLIENT_SIDE) # In[ ]: my_renderer.DisplayShape(torus_shp1, shape_color="blue", topo_level="Face",
def print_xy_click(shp, *kwargs): for shape in shp: print("Shape selected: ", shape) print(kwargs) def compute_bbox(shp, *kwargs): print("Compute bbox for %s " % shp) for shape in shp: bbox = Bnd_Box() brepbndlib_Add(shape, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() dx = xmax - xmin dy = ymax - ymin dz = zmax - zmin print("Selected shape bounding box : dx=%f, dy=%f, dz=%f." % (dx, dy, dz)) print(" bounding box center: x=%f, y=%f, z=%f" % (xmin + dx/2., ymin + dy/2., zmin + dz/2.)) display, start_display, add_menu, add_function_to_menu = init_display() # register callbacks display.register_select_callback(print_xy_click) display.register_select_callback(compute_bbox) # creating geometry my_torus = BRepPrimAPI_MakeBox(10., 20., 30.).Shape() my_box = BRepPrimAPI_MakeTorus(30., 5.).Shape() # and finally display geometry display.DisplayShape(my_torus) display.DisplayShape(my_box, update=True) start_display()
X3D_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 4.0//EN" "https://www.web3d.org/specifications/x3d-4.0.dtd"> <X3D profile='Immersive' version='4.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='http://www.web3d.org/specifications/x3d-4.0.xsd'> <head> <meta name='generator' content='pythonocc-7.4.1-dev X3D exporter (www.pythonocc.org)'/> <meta name='creator' content='pythonocc-7.4.1-dev generator'/> <meta name='identifier' content='http://www.pythonocc.org'/> <meta name='description' content='pythonocc-7.4.1-dev x3dom based shape rendering'/> </head> <Scene> %s </Scene> </X3D> """ base_shape = BRepPrimAPI_MakeTorus(3, 1).Shape() # conversion to a nurbs representation nurbs_converter = BRepBuilderAPI_NurbsConvert(base_shape, True) # nurbs_converter.Perform() converted_shape = nurbs_converter.Shape() # now, all edges should be BSpline curves and surfaces BSpline surfaces # https://castle-engine.io/x3d_implementation_nurbs.php#section_homogeneous_coordinates expl = TopologyExplorer(converted_shape) nurbs_node_str = "" face_idx = 1
from __future__ import print_function import random from OCC.Display.WebGl import threejs_renderer from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus from OCC.Core.gp import gp_Vec from OCC.Extend.ShapeFactory import translate_shp, rotate_shp_3_axis my_ren = threejs_renderer.ThreejsRenderer() n_toruses = 100 idx = 0 for i in range(n_toruses): torus_shp = BRepPrimAPI_MakeTorus(10 + random.random() * 10, random.random() * 10).Shape() # random position and orientation and color angle_x = random.random() * 360 angle_y = random.random() * 360 angle_z = random.random() * 360 rotated_torus = rotate_shp_3_axis(torus_shp, angle_x, angle_y, angle_z, 'deg') tr_x = random.uniform(-70, 50) tr_y = random.uniform(-70, 50) tr_z = random.uniform(-50, 50) trans_torus = translate_shp(rotated_torus, gp_Vec(tr_x, tr_y, tr_z)) rnd_color = (random.random(), random.random(), random.random()) my_ren.DisplayShape(trans_torus, export_edges=True, color=rnd_color, transparency=random.random())
#!/usr/bin/env python ##Copyright 2009-2016 Thomas Paviot ([email protected]) ## ##This file is part of pythonOCC. ## ##pythonOCC is free software: you can redistribute it and/or modify ##it under the terms of the GNU Lesser General Public License as published by ##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.Display.WebGl import threejs_renderer from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus torus_shp = BRepPrimAPI_MakeTorus(20., 10.).Shape() my_renderer = threejs_renderer.ThreejsRenderer() my_renderer.DisplayShape(torus_shp) if __name__ == '__main__': my_renderer.render(server_port=8000, addr="0.0.0.0")
for shape in shp: # this should be a TopoDS_Face TODO check it is print("Face selected: ", shape) recognize_face(shape) def recognize_batch(event=None): """ Menu item : process all the faces of a single shape """ # loop over faces only for face in TopologyExplorer(shp).faces(): # call the recognition function recognize_face(face) def exit(event=None): sys.exit() if __name__ == '__main__': display, start_display, add_menu, add_function_to_menu = init_display() display.SetSelectionModeFace() # switch to Face selection mode display.register_select_callback(recognize_clicked) # first loads the STEP file and display from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus shp = BRepPrimAPI_MakeTorus(30, 15).Shape() #shp = read_step_file('../assets/models/as1_pe_203.stp') display.DisplayShape(shp, update=True) add_menu('recognition') add_function_to_menu('recognition', recognize_batch) start_display()
def get_test_fullname(filename): return os.path.join(SAMPLES_DIRECTORY, filename) # the sample files STEP_AP203_SAMPLE_FILE = get_test_fullname("as1_pe_203.stp") STEP_AP214_SAMPLE_FILE = get_test_fullname("as1-oc-214.stp") STEP_MULTIPLE_ROOT = get_test_fullname("stp_multiple_shp_at_root.stp") IGES_SAMPLE_FILE = get_test_fullname("sunglasses_lens.igs") IGES_45_FACES = get_test_fullname("example_45_faces.iges") STL_ASCII_SAMPLE_FILE = get_test_fullname("bottle_ascii.stl") STL_BINARY_SAMPLE_FILE = get_test_fullname("cube_binary.stl") # the basic geometry to test exporters A_TOPODS_SHAPE = BRepPrimAPI_MakeTorus(200, 50).Shape() class TestExtendDataExchange(unittest.TestCase): def test_read_step_file(self): read_step_file(STEP_AP203_SAMPLE_FILE) read_step_file(STEP_AP214_SAMPLE_FILE) def test_read_step_file_multiple_shape_as_root(self): t = read_step_file(STEP_MULTIPLE_ROOT, as_compound=True) self.assertTrue(isinstance(t, TopoDS_Compound)) l = read_step_file(STEP_MULTIPLE_ROOT, as_compound=False) self.assertEqual(len(l), 3) def test_read_step_file_names_colors(self):
def renderTorus(self, aRadius1, aRadius2): shape = BRepPrimAPI_MakeTorus(aRadius1, aRadius2).Shape() self._renderShapeObj(shape)
##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.Display.SimpleGui import init_display from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus from OCC.Core.Graphic3d import (Graphic3d_EF_PDF, Graphic3d_EF_SVG, Graphic3d_EF_TEX, Graphic3d_EF_PostScript, Graphic3d_EF_EnhPostScript) display, start_display, add_menu, add_function_to_menu = init_display() my_box = BRepPrimAPI_MakeTorus(40., 20.).Shape() display.DisplayShape(my_box, update=True) f = display.View.View().GetObject() #------------------------------------------------------------------------------- # for this example to work, pythonocc / OCE needs to be built with the gl2ps lib #------------------------------------------------------------------------------- def export_to_PDF(event=None): f.Export('torus_export.pdf', Graphic3d_EF_PDF) def export_to_SVG(event=None): f.Export('torus_export.svg', Graphic3d_EF_SVG)
def test_discretize_edge(self): tor = BRepPrimAPI_MakeTorus(50, 20).Shape() topo = TopologyExplorer(tor) for edge in topo.edges(): discretize_edge(edge)
fp.write("</html>\n") def render(self, server_port=8080, open_webbrowser=False): ''' render the scene into the browser. ''' # generate HTML file self.GenerateHTMLFile() # then create a simple web server start_server(server_port, self._path, open_webbrowser) if __name__ == "__main__": from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeTorus from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform from OCC.Core.gp import gp_Trsf, gp_Vec def translate_shp(shp, vec, copy=False): trns = gp_Trsf() trns.SetTranslation(vec) brep_trns = BRepBuilderAPI_Transform(shp, trns, copy) brep_trns.Build() return brep_trns.Shape() box = BRepPrimAPI_MakeBox(100., 200., 300.).Shape() torus = BRepPrimAPI_MakeTorus(300., 105).Shape() t_torus = translate_shp(torus, gp_Vec(700, 0, 0)) my_ren = ThreejsRenderer() my_ren.DisplayShape(box) my_ren.DisplayShape(t_torus) my_ren.render()
##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.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_NurbsConvert from OCC.Core.BRepAdaptor import BRepAdaptor_Surface from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Core.GeomAbs import GeomAbs_BSplineSurface base_shape = BRepPrimAPI_MakeTorus(30, 10).Shape() # conversion to a nurbs representation nurbs_converter = BRepBuilderAPI_NurbsConvert(base_shape, True) #nurbs_converter.Perform() converted_shape = nurbs_converter.Shape() # now, all edges should be BSpline curves and surfaces BSpline surfaces # see https://www.opencascade.com/doc/occt-7.4.0/refman/html/class_b_rep_builder_a_p_i___nurbs_convert.html#details expl = TopologyExplorer(converted_shape) # loop over faces fc_idx = 1 for face in expl.faces():