### Install wxPython GUI Framework Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Install the wxPython framework if you want to use it as the GUI backend. This is an alternative to PyQt5. ```bash pip install wxPython ``` -------------------------------- ### Install PyQt5 GUI Framework Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Install the PyQt5 framework if you want to use it as the GUI backend. This is a prerequisite for the GUI to appear. ```bash pip install PyQt5 ``` -------------------------------- ### Install pythonocc-core with Pip Source: https://github.com/tpaviot/pythonocc-core/wiki/Installation-with-conda This is the standard installation method for pythonocc-core. Ensure you have pip installed and updated. ```bash # Required: pythonocc-core # Install with: pip install pythonocc-core ``` -------------------------------- ### Install pythonOCC Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Install the built pythonOCC library to your system. This step makes the library available for import in Python. ```batch cmake --install . ``` -------------------------------- ### Check Installed Qt Packages Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md List installed packages that contain 'qt' to verify your Qt installation. This is useful for troubleshooting backend loading issues. ```bash pip list | grep -i qt ``` -------------------------------- ### Quick Installation Test Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Verify the pythonOCC installation by importing a core class and creating an object. This basic test confirms the library is accessible. ```python python3 >>> from OCC.Core.gp import gp_Pnt >>> p = gp_Pnt(1., 2., 3.) >>> p.X() 1.0 ``` -------------------------------- ### WebGL Renderer Setup Source: https://github.com/tpaviot/pythonocc-core/blob/master/src/Display/WebGl/templates/index.html Initializes the WebGLRenderer with antialiasing and alpha support, setting the size to match the window dimensions. ```javascript renderer = new THREE.WebGLRenderer({antialias:true, alpha: true}); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRa ``` -------------------------------- ### Install System Dependencies (Linux) Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Installs essential packages for building pythonOCC on Ubuntu. Ensure your system is updated before running. ```bash sudo apt-get update sudo apt-get install -y \ wget \ libglu1-mesa-dev \ libgl1-mesa-dev \ libxmu-dev \ libxi-dev \ build-essential \ cmake \ libfreetype6-dev \ tk-dev \ python3-dev \ rapidjson-dev \ python3 \ git \ python3-pip \ libpcre2-dev ``` -------------------------------- ### Shape Hierarchy Example Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/README.md Illustrates the topological hierarchy of shapes in pythonocc-core, starting from a compound down to a vertex. All shapes inherit from TopoDS_Shape. ```text TopoDS_Compound (container) ├─ TopoDS_Solid (3D volume) │ └─ TopoDS_Shell (surface) │ └─ TopoDS_Face (2D surface) │ └─ TopoDS_Wire (boundary) │ └─ TopoDS_Edge (1D curve) │ └─ TopoDS_Vertex (0D point) ``` -------------------------------- ### Run Demo Application Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Clone the pythonOCC demos repository and run an example script to see pythonOCC in action. This helps in understanding its capabilities. ```bash git clone https://github.com/tpaviot/pythonocc-demos cd pythonocc-demos python3 examples/core_classic_occ_bottle.py ``` -------------------------------- ### Print pythonocc-core Version Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/QUICK-START.md Use this snippet to display the currently installed version of the pythonocc-core library. It also includes commented-out links to online resources for documentation, examples, and issue tracking. ```python from OCC import VERSION print(VERSION) # Current version # Online resources # Documentation: https://github.com/tpaviot/pythonocc-documentation # Examples: https://github.com/tpaviot/pythonocc-demos # Issues: https://github.com/tpaviot/pythonocc-core/issues ``` -------------------------------- ### Create Conda Environment and Install pythonocc-core Source: https://github.com/tpaviot/pythonocc-core/wiki/Installation-with-conda Use this command to create a new Conda environment and install the pythonocc-core package. This is the recommended method for setting up your development environment. ```bash conda create -n pythonocc -c conda-forge pythonocc-core ``` -------------------------------- ### Build pythonOCC (Linux) Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Clones the pythonOCC repository, configures the build with OpenCascade paths, and installs it. The installation directory can be customized. ```bash git clone https://github.com/tpaviot/pythonocc-core.git cd pythonocc-core mkdir cmake-build && cd cmake-build # Set installation directory (optional) PYTHONOCC_INSTALL_DIRECTORY=${PYTHONOCC_INSTALL_DIRECTORY:-\/usr\/local} cmake \ -DOCCT_INCLUDE_DIR=/opt/occt790/include/opencascade \ -DOCCT_LIBRARY_DIR=/opt/occt790/lib \ -DCMAKE_BUILD_TYPE=Release \ -DPYTHONOCC_INSTALL_DIRECTORY=$PYTHONOCC_INSTALL_DIRECTORY \ .. make -j$(nproc) && sudo make install ``` -------------------------------- ### Install pythonocc-core with Conda or Pip Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/QUICK-START.md Use Conda for a managed installation or Pip for a direct installation. Ensure Python 3.10+ and OpenCascade runtime libraries are available. ```bash conda install -c conda-forge pythonocc-core=7.9.3 # or pip install pythonocc-core ``` -------------------------------- ### Display a Single Shape with SimpleGui Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Initializes the display, creates a box shape, displays it, and starts the GUI. Ensure OCC.Display.SimpleGui and OCC.Core.BRepPrimAPI are imported. ```python from OCC.Display.SimpleGui import init_display from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox # Initialize viewer display, start_display, add_menu, add_function_to_menu = init_display() # Create and display a shape box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() display.DisplayShape(box, update=True) # Start GUI start_display() ``` -------------------------------- ### Example Usage of TopologyExplorer Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Demonstrates how to create a TopologyExplorer instance and extract all faces, edges, and vertices from a shape. ```python from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() explorer = TopologyExplorer(box, ignore_orientation=True) # Traverse all entities all_faces = list(explorer.faces()) all_edges = list(explorer.edges()) all_vertices = list(explorer.vertices()) ``` -------------------------------- ### Run Full Test Suite Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Install the pytest framework and run the complete pythonOCC test suite to ensure all functionalities are working correctly. ```bash pip install pytest pytest ``` -------------------------------- ### Build SWIG from Source (Linux) Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Compiles and installs SWIG version 4.3.0 from source, as it's not available in default Ubuntu repositories. Requires downloading, extracting, configuring, and installing. ```bash wget http://prdownloads.sourceforge.net/swig/swig-4.3.0.tar.gz tar -zxvf swig-4.3.0.tar.gz cd swig-4.3.0 ./configure make -j$(nproc) sudo make install ``` -------------------------------- ### Check Installed wx Packages Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md List installed packages that contain 'wx' to verify your wxPython installation. This is useful for troubleshooting backend loading issues. ```bash pip list | grep -i wx ``` -------------------------------- ### Verify SWIG Installation Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Check the installed SWIG version using `swig -version`. Ensure no conflicting versions are present, especially on Linux. ```bash swig -version ``` -------------------------------- ### Installation Configuration Source: https://github.com/tpaviot/pythonocc-core/blob/master/CMakeLists.txt Defines the file extension for shared libraries based on the operating system (Windows vs. others) and sets up the installation paths for PythonOCC modules and their associated files. ```cmake if(WIN32) set(EXTENSION "pyd") else(WIN32) set(EXTENSION "so") endif(WIN32) set(BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${LIBRARY_OUTPUT_PATH}) # install pythonOCC modules foreach(OCCT_MODULE ${OCCT_TOOLKIT_MODEL}) install(FILES ${BUILD_DIR}/${OCCT_MODULE}.py DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core) install(TARGETS ${OCCT_MODULE} DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/SWIG_files/wrapper/${OCCT_MODULE}.pyi DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY}/Core) endforeach(OCCT_MODULE) ``` -------------------------------- ### Specify Tkinter Backend for Display Initialization Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/configuration.md Use this option when PyQt5 is not installed or to ensure compatibility. Tkinter is always available. ```python display, _, _, _ = init_display(backend_str="tk") ``` -------------------------------- ### Minimal Viewer Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Initializes the display and shows a basic sphere shape. This is a starting point for visualizing shapes. ```python from OCC.Display.SimpleGui import init_display from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeSphere display, start_display, _, _ = init_display() shape = BRepPrimAPI_MakeSphere(5.0).Shape() display.DisplayShape(shape) start_display() ``` -------------------------------- ### Install Additional Python Packages Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Install extra Python packages for full pythonOCC functionality, including SVG generation, NumPy, Matplotlib, and PyQt5. ```bash pip install svgwrite numpy numpy-stl matplotlib PyQt5 ``` -------------------------------- ### Configure config.py.in on Windows Source: https://github.com/tpaviot/pythonocc-core/blob/master/CMakeLists.txt Generates a configuration file for Windows when OCCT_ESSENTIALS_ROOT is defined. This file is then installed to the Python site-packages directory. ```cmake if(WIN32 AND OCCT_ESSENTIALS_ROOT) # Générer le fichier config.py.in file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/config.py.in "# Configuration file generated by CMake OCCT_ESSENTIALS_ROOT = \"@OCCT_ESSENTIALS_ROOT@\" ") configure_file( ${CMAKE_CURRENT_BINARY_DIR}/config.py.in ${CMAKE_CURRENT_BINARY_DIR}/config.py @ONLY ) set(OCCT_ESSENTIALS_PATH ${OCCT_ESSENTIALS_ROOT}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/config.py DESTINATION ${PYTHONOCC_INSTALL_DIRECTORY} ) endif() ``` -------------------------------- ### Build OpenCascade (Linux) Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Downloads, configures, and builds OpenCascade version 7.9.0. It specifies an installation directory and enables exceptions. ```bash wget https://github.com/Open-Cascade-SAS/OCCT/archive/refs/tags/V7_9_0.tar.gz tar -xvzf V7_9_0.tar.gz cd OCCT-7_9_0 mkdir cmake-build cd cmake-build cmake -DINSTALL_DIR=/opt/occt790 \ -DBUILD_RELEASE_DISABLE_EXCEPTIONS=OFF \ .. make -j$(nproc) sudo make install ``` -------------------------------- ### Export Shape to SVG Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/errors.md Example of exporting a shape to an SVG file after ensuring the 'svgwrite' dependency is installed. This function requires the 'svgwrite' package to be available. ```python from OCC.Extend.DataExchange import export_shape_to_svg export_shape_to_svg(shape, filename="output.svg") ``` -------------------------------- ### Initialize Interactive 3D Viewer Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/configuration.md Set up the display environment for interactive 3D visualization using the PyQt5 backend. Configure window size and background gradient. ```python from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display( backend_str="pyqt5", size=(1280, 960), display_triedron=True, background_gradient_color1=[240, 240, 240], background_gradient_color2=[200, 200, 200] ) ``` -------------------------------- ### Initialize Display with Default Settings Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/configuration.md Initializes the display with default settings for backend, size, and colors. Imports the necessary function. ```python from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display( backend_str="pyqt5", # GUI framework size=(1024, 768), # Window dimensions display_triedron=True, # Show XYZ indicator background_gradient_color1=[206, 215, 222], # Top gradient color background_gradient_color2=[128, 128, 128] # Bottom gradient color ) ``` -------------------------------- ### Specify 3D Display Backend Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/QUICK-START.md Initialize the display, optionally forcing a specific backend like pyqt5, pyqt6, pyside2, pyside6, wx, or tk. ```python # Auto-detect (default) display, _, _, _ = init_display() # Force specific backend display, _, _, _ = init_display(backend_str="pyqt5") # pyqt5, pyqt6, pyside2, pyside6, wx, tk ``` -------------------------------- ### Basic 3D Visualization Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/PROJECT-OVERVIEW.md Initializes the display backend and visualizes a simple box shape. Call `start_display()` to open the GUI. ```python from OCC.Display.SimpleGui import init_display from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox display, start_display, add_menu, add_function_to_menu = init_display() box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() display.DisplayShape(box) start_display() ``` -------------------------------- ### Check svgwrite Installation Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/data-exchange.md Verifies if the svgwrite package is installed. Raises an IOError if the package is not found. ```python def check_svgwrite_installed() -> None ``` -------------------------------- ### Specify Backend Explicitly with SimpleGui Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Forces the use of the PyQt5 backend for the SimpleGui display and sets a custom window size. The rest of the code follows this initialization. ```python from OCC.Display.SimpleGui import init_display # Force PyQt5 backend display, start_display, add_menu, add_function_to_menu = init_display( backend_str="pyqt5", size=(1280, 1024) ) # ... rest of code ``` -------------------------------- ### Initialize WebGL Renderer and Scene Source: https://github.com/tpaviot/pythonocc-core/blob/master/src/Display/WebGl/templates/index.html Sets up the WebGL renderer, attaches it to the DOM, and configures shadow mapping. This is typically called once during application startup. ```javascript renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFShadowMap; controls = new THREE.TrackballControls(camera, renderer.domElement); stats = new Stats(); stats.domElement.style.position = 'absolute'; stats.domElement.style.top = '2%'; stats.domElement.style.left = '1%'; container.appendChild(stats.domElement); ``` -------------------------------- ### Install pythonocc-core with Conda Source: https://github.com/tpaviot/pythonocc-core/blob/master/README.md Installs pythonocc-core version 7.9.3 for Python 3.12 using conda. Ensure you create and activate a new environment first. ```bash # first create an environment conda create --name=pyoccenv python=3.12 conda activate pyoccenv conda install -c conda-forge pythonocc-core=7.9.3 ``` -------------------------------- ### Set Window Size Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/configuration.md Configure the width and height of the display window in pixels. Use (0, 0) to let Qt decide defaults. ```python display, _, _, _ = init_display(size=(1920, 1080)) # Full HD display, _, _, _ = init_display(size=(800, 600)) # Small window ``` -------------------------------- ### Set up Event Listeners Source: https://github.com/tpaviot/pythonocc-core/blob/master/src/Display/WebGl/templates/index.html Attaches event listeners for keyboard, mouse clicks, and window resizing to enable user interaction and responsiveness. ```javascript document.addEventListener('keypress', onDocumentKeyPress, false); document.addEventListener('click', onDocumentMouseClick, false); window.addEventListener('resize', onWindowResize, false); ``` -------------------------------- ### Compute Linear Properties using GProp_GProps Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/types.md Shows how to initialize GProp_GProps and use brepgprop.LinearProperties to calculate properties like mass for a shape. ```python from OCC.Core.GProp import GProp_GProps from OCC.Core.BRepGProp import brepgprop props = GProp_GProps() brepgprop.LinearProperties(shape, props) # For wires/edges mass = props.Mass() ``` -------------------------------- ### Install NumPy for pythonOCC Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md Install the NumPy library using pip. This is required for enabling fast STL file loading with NumPy support in pythonOCC. ```bash pip install numpy ``` -------------------------------- ### Install svgwrite for SVG Export Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/errors.md Provides the command to install the 'svgwrite' package, which is a dependency for SVG export functionality in pythonocc-core. This is required before attempting SVG export. ```bash pip install svgwrite ``` -------------------------------- ### Configure OCE Include and Library Paths Source: https://github.com/tpaviot/pythonocc-core/blob/master/CMakeLists.txt Sets up include and library directories for OpenCASCADE (OCE). It either uses provided OCCT_INCLUDE_DIR and OCCT_LIBRARY_DIR or attempts to find the package automatically. If not found, it falls back to default paths. ```cmake if(DEFINED OCCT_INCLUDE_DIR) if(NOT DEFINED OCCT_LIBRARY_DIR) message(FATAL_ERROR "OCCT_LIBRARY_DIR must be defined") endif(NOT DEFINED OCCT_LIBRARY_DIR) set(OCCT_INCLUDE_DIR ${OCCT_INCLUDE_DIR} CACHE PATH "OCCT include path") set(OCCT_LIBRARY_DIR ${OCCT_LIBRARY_DIR} CACHE PATH "OCCT library path") include_directories(${OCCT_INCLUDE_DIR}) link_directories(${OCCT_LIBRARY_DIR}) # if OCCT_INCLUDE_DIR is not passed at command line, # find OCE automatically else(OCCT_INCLUDE_DIR) find_package(OpenCASCADE ${OCCT_VERSION_MAJOR}.${OCCT_VERSION_MINOR}.${OCCT_VERSION_PATCH} EXACT REQUIRED) if(OpenCASCADE_FOUND) message(STATUS "OpenCASCADE version found: " ${OpenCASCADE_MAJOR_VERSION} "." ${OpenCASCADE_MINOR_VERSION} "." ${OpenCASCADE_MAINTENANCE_VERSION}) message(STATUS "OpenCASCADE include directory: " ${OpenCASCADE_INCLUDE_DIR}) message(STATUS "OpenCASCADE binary directory: " ${OpenCASCADE_BINARY_DIR}) include_directories(${OpenCASCADE_INCLUDE_DIR}) else(OpenCASCADE_FOUND) # set default paths set(OCCT_INCLUDE_DIR /usr/local/include/opencascade CACHE PATH "OpenCASCADE include path") set(OCCT_LIBRARY_DIR /usr/local/lib CACHE PATH "OpenCASCADE lib path") include_directories(${OCCT_INCLUDE_DIR}) link_directories(${OCCT_LIBRARY_DIR}) endif(OpenCASCADE_FOUND) endif(DEFINED OCCT_INCLUDE_DIR) ``` -------------------------------- ### Configure Visualization Module Source: https://github.com/tpaviot/pythonocc-core/blob/master/CMakeLists.txt Sets up the build for the visualization module, including finding OpenGL and adding SWIG-generated libraries. This is enabled when PYTHONOCC_WRAP_VISU is set. ```cmake if(PYTHONOCC_WRAP_VISU) find_package(OpenGL REQUIRED) if(OPENGL_FOUND) message(STATUS "OpenGL found; Visualization support enabled") endif() include_directories(${OPENGL_INCLUDE_DIR}) foreach(OCCT_MODULE ${OCCT_TOOLKIT_VISUALIZATION}) set(FILE ${SWIG_FILES_PATH}/${OCCT_MODULE}.i) set_source_files_properties(${FILE} PROPERTIES CPLUSPLUS ON) swig_add_library (${OCCT_MODULE} LANGUAGE python SOURCES ${FILE} TYPE MODULE) target_link_libraries(${OCCT_MODULE} ${OCCT_MODEL_LIBRARIES} ${OCCT_VISUALIZATION_LIBRARIES} Python3::Module) endforeach(OCCT_MODULE) # Build third part modules # TODO : the following line is strange but necessary execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory src/Visualization) set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization/Visualization.i PROPERTIES CPLUSPLUS ON) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization) set(VISUALIZATION_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization/Visualization.i ${CMAKE_CURRENT_SOURCE_DIR}/src/Visualization/Display3d.cpp) swig_add_library(Visualization LANGUAGE python SOURCES ${VISUALIZATION_SOURCE_FILES} TYPE MODULE) target_link_libraries(Visualization ${OCCT_MODEL_LIBRARIES} ${OCCT_VISUALIZATION_LIBRARIES} Python3::Module) if(APPLE) # on OSX, always add /System/Library/Frameworks/Cocoa.framework, even # if GLX is enabled target_link_libraries(Visualization /System/Library/Frameworks/Cocoa.framework) endif(APPLE) ########################## # MeshDS module addition # ########################## execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory src/MeshDataSource) set(FILE ${SWIG_FILES_PATH}/MeshDS.i) set_source_files_properties(${FILE} PROPERTIES CPLUSPLUS ON) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/MeshDataSource) set(MESHDATASOURCE_SOURCE_FILES ${FILE} ${CMAKE_CURRENT_SOURCE_DIR}/src/MeshDataSource/MeshDataSource.cpp) swig_add_library(MeshDS LANGUAGE python SOURCES ${MESHDATASOURCE_SOURCE_FILES} TYPE MODULE) target_link_libraries(MeshDS ${OCCT_MODEL_LIBRARIES} ${OCCT_VISUALIZATION_LIBRARIES} Python3::Module) endif(PYTHONOCC_WRAP_VISU) ``` -------------------------------- ### Determine PythonOCC Installation Directory Source: https://github.com/tpaviot/pythonocc-core/blob/master/CMakeLists.txt Sets the installation directory for pythonocc modules. It prioritizes the SP_DIR environment variable on Windows (for Azure specific cases) or dynamically determines the 'platlib' path using Python's sysconfig module. ```cmake if(NOT DEFINED PYTHONOCC_INSTALL_DIRECTORY) if(DEFINED ENV{SP_DIR} AND WIN32) # TODO: following hack is azure specific, a recent update in azure # prevent cmake to find correct paths of python3 on windows # this should be removed as soon as possible message(STATUS "conda-build running, using $ENV{SP_DIR} as install dir") set(PYTHONOCC_INSTALL_DIRECTORY $ENV{SP_DIR}/OCC CACHE PATH "pythonocc install directory") else(DEFINED ENV{SP_DIR} AND WIN32) execute_process( COMMAND ${Python3_EXECUTABLE} -c "import sysconfig, os, sys; print(os.path.relpath(sysconfig.get_path('platlib'), sys.prefix))" OUTPUT_VARIABLE python_lib OUTPUT_STRIP_TRAILING_WHITESPACE) set(PYTHONOCC_INSTALL_DIRECTORY ${python_lib}/OCC CACHE PATH "pythonocc install directory") endif(DEFINED ENV{SP_DIR} AND WIN32) endif(NOT DEFINED PYTHONOCC_INSTALL_DIRECTORY) ``` -------------------------------- ### check_svgwrite_installed Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/data-exchange.md Verifies if the svgwrite package is installed. Raises an IOError if the package is not found. ```APIDOC ## check_svgwrite_installed ### Description Verifies that the svgwrite package is installed. Raises `IOError` if not found. ### Function Signature ```python def check_svgwrite_installed() -> None ``` ``` -------------------------------- ### Load and Display STEP File Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/README.md Loads a STEP file and displays the resulting shape using the simple GUI. Ensure the 'model.step' file exists in the same directory. ```python from OCC.Display.SimpleGui import init_display from OCC.Extend.DataExchange import read_step_file display, start_display, _, _ = init_display() shape = read_step_file("model.step") display.DisplayShape(shape) start_display() ``` -------------------------------- ### Custom Window Size and Colors with SimpleGui Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Initializes SimpleGui with a custom window size (1920x1080), enables the display of the triedron (coordinate system axes), and sets a white-to-black background gradient. ```python from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display( size=(1920, 1080), display_triedron=True, background_gradient_color1=[255, 255, 255], # White background_gradient_color2=[0, 0, 0] # Black ) ``` -------------------------------- ### Get Iterator of Compounds Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all compounds within the shape. This method is part of the TopologyExplorer class. ```python def compounds(self) -> Iterator[TopoDS_Compound]: ``` -------------------------------- ### Get Iterator of Solids Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all solids within the shape. This method is part of the TopologyExplorer class. ```python def solids(self) -> Iterator[TopoDS_Solid]: ``` -------------------------------- ### Interactive Model Viewer with Menu Options Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/QUICK-START.md Create an interactive 3D viewer using PyQt5 backend. Allows users to switch between displaying faces and edges of a loaded STEP model via a menu. Initializes display with custom background colors. ```python from OCC.Display.SimpleGui import init_display from OCC.Extend.DataExchange import read_step_file from OCC.Extend.TopologyUtils import TopologyExplorer display, start_display, add_menu, add_function_to_menu = init_display( backend_str="pyqt5", size=(1280, 1024), background_gradient_color1=[240, 240, 240], background_gradient_color2=[200, 200, 200] ) shape = read_step_file("assembly.step") def show_faces(): explorer = TopologyExplorer(shape) display.EraseAll() for i, face in enumerate(explorer.faces()): display.DisplayShape(face, color="BLUE", update=(i == explorer.number_of_faces() - 1)) def show_edges(): explorer = TopologyExplorer(shape) display.EraseAll() for i, edge in enumerate(explorer.edges()): display.DisplayShape(edge, color="RED", update=(i == explorer.number_of_edges() - 1)) add_menu("View") add_function_to_menu("View", show_faces) add_function_to_menu("View", show_edges) display.DisplayShape(shape) start_display() ``` -------------------------------- ### OCC.Display.SimpleGui Functions Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/INDEX.md Functions for initializing the display module. ```APIDOC ## init_display() ### Description Initializes the display module. ### Method Not specified (assumed to be a function call). ### Endpoint Not applicable. ### Parameters None specified. ### Request Example ```python init_display() ``` ### Response None specified. ``` -------------------------------- ### Get Iterator of Shells Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all shells within the shape. This method is part of the TopologyExplorer class. ```python def shells(self) -> Iterator[TopoDS_Shell]: ``` -------------------------------- ### Add and Populate Menus Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Demonstrates how to create top-level menus and add functions to them. Function names are used as menu item labels, with underscores replaced by spaces. ```python # Add a top-level menu add_menu("File") add_menu("Edit") add_menu("View") # Add function to menu (uses function name as label) def on_exit(): import sys sys.exit(0) add_function_to_menu("File", on_exit) # Creates "on_exit" menu item ``` -------------------------------- ### Get Iterator of Faces Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all faces within the shape. This method is part of the TopologyExplorer class. ```python def faces(self) -> Iterator[TopoDS_Face]: ``` -------------------------------- ### Get Iterator of Wires Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all wires within the shape. This method is part of the TopologyExplorer class. ```python def wires(self) -> Iterator[TopoDS_Wire]: ``` -------------------------------- ### Get Iterator of Edges Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all edges within the shape. This method is part of the TopologyExplorer class. ```python def edges(self) -> Iterator[TopoDS_Edge]: ``` -------------------------------- ### Initialize SimpleGui Display Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Initializes a 3D viewer with automatic backend detection. Supports customization of window size, triedron display, and background gradient colors. Can also return an offscreen renderer if the PYTHONOCC_OFFSCREEN_RENDERER environment variable is set. ```python from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() display.View.Dump("screenshot.png") ``` -------------------------------- ### Get Iterator of Vertices Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all vertices within the shape. This method is part of the TopologyExplorer class. ```python def vertices(self) -> Iterator[TopoDS_Vertex]: ``` -------------------------------- ### Create Basic Geometry with pythonocc-core Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/QUICK-START.md Shows how to create fundamental geometric entities like points, edges, wires, and faces using ShapeFactory. ```python from OCC.Extend.ShapeFactory import ( make_vertex, make_edge, make_wire, make_face, make_n_sided ) from OCC.Core.gp import gp_Pnt # Points v1 = make_vertex(gp_Pnt(0, 0, 0)) v2 = make_vertex(1, 2, 3) # Line between points edge = make_edge(gp_Pnt(0, 0, 0), gp_Pnt(10, 0, 0)) # Wire from edges wire = make_wire([edge1, edge2, edge3, edge4]) # Face from wire face = make_face(wire) # N-sided face from edges (fills interior with surface) face = make_n_sided([edge1, edge2, edge3, edge4]) ``` -------------------------------- ### Bnd_Box Methods Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/types.md Demonstrates the creation and basic usage of a Bnd_Box, including setting a gap tolerance and retrieving its boundaries. ```python from OCC.Core.Bnd import Bnd_Box bbox = Bnd_Box()box.SetGap(1e-6) # Tolerance xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() ``` -------------------------------- ### ordered_edges_from_wire Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Get an iterator over the edges of a wire in connection order. This function is useful for traversing the edges of a wire sequentially. ```APIDOC ## ordered_edges_from_wire ### Description Get an iterator over the edges of a wire in connection order. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Method Signature ```python def ordered_edges_from_wire(wire: TopoDS_Wire) -> Iterator[TopoDS_Edge] ``` ### Parameters - **wire** (TopoDS_Wire) - Required - The wire to explore ### Return Iterator of `TopoDS_Edge` in traversal order ### Example ```python from OCC.Extend.TopologyUtils import ordered_edges_from_wire for edge in ordered_edges_from_wire(wire): print(edge) ``` ``` -------------------------------- ### Apply Transformations with gp_Trsf Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/types.md Shows how to initialize a transformation matrix (gp_Trsf) and apply common transformations like translation, rotation, and scaling. ```python trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(5, 10, 15)) # Translation trsf.SetRotation(gp_Ax1(...), 0.5236) # Rotation (radians) trsf.SetScale(gp_Pnt(...), 2.0) # Scaling ``` -------------------------------- ### ordered_vertices_from_wire Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Get an iterator over the vertices of a wire in connection order. This function is useful for traversing the vertices of a wire sequentially. ```APIDOC ## ordered_vertices_from_wire ### Description Get an iterator over the vertices of a wire in connection order. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Method Signature ```python def ordered_vertices_from_wire(wire: TopoDS_Wire) -> Iterator[TopoDS_Vertex] ``` ### Parameters - **wire** (TopoDS_Wire) - Required - The wire to explore ### Return Iterator of `TopoDS_Vertex` in traversal order ### Example ```python from OCC.Extend.TopologyUtils import ordered_vertices_from_wire for vertex in ordered_vertices_from_wire(wire): print(vertex) ``` ``` -------------------------------- ### Initialize Display with Explicit Size Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/configuration.md Ensure the 'size' parameter is always provided to avoid errors related to window dimensions. ```python display, _, _, _ = init_display(size=(1024, 768)) # Don't pass None ``` -------------------------------- ### Force GUI Backend Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Force the use of a specific GUI backend, such as pyqt5, by passing the backend_str argument to init_display. This helps resolve issues with the wrong backend being loaded. ```python init_display(backend_str="pyqt5") ``` -------------------------------- ### CMake GUI Configuration Variables Source: https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md When using CMake GUI, set these variables to point to your OpenCascade installation before generating the build files. ```text OCCT_INCLUDE_DIR=C:\OpenCASCADE-7.9.0-vc10-64\occt-7.9.0\inc OCCT_LIBRARY_DIR=C:\OpenCASCADE-7.9.0-vc10-64\occt-7.9.0\win64\vc14\lib ``` -------------------------------- ### Specify Window Size Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Ensure the 'size' parameter is not None when initializing the GUI. Provide a default or specify a tuple for the desired window dimensions. ```python size=(1024, 768) ``` -------------------------------- ### Get Ordered Edges from Wire Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Iterates over the edges of a TopoDS_Wire in their connection order. Ensure the 'wire' object is a valid TopoDS_Wire instance. ```python from OCC.Extend.TopologyUtils import ordered_edges_from_wire for edge in ordered_edges_from_wire(wire): print(edge) ``` -------------------------------- ### Get Ordered Vertices from Wire Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Iterates over the vertices of a TopoDS_Wire in their connection order. Ensure the 'wire' object is a valid TopoDS_Wire instance. ```python from OCC.Extend.TopologyUtils import ordered_vertices_from_wire for vertex in ordered_vertices_from_wire(wire): print(vertex) ``` -------------------------------- ### Force Backend Selection Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/configuration.md Explicitly set the GUI backend to use. Auto-detection is the default if `backend_str` is None. ```python # Force Qt5 display, _, _, _ = init_display(backend_str="pyqt5") # Force wxPython display, _, _, _ = init_display(backend_str="wx") # Auto-detect (default) display, _, _, _ = init_display(backend_str=None) ``` -------------------------------- ### Get Iterator of Composite Solids Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all composite solids (CompSolid) within the shape. This method is part of the TopologyExplorer class. ```python def comp_solids(self) -> Iterator[TopoDS_CompSolid]: ``` -------------------------------- ### init_display Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Initializes a GUI for 3D visualization with automatic backend detection. It creates a 3D viewer window with menu support across multiple GUI frameworks. If the `PYTHONOCC_OFFSCREEN_RENDERER` environment variable is set to "1", it returns an offscreen renderer instead. ```APIDOC ## init_display ### Description Initializes a GUI for 3D visualization with automatic backend detection. Creates a 3D viewer window with menu support across multiple GUI frameworks. If `PYTHONOCC_OFFSCREEN_RENDERER` environment variable is "1", returns an offscreen renderer instead. ### Method `init_display` ### Parameters #### Optional Parameters - **backend_str** (str) - Optional - GUI backend to use: "tk" (tkinter), "wx" (wxPython), "pyqt5", "pyqt6", "pyside2", "pyside6". If None, auto-detect (tries PyQt5/6, PySide2/6, wxPython, then tkinter). - **size** (Tuple[int, int]) - Optional - Window dimensions in pixels (width, height). Defaults to (1024, 768). - **display_triedron** (bool) - Optional - If True, show the XYZ triedron orientation indicator. Defaults to True. - **background_gradient_color1** (List[int]) - Optional - RGB color for gradient top: [R, G, B] where 0-255. Defaults to [206, 215, 222]. - **background_gradient_color2** (List[int]) - Optional - RGB color for gradient bottom: [R, G, B] where 0-255. Defaults to [128, 128, 128]. ### Return Tuple of four objects: - `Viewer3d display` — The 3D viewer instance for shape display. - `Callable start_display()` — Function to start the GUI main loop. - `Callable add_menu(menu_name: str)` — Function to add a top-level menu. - `Callable add_function_to_menu(menu_name: str, func: Callable)` — Function to add menu items. ### Return Type Details The returned `Viewer3d` instance has the following key methods: - `display.DisplayShape(shape, update=True)` — Add shape to view. - `display.FitAll()` — Auto-fit view to all shapes. - `display.View.Dump(filename)` — Screenshot to file. ### Raises - `AssertionError`: If `size` is None. - `ImportError`: If no GUI framework is available and not in offscreen mode. ``` -------------------------------- ### Quantity_Color Construction and Access Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/types.md Demonstrates creating a red color using RGB values and accessing its individual color components. ```python from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB # From RGB (0.0-1.0) color = Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB) # Red # Access components red = color.Red() green = color.Green() blue = color.Blue() color_name = color.Name(red, green, blue) ``` -------------------------------- ### Check for Null Shape After Construction Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/errors.md Always verify that a shape is not null after construction operations. This example shows how to check the result of BRepPrimAPI_MakeBox. ```python from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox # Always check for null after construction box = BRepPrimAPI_MakeBox(10, 20, 30) if box.IsDone(): shape = box.Shape() else: raise AssertionError("Box construction failed") ``` -------------------------------- ### Create Primitive Shapes with pythonocc-core Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/QUICK-START.md Demonstrates the creation of basic 3D shapes such as boxes, spheres, cylinders, cones, and tori using BRepPrimAPI. ```python from OCC.Core.BRepPrimAPI import ( BRepPrimAPI_MakeBox, BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeCylinder, BRepPrimAPI_MakeCone, BRepPrimAPI_MakeTorus ) box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() sphere = BRepPrimAPI_MakeSphere(5.0).Shape() cylinder = BRepPrimAPI_MakeCylinder(3.0, 10.0).Shape() cone = BRepPrimAPI_MakeCone(5.0, 2.0, 10.0).Shape() torus = BRepPrimAPI_MakeTorus(5.0, 2.0).Shape() ``` -------------------------------- ### Add Menus and Functions to SimpleGui Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/simple-gui.md Demonstrates how to add custom menus and functions to the SimpleGui interface for displaying different shapes and resetting the view. Requires imports for display and shape creation. ```python from OCC.Display.SimpleGui import init_display from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeCone display, start_display, add_menu, add_function_to_menu = init_display() def show_sphere(): sphere = BRepPrimAPI_MakeSphere(5.0).Shape() display.DisplayShape(sphere, update=True) def show_cone(): cone = BRepPrimAPI_MakeCone(3.0, 1.0, 8.0).Shape() display.DisplayShape(cone, update=True) def reset_view(): display.FitAll() # Create menu structure add_menu("Shapes") add_function_to_menu("Shapes", show_sphere) add_function_to_menu("Shapes", show_cone) add_menu("View") add_function_to_menu("View", reset_view) start_display() ``` -------------------------------- ### Get Wires of a Face Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Retrieves an iterator for all wires associated with a specific face, including outer wires and holes. This method is part of the TopologyExplorer class. ```python def face_wires(self, face: TopoDS_Face) -> Iterator[TopoDS_Wire]: ``` -------------------------------- ### Iterate Over Ordered Vertices of a Wire Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/api-reference/topology-utils.md Use WireExplorer to get an iterator over the vertices of a wire in their connection order. This is useful for accessing vertex data sequentially. ```python explorer = WireExplorer(wire) for vertex in explorer.ordered_vertices(): print(f"Vertex: {vertex}") ``` -------------------------------- ### Construct and Use gp_Pnt Source: https://github.com/tpaviot/pythonocc-core/blob/master/_autodocs/types.md Demonstrates the construction of a 3D point (gp_Pnt) with specified coordinates and how to extract its X, Y, and Z values. ```python p = gp_Pnt(10.0, 20.0, 30.0) x = p.X() # Extract coordinates y = p.Y() z = p.Z() ```