PythonOCC getting started guide

Alexander

Looking around for some CAD
Hi Quaoar, thanks a lot for these great tutorials. I am looking forward to the next episodes!

It is actually straight forward to get started with PythonOCC, but it requires the conda package manager. Here is a guide:

Get conda package manager
Install the anaconda distribution (individual edition). You can obtain an installer for your OS here. The installation will give you the conda package manager and a base environment containing python together with a lot of commonly used python packages for data science (e.g. numpy, matplotlib and more).

Note:
  • The windows installation wizard will by default not add anaconda to your path (but there is a non-recommended option to do so (which I always use, alternatively you can use the shell coming with anaconda))
  • Depending on your shell/terminal you may have to run some initialization script after installation.

If you don't want the additional bloat (i.e. data science packages) comming with a full anaconda installation (which seems unnecessary if the only purpose is to install PythonOCC), then you may want to install miniconda instead. I imagine the installation procedure is very similar, but I never tried miniconda. From a quick google search it seems like you can get an installer to miniconda here

Obtaining PythonOCC using conda
Open your favorite shell (that has conda in its path), and run the following three commands (you may be prompted for a y/n answer):
  • conda create -n pyoccenv python=3.8
  • conda activate pyoccenv
  • conda install -c conda-forge pythonocc-core

First command can be omitted if you don't want to create a separate environment for PythonOCC. In that case the second command should be changed to 'conda activate base'.

You are now good to go. You can test the installation by activating the environment containing PythonOCC (e.g. 'conda activate base' or 'conda activate pyoccenv') and run
  • python -c "import OCC; print(OCC.VERSION)"


Resources
PythonOCC has a lot of examples in one of their github repos which you might find interesting. You can find them here:

https://github.com/tpaviot/pythonocc-demos

