wiki:PythonOcc

Version 28 (modified by skulovec, 12 years ago) (diff)

--

PythonOCC omogoča enostavnejšo uporabo jedra modelirnika OpenCascade v jeziku Python. Prednost Pythona v primerjavi z C++ je:

  • Prenosljivosti. Programi se interpretirajo in jih ni potrebno prevajati zato delujejo na vseh operacijskih sistemih. So pa nekoliko pošasnejši.
  • Enostavnejša namestitev potrebnih knjižnic, brez zahtevne konfiguracije povezovalnih parametrov, ki so značilni za C++.
  • Lažje učenje jezika. V interaktivnem načinu obstaja tudi refleksija oziroma dinamično prepoznavanje možnih ukazov v objektu.

Za vaje je potrebno na Windowsih (tudi 63 bitnih) namestiti naslednje pakete:

  1. Python 2.6 interpreter jezika z osnovnimi knjižnicami
  2. wxPython za opravljanje z okni
  3. PythonOCC povezava Pytona z jedrom OpenCascade

Neobvezno vendar priporočljivo je namesti še:

  1. iPython za interektivno delo
  2. NumPy za delo z numeričnimi metodami in matrikami.
  3. SciPy za delo z znanstvenimi algoritmi.

Pred preizkusom prvih primerov je potrebno nastaviti še pot do interpreterja v Moj računalnik -> Lastnosti -> Dodatne nastavitve sistema -> Spremenljivke okolja ... -> Sistemske spremenljivke -> Path -> Uredi -> Vrednost spremeljivke: in na koncu dodamo ;c:\Python26;. Po tem je potremn računalnik vnovič zagnati.

V primeru, da nam klik na Start->Vsi programi->pythonOCC -> Examples -> Level1 -> HelloWorld -> helloworld.py okno na hitro odpre, vendar modela ne prikaže, imamo težave z OpenGL krmilniki. Najenostavneje težavo odpravimo s spremembo osnovnega prikazovalnika v datoteki C:\Python26\Lib\site-packages\OCC\Display\wxDisplay.py v kateri platformo darwin poistovetimo z win32 tako da del kode v tej datoteki izgleda:

if sys.platform=='win32':
    BaseClass = wx.Panel
else:
    import wx.glcanvas
    BaseClass = wx.glcanvas.GLCanvas

V primeru, da nam namestitveni program nepravilno namesti imenik na C:\OCC ga prestavimo v podimenik C:\Python26\Lib\site-packages

Predstavitev CAD-jedra Open CASCADE na primerih

Uvod v modeliranje PythonOCC s 3D primitivi

V tem primeru je namen pokazati enostavne aplikacije:

  • izdelava menuja in dva podmenuja
  • ta dva menuja izdelata: i. kocko in ii. cilinder

Najprej kličemo knjižnico za enostaven uporabniški vmesnik SimpleGUI:

from OCC.Display.SimpleGui import *

The next step is to init the display functions: V naslednjem koraku inicializiramo funkcije za display:

display, start_display, add_menu, add_function_to_menu = init_display()

Let’s now define the two functions that create and display a box and a cylinder: Nato definiramo dve funkciji, ki izdelata kocko in cilinder:

def kocka(event=None):
    from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
    display.Erase.All()
    my_box= BRepPrimAPI_MakeBox(10., 20., 30.)
    display.DisplayShape(my_box.Shape())

def valj(event=None):
    from OCC.BRepPrimAPI import BRepPrimAPI_MakeCylinder
    display.Erase.All()
    my_cylinder = BRepPrimAPI_MakeCylinder(60, 200)
    display.DisplayShape(my_cylinder.Shape())

Izdelava menuja 'simple test' in dodajanje funkcij v podmenuje:

add_menu('enostaven primer')
add_function_to_menu('enostaven primer', kocka)
add_function_to_menu('enostaven primer', valj)

Na koncu renderiramo 3D model:

display.View_Iso()
display.FitAll()

start_display() # Zanka start_display() je neskončna

Posnetek programa v sliki:
3Dprimitives

Primer kode 3D modela:

## Izdelava kocke

