pythonOCC Parametric Modeling Tutorial
pythonOCC Parametric Modeling Tutorial
Abstract
This guide aims at introducing in a few stages the pythonOCC package
intended to parametric modeling.
1
License
This document is distributed under the terms of the Creative Common BY-NC-
SA 3.0 license. In a few words:
You are free:
• to Share
• to copy, distribute and transmit the work to Remix
• to adapt the work
• Noncommercial — You may not use this work for commercial purposes.
• Share Alike — If you alter, transform, or build upon this work, you may
distribute the resulting work only under the same or similar license to this
one.
With the understanding that:
• Waiver — Any of the above conditions can be waived if you get permission
from the copyright holder.
• Public Domain — Where the work or any of its elements is in the public
domain under applicable law, that status is in no way affected by the
license.
• Other Rights — In no way are any of the following rights affected by the
license:
• Notice — For any reuse or distribution, you must make clear to others the
license terms of this work. The best way to do this is with a link to this1
web page.
1 http://creativecommons.org/licenses/by-nc-sa/3.0/
2
Contents
1 What is PAF and Why Might I Need It? 4
3
1 What is PAF and Why Might I Need It?
The Parametric Application Framework (PAF) is a subpackage of the pythonOCC2
development library. parametric modeling application framework based upon the
OpenCASCADE 3D modeling kernel3 and the SalomeGEOM4 libraries. PAF is
part of the pythonOCC distribution since the release 0.3.
import s y s
print s y s . path
4
3.1.1 Step 1 : initialization of the framework.
You have to first import the classes needed whenever you want to use the PAF :
from OCC.PAF. Context import P ar a me t ri c Mo d el i ng C on t ex t
from OCC.PAF. P a r a m e t r i c import Parameters
We’ll see in details these classes in the next part of this tutorial.
let’s tell the context that a graphic window has to be set up in order to
display the geometry:
tutorial_context . init_display ()
5
Figure 1: parametric cylinder created from an ipython console
Here we are. You have a parameterized model of a cylinder! Let’s first make a
few tests from this (small!) script.
6
Figure 2: Impact of a parameter modification
Now just change the value of the radius parameter from 12.9 to 100. You’ll
see the modified geometry displayed in the graphic window (cf. figure 2).
symbols . r a d i u s = p . r a d i u s . symbol
h e i g h t = p . h e i g h t . symbol
6 http://code.google.com/p/sympy/
7
3.3.3 Step 3: write the relation
Write here the second member of the relation.
my_relation = 2∗ r a d i u s
Take care to not forget the "" for the parameter height.
The complete code now looks like:
from OCC.PAF. Context import P ar a me t ri c Mo d el i ng C on t ex t
from OCC.PAF. P a r a m e t r i c import Parameters
from OCC.PAF. P a r a m e t r i c import R e l a t i o n
p = Parameters ( )
t c = P ar a me t ri c Mo d el i ng C on t ex t ( p )
tc . init_display ()
p . height = 43.3
p . radius = 12.9
tc . r e g i s t e r _ o p e r a t i o n s ( t u t o r i a l _ c o n t e x t . prim_operations )
my_cylinder = t c . p r i m _ o p e r a t i o n s . MakeCylinderRH ( p . r a d i u s , \
p . h e i g h t , name=" c y l i n d e r 1 " , show=True )
r a d i u s = p . r a d i u s . symbol
h e i g h t = p . h e i g h t . symbol
my_relation = 2∗ r a d i u s
R e l a t i o n ( p , " h e i g h t " , my_relation )
From an ipython session, change the value of the radius parameter from 12.9
to 30:
ipython -wthread tutorial_paf_part3.py
>> p.radius = 30
You’ll see that the new value of the parameter height is 60 and have the
following screen (cf. figure 3).
Note: if you change the parameter height value from 60.0 to another value,
for instance 70.0, you will have no error message, but you will see that the height
parameter has still the value 60.0. This parameter is driven by radius. Each
time a parameter is changes, all relations are computed again.
8
Figure 3: Relation between the radius and height parameters
9
3.4.2 Step 2 : define the python function
In order to check that the radius is positive, the IsPositive function is defined as:
def I s P o s i t i v e ( x ) :
return x >0.0
Note that this basic python function can be used as usual. For instance:
print IsPositive(1)
>>> True
print IsPositive(-2)
>>> False
NOTE: each time a parameter change, all the Relation are updated and
all the Rule are checked again. If one rule is not checked (that is, the python
function returns False), then an BrokeRule python exception is raised. Let’s now
try the complete code within a ipython session.
The complete code now looks like:
from OCC.PAF. Context import P ar a me t ri c Mo d el i ng C on t ex t
from OCC.PAF. P a r a m e t r i c import Parameters
from OCC.PAF. P a r a m e t r i c import R e l a t i o n
from OCC.PAF. P a r a m e t r i c import Rule
p = Parameters ( )
t c = P ar a me t ri c Mo d el i ng C on t ex t ( p )
tc . init_display ()
# i n i t parameters v a l u e s
p . height = 43.3
p . radius = 12.9
# c r e a t e geometry
tc . r e g i s t e r _ o p e r a t i o n s ( t u t o r i a l _ c o n t e x t . prim_operations )
my_cylinder = t c . p r i m _ o p e r a t i o n s . MakeCylinderRH ( p . r a d i u s , \
p . h e i g h t , name=" c y l i n d e r 1 " , show=True )
# define a relation
r a d i u s = p . r a d i u s . symbol
h e i g h t = p . h e i g h t . symbol
my_relation = 2∗ r a d i u s
R e l a t i o n ( p , " h e i g h t " , my_relation )
10
# c r e a t e a r u l e and c o n n e c t i t t o t h e parameter
def I s P o s i t i v e ( x ) : return x>0
Rule ( p , " r a d i u s " , I s P o s i t i v e )
11