Quick tutorial for getting started with pythonOCC

pythonOCC viewing IGES file

pythonOCC (http://www.pythonocc.org) is a wrapper for the free geometry library OpenCASCADE (https://www.opencascade.com/).

It allows the programmer to leverage the extensive libraries and ease of use of the Python language with the powerful CAD tools of OpenCASCADE.

The steps of this tutorial were performed in Windows, but it should work similarly under Linux after the first step.

First, we will install the Anaconda package manager which comes with Python. From https://www.anaconda.com/distribution/#download-section, download the Python 3.7 version for your OS.

Anaconda package manager

Once you have installed Anaconda, run the prompt:

Anaconda package manager

From the prompt, check Python is there by simply typing Python. Note- Python version will be downgraded when installing pythonOCC. You are now in the Python interpreter. Type exit() to go back to the Anaconda prompt.

pythonOCC command

As per instructions at http://www.pythonocc.org/download/ install pythonOCC in Anaconda: conda install -c conda-forge -c dlr-sc -c pythonocc -c oce pythonocc-core==0.18.1

Go back into the Python interpreter and check OpenCASCADE libraries are available:

pythonOCC command

We will write a program to load and display an IGES file using pythonOCC. You can download a sample IGES file here:
https://gofile.io/?c=1pIdts

This is the code. Save it with .py extension:

#load needed libraries
from OCC import IGESControl
from OCC.IGESControl import IGESControl_Reader
from OCC.Display.SimpleGui import init_display
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
#create new IGES reader class
reader = IGESControl_Reader()
#read file, make sure you update path. Note in Windows slash needs to be used
reader.ReadFile('D:/projects/pythonocc/test.iges')
#no idea what this does but without it the shape won't be created :)
reader.TransferRoots()
#generate Shape
shape = reader.Shape()
#initialize built-in viewer
display, start_display, add_menu, add_function_to_menu = init_display()
#load shape from IGES
display.DisplayShape(shape, update=True)
#show viewer
start_display()

From the Anaconda prompt (not the Python interpreter!) launch python with the .py file as argument:

pythonOCC command

The viewer should pop up displaying your IGES file:

pythonOCC viewer

About Post Author

One thought on “Quick tutorial for getting started with pythonOCC

Leave a Reply