from OCC.Display.SimpleGui import *
from OCC.BRepPrimAPI import *
from OCC.gp import *
from OCC.GC import *
from OCC.BRepBuilderAPI import *
#from OCC.TopoDS import *

display, start_display, add_menu, add_function_to_menu = init_display()

#Definiranje točk v prostoru
aPnt1 = gp_Pnt(0 , 0 , 0)
aPnt2 = gp_Pnt(10 , 0, 0)
aPnt3 = gp_Pnt(10 , 10 , 0)
aPnt4 = gp_Pnt(0, 10 , 0)

#Izdelava segmentov--definiranje geometrije
aSegment1 = GC_MakeSegment(aPnt1 , aPnt2)
aSegment2 = GC_MakeSegment(aPnt2 , aPnt3)
aSegment3 = GC_MakeSegment(aPnt3 , aPnt4)
aSegment4 = GC_MakeSegment(aPnt4 , aPnt1)

#Izdelava robov -- definiranje topologije
aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aEdge2 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
aEdge3 = BRepBuilderAPI_MakeEdge(aSegment3.Value())
aEdge4 = BRepBuilderAPI_MakeEdge(aSegment4.Value())

#Povezovanje robov v mrežo
aWire  = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() ,\
                                 aEdge3.Edge(), aEdge4.Edge())

#Telo: Iz profila se izdela telo
myFaceProfile = BRepBuilderAPI_MakeFace(aWire.Wire())

aPrismVec = gp_Vec(0 , 0 , 10)

myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face() , aPrismVec).Shape()

display.DisplayShape(myBody)
start_display()

Slikovni pregled izdelave 3D modela

a. Definiranje tock v prostoru:
aPnt1 = gp_Pnt(x_1 , y_1 , z_1) --> aPnt4 = gp_Pnt(x_4 , y_4 , z_4)

b. Iz tock v prostoru se tvori robove (en rob je sestavljen iz najmanj dveh tock): 
myEdge1 = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2) --> myEdge4 = BRepBuilderAPI_MakeEdge(aPnt4, aPnt1)

c. Ko imamo vse robove izdelamo mrezo: 
myWire  = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() ,aEdge3.Edge(), aEdge4.Edge())

d. Iz mreze tvorimo povrsino (mreza mora biti zaprta):
myFace = BRepBuilderAPI_MakeFace(myWire)

e. Definiranje prostorskega vektorja, ki bo dolocal smer ekstrudiranja povrsine: 
myVec = gp_Vec(n_x , n_y , n_z)

f. Z povrsino in smernim vektorjem izdelamo 3D model: 
myBody = BRepPrimAPI_MakePrism(myFace.Face() , myVec)

Pregled uporabljenih OCC knjižnic

## Importanje različnih knjižnic

# Uporabniški vmesnik GUI
from OCC.Display.SimpleGui import *

# Matematična knjižnica
import math

# OpenCascade
from OCC.gp import * #točke
from OCC.BRepBuilderAPI import * #gradimo robove, segmente, mreže ...
from OCC.BRepPrimAPI import * #izdelava osnovnih geometrijskih primitivov
from OCC.BRepFilletAPI import * #izdelava zaokrožitev

Točnejša navodila, opis funkcij in knjižnic lahko dobimo v PythonOCC dokumentaciji.

Inicializacija zaslona in izdelava grafičnega vmesnika

# OCC.Display.SimpleGui.init_display() returns multiple
# values which are assigned here
display, start_display, add_menu, add_function_to_menu = \
init_display()
draw_bottle() #kličemo CAD model, ki ga želimo prikazati na zaslonu
start_display()

Risanje točk v prostoru

Izdelava točk v prostoru je najosnovnejša operacija v OCC.

# Definiranje začetnih točk
aPnt1 = gp_Pnt(-myWidth / 2. , 0 , 0)
aPnt2 = gp_Pnt(-myWidth / 2. , -myThickness / 4. , 0)
aPnt3 = gp_Pnt(0 , -myThickness / 2. , 0)
aPnt4 = gp_Pnt(myWidth / 2. , -myThickness / 4. , 0)
aPnt5 = gp_Pnt(myWidth / 2. , 0 , 0)

Izdelava robnih elementov

V naslednjem koraku se iz začetnih točk izdela robove:

# Definiranje geometrije
aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4)
aSegment1 = GC_MakeSegment(aPnt1 , aPnt2)
aSegment2 = GC_MakeSegment(aPnt4 , aPnt5)

# Definiranje topologije
aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())

Povezovanje robnih elementov v mreže

Robne elemente se v nadaljevanju združi v mrežo.

# Izdelava mreže
aWire  = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() ,aEdge3.Edge())

Uporaba funkcij za izdelavo objektov v prostoru

# Izdelava celotnega profila - mirror
xAxis = gp_OX()
aTrsf = gp_Trsf()
aTrsf.SetMirror(xAxis)
aBRepTrsf = BRepBuilderAPI_Transform(aWire.Shape() , aTrsf)
aMirroredShape = aBRepTrsf.Shape()
aMirroredWire = TopoDS_wire(aMirroredShape)
mkWire = BRepBuilderAPI_MakeWire()
mkWire.Add(aWire.Wire())
mkWire.Add(aMirroredWire)
myWireProfile = mkWire.Wire()

# Telo: Iz profila se izdela telo (Funkcija izvleka 3D)
myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
aPrismVec = gp_Vec(0 , 0 , myHeight)
myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face() , aPrismVec)

Risanje geometrijskih primitivov

V OCC že obstajajo funkcije za izdelavo geometrijskih primitivov (kocka, valj,...), kar je prikazano na spodnjem primeru.

from OCC.Display.SimpleGui import *
from OCC.BRepPrimAPI import *

display, start_display, add_menu, add_function_to_menu = init_display()
my_box = BRepPrimAPI_MakeBox(10.,20.,30.).Shape()
# ali my_cylinder = BRepPrimAPI_MakeCylinder(neckAx2 , myNeckRadius , myNeckHeight), kjer so spremenljivke že preddefinirane 

display.DisplayShape(my_box) # ali display.DisplayShape(my_cylinder)
start_display()

Izdelava primera Bottle z uporabo programskega jezika Python in knjižnice OCC

Naslednji primer prikazuje izdelavo primera BottleCAD. Podrobnejši razdelek posameznih delov programske kode dobimo na MakeBottleCAD(C++).

##Copyright 2011 Simon Kulovec (simon.kulovec@lecad.si)
##Example: MakeCADBottle
##This file is part of pythonOCC.

## Importanje različnih knjižnic

# Uporabniški vmesnik GUI
from OCC.Display.SimpleGui import *

# OpenCascade
from OCC.gp import *
from OCC.TopoDS import *
from OCC.GC import *
from OCC.BRepBuilderAPI import *
from OCC.BRepPrimAPI import *
from OCC.BRepFilletAPI import *
from OCC.BRepAlgoAPI import *
from OCC.Utils.Topology import *
from OCC.Geom import *
from OCC.Geom2d import *
from OCC.GCE2d import *
from OCC.BRep import *
from OCC.BRepLib import *
from OCC.BRepOffsetAPI import *
from OCC.TopTools import *
from OCC.TopAbs import *
from OCC.TopExp import *

import math

def show_bottle(aRes):
    display.EraseAll()
    print dir(display)
    display.DisplayShape(aRes)