The examples requires a gui backend (for instance PySide2) to be installed in the conda environment containing PythonOCC. One warning, though, I had to make a small change (using PySide2 backend) to get the examples to work (can't remember exactly what, but when having the error message it was quite easy to google).

Alternatives to PythonOCC
PythonOCC seems to have the largest community and a lot of good examples, but in case you are interested there are two other projects (that I know of) that provides python bindings to OpenCascade:
 

Quaoar

Administrator
Staff member
@Alexander wow, that was easy and straightforward, as you promised :D Thanks a lot for this thorough and well-structured memo. I'm sure it deserves a separate thread so that other folks could easily find it. Let me see if this forum engine allows for such a trick.

I'll add some notes down below for noobs like me. My session with PythonOCC starts from the following commands in Anaconda Prompt:

Code:
> conda activate pyoccenv
> python -c "import OCC; print(OCC.VERSION)"
> python ../lessons/extras_PyTest/test.py

The last command is to run a demo script (see in the following posts).

1626432527133.png

The PythonOCC environment is listed in Anaconda Navigator, btw. It should mean that this environment is kinda up and running.

1635838694475.png

Update from some point in the future:

The windows installation wizard will by default not add anaconda to your path (but there is a non-recommended option to do so (which I always use, alternatively you can use the shell coming with anaconda))
Having anaconda in the global path has some non-obvious implications. The thing is that anaconda will bring its runtime to your apps. That happened to me when I started my app from Visual Studio IDE. Have a look here:

1635934834989.png
What it means is that on launch time the anaconda's Qt was loaded instead of mine, and, of course, you cannot expect that Qt versions are binary compatible. So, watch out!

Update:

Video animating the instruction by Alexander:


FreeCAD about conda: https://forum.freecadweb.org/viewtopic.php?t=39656
 
Last edited:

Quaoar

Administrator
Staff member
So I started off with the following program:

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()
my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()

display.DisplayShape(my_box, update=True)
start_display()

Then installed PySide2 in conda:

Code:
conda install -c conda-forge pyside2

Executing it from the console led to the following error:

(pyoccenv) C:\Users\serge>python c:\Users\serge\Desktop\Untitled-2.py
qt-pyside2 backend - Qt version 5.12.9
####### 3D rendering pipe initialisation #####
Display3d class initialization starting ...
Aspect_DisplayConnection created.
OpenGl_GraphicDriver created.
V3d_Viewer created.
AIS_InteractiveContext created.
V3d_View created
WNT window created.
Traceback (most recent call last):
File "c:\Users\serge\Desktop\Untitled-2.py", line 4, in <module>
display, start_display, add_menu, add_function_to_menu = init_display()
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Display\SimpleGui.py", line 198, in init_display
win.canva.InitDriver()
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Display\qtDisplay.py", line 102, in InitDriver
self._display.Create(window_handle=int(self.winId()), parent=self)
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Display\OCCViewer.py", line 176, in Create
self.Init(self._window_handle)
File "C:\Users\serge\.conda\envs\pyoccenv\lib\site-packages\OCC\Core\Visualization.py", line 171, in Init
return _Visualization.Display3d_Init(self, handle)
RuntimeError: Aspect_GraphicDeviceDefinitionErrorOpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: 2000 raised from method Init of class Display3d
That's likely what @Alexander mentioned at the end. Thomas Paviot's answer regarding this issue is here. Following his advice, I changed the class name from `QtOpenGL.QGLWidget` to `QtWidgets.QWidget` in the file qtDisplay.py buried somewhere in local profile (I just used file search to find it):

1626434320731.png

And here we go!

1626434355986.png

Personally, I've never used Python and this whole thing is kinda completely new to me. But it's very satisfying to see how easily one could get started.
 

rpurohit15

Looking around for some CAD
Hi Quaoar, thanks a lot for these great tutorials. I am looking forward to the next episodes!

It is actually straight forward to get started with PythonOCC, but it requires the conda package manager. Here is a guide:

Get conda package manager
Install the anaconda distribution (individual edition). You can obtain an installer for your OS here. The installation will give you the conda package manager and a base environment containing python together with a lot of commonly used python packages for data science (e.g. numpy, matplotlib and more).

Note:
  • The windows installation wizard will by default not add anaconda to your path (but there is a non-recommended option to do so (which I always use, alternatively you can use the shell coming with anaconda))
  • Depending on your shell/terminal you may have to run some initialization script after installation.

If you don't want the additional bloat (i.e. data science packages) comming with a full anaconda installation (which seems unnecessary if the only purpose is to install PythonOCC), then you may want to install miniconda instead. I imagine the installation procedure is very similar, but I never tried miniconda. From a quick google search it seems like you can get an installer to miniconda here

Obtaining PythonOCC using conda
Open your favorite shell (that has conda in its path), and run the following three commands (you may be prompted for a y/n answer):
  • conda create -n pyoccenv python=3.8
  • conda activate pyoccenv
  • conda install -c conda-forge pythonocc-core

First command can be omitted if you don't want to create a separate environment for PythonOCC. In that case the second command should be changed to 'conda activate base'.

You are now good to go. You can test the installation by activating the environment containing PythonOCC (e.g. 'conda activate base' or 'conda activate pyoccenv') and run
  • python -c "import OCC; print(OCC.VERSION)"


Resources
PythonOCC has a lot of examples in one of their github repos which you might find interesting. You can find them here:

https://github.com/tpaviot/pythonocc-demos

The examples requires a gui backend (for instance PySide2) to be installed in the conda environment containing PythonOCC. One warning, though, I had to make a small change (using PySide2 backend) to get the examples to work (can't remember exactly what, but when having the error message it was quite easy to google).

Alternatives to PythonOCC
PythonOCC seems to have the largest community and a lot of good examples, but in case you are interested there are two other projects (that I know of) that provides python bindings to OpenCascade:
I started with pyocct and pyqt5 but had some dependency errors while setting up conda environment. Then I tried pythonocc with pyside2. I could see most of the demo scripts mentioned in the above directory are working well. Thanks for the post.
 

Jojain

CAD practitioner
About python bindings for OCCT, there is the Cadquery project that is using the OCP bindings and implement an high level API on top of OCCT, it may be interesting for some of you that want inspiration.
 

rpurohit15

Looking around for some CAD
Which pythonocc module provides functionality to users to draw line and different shapes? Think of a simplest AutoCAD like desktop application.
 
Last edited:

Quaoar

Administrator
Staff member
Cannot you just construct primitive edges for that purpose? I think you'll also need some constraint solver like PlaneGCS (from FreeCAD) or SolveSpace.
 

Zoltan

Looking around for some CAD
@rpurohit15 PythonOCC is merely a wrapper (with some additional utilities) around OpenCASCADE, so that other applications can build higher-level features such as the ones you wish. However, PythonOCC wraps almost the entire OpenCASCADE, preserving the function signatures, so you can search for help on the C++ forums and apply them straightforwardly in your Python application. PythonOCC uses SWIG for exposing OpenCASCADE to Python. There are other approaches (both mentioned in the original post), such as pyOCCT, which uses pybind11 for the interface, or OCP, which makes the wrapping with pywrap. However, I have no experience with the latter two.
 

rpurohit15

Looking around for some CAD
Cannot you just construct primitive edges for that purpose? I think you'll also need some constraint solver like PlaneGCS (from FreeCAD) or SolveSpace.
Thanks for the guidance!! I checked out PlaneGCS and SolveSpace seems that both are available only in c++. Is there any constrain solver for Python?
 

Quaoar

Administrator
Staff member
Thanks for the guidance!! I checked out PlaneGCS and SolveSpace seems that both are available only in c++. Is there any constrain solver for Python?
I've never heard about any pure Python implementation of such a thing as a constraint solver. Actually, since all geometric kernels are written in C/C++, one would expect to find the constraint solvers also written in C/C++. I guess you'd need a wrapper. SWIG maybe?
 

Quaoar

Administrator
Staff member
@A-U Thank you for the link. Would you recommend any good technical reading about solvers and stuff?
 

A-U

Active CAD practitioner
If I remember correctly I used this paper to get some background: Geometric constraint satisfaction using optimization methods Computer-Aided Design 31 (1999) 867–879. In the end it is nothing spectacular - you construct a cost function that is a sum of individual constraint cost functions and solve it with a nonlinear optimization solver.
 

DanB

CAD community veteran
I was first introduced to OpenCascade through PythonOCC and later used it for prototyping. Do you guys have any idea how the performance is affected?
 

Quaoar

Administrator
Staff member
@DanB I'm going to continue working with PythonOCC for a while to port some existing Python code to C++. Hopefully, I'll get a better idea of the performance overheads, if there are any. In my situation, the only reason for porting is to be able to plug in some fancy OpenCascade toys, such as BVH and also supplementary data structures that I have in Analysis Situs and not it OCC itself (hence not wrapped with Python). I'll keep this thread posted.

What I noticed so far is a quite significant launch time. Not sure what's happening there, looks like some heavy deps are being loaded into a memory,
 
Last edited:

sandesh chand

Looking around for some CAD

Getting the relationship of connected subparts from assembly step files using pythonocc​

I have many parts in the assembly step files.Some parts are connected to other parts and some are not connected.(For example A table has four legs and one flat surface.All legs are connected to the flat surface.Relationship between each leg with flat surface).I want to make a relationship of parts which are directly connected to each other.how could i achieve my goal using pythonocc.It would be great help.Thanks
 

Quaoar

Administrator
Staff member
I would start from implementing it in C++ that is more native and then porting to Python using the PythonOCC wrappers. But even for C++, the information provided is not enough to prototype anything. Have you tried XDE?
 

topologic

Looking around for some CAD

Getting the relationship of connected subparts from assembly step files using pythonocc​

I have many parts in the assembly step files.Some parts are connected to other parts and some are not connected.(For example A table has four legs and one flat surface.All legs are connected to the flat surface.Relationship between each leg with flat surface).I want to make a relationship of parts which are directly connected to each other.how could i achieve my goal using pythonocc.It would be great help.Thanks
I know this question is from April 2022, but just on the off-chance that you have not received a satisfactory answer, or if others are looking for the same thing, I want to point you to my software, Topologic (aka topologicpy), which is based on OCCT and will give you exactly what you asked for. For example, you can ask an object for its "adjacent" objects. It also build a full dual graph with sophisticated options (such as connecting to exterior faces or to create an edge only if an aperture exists between cells etc etc). For more information, check topologicpy on https://pypi.org/project/topologicpy/
 
Top