### Launch Jupyter Notebooks Source: https://github.com/tpaviot/pythonocc-demos/blob/master/README.md Starts a Jupyter Notebook server to run interactive examples of pythonocc within a notebook environment. Requires Jupyter and pythreejs to be installed. ```Shell cd jupyter_notebooks jupyter notebook ``` -------------------------------- ### Run Hello World Example Source: https://github.com/tpaviot/pythonocc-demos/blob/master/README.md Executes a basic Python script demonstrating a core feature of pythonocc. Navigate to the 'examples' directory and run this script. ```Shell cd examples python core_helloworld.py ``` -------------------------------- ### Install pythonocc-core Source: https://github.com/tpaviot/pythonocc-demos/blob/master/README.md Installs the pythonocc-core library version 7.8.1 using Conda. This is a prerequisite for running the provided examples. ```Shell conda install -c conda-forge pythonocc-core=7.8.1 ``` -------------------------------- ### Create Jupyter Renderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Initializes an instance of the JupyterRenderer to facilitate the visualization of 3D shapes. ```Python my_renderer = JupyterRenderer() ``` -------------------------------- ### Render Display Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Finalizes and displays the rendered 3D scene containing the prism. ```Python my_renderer.Display() ``` -------------------------------- ### Initialize JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_brep.ipynb Initializes an instance of the JupyterRenderer. This object will be used to render and display the loaded OCCT shapes. ```Python my_renderer = JupyterRenderer() ``` -------------------------------- ### Split Box with Wire Frame using BOPAlgo_Splitter Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/splitter_demo_V2.ipynb This example illustrates splitting a box using a wire frame directly, contrasting with the previous example that used a face. It highlights how using a wire frame as a tool affects the splitting outcome, resulting in surface cuts rather than solid cuts. The code visualizes the result using JupyterRenderer. ```Python # See https://dev.opencascade.org/doc/overview/html/occt_user_guides__modeling_algos.html W = BRepBuilderAPI_MakePolygon() W.Add(gp_Pnt(-0.25, -0.25, 1)) W.Add(gp_Pnt(0.25, -0.25, 1)) W.Add(gp_Pnt(0.25, 0.25, 1)) W.Add(gp_Pnt(-0.25, 0.25, 1)) W.Close() # At this point, the polygon shape is a "wire frame" shape1_wire = W.Shape() print("Shape type of Polygon after .Close()", shape1_wire.ShapeType) box = BRepPrimAPI_MakeBox(gp_Pnt(-0.25, -0.25, 0), gp_Pnt(0.25, 0.25, 2)).Shape() # section_shp = BRepAlgoAPI_Section(shape1_face.Shape(), wall) splitter = BOPAlgo_Splitter() splitter.SetNonDestructive(False) splitter.AddArgument(box) # arugment means object to cut splitter.AddTool(shape1_wire) # tool means arguments are cut by this splitter.Perform() result = splitter.Shape() print("Shape type of result", result.ShapeType) exp = TopExp_Explorer() exp.Init(result, TopAbs.TopAbs_SOLID) sub_shapes = [] while exp.More(): sub_shapes.append(exp.Current()) exp.Next() print("Sub shapes after split", sub_shapes) rnd = JupyterRenderer() # rnd.DisplayShape(result, render_edges=True) rnd.DisplayShape(result, render_edges=True) rnd.Display() ``` -------------------------------- ### Initialize JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/set_callback.ipynb Initializes the JupyterRenderer for displaying shapes within a Jupyter Notebook or similar web-based environment. It mentions the use of NORMAL.CLIENT_SIDE for clear face rendering. ```Python # 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() ``` -------------------------------- ### Build Prism Model Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Constructs the prism shape by performing a linear extrusion of the defined profile along the specified vector using BRepPrimAPI_MakePrism. ```Python # extrusion prism = BRepPrimAPI_MakePrism(profile, vec).Shape() ``` -------------------------------- ### Import OCC Core Modules Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Imports necessary classes from the OCC.Core library for geometric operations, including points, vectors, B-spline creation, edge creation, and prism construction. ```Python from OCC.Core.gp import gp_Pnt, gp_Vec from OCC.Core.GeomAPI import GeomAPI_PointsToBSpline from OCC.Core.TColgp import TColgp_Array1OfPnt from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakePrism ``` -------------------------------- ### Import PythonOCC Modules Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/set_callback.ipynb Imports necessary modules from the PythonOCC library, including BRepPrimAPI for shape creation, gp for geometric vectors, and JupyterRenderer for display. It also includes path manipulation for module imports. ```Python import sys sys.path.append("..\..") from OCC.Core.BRepPrimAPI import ( BRepPrimAPI_MakeTorus, BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere, ) from OCC.Core.gp import gp_Vec from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, NORMAL from OCC.Core.GProp import GProp_GProps from OCC.Core.BRepGProp import brepgprop_VolumeProperties from OCC.Extend.ShapeFactory import translate_shp ``` -------------------------------- ### Import Display and Extend Modules Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Imports the JupyterRenderer class from OCC.Display.WebGl.jupyter_renderer for displaying shapes within a Jupyter environment. ```Python from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer ``` -------------------------------- ### Display Prism Shape Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Renders the generated prism shape using the JupyterRenderer, with edges highlighted for better visualization. ```Python my_renderer.DisplayShape(prism, render_edges=True) ``` -------------------------------- ### Generate Linear Path Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Defines a linear path for the extrusion by creating a vector between a starting point and an ending point. An edge representing this path is also created. ```Python # the linear path starting_point = gp_Pnt(0.0, 0.0, 0.0) end_point = gp_Pnt(0.0, 0.0, 6.0) vec = gp_Vec(starting_point, end_point) path = BRepBuilderAPI_MakeEdge(starting_point, end_point).Edge() ``` -------------------------------- ### Create Torus, Box, and Sphere Shapes Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/set_callback.ipynb Demonstrates the creation of a torus, a box, and a sphere using BRepPrimAPI. The box and sphere are translated using gp_Vec and the translate_shp function. It highlights the importance of setting `copy=True` for independent shape meshes. ```Python # create 3 toruses # be careful to set copy to True or all the shapes will share the same mesh torus_shp = BRepPrimAPI_MakeTorus(20, 5).Shape() box_shp = translate_shp(BRepPrimAPI_MakeBox(10, 20, 3).Shape(), gp_Vec(60, 0, 0)) sphere_shp = translate_shp(BRepPrimAPI_MakeSphere(20.0).Shape(), gp_Vec(-60, 0, 0)) ``` -------------------------------- ### Generate B-spline Profile Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_prism.ipynb Creates a B-spline curve by interpolating through an array of 5 points and then constructs a B-rep edge from this B-spline. This edge will serve as the profile for the prism. ```Python # the bspline profile array = TColgp_Array1OfPnt(1, 5) array.SetValue(1, gp_Pnt(0, 0, 0)) array.SetValue(2, gp_Pnt(1, 2, 0)) array.SetValue(3, gp_Pnt(2, 3, 0)) array.SetValue(4, gp_Pnt(4, 3, 0)) array.SetValue(5, gp_Pnt(5, 5, 0)) bspline = GeomAPI_PointsToBSpline(array).Curve() profile = BRepBuilderAPI_MakeEdge(bspline).Edge() ``` -------------------------------- ### PythonOCC Imports and Setup Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/triangle_mesh_gmsh.ipynb Imports necessary modules from PythonOCC for geometry manipulation, file exchange, and rendering, along with standard Python libraries for file system operations and temporary directories. ```Python # standard import import os import sys import tempfile # OCC imports from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer from OCC.Core.BRepTools import breptools from OCC.Core.gp import gp_Vec from OCC.Extend.ShapeFactory import translate_shp from OCC.Extend.DataExchange import read_step_file, read_stl_file ``` -------------------------------- ### Read BREP File with BRepTools Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_brep.ipynb Demonstrates reading a BREP file named 'cylinder_head.brep' into a TopoDS_Shape object using BRepTools.Read. A BRep_Builder is required as an argument for the Read function. ```Python cylinder_head = TopoDS_Shape() builder = BRep_Builder() assert breptools.Read(cylinder_head, "../assets/models/cylinder_head.brep", builder) ``` -------------------------------- ### Import BRepTools and TopoDS_Shape Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_brep.ipynb Imports necessary classes from OCC.Core for working with topological shapes and BREP tools. BRepTools is used for reading and writing BREP files, while TopoDS_Shape represents a topological data structure. ```Python from OCC.Core.BRepTools import breptools from OCC.Core.TopoDS import TopoDS_Shape from OCC.Core.BRep import BRep_Builder ``` -------------------------------- ### Import JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_brep.ipynb Imports the JupyterRenderer class from OCC.Display.WebGl. This class is used to display OCCT shapes within a Jupyter Notebook or similar web-based environments. ```Python from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer ``` -------------------------------- ### Render JupyterRenderer Output Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_brep.ipynb This line implicitly displays the output of the JupyterRenderer in a Jupyter environment, showing the rendered shape. ```Python my_renderer ``` -------------------------------- ### Import OCC Core Modules for Topology Operations Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_draft_angle.ipynb Imports essential modules from OCC.Core for geometric and topological operations, including shape creation, draft angle application, and precision settings. These are foundational for manipulating shapes in PythonOCC. ```python import math from OCC.Core.gp import gp_Dir, gp_Pln, gp_Ax3, gp from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_DraftAngle from OCC.Core.Precision import precision from OCC.Core.BRep import BRep_Tool from OCC.Core.TopExp import TopExp_Explorer from OCC.Core.TopAbs import TopAbs_FACE from OCC.Core.Geom import Geom_Plane from OCC.Core.TopoDS import topods ``` -------------------------------- ### Initialize Jupyter Renderer for Display Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_draft_angle.ipynb Initializes the JupyterRenderer from OCC.Display.WebGl. This renderer is used to display 3D shapes within a Jupyter Notebook environment, enabling interactive visualization. ```python from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer my_renderer = JupyterRenderer() ``` -------------------------------- ### Display Shapes with JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/set_callback.ipynb Displays the created torus, box, and sphere shapes using the `DisplayShape` method of the `JupyterRenderer`. Each shape is assigned a different color (blue, red, green), and the `update=True` argument is used for the last shape to refresh the display. ```Python my_renderer.DisplayShape(torus_shp, shape_color="blue") my_renderer.DisplayShape(box_shp, shape_color="red") my_renderer.DisplayShape(sphere_shp, shape_color="green", update=True) ``` -------------------------------- ### Generate a Box Shape Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_draft_angle.ipynb Creates a simple box shape using BRepPrimAPI_MakeBox with specified dimensions (length, width, height). This box serves as the base shape for applying draft angles. ```python S = BRepPrimAPI_MakeBox(20.0, 30.0, 15.0).Shape() ``` -------------------------------- ### Display Shape with JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_brep.ipynb Displays the loaded 'cylinder_head' shape using the JupyterRenderer. The 'render_edges=True' argument ensures that the edges of the shape are also rendered. ```Python my_renderer.DisplayShape(cylinder_head, render_edges=True) ``` -------------------------------- ### Register Callback for Shape Selection Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/set_callback.ipynb Defines a callback function `a_callback` that is executed when a shape is double-clicked. This function updates the renderer's HTML value with a message indicating the callback execution and the hash of the selected shape. The callback is then registered with the renderer. ```Python def a_callback(shp): """called each time a double click is performed""" my_renderer.html.value = f"Callback executed from shape {hash(shp)}" my_renderer.register_select_callback(a_callback) ``` -------------------------------- ### Display Cylinder with Custom Rendering Options Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helloworld.ipynb Displays a previously created cylinder shape using the JupyterRenderer. This example shows how to enable edge rendering, specify the topological level to display ('Face'), set a custom shape color, and update the display. ```python my_renderer.DisplayShape( cylinder_shape, render_edges=True, topo_level="Face", shape_color="#abdda4", update=True, ) ``` -------------------------------- ### Display Modified Shape Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_draft_angle.ipynb Displays the shape after the draft angle has been applied using the initialized JupyterRenderer. The `render_edges=True` argument ensures that the edges of the shape are also rendered, and `update=True` refreshes the display. ```python my_renderer.DisplayShape(shp, render_edges=True, update=True) ``` -------------------------------- ### Apply Draft Angle to Shape Faces Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/make_draft_angle.ipynb Applies a draft angle to the faces of a given shape. It iterates through each face, checks if it's planar and normal to a specific direction, and then applies the draft angle using BRepOffsetAPI_DraftAngle. The draft angle is set to 15 degrees. ```python adraft = BRepOffsetAPI_DraftAngle(S) topExp = TopExp_Explorer() topExp.Init(S, TopAbs_FACE) while topExp.More(): face = topods.Face(topExp.Current()) surf = Geom_Plane.DownCast(BRep_Tool.Surface(face)) dirf = surf.Pln().Axis().Direction() ddd = gp_Dir(0, 0, 1) if dirf.IsNormal(ddd, precision.Angular()): adraft.Add(face, ddd, math.radians(15), gp_Pln(gp_Ax3(gp.XOY()))) topExp.Next() adraft.Build() shp = adraft.Shape() ``` -------------------------------- ### Import PythonOCC Modules for Geometry Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helloworld.ipynb Imports necessary classes from PythonOCC for creating geometric primitives (Box, Sphere, Cylinder) and for using the JupyterRenderer for visualization. ```python from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer from OCC.Core.BRepPrimAPI import ( BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeCylinder, ) from OCC.Core.gp import gp_Pnt ``` -------------------------------- ### Create Geometric Primitives (Box, Cylinder) Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helloworld.ipynb Demonstrates the creation of a box and a cylinder using the BRepPrimAPI. The MakeBox function takes dimensions (length, width, height), and MakeCylinder takes radius and height. ```python box_shape = BRepPrimAPI_MakeBox(10, 20, 30).Shape() cylinder_shape = BRepPrimAPI_MakeCylinder(10, 30).Shape() ``` -------------------------------- ### Import Libraries for IFC Processing and Rendering Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Imports necessary libraries from OCC (Open CASCADE Technology) for web-based rendering in Jupyter and the ifcopenshell library for handling IFC files. ```python import os from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, format_color import ifcopenshell, ifcopenshell.geom ``` -------------------------------- ### Gmsh Binary Configuration and Temporary Directory Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/triangle_mesh_gmsh.ipynb Defines the path to the Gmsh executable and sets up a temporary directory for storing intermediate files generated during the meshing process. ```Python # gmsh binary location GMSH_BINARY = "gmsh" # create a temporary directory to store gmsh files tmp = tempfile.TemporaryDirectory() TMP_DIR = tmp.name print("Files will be saved to ", TMP_DIR) ``` -------------------------------- ### Display Points with JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helloworld.ipynb Creates a list of 3D points (gp_Pnt) and displays them using the JupyterRenderer's DisplayShape method. ```python vertices = [gp_Pnt(5, 10, 40), gp_Pnt(10, -4, -10)] my_renderer.DisplayShape(vertices) ``` -------------------------------- ### Import PythonOCC Libraries Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_multiple_shapes.ipynb Imports necessary modules from the PythonOCC library for topology exploration, data exchange, and web rendering, along with standard Python libraries for OS interaction and random number generation. ```python import os from random import randint from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, format_color from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Extend.DataExchange import read_step_file ``` -------------------------------- ### Python: Create and Display Toruses Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/torus_mesh_quality.ipynb This snippet shows how to create three torus shapes using BRepPrimAPI_MakeTorus and translate them using OCC.Extend.ShapeFactory. The toruses are then displayed using JupyterRenderer with different color and quality settings. ```Python import sys from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeTorus from OCC.Core.gp import gp_Vec from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer, NORMAL sys.path.append("..") from OCC.Extend.ShapeFactory import translate_shp # 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) # 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) my_renderer.DisplayShape( torus_shp1, shape_color="blue", topo_level="Face", quality=1.0 ) # default quality my_renderer.DisplayShape(torus_shp2, shape_color="red", quality=4) # lower quality my_renderer.DisplayShape(torus_shp3, shape_color="green", quality=0.5) # better quality my_renderer.Display() ``` -------------------------------- ### Initialize Jupyter Renderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Creates an instance of JupyterRenderer with a specified size for displaying 3D models within a Jupyter Notebook. ```python my_renderer = JupyterRenderer(size=(700, 700)) ``` -------------------------------- ### Process and Render IFC Products Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Iterates through each IfcProduct, creates its 3D shape if available, extracts its color and transparency, and displays it using the JupyterRenderer. ```python for product in products: if ( product.Representation is not None ): # some IfcProducts don't have any 3d representation pdct_shape = ifcopenshell.geom.create_shape(settings, inst=product) r, g, b, alpha = pdct_shape.styles[0] # the shape color color = format_color(int(abs(r) * 255), int(abs(g) * 255), int(abs(b) * 255)) # below, the pdct_shape.geometry is a TopoDS_Shape, i.e. can be rendered using # any renderer (threejs, x3dom, jupyter, qt5 based etc.) my_renderer.DisplayShape( pdct_shape.geometry, shape_color=color, transparency=True, opacity=alpha ) ``` -------------------------------- ### BREP Conversion, Gmsh File Generation, and Meshing Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/triangle_mesh_gmsh.ipynb Converts the loaded STEP model to BREP format, creates a Gmsh geometry file, invokes Gmsh to generate an STL mesh, and then loads the resulting mesh. ```Python # dump the geometry to a brep file, check it worked BREP_BASENAME = "ventilator.brep" BREP_FILENAME = os.path.join(TMP_DIR, BREP_BASENAME) breptools.Write(ventilator_shp, BREP_FILENAME) assert os.path.isfile(BREP_FILENAME) # create the gmesh file gmsh_file_content = ( """SetFactory(\"OpenCASCADE\"); Mesh.CharacteristicLengthMin = 1; Mesh.CharacteristicLengthMax = 5; a() = ShapeFromFile('%s'); """ % BREP_BASENAME ) GEO_FILENAME = os.path.join(TMP_DIR, "ventilator.geo") gmsh_file = open(GEO_FILENAME, "w") gmsh_file.write(gmsh_file_content) gmsh_file.close() assert os.path.isfile(GEO_FILENAME) # call gmsh, generate an STL file STL_FILENAME = os.path.join(TMP_DIR, "ventilator.stl") os.system("%s %s -2 -o %s -format stl" % (GMSH_BINARY, GEO_FILENAME, STL_FILENAME)) assert os.path.isfile(STL_FILENAME) # load the stl file meshed_ventilator_shp = read_stl_file(STL_FILENAME) ``` -------------------------------- ### Import necessary libraries for pythonocc and Jupyter rendering Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_one_shape.ipynb Imports modules for file path operations, Jupyter rendering, and data exchange (reading STEP files, exporting to SVG). These are essential for loading and visualizing CAD models in a web environment. ```Python import os.path from IPython.core.display import SVG from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer from OCC.Extend.DataExchange import read_step_file, export_shape_to_svg ``` -------------------------------- ### Split Box with Face using BOPAlgo_Splitter Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/splitter_demo_V2.ipynb This snippet demonstrates splitting a box shape using a face created from a polygon. It utilizes BOPAlgo_Splitter to perform the operation and then decomposes the resulting compound shape into individual solids using TopExp_Explorer. The results are visualized using JupyterRenderer. ```Python from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Splitter from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_MakePolygon from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Core.gp import gp_Pnt from OCC.Core import TopAbs from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer from OCC.Core.BRep import BRep_Polygon3D from OCC.Core.TopExp import TopExp_Explorer from OCC.Core.BOPAlgo import BOPAlgo_Splitter # Define Polygon and perform split operation W = BRepBuilderAPI_MakePolygon() W.Add(gp_Pnt(-2, -2, 1)) W.Add(gp_Pnt(-2, 2, 1)) W.Add(gp_Pnt(2, 2, 1)) W.Add(gp_Pnt(2, -2, 1)) W.Close() # At this point, the polygon shape is a "wire frame" shape1_wire = W.Shape() print("Shape type of Polygon after .Close()", shape1_wire.ShapeType) # in order to perform a split with the enture surface of the polygon, we have to convert the wire frame to a face shape1_face = BRepBuilderAPI_MakeFace(shape1_wire) print("Shape type of Face", shape1_face.Shape().ShapeType) box = BRepPrimAPI_MakeBox(gp_Pnt(-0.25, 0, 0), gp_Pnt(0.25, 1, 2)).Shape() # section_shp = BRepAlgoAPI_Section(shape1_face.Shape(), wall) splitter = BOPAlgo_Splitter() splitter.SetNonDestructive(False) splitter.AddArgument(box) # arugment means object to cut splitter.AddTool(shape1_face.Shape()) # tool means arguments are cut by this splitter.Perform() result = splitter.Shape() rnd = JupyterRenderer() rnd.DisplayShape(result, render_edges=True) rnd.Display() print(result.ShapeType) exp = TopExp_Explorer() exp.Init(result, TopAbs.TopAbs_SOLID) sub_shapes = [] while exp.More(): sub_shapes.append(exp.Current()) exp.Next() rnd = JupyterRenderer() colors = ["#DB0570", "#0506DB"] for s, c in zip(sub_shapes, colors): rnd.DisplayShape(s, render_edges=True, shape_color=c) rnd.Display() ``` -------------------------------- ### Import OCC Core Modules for Helix Creation Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helix.ipynb Imports essential modules from OCC.Core for geometric operations, including points, lines, axes, edges, cylindrical surfaces, and segment creation. These are foundational for building the helix geometry. ```python from math import pi from OCC.Core.gp import gp_Pnt2d, gp, gp_Lin2d, gp_Ax3, gp_Dir2d from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge from OCC.Core.Geom import Geom_CylindricalSurface from OCC.Core.GCE2d import GCE2d_MakeSegment ``` -------------------------------- ### Build Helix Geometry with PythonOCC Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helix.ipynb Constructs a helix edge by defining a cylindrical surface and a 2D line segment. The GCE2d_MakeSegment creates a segment from the line, and BRepBuilderAPI_MakeEdge uses this segment and the cylinder to generate the helix edge. ```python # Build an helix aCylinder = Geom_CylindricalSurface(gp_Ax3(gp.XOY()), 6.0) aLine2d = gp_Lin2d(gp_Pnt2d(0.0, 0.0), gp_Dir2d(1.0, 1.0)) aSegment = GCE2d_MakeSegment(aLine2d, 0.0, pi * 2.0) helix_edge = BRepBuilderAPI_MakeEdge(aSegment.Value(), aCylinder, 0.0, 6.0 * pi).Edge() ``` -------------------------------- ### Import ipywidgets for Interactivity Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_ops.ipynb Imports the necessary components from the ipywidgets library to create interactive controls for the boolean operations demonstration. ```Python from ipywidgets import interact, interactive, fixed, interact_manual import ipywidgets as widgets ``` -------------------------------- ### Loading STEP File Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/triangle_mesh_gmsh.ipynb Loads a 3D model from a STEP file into a PythonOCC shape object. ```Python ventilator_shp = read_step_file( os.path.join("..", "assets", "models", "Ventilator.stp") ) ``` -------------------------------- ### Initialize JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helloworld.ipynb Initializes an instance of the JupyterRenderer, which is used to display 3D shapes within a Jupyter Notebook or similar environment. ```python my_renderer = JupyterRenderer() ``` -------------------------------- ### JupyterRenderer Initialization Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/triangle_mesh_gmsh.ipynb Initializes the JupyterRenderer for displaying 3D shapes within a Jupyter Notebook environment. ```Python my_renderer = JupyterRenderer(size=(900, 900)) ``` -------------------------------- ### Initialize WebGL Renderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_multiple_shapes.ipynb Initializes a JupyterRenderer for displaying shapes in a web environment. The renderer is configured with a specific size of 700x700 pixels. ```python my_renderer = JupyterRenderer(size=(700, 700)) ``` -------------------------------- ### Import Core BRepAlgoAPI and BRepBuilderAPI Modules Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_ops.ipynb Imports necessary classes from OCC.Core for performing boolean operations (Fuse, Common, Section, Cut) and building/transforming shapes. Also imports classes for creating primitive shapes like boxes, wedges, spheres, and tori. ```Python from OCC.Core.BRepAlgoAPI import ( BRepAlgoAPI_Fuse, BRepAlgoAPI_Common, BRepAlgoAPI_Section, BRepAlgoAPI_Cut, ) from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform from OCC.Core.BRepPrimAPI import ( BRepPrimAPI_MakeBox, BRepPrimAPI_MakeWedge, BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeTorus, ) from OCC.Core.gp import gp_Vec, gp_Ax2, gp_Pnt, gp_Dir, gp_Pln, gp_Trsf ``` -------------------------------- ### PythonOCC Imports for Topology Operations Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_shape_upgrade.ipynb Imports necessary classes from the PythonOCC library for performing topological operations such as fusing shapes and applying upgrades. Includes BRepAlgoAPI_Fuse, BRepBuilderAPI_Transform, BRepPrimAPI_MakeBox, gp_Vec, gp_Trsf, and ShapeUpgrade_UnifySameDomain. ```Python from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Fuse from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.Core.gp import gp_Vec, gp_Trsf from OCC.Core.ShapeUpgrade import ShapeUpgrade_UnifySameDomain ``` -------------------------------- ### Configure IFC Geometry Settings Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Sets up geometry processing settings for ifcopenshell, specifically enabling the use of pythonocc for shape creation. ```python settings = ifcopenshell.geom.settings() settings.set( settings.USE_PYTHON_OPENCASCADE, True ) # tells ifcopenshell to use pythonocc ``` -------------------------------- ### Initialize JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helix.ipynb Initializes an instance of the JupyterRenderer. This object will be used to display the geometric shapes created using PythonOCC within the Jupyter environment. ```python my_renderer = JupyterRenderer() ``` -------------------------------- ### Retrieve IFC Products Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Extracts all entities of type 'IfcProduct' from the opened IFC file, which represent the building elements. ```python products = ifc_file.by_type("IfcProduct") # traverse all IfcProducts ``` -------------------------------- ### Initialize JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/point_cloud.ipynb Initializes the JupyterRenderer, which is part of the OCC.Display.WebGl module, for displaying 3D shapes within a Jupyter Notebook environment. ```python my_renderer = JupyterRenderer() ``` -------------------------------- ### Load STEP file using pythonocc Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_one_shape.ipynb Reads a STEP file named 'RC_Buggy_2_front_suspension.stp' located in a relative path. This function is part of OCC.Extend.DataExchange and requires a valid file path as input. ```Python shp = read_step_file( os.path.join("..", "assets", "models", "RC_Buggy_2_front_suspension.stp") ) ``` -------------------------------- ### Initialize JupyterRenderer for visualization Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_one_shape.ipynb Creates an instance of JupyterRenderer with a specified size (800x700 pixels). This renderer is used to display OCC shapes directly within a Jupyter Notebook or similar environment. ```Python my_renderer = JupyterRenderer(size=(800, 700)) ``` -------------------------------- ### Display Solids with Random Colors Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_multiple_shapes.ipynb Iterates through the solids within the loaded STEP file's topology. Each solid is displayed using the initialized renderer with a randomly generated color and edge rendering enabled. ```python # loop over subshapes so that each subshape is meshed/displayed t = TopologyExplorer(shp) for solid in t.solids(): # random colors, just for fun random_color = format_color(randint(0, 255), randint(0, 255), randint(0, 255)) my_renderer.DisplayShape(solid, shape_color=random_color, render_edges=True) ``` -------------------------------- ### Specify IFC File Path Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Defines the path to the IFC model file and asserts that the file exists before proceeding with processing. ```python ifc_filename = os.path.join( "..", "assets", "ifc_models", "AC-11-Smiley-West-04-07-2007.ifc" ) assert os.path.isfile(ifc_filename) ``` -------------------------------- ### Import JupyterRenderer for Visualization Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_ops.ipynb Imports the JupyterRenderer class from OCC.Display.WebGl, which is used for displaying 3D shapes within a Jupyter Notebook environment. ```Python from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer ``` -------------------------------- ### Display Upgraded Shape in PythonOCC Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_shape_upgrade.ipynb Initializes a new JupyterRenderer and displays the upgraded shape after applying the UnifySameDomain tool. This visualization helps to see the effect of the unification process on the geometry. ```Python rnd_upgrade = JupyterRenderer() rnd_upgrade.DisplayShape(fused_shp_upgrade, render_edges=True, update=True) ``` -------------------------------- ### Open IFC File Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Opens the specified IFC file using ifcopenshell, making its contents available for further processing. ```python ifc_file = ifcopenshell.open(ifc_filename) ``` -------------------------------- ### Load STEP File Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_multiple_shapes.ipynb Loads a STEP file named 'RC_Buggy_2_front_suspension.stp' located in a relative path. This function is part of the OCC.Extend.DataExchange module. ```python shp = read_step_file( os.path.join("..", "assets", "models", "RC_Buggy_2_front_suspension.stp") ) ``` -------------------------------- ### PythonOCC Jupyter Renderer Import Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_shape_upgrade.ipynb Imports the JupyterRenderer class from the PythonOCC display module, used for rendering shapes within a Jupyter Notebook environment. ```Python from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer ``` -------------------------------- ### Render the display Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_one_shape.ipynb Finalizes and displays the rendered shape in the Jupyter environment. This command should be called after setting up the renderer and displaying the shape. ```Python my_renderer.Display() ``` -------------------------------- ### Displaying Shapes in Jupyter Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/triangle_mesh_gmsh.ipynb Displays the original ventilator shape (translated) and the meshed ventilator shape using the JupyterRenderer, with options for edge rendering and color. ```Python my_renderer.DisplayShape( translate_shp(ventilator_shp, gp_Vec(-100, 0, 0)), render_edges=True, shape_color="cyan", ) my_renderer.DisplayShape( meshed_ventilator_shp, render_edges=True, shape_color="cyan", update=True ) ``` -------------------------------- ### Perform Cut Operation Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_ops.ipynb Demonstrates the Cut operation by creating a box and a sphere, then cutting the sphere with the box. The box is displayed with transparency, and the resulting cut shape is shown with edges rendered. ```Python 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() rnd = JupyterRenderer() rnd.DisplayShape(Box, transparency=True, opacity=0.2) rnd.DisplayShape(Cut, render_edges=True) rnd.Display() ``` -------------------------------- ### Import JupyterRenderer for Display Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helix.ipynb Imports the JupyterRenderer class from OCC.Display.WebGl. This module is specifically used for rendering OCC shapes within a Jupyter Notebook or similar web-based environments. ```python from OCC.Display.WebGl.jupyter_renderer import JupyterRenderer ``` -------------------------------- ### Interactive Boolean Operation Selection Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_ops.ipynb Uses ipywidgets.interact to create a dropdown menu allowing the user to select and trigger different boolean operations (fuse, common, slicer, section, cut). ```Python interact(f, boolop=["fuse", "common", "slicer", "section", "cut"]); ``` -------------------------------- ### Extract Vertices from PCD File Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/point_cloud.ipynb This code snippet demonstrates how to read a PCD file after determining the number of vertices. It skips the header lines and then parses each subsequent line to extract X, Y, and Z coordinates, creating gp_Pnt objects. ```python pcd_file_name = os.path.join("..", "assets", "models", "bunny.pcd") # compute number of lines nbr_of_vertices = pcd_get_number_of_vertices(pcd_file_name) print("Number of vertices :", nbr_of_vertices) vertices = [] # fedd it with vertices fp = open(pcd_file_name, "r") # read 11 lines to skip header for i in range(10): fp.readline() for i in range(nbr_of_vertices): line = fp.readline() x, y, z = map(float, line.split()) vertices.append(gp_Pnt(x, y, z)) ``` -------------------------------- ### Perform Common Operation Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_ops.ipynb Demonstrates the Common (intersection) operation by creating a box and a wedge, then finding their common volume. Both original shapes and the common shape are displayed with transparency. ```Python def common(event=None): # Create Box axe = gp_Ax2(gp_Pnt(10, 10, 10), gp_Dir(1, 2, 1)) Box = BRepPrimAPI_MakeBox(axe, 60, 80, 100).Shape() # Create wedge Wedge = BRepPrimAPI_MakeWedge(60.0, 100.0, 80.0, 20.0).Shape() # Common surface CommonSurface = BRepAlgoAPI_Common(Box, Wedge).Shape() rnd = JupyterRenderer() rnd.DisplayShape(Box, transparency=True, opacity=0.2) rnd.DisplayShape(Wedge, transparency=True, opacity=0.2) rnd.DisplayShape(CommonSurface, render_edges=True) rnd.Display() ``` -------------------------------- ### Perform Slicer Operation Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_ops.ipynb Demonstrates slicing a sphere with multiple planes along the Z-axis. The original sphere is displayed, and the code includes commented-out sections for displaying the resulting slices. ```Python 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.0).Shape() # Define the direction D = gp_Dir(0.0, 0.0, 1.0) # 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) rnd = JupyterRenderer() rnd.DisplayShape(shape) rnd.Display() # for section_ in sections: # rnd.DisplayShape(section_.Shape()) ``` -------------------------------- ### Display Shape with JupyterRenderer Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/point_cloud.ipynb Uses the initialized JupyterRenderer to display a list of vertices (gp_Pnt objects). The `update=True` argument suggests that this display might be part of an interactive update cycle. ```python my_renderer.DisplayShape(vertices, update=True) ``` -------------------------------- ### Display Fused Boxes in PythonOCC Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_shape_upgrade.ipynb Initializes a JupyterRenderer and displays the fused shape. The render_edges parameter is set to True to show the boundaries of the original boxes within the fused shape. ```Python rnd = JupyterRenderer() rnd.DisplayShape(fused_shp, render_edges=True, update=True) ``` -------------------------------- ### Display loaded shape with edges Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_one_shape.ipynb Renders the loaded STEP shape ('shp') using the initialized JupyterRenderer. The 'render_edges=True' argument ensures that the edges of the shape are also displayed, providing a clearer view of the geometry. ```Python my_renderer.DisplayShape(shp, render_edges=True) ``` -------------------------------- ### Fuse Two Boxes in PythonOCC Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/boolean_shape_upgrade.ipynb Creates two box shapes using BRepPrimAPI_MakeBox and then fuses them together using BRepAlgoAPI_Fuse. The resulting fused shape is stored in fused_shp. ```Python box1 = BRepPrimAPI_MakeBox(10.0, 20.0, 30.0).Shape() box2 = BRepPrimAPI_MakeBox(20.0, 1.0, 30.0).Shape() fused_shp = BRepAlgoAPI_Fuse(box1, box2).Shape() ``` -------------------------------- ### Render Displayed Shapes Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/load_step_ap203_multiple_shapes.ipynb Triggers the display of all shapes that have been added to the renderer. This command finalizes the rendering process for the current view. ```python my_renderer.Display() ``` -------------------------------- ### Display Helix in Jupyter Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/helix.ipynb Renders the previously created helix_edge using the initialized JupyterRenderer. The `update=True` argument ensures that the display is refreshed immediately after the shape is rendered. ```python my_renderer.DisplayShape(helix_edge, update=True) ``` -------------------------------- ### Display Renderer Output Source: https://github.com/tpaviot/pythonocc-demos/blob/master/jupyter_notebooks/ifc_display_basic_file.ipynb Outputs the JupyterRenderer object to display the rendered IFC model in the notebook. ```python my_renderer ```