def define_points(myWidth, myThickness, myHeight):
    #Definiranje začetnih točk
    aPnt1 = gp_Pnt(-myWidth / 2. , 0 , 0)
    aPnt2 = gp_Pnt(-myWidth / 2. , -myThickness / 4. , 0)
    aPnt3 = gp_Pnt(0 , -myThickness / 2. , 0)
    aPnt4 = gp_Pnt(myWidth / 2. , -myThickness / 4. , 0)
    aPnt5 = gp_Pnt(myWidth / 2. , 0 , 0)

    #Definiranje geometrije
    aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4)
    aSegment1 = GC_MakeSegment(aPnt1 , aPnt2)
    aSegment2 = GC_MakeSegment(aPnt4 , aPnt5)

    #Definiranje topologije
    aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
    aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
    aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
    aWire  = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() ,aEdge3.Edge())

    #Izdelava celotnega profila - mirror
    xAxis = gp_OX()
    aTrsf = gp_Trsf()
    aTrsf.SetMirror(xAxis)
    aBRepTrsf = BRepBuilderAPI_Transform(aWire.Shape() , aTrsf)
    aMirroredShape = aBRepTrsf.Shape()
    aMirroredWire = TopoDS_wire(aMirroredShape)
    mkWire = BRepBuilderAPI_MakeWire()
    mkWire.Add(aWire.Wire())
    mkWire.Add(aMirroredWire)
    myWireProfile = mkWire.Wire()

    # Telo: Iz profila se izdela telo
    myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
    aPrismVec = gp_Vec(0 , 0 , myHeight)
    myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face() , aPrismVec)

    # Telo: Dodamo zaokrožitve (fillet)
    mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape())
    topology_traverser = Topo(myBody.Shape())
    for aEdge in topology_traverser.edges():
        mkFillet.Add(myThickness / 12. , aEdge)
    myBody = mkFillet.Shape()

    #Dodajanje grla na steklenico
    neckLocation = gp_Pnt(0, 0, myHeight)
    neckNormal = gp_DZ()
    neckAx2 = gp_Ax2(neckLocation, neckNormal)

    myNeckRadius = myThickness / 4
    myNeckHeight = myHeight / 10

    mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2 , myNeckRadius , \
                                          myNeckHeight)
    myNeck = mkCylinder.Shape();

    myBody = BRepAlgoAPI_Fuse(myBody, myNeck)

    # Izdelava votle steklenice
    faceToRemove = None
    zMax = -1;
    t = Topo(myBody.Shape())
    k=1
    for  aFace in t.faces():
    
        aSurface = BRep_Tool().Surface(aFace)
    
        if aSurface.GetObject().IsInstance('Geom_Plane'):
            aPlane = Handle_Geom_Plane().DownCast(aSurface).GetObject()
            aPnt = aPlane.Location()
            aZ = aPnt.Z()
            if aZ>zMax:
                faceToRemove = aFace

    facesToRemove = TopTools_ListOfShape()
    facesToRemove.Append(faceToRemove)
    myBody = BRepOffsetAPI_MakeThickSolid(myBody.Shape() , facesToRemove , \
                                          -myThickness/50 , 1.e-3)
    
    # Threading : Create Surfaces
    aCyl1 = Geom_CylindricalSurface(gp_Ax3(neckAx2) , myNeckRadius * 0.99)
    aCyl2 = Geom_CylindricalSurface(gp_Ax3(neckAx2) , myNeckRadius * 1.05)

    # Threading : Define 2D Curves
    aPnt = gp_Pnt2d(2. * 3.141592 , myNeckHeight / 2.)
    aDir = gp_Dir2d(2. * 3.141592 , myNeckHeight / 4.)
    aAx2d = gp_Ax2d(aPnt , aDir)
        
    aMajor = 2. * 3.141592
    aMinor = myNeckHeight / 10.
    
    anEllipse1 = Geom2d_Ellipse(aAx2d , aMajor , aMinor)
    anEllipse2 = Geom2d_Ellipse(aAx2d , aMajor , aMinor / 4)

    aArc2 = Geom2d_TrimmedCurve(anEllipse1.GetHandle() , 3.141592, 0.)
    aArc1 = Geom2d_TrimmedCurve(anEllipse2.GetHandle() , 3.141592, 0.)

    anEllipsePnt2 = anEllipse1.Value(0.)
    anEllipsePnt1 = anEllipse1.Value(3.141592)
    
    aSegment = GCE2d_MakeSegment(anEllipsePnt1 , anEllipsePnt2)

    # Threading : Build Edges and Wires
    aEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(aArc1.GetHandle() , aCyl1.GetHandle())
    aEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment.Value() , aCyl1.GetHandle())
    aEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(aArc2.GetHandle() , aCyl2.GetHandle())
    aEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value() , aCyl2.GetHandle())
    print dir(aEdge1OnSurf1)
    threadingWire1 = BRepBuilderAPI_MakeWire(aEdge1OnSurf1.Edge() , aEdge2OnSurf1.Edge())
    threadingWire2 = BRepBuilderAPI_MakeWire(aEdge1OnSurf2.Edge() , aEdge2OnSurf2.Edge())


    BRepLib().BuildCurves3d(threadingWire1.Wire())
    BRepLib().BuildCurves3d(threadingWire2.Wire())

    # Create Threading
    aTool = BRepOffsetAPI_ThruSections(True)

    aTool.AddWire(threadingWire1.Wire())
    aTool.AddWire(threadingWire2.Wire())
    aTool.CheckCompatibility(False)

    myThreading = aTool.Shape()

    # Izdelava sestava
    aRes = TopoDS_Compound()

    aBuilder = BRep_Builder()
    aBuilder.MakeCompound (aRes)

    aBuilder.Add (aRes, myBody.Shape())
    aBuilder.Add (aRes, myThreading)

    # Izris oblike
    show_bottle(aRes)

