Pythonocc Getting Started

download Pythonocc Getting Started

of 23

Transcript of Pythonocc Getting Started

  • 8/10/2019 Pythonocc Getting Started

    1/23

    Getting started with pythonoccRelease 0.16.0

    Thomas Paviot

    November 13, 2014

  • 8/10/2019 Pythonocc Getting Started

    2/23

  • 8/10/2019 Pythonocc Getting Started

    3/23

    CONTENTS

    1 Introduction 3

    2 Build / Install 5

    2.1 Build / install from source . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Download / install precompiled binaries . . . . . . . . . . . . . . . . . . . . . 62.3 Conda packages (OSX only) . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    3 Thirty minutes with pythonocc 93.1 The use-case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 Import a standard le and display a shape . . . . . . . . . . . . . . . . . . . . 103.3 Create the mold print using a boolean cut operation . . . . . . . . . . . . . . . 13

    4 Conclusion 17

    5 Indices and tables 19

    i

  • 8/10/2019 Pythonocc Getting Started

    4/23

    ii

  • 8/10/2019 Pythonocc Getting Started

    5/23

    Getting started with pythonocc, Release 0.16.0

    Contents:

    CONTENTS 1

  • 8/10/2019 Pythonocc Getting Started

    6/23

    Getting started with pythonocc, Release 0.16.0

    2 CONTENTS

  • 8/10/2019 Pythonocc Getting Started

    7/23

    CHAPTER

    ONE

    INTRODUCTION

    This document explains how to get starting with pythonocc: how to build/install, 30 rst min-utes experience with the package.

    pythonocc will allow you to write python scripts that creates and operates 3D BRep geometry.

    3

  • 8/10/2019 Pythonocc Getting Started

    8/23

    Getting started with pythonocc, Release 0.16.0

    4 Chapter 1. Introduction

  • 8/10/2019 Pythonocc Getting Started

    9/23

    CHAPTER

    TWO

    BUILD / INSTALL

    The build system uses cmake to congure the project. This is a crossplatform solution: stepsbelow are the same for Linux, OSX, or Windows platforms.

    2.1 Build / install from source

    2.1.1 Requirements

    pythonOCC needs the following libraries or programs to be installed before you can com-pile/use it :

    the python programming language ( http://www.python.org ). Python 2.7 and 3.x are of-cially supported,

    OpenCascade Community Edition 0.16 ( https://github.com/tpaviot/oce ),

    SWIG 2.0.10 or higher ( http://www.swig.org ),

    CMake 2.8 or higher ( http://www.cmake.org ).

    2.1.2 pythonocc-core compilation

    From the current directory:

    $ cd pythonocc-core$ mkdir cmake-build$ cd cmake-build

    The conguration steps uses cmake:

    $ cmake ..

    By default, cmake looks for oce include headers in /usr/local/include/oce and librariesin /usr/local/include/lib. If these paths dont match your installation, you have to setOCE_INCLUDE_PATH and OCE_LIB_PATH

    5

    http://www.cmake.org/http://www.python.org/https://github.com/tpaviot/ocehttp://www.swig.org/http://www.cmake.org/http://www.cmake.org/http://www.swig.org/https://github.com/tpaviot/ocehttp://www.python.org/http://www.cmake.org/
  • 8/10/2019 Pythonocc Getting Started

    10/23

    Getting started with pythonocc, Release 0.16.0

    $ cmake -DOCE_INCLUDE_PATH=/your_oce_headers -DOCE_LIB_PATH=/your_lib_dir ..

    If you prefer, you can launche the cmake-gui using the following command

    $ cmake-gui ..

    And launch the build process

    $ make

    If you have many cpus, you can increase the compilation speed with::

    $ make -j$ncpus

    According to your machine/os/ncpus, the total compilation time shold be between 5 to 15minutes.

    Then

    $ make install

    You may require admin privileges to install

    $ sudo make install

    2.1.3 test

    In order to check that everything is ok, run the pythonocc unittest suite:

    $ cd ../test$ python run_tests.py

    You can also run the examples avalaible in the pythonocc-core/examples directory.

    2.2 Download / install precompiled binaries

    Windows

    Binary packages for Windows/py2 and py3 are available at our github release page .

    Debian

    .deb binary packages are available at our github release page .

    Ubuntu

    6 Chapter 2. Build / Install

    https://github.com/tpaviot/pythonocc-core/releaseshttps://github.com/tpaviot/pythonocc-core/releaseshttps://github.com/tpaviot/pythonocc-core/releaseshttps://github.com/tpaviot/pythonocc-core/releases
  • 8/10/2019 Pythonocc Getting Started

    11/23

    Getting started with pythonocc, Release 0.16.0

    $ sudo add-apt-repository ppa:freecad-maintainers/oce-release -y$ sudo add-apt-repository ppa:tpaviot/pythonocc -y$ sudo apt-get update -q$ sudo apt-get install pythonocc-core

    OSX

    .dmg binary package is available at our github release page .

    2.3 Conda packages (OSX only)

    Packaging with conda

    pythonocc depends on one of the largest Open Source code bases out there, OpenCasCADEspecically, the OpenCasCADE Community Edition (oce) .

    conda is a solution to install pythonOCC and all of its dependencies in about 5 minutes.

    How is this done?

    First, decide if you prefer the completeanaconda < http://docs.continuum.io/anaconda/index.htm l#packages-included-in-anaconda >_ that comes with many great modules readily installed or a lighterhanded approach (prefered), by installing miniconda . This is accomplished through thefollowing command on linux:

    $ curl 'http://repo.continuum.io/miniconda/Miniconda-3.7.0-Linux-x86_64.sh' > M$ bash Miniconda.sh

    and for mac osx through:

    $ curl 'http://repo.continuum.io/miniconda/Miniconda-3.7.0-MacOSX-x86_64.sh' >$ bash Miniconda.sh

    that installs a fresh python interpreter.

    As of now, only an osx build is completed, as conda support for pythonocc is a work in progress.You can install pythonocc-core like so:

    # add the channel$ conda config --add channels http://conda.binstar.org/jf# create a pythonocc environment$ conda create -n pythonocc python# grab and install pythonocc$ conda install pythonocc-core

    Youre encouraged to install a GUI manager PyQt4 or wxPython:

    $ conda install pyqt

    2.3. Conda packages (OSX only) 7

    https://github.com/tpaviot/pythonocc-core/releaseshttps://github.com/tpaviot/pythonocc-core/releaseshttp://opencascade.org/https://github.com/tpaviot/ocehttps://github.com/tpaviot/ocehttp://conda.pydata.org/docs/http://docs.continuum.io/anaconda/index.html#packages-included-in-anacondahttp://docs.continuum.io/anaconda/index.html#packages-included-in-anacondahttp://conda.pydata.org/miniconda.htmlhttp://conda.pydata.org/miniconda.htmlhttp://docs.continuum.io/anaconda/index.html#packages-included-in-anacondahttp://docs.continuum.io/anaconda/index.html#packages-included-in-anacondahttp://conda.pydata.org/docs/https://github.com/tpaviot/ocehttp://opencascade.org/https://github.com/tpaviot/pythonocc-core/releases
  • 8/10/2019 Pythonocc Getting Started

    12/23

    Getting started with pythonocc, Release 0.16.0

    Environments

    Environments are a very important concept, and conda provides more fundamental supportfor environments than for instance virtualenv . This is again, related to dependencies. Withconda, mutiple version of pythonocc supporting different version of the OCE library can be

    supported. By changing your environment, its easy to upgrade your codebase to a newer versionof PythonOCC while still being able to switch to any older version.

    packages at binstar

    binstar is a site where you can upload and distribute your own repository of packages (heresJelle ). Ownership of packages can be shared, which is great for working together on supportinga package for different platforms. Check out available packages by the [maintainer of condaand binstar]( https://binstar.org/asmeurer/_list-packages )

    Contribute pythonocc package for your platform

    Using conda, you can also compile and upload binaries with others. First install necessary stuff to build conda packages:

    $ conda install conda-build$ conda install jinja2

    The build the package:

    $ cd pkg/conda$ conda-build .

    To go further

    If your interested to learn more about conda, check out the documentation . Youre also encour-aged you to see this excellent talk by Aaron Meurer .

    8 Chapter 2. Build / Install

    http://docs.python-guide.org/en/latest/dev/virtualenvs/http://docs.python-guide.org/en/latest/dev/virtualenvs/https://binstar.org/https://binstar.org/jf/https://binstar.org/asmeurer/_list-packageshttp://conda.pydata.org/docshttp://www.python.org/https://github.com/asmeurerhttps://github.com/asmeurerhttps://github.com/asmeurerhttp://www.python.org/http://conda.pydata.org/docshttps://binstar.org/asmeurer/_list-packageshttps://binstar.org/jf/https://binstar.org/http://docs.python-guide.org/en/latest/dev/virtualenvs/
  • 8/10/2019 Pythonocc Getting Started

    13/23

    CHAPTER

    THREE

    THIRTY MINUTES WITH PYTHONOCC

    In this section, you will discover, in less than 30 minutes, a set of features that covers thefollowing elds: data import from a STEP AP 203 le, modeling a box, perform a booleanoperation (cut), visualize a shape and render in WebGl.

    This tutorial is based upon the ipython console: the qt ipython mode allows an interactive ses-sion with pythonocc, without worrying about gui/event management/program freeze commingalong.

    3.1 The use-case

    The use case suggested comes from a current situation in the industry: youre a manufacturingengineer, specialized in molding, receiving a 3d model for which you have to design the mold.

    Your customer needs your skills to produce a set of 10.000 engines block, as you can see ongure model_engine .

    Your customer uses a famous CAD package to design its products: Autodesk Inventor Profes-sional 15 (see image 2).

    9

  • 8/10/2019 Pythonocc Getting Started

    14/23

    Getting started with pythonocc, Release 0.16.0

    3.2 Import a standard le and display a shape

    3.2.1 Launch the ipython environnement

    $ ipython --gui='qt'

    You should have a python prompt.

    3.2.2 Import the STEP le

    The customer sent to you, via an email, a 2.2Mb step le: cylinder_block.stp , gener-

    ated with Autodesk Inventor (Note: most CAD vendors provide this feature).A STEPControl_Reader instance is necessary to read/import STEP les::

    >>> from OCC.STEPControl import STEPControl_Reader>>> step_reader = STEPControl_Reader()>>> step_reader . ReadFile( '../_models/cylinder_block.stp' )>>> step_reader . TransferRoot()>>> block_cylinder_shape = step_reader . Shape()

    ... Step File Reading : ../_models/cylinder_block.stp

    ... STEP File Read ...Elapsed time: 0 Hours 0 Minutes 0.056925 SecondsCPU user time: 0.04 secondsCPU system time: 0 seconds

    10 Chapter 3. Thirty minutes with pythonocc

  • 8/10/2019 Pythonocc Getting Started

    15/23

    Getting started with pythonocc, Release 0.16.0

    ... Step File loaded ...Elapsed time: 0 Hours 0 Minutes 0.110026 SecondsCPU user time: 0.09 secondsCPU system time: 0.01 seconds

    54737 records (entities,sub-lists,scopes), 171664 parameters... Parameters prepared ... Elapsed time: 0 Hours 0 Minutes 0.158572 SeCPU user time: 0.13 secondsCPU system time: 0.01 secondsReport : 4 unknown entities

    ... Objets analysed ...Elapsed time: 0 Hours 0 Minutes 0.239736 SecondsCPU user time: 0.21 secondsCPU system time: 0.02 secondsSTEP Loading done : 31486 Entities

    STEP le was successfull. Lets now display the shape in the graphic window.

    3.2.3 Display the block engine shape

    First import the module providing a basic qt based gui

    >>> from OCC.Display.SimpleGui import init_display

    Then initialize the graphic window

    >>> display, start_display, add_menu, add_function_to_menu = init_display()###### 3D rendering pipe initialisation #####Display3d class initialization starting ...Aspect_DisplayConnection created.Graphic_Driver created.Cocoa window created.V3d_Viewer created.V3d_View createdAIS_InteractiveContext created.Display3d class successfully initialized.

    ########################################

    You should now see a graphic window, empty, with just a rvb triedron at the lower right corner.

    Just pass the blocke_engine_shape to the display, and ask for a refresh

    >>> display . DisplayShape(block_cylinder_shape, update = True )

    You can see the cylinder block shape, in a light orange color.

    Using your mouse you can: * left click and move to rotate * right click and move to pan *middle click (or mouse wheel) and move to zoom in/out.

    3.2. Import a standard le and display a shape 11

  • 8/10/2019 Pythonocc Getting Started

    16/23

    Getting started with pythonocc, Release 0.16.0

    12 Chapter 3. Thirty minutes with pythonocc

  • 8/10/2019 Pythonocc Getting Started

    17/23

  • 8/10/2019 Pythonocc Getting Started

    18/23

    Getting started with pythonocc, Release 0.16.0

    >>> from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox>>> from OCC.gp import gp_Vec, gp_Trsf>>> from OCC.BRepBuilderAPI import BRepBuilderAPI_Transform>>> box = BRepPrimAPI_MakeBox( 30 , 90 , 90 ) . Shape()

    >>> trns = gp_Trsf()>>> trns . SetTranslation(gp_Vec( 0 , - 35 , - 35 ))>>> mold_basis = BRepBuilderAPI_Transform(box, trns) . Shape()

    The mold basis is created, lets display it with another material

    >>> from OCC.Graphic3d import Graphic3d_NOM_STEEL>>> display . DisplayShape(mold_basis, update = True , material = Graphic3d_NOM_STEEL)

    And here is what we obtain

    3.3.3 Compute the mold print

    We have to perform a boolean cut operation between the box and the block. This is achievedusing the BRepAlgoAPI_Cut class

    14 Chapter 3. Thirty minutes with pythonocc

  • 8/10/2019 Pythonocc Getting Started

    19/23

    Getting started with pythonocc, Release 0.16.0

    >>> from OCC.BRepAlgoAPI import BRepAlgoAPI_Cut>>> mold = BRepAlgoAPI_Cut(mold_basis, block_cylinder_shape) . Shape()

    Display the result, but rst erase what is curently displayed

    >>> display . EraseAll()>>> display . DisplayShape(mold, update = True , material = Graphic3d_NOM_STEEL)

    Here we are!

    3.3.4 At last, display the shape in the browser

    Imagine you want to share the shape with someone would dont have any CAD software in-stalled, of no STEP or CAD le viewer. Lets provide him a single html page he will be able toview in its web browser, without any plugin. For that, we will use the x3dom renderer.

    >>> from OCC.Display.WebGl import x3dom_renderer

    >>> my_webgl_renderer = x3dom_renderer . X3DomRenderer()>>> my_webgl_renderer . DisplayShape(mold)

    3.3. Create the mold print using a boolean cut operation 15

  • 8/10/2019 Pythonocc Getting Started

    20/23

    Getting started with pythonocc, Release 0.16.0

    Your default webbrowser will display the generated html page: handle the 3D model the sameway you did it in the qt based window.

    16 Chapter 3. Thirty minutes with pythonocc

  • 8/10/2019 Pythonocc Getting Started

    21/23

    CHAPTER

    FOUR

    CONCLUSION

    There are many many more features available in pythonocc. To go further, here is what youcan do:

    go to the /examples folder and run/study/modify what you see, read the api documentation,

    ask questions to [email protected] ,

    check oce.

    Please report to [email protected] any comment or suggestion to improve this document.

    17

    mailto:[email protected]:[email protected]:[email protected]:[email protected]
  • 8/10/2019 Pythonocc Getting Started

    22/23

    Getting started with pythonocc, Release 0.16.0

    18 Chapter 4. Conclusion

  • 8/10/2019 Pythonocc Getting Started

    23/23

    CHAPTER

    FIVE

    INDICES AND TABLES

    genindex

    modindex

    search