def draw_bottle(event=None):
    # Definiranje razdalj: širina, dolžina, višina
    myWidth = 50.0
    myThickness = 30.0
    myHeight = 70.0
    # Define Points
    define_points(myWidth, myThickness, myHeight)
    
if __name__ == '__main__':
    # OCC.Display.SimpleGui.init_display() returns multiple
    # values which are assigned here
    display, start_display, add_menu, add_function_to_menu = \
    init_display()
    draw_bottle() #kličemo podprogram za izris bottle
    start_display()

Izris CAD prizme

## Izdelava prizme --primer bottle

from OCC.Display.SimpleGui import *
from OCC.BRepPrimAPI import *
from OCC.gp import *
from OCC.GC import *
from OCC.BRepBuilderAPI import *
from OCC.TopoDS import *

display, start_display, add_menu, add_function_to_menu = init_display()

myWidth = 50.0
myThickness = 30.0
myHeight = 70.0

#Definiranje začetnih točk
aPnt1 = gp_Pnt(-myWidth / 2. , 0 , 0)
aPnt2 = gp_Pnt(-myWidth / 2. , -myThickness / 4. , 0)
aPnt3 = gp_Pnt(0 , -myThickness / 2. , 0)
aPnt4 = gp_Pnt(myWidth / 2. , -myThickness / 4. , 0)
aPnt5 = gp_Pnt(myWidth / 2. , 0 , 0)

#Izdelava segmentov--definiranje geometrije
aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4)
aSegment1 = GC_MakeSegment(aPnt1 , aPnt2)
aSegment2 = GC_MakeSegment(aPnt4 , aPnt5)

#Izdelava robov -- definiranje topologije
aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())

#Povezovanje robov v mrežo
aWire  = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() ,\
                                 aEdge3.Edge())

#Izdelava celotnega profila - mirror
xAxis = gp_OX()
aTrsf = gp_Trsf()
aTrsf.SetMirror(xAxis)
aBRepTrsf = BRepBuilderAPI_Transform(aWire.Shape() , aTrsf)
aMirroredShape = aBRepTrsf.Shape()
aMirroredWire = TopoDS_wire(aMirroredShape)
mkWire = BRepBuilderAPI_MakeWire()
mkWire.Add(aWire.Wire())
mkWire.Add(aMirroredWire)
myWireProfile = mkWire.Wire()

#Telo: Iz profila se izdela telo
myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
aPrismVec = gp_Vec(0 , 0 , myHeight)
myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face() , aPrismVec).Shape()

display.DisplayShape(myBody)
start_display()

Povzetek prve vaje

Prikaz enostavnega izvoza STEP formata za eno obliko

# Vključimo knjižnico za izvoz STEP formata
from OCC.Utils.DataExchange.STEP import STEPExporter

from OCC.BRepPrimAPI import *

# Pripravimo enostaven CAD primer za izvoz v STEP format
my_box_shape = BRepPrimAPI_MakeBox(50,50,50).Shape() # Izdelava kocke

# Obliko my_box_shape izvozimo v STEP format
my_step_exporter = STEPExporter("result_export_single.stp") # Določitev imena file.a
my_step_exporter.add_shape(my_box_shape)
my_step_exporter.write_file()

Prikaz enostavnega izvoza STEP formata za več oblik

# Vključimo knjižnico za izvoz STEP formata
from OCC.Utils.DataExchange.STEP import STEPExporter

from OCC.BRepPrimAPI import *

# Pripravimo enostavna CAD primera za izvoz v STEP format
my_box_shape = BRepPrimAPI_MakeBox(50,50,50).Shape() # Izdelava kocke
my_sphere_shape = BRepPrimAPI_MakeSphere(20).Shape() # Izdelava krogle

# Obliki my_box_shape in my_sphere_shape izvozimo v STEP format
my_step_exporter = STEPExporter("result_export_multi.stp") # Določitev imena file.a
my_step_exporter.add_shape(my_box_shape)
my_step_exporter.add_shape(my_sphere_shape) #funkcijo ADD uporabimo za dodajanje oblik v STEP format
my_step_exporter.write_file()

Prikaz izvoza CAD modelov z barvami in layer.ji

# Vključimo knjižnico za izvoz STEP formata
from OCC.Utils.DataExchange.STEP import StepOCAF_Export

from OCC.BRepPrimAPI import *

# Pripravimo enostavna CAD primera za izvoz v STEP format
my_box_shape = BRepPrimAPI_MakeBox(50,50,50).Shape() # Izdelava kocke
my_sphere_shape = BRepPrimAPI_MakeSphere(20).Shape() # Izdelava krogle

# Export to STEP
my_step_exporter = StepOCAF_Export("result_export_multi_color_layer.stp") # Določitev imena file.a
my_step_exporter.set_color(1,0,0) # določitev barve (rdeča) -> RedGreenBlue paleta barv (RGB)
my_step_exporter.set_layer('red') # določitev layer.ja
my_step_exporter.add_shape(my_box_shape) # izbira oblike za izbrane parametre my_box_shape
my_step_exporter.set_color(0,1,0) 
my_step_exporter.set_layer('green')
my_step_exporter.add_shape(my_sphere_shape)
my_step_exporter.write_file()

Uporaba funkcije zaokrožitve, pozicioniranje valja na izbrano mesto, združevanje CAD modelov ter izvoz v STEP format

V naslednjem primeru so prikazane naslednje funkcije:

  • zaokrožitve
  • pozicioniranje elementa
  • združevanje CAD modelov
  • enostaven izvoz CAD modela v STEP format
##Copyright 2011 Simon Kulovec (simon.kulovec@lecad.si)
##This file is part of pythonOCC.

## Importanje razlicnih knjiznic

# Uporabniski vmesnik GUI
from OCC.Display.SimpleGui import *

# OpenCascade
from OCC.gp import *
from OCC.TopoDS import *
from OCC.GC import *
from OCC.BRepBuilderAPI import *
from OCC.BRepPrimAPI import *
from OCC.BRepFilletAPI import *
from OCC.BRepAlgoAPI import *
from OCC.Utils.Topology import *
from OCC.BRep import *
from OCC.Utils.DataExchange.STEP import STEPExporter

# OCC.Display.SimpleGui.init_display() returns multiple
# values which are assigned here
display, start_display, add_menu, add_function_to_menu = \
init_display()

# Definiranje spremenljivk
myWidth = 50.0
myThickness = 30.0
myHeight = 70.0

# Definiranje zacetnih tock
aPnt1 = gp_Pnt(-myWidth / 2. , 0 , 0)
aPnt2 = gp_Pnt(-myWidth / 2. , -myThickness / 4. , 0)
aPnt3 = gp_Pnt(0 , -myThickness / 2. , 0)
aPnt4 = gp_Pnt(myWidth / 2. , -myThickness / 4. , 0)
aPnt5 = gp_Pnt(myWidth / 2. , 0 , 0)

# Definiranje geometrije
aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4)
aSegment1 = GC_MakeSegment(aPnt1 , aPnt2)
aSegment2 = GC_MakeSegment(aPnt4 , aPnt5)

# Definiranje topologije
aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
aWire  = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() ,\
                                         aEdge3.Edge())

# Izdelava celotnega profila - mirror

xAxis = gp_OX()
aTrsf = gp_Trsf()
aTrsf.SetMirror(xAxis)
aBRepTrsf = BRepBuilderAPI_Transform(aWire.Shape() , aTrsf)
aMirroredShape = aBRepTrsf.Shape()
aMirroredWire = TopoDS_wire(aMirroredShape)
mkWire = BRepBuilderAPI_MakeWire()
mkWire.Add(aWire.Wire())
mkWire.Add(aMirroredWire)
myWireProfile = mkWire.Wire()

# Telo: Iz profila se izdela telo
myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
aPrismVec = gp_Vec(0 , 0 , myHeight)
myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face() , aPrismVec)

# Telo: Dodamo zaokrozitve (fillet)
mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape())
topology_traverser = Topo(myBody.Shape())
for aEdge in topology_traverser.edges(): #z uporabo for zanke iščemo robove na CAD modelu in jih zaokrožujemo s funkcijo mkFillet in združujemo z .Add
    mkFillet.Add(myThickness / 12. , aEdge) #velikost zaokrožitve myThickness / 12.
myBody = mkFillet.Shape() #vse zaokrožitve priključimo k prvotni obliki myBody

# Dodajanje grla na steklenico (valj)
neckLocation = gp_Pnt(0, 0, myHeight) #Določitev lokacije valja
neckNormal = gp_DZ() #smer normale, v katero bomo valj izvlekli
neckAx2 = gp_Ax2(neckLocation, neckNormal) 

myNeckRadius = myThickness / 4 #radij valja
myNeckHeight = myHeight / 10 # višina valja

mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2 , myNeckRadius , \
                                          myNeckHeight)
myNeck = mkCylinder.Shape();

myBody = BRepAlgoAPI_Fuse(myBody, myNeck) #dodajanje valja k obliki myBody

# Izdelava sestava
aRes = TopoDS_Compound() #Določitev spremenljivke za sestav
aBuilder = BRep_Builder()
aBuilder.MakeCompound (aRes)
aBuilder.Add (aRes, myBody.Shape()) #Dodajanje različnih oblik v sestav aRes

# Export to STEP ()
my_step_exporter = STEPExporter("export_step_file.stp") #Določevanje imena STEP file.a 
my_step_exporter.add_shape(aRes) #Dodajanje oblike v STEP file
my_step_exporter.write_file()

# Izris oblike
display.EraseAll()
print dir(display)
display.DisplayShape(aRes)

start_display()

Povzetek druge vaje

Izdelava CAD kocke z predhodno definiranimi točkami (Uporaba for zanke za generiranje točk, površin, mreže)

import sys
from OCC.Display.SimpleGui import *

from OCC.gp import gp_Pnt
from OCC.GC import GC_MakeSegment
from OCC.BRepBuilderAPI import \
  BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace, BRepBuilderAPI_MakeWire,\
  BRepBuilderAPI_MakeShell, BRepBuilderAPI_MakeSolid
from OCC.BRep import BRep_Builder
from OCC.TopoDS import TopoDS_Shell, TopoDS_Solid
from OCC import StlAPI

mesh = {
  "vertices":[[-0.2,-0.2,0.2],[0.2,-0.2,0.2],[0.2,0.2,0.2],[-0.2,0.2,0.2],\
                  [-0.2,-0.2,0.6000000000000001],[0.2,-0.2,0.6000000000000001],\
                  [0.2,0.2,0.6000000000000001],[-0.2,0.2,0.6000000000000001]],
  "faces":[[3,2,1,0],[4,5,6,7],[7,6,2,3],[5,4,0,1],[6,5,1,2],[4,7,3,0]]
}

def main():

  vertices = [ gp_Pnt(p[0],p[1],p[2]) for p in mesh['vertices'] ]
  oFaces = []

  builder = BRep_Builder()
  shell = TopoDS_Shell()
  builder.MakeShell(shell)

  for face in mesh['faces']:
    edges = []
    face.reverse()
    for i in range(len(face)):
      cur = face[i]
      nxt = face[(i+1)%len(face)]
      segment = GC_MakeSegment(vertices[cur],vertices[nxt])
      edges.append(BRepBuilderAPI_MakeEdge(segment.Value()))

    wire = BRepBuilderAPI_MakeWire()
    for edge in edges:
      wire.Add(edge.Edge())

    oFace = BRepBuilderAPI_MakeFace(wire.Wire())

    builder.Add(shell, oFace.Shape())
    display.DisplayShape(shell)

if __name__ == '__main__':
    display, start_display, add_menu, add_function_to_menu = \
        init_display()
    main()
    start_display()

Branje vrednosti iz datoteke (input_file.dat) in generiranje CAD modela (Parametriziran CAD model)

Datoteka: input_file.dat

70 70 70

Programska koda (.py)

#Odpiranje datoteke input_file.dat, ter branje iz nje v izbrane spremenljivke
#Simon Kulovec
from OCC.Display.SimpleGui import *
from OCC.BRepPrimAPI import *

display, start_display, add_menu, add_function_to_menu = init_display()

#Branje iz datoteke: input_file.dat
f= open("input_file.dat", "r")
lines = f.readlines()
box0 = [] #Vektor v katerega shranjujemo prebrane vrednosti

#Stevilo prebranih vrstic je 1
for i in range(1): 
    x,y,z=[eval(s) for s in lines[i].split(" ")]
    box0.append(x)
    box0.append(y)
    box0.append(z)
    #Izpis prebranih vrednosti iz datoteke v terminal
    print "%4.1f %4.1f %4.1f "  % ( box0[i], box0[i+1],\
                                        box0[i+2])
f.close()


my_box = BRepPrimAPI_MakeBox(box0[0],box0[1],box0[2]).Shape()

display.DisplayShape(my_box)
start_display()

Prikaz izdelave kocke in uporaba funkcije krožnega izvleka

##Prikaz izdelave kocke in uporaba funkcije krožnega izvleka
##S.Kulovec, 2011

from OCC.gp import *
from OCC.BRepPrimAPI import *
from OCC.TopExp import *
from OCC.TopAbs import *
import OCC.TopoDS
from OCC.BRep import *
from OCC.Geom import *
from OCC.GCE2d import *
from OCC.Geom2d import *
from OCC.BRepLib import *
from OCC.BRepFeat import *

from OCC.Utils.Topology import Topo
from OCC.BRepBuilderAPI import *

import sys, time
from OCC.Display.SimpleGui import *
display, start_display, add_menu, add_function_to_menu = init_display()

S = BRepPrimAPI_MakeBox(400.,250.,300.).Shape()
faces = list(Topo(S).faces())
F1 = faces[2]
surf = BRep_Tool_Surface(F1)
Pl = Handle_Geom_Plane_DownCast(surf)

D = gp.gp_OX()

MW1 = BRepBuilderAPI_MakeWire() 
p1 = gp_Pnt2d(100.,100.)
p2 = gp_Pnt2d(200.,100.)
aline = GCE2d_MakeLine(p1,p2).Value()
MW1.Add(BRepBuilderAPI_MakeEdge(aline,surf,0.,p1.Distance(p2)).Edge())

p1 = gp_Pnt2d(200.,100.)
p2 = gp_Pnt2d(150.,200.)
aline = GCE2d_MakeLine(p1,p2).Value()
MW1.Add(BRepBuilderAPI_MakeEdge(aline,surf,0.,p1.Distance(p2)).Edge())

p1 = gp_Pnt2d(150.,200.)
p2 = gp_Pnt2d(100.,100.)
aline = GCE2d_MakeLine(p1,p2).Value()
MW1.Add(BRepBuilderAPI_MakeEdge(aline,surf,0.,p1.Distance(p2)).Edge())

MKF1 = BRepBuilderAPI_MakeFace() 
MKF1.Init(surf,False)
MKF1.Add(MW1.Wire())
FP = MKF1.Face()
BRepLib_BuildCurves3d(FP)
MKrev = BRepFeat_MakeRevol(S,FP,F1,D,1,True)
F2 = faces[4]
MKrev.Perform(F2)
display.EraseAll()
display.DisplayShape(MKrev.Shape())

start_display()

Napotki

Q: Zanima me kako se v PythonOCC dela luknje oz izvrtine(ali izreze). Katere funkcije se uporablja?

A: Podobno kot fuse je za izreze cut.

import OCC.Utils.Construct as construct
drzalo = construct.boolean_fuse(roka,nosilec) #celoten sestav
drzalo = construct.boolean_cut(drzalo, izvrtina)

Boolove operacije.

Povezave

Attachments (11)