Changes between Version 53 and Version 54 of PythonOcc


Ignore:
Timestamp:
Nov 3, 2015, 11:35:48 AM (8 years ago)
Author:
dpenko
Comment:

Posodobil "bottle" primer.

Legend:

Unmodified
Added
Removed
Modified
  • PythonOcc

    v53 v54  
    310310{{{
    311311#!python
    312 ##Copyright 2011 Simon Kulovec (simon.kulovec@lecad.si)
     312##Copyright 2011 Simon Kulovec (simon.kulovec@lecad.si)
     313##Uredil in posodobil 2015 Dejan Penko (dejan.penko@lecad.fs.uni-lj.si)
    313314##Example: MakeCADBottle
    314315##This file is part of pythonOCC.
     
    327328from OCC.BRepFilletAPI import *
    328329from OCC.BRepAlgoAPI import *
    329 from OCC.Utils.Topology import *
    330330from OCC.Geom import *
    331331from OCC.Geom2d import *
     
    340340import math
    341341
     342#Naslednja podprograma "face_is_plane" in "geom_plane_from_face" sta napisana za pomoč v nadaljevanju programa
     343def face_is_plane(face):
     344    """
     345    Vrne True če je TopoDS_Shape ravnina, False v nasprotnem primeru
     346    """
     347    hs = BRep_Tool_Surface(face)
     348    downcast_result = Handle_Geom_Plane.DownCast(hs)
     349    # Vrednost je "null", če se izvedba tega podprograma ne izvede pravilo ali je ni mogoče izvesti. Iz tega sledi, da profil ni ravnina
     350    if downcast_result.IsNull():
     351        return False
     352    else:
     353        return True
     354
     355def geom_plane_from_face(aFace):
     356    """
     357    Vrne subjekt geometrične ravnine iz ravninske površine
     358    """
     359    return Handle_Geom_Plane.DownCast(BRep_Tool_Surface(aFace)).GetObject()
     360
    342361def show_bottle(aRes):
    343362    display.EraseAll()
    344     print dir(display)
     363    #print (dir(display))
    345364    display.DisplayShape(aRes)
    346365
    347 def define_points(myWidth, myThickness, myHeight):
    348     #Definiranje začetnih točk
     366def define_body(myWidth, myThickness, myHeight):
     367    ## Definiranje začetnih točk
    349368    aPnt1 = gp_Pnt(-myWidth / 2. , 0 , 0)
    350369    aPnt2 = gp_Pnt(-myWidth / 2. , -myThickness / 4. , 0)
     
    353372    aPnt5 = gp_Pnt(myWidth / 2. , 0 , 0)
    354373
    355     #Definiranje geometrije
     374    ## Definiranje geometrije
    356375    aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4)
    357376    aSegment1 = GC_MakeSegment(aPnt1 , aPnt2)
    358377    aSegment2 = GC_MakeSegment(aPnt4 , aPnt5)
    359378
    360     #Definiranje topologije
     379    ## Definiranje topologije
     380    #Definiranje robov
    361381    aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
    362382    aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
    363383    aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
     384    #Definiranje robov iz mreže
    364385    aWire  = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() ,aEdge3.Edge())
    365386
    366     #Izdelava celotnega profila - mirror
     387    ## Izdelava celotnega profila - mirror
     388    #Definiramo os zrcaljenja (os X)
    367389    xAxis = gp_OX()
     390    #Nastavimo "zrcalo"
    368391    aTrsf = gp_Trsf()
    369392    aTrsf.SetMirror(xAxis)
     393    #Uporabimo zrcalno transformacijo
    370394    aBRepTrsf = BRepBuilderAPI_Transform(aWire.Shape() , aTrsf)
     395    #Zrcalno obliko dobimo nazaj iz transformacije in pretvorimo v mrežo
    371396    aMirroredShape = aBRepTrsf.Shape()
    372     aMirroredWire = TopoDS_wire(aMirroredShape)
     397    aMirroredWire = topods_Wire(aMirroredShape)
     398    #Kombiniramo dve konsistentni mreži
    373399    mkWire = BRepBuilderAPI_MakeWire()
    374400    mkWire.Add(aWire.Wire())
     
    376402    myWireProfile = mkWire.Wire()
    377403
    378     # Telo: Iz profila se izdela telo
     404    ## Telo:
     405    #Iz profila se izdela telo
    379406    myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
    380407    aPrismVec = gp_Vec(0 , 0 , myHeight)
    381408    myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face() , aPrismVec)
    382409
    383     # Telo: Dodamo zaokrožitve (fillet)
     410    #Dodamo zaokrožitve (fillet) s pomočjo t. i. Explorerja
    384411    mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape())
    385     topology_traverser = Topo(myBody.Shape())
    386     for aEdge in topology_traverser.edges():
    387         mkFillet.Add(myThickness / 12. , aEdge)
    388     myBody = mkFillet.Shape()
    389 
    390     #Dodajanje grla na steklenico
     412
     413    anEdgeExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_EDGE)
     414
     415    while anEdgeExplorer.More():
     416        anEdge = topods.Edge(anEdgeExplorer.Current())
     417        mkFillet.Add(myThickness / 12.0, anEdge)
     418
     419        anEdgeExplorer.Next()
     420
     421    myBody = mkFillet
     422
     423    ##Dodajanje grla na steklenico
    391424    neckLocation = gp_Pnt(0, 0, myHeight)
    392425    neckNormal = gp_DZ()
     
    396429    myNeckHeight = myHeight / 10
    397430
    398     mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2 , myNeckRadius , \
    399                                           myNeckHeight)
    400     myNeck = mkCylinder.Shape();
    401 
    402     myBody = BRepAlgoAPI_Fuse(myBody, myNeck)
    403 
    404     # Izdelava votle steklenice
     431    mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2 , myNeckRadius , myNeckHeight)
     432                                         
     433    myNeck = mkCylinder;
     434
     435    myBody = BRepAlgoAPI_Fuse(myBody.Shape(), myNeck.Shape())
     436
     437    ## Izdelava votle steklenice
     438    #CILJ: Poiščemo najvišji Z profil in ga odstranimo
    405439    faceToRemove = None
    406440    zMax = -1;
    407     t = Topo(myBody.Shape())
    408     k=1
    409     for  aFace in t.faces():
    410    
    411         aSurface = BRep_Tool().Surface(aFace)
    412    
    413         if aSurface.GetObject().IsInstance('Geom_Plane'):
    414             aPlane = Handle_Geom_Plane().DownCast(aSurface).GetObject()
     441
     442    #Če želimo najti najvišji Z profil, katerega želimo odstraniti iz lupine, je potrebno iti skozi vse profile
     443    aFaceExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_FACE)
     444    while aFaceExplorer.More():
     445        aFace = topods.Face(aFaceExplorer.Current())
     446
     447        if face_is_plane(aFace):
     448            aPlane = geom_plane_from_face(aFace)
     449
     450            # We want the highest Z face, so compare this to the previous faces
    415451            aPnt = aPlane.Location()
    416452            aZ = aPnt.Z()
    417             if aZ>zMax:
     453            if aZ > zMax:
     454                zMax = aZ
    418455                faceToRemove = aFace
    419456
     457        aFaceExplorer.Next()
     458   
    420459    facesToRemove = TopTools_ListOfShape()
    421460    facesToRemove.Append(faceToRemove)
     
    423462                                          -myThickness/50 , 1.e-3)
    424463   
    425     # Threading : Create Surfaces
     464    ## Navoj na vratu steklenice:
     465    #Ustvarjanje površine
    426466    aCyl1 = Geom_CylindricalSurface(gp_Ax3(neckAx2) , myNeckRadius * 0.99)
    427467    aCyl2 = Geom_CylindricalSurface(gp_Ax3(neckAx2) , myNeckRadius * 1.05)
    428468
    429     # Threading : Define 2D Curves
    430     aPnt = gp_Pnt2d(2. * 3.141592 , myNeckHeight / 2.)
    431     aDir = gp_Dir2d(2. * 3.141592 , myNeckHeight / 4.)
     469    #Definiranje 2D krivulje navoja
     470    aPnt = gp_Pnt2d(2. * math.pi , myNeckHeight / 2.)
     471    aDir = gp_Dir2d(2. * math.pi , myNeckHeight / 4.)
    432472    aAx2d = gp_Ax2d(aPnt , aDir)
    433473       
    434     aMajor = 2. * 3.141592
     474    aMajor = 2.0 * math.pi
    435475    aMinor = myNeckHeight / 10.
    436476   
    437477    anEllipse1 = Geom2d_Ellipse(aAx2d , aMajor , aMinor)
    438     anEllipse2 = Geom2d_Ellipse(aAx2d , aMajor , aMinor / 4)
    439 
    440     aArc2 = Geom2d_TrimmedCurve(anEllipse1.GetHandle() , 3.141592, 0.)
    441     aArc1 = Geom2d_TrimmedCurve(anEllipse2.GetHandle() , 3.141592, 0.)
    442 
     478    anEllipse2 = Geom2d_Ellipse(aAx2d , aMajor , aMinor / 4.0)
     479
     480    anArc1 = Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse1), 0, math.pi)
     481    anArc2 = Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse2), 0, math.pi)
     482   
    443483    anEllipsePnt2 = anEllipse1.Value(0.)
    444     anEllipsePnt1 = anEllipse1.Value(3.141592)
     484    anEllipsePnt1 = anEllipse1.Value(math.pi)
    445485   
    446486    aSegment = GCE2d_MakeSegment(anEllipsePnt1 , anEllipsePnt2)
    447487
    448     # Threading : Build Edges and Wires
    449     aEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(aArc1.GetHandle() , aCyl1.GetHandle())
     488    #Robovi in mreže
     489    aEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1.GetHandle() , aCyl1.GetHandle())
    450490    aEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment.Value() , aCyl1.GetHandle())
    451     aEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(aArc2.GetHandle() , aCyl2.GetHandle())
     491    aEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2.GetHandle() , aCyl2.GetHandle())
    452492    aEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value() , aCyl2.GetHandle())
    453     print dir(aEdge1OnSurf1)
     493    #print (dir(aEdge1OnSurf1))
    454494    threadingWire1 = BRepBuilderAPI_MakeWire(aEdge1OnSurf1.Edge() , aEdge2OnSurf1.Edge())
    455495    threadingWire2 = BRepBuilderAPI_MakeWire(aEdge1OnSurf2.Edge() , aEdge2OnSurf2.Edge())
    456496
    457 
    458     BRepLib().BuildCurves3d(threadingWire1.Wire())
    459     BRepLib().BuildCurves3d(threadingWire2.Wire())
    460 
    461     # Create Threading
     497    #3D predstava robov/mrež
     498    breplib.BuildCurves3d(threadingWire1.Shape())
     499    breplib.BuildCurves3d(threadingWire2.Shape())
     500   
     501    #Površina navoja
    462502    aTool = BRepOffsetAPI_ThruSections(True)
    463 
    464503    aTool.AddWire(threadingWire1.Wire())
    465504    aTool.AddWire(threadingWire2.Wire())
    466505    aTool.CheckCompatibility(False)
    467 
    468506    myThreading = aTool.Shape()
    469507
    470     # Izdelava sestava
     508    ## Izdelava sestava
    471509    aRes = TopoDS_Compound()
    472510
     
    477515    aBuilder.Add (aRes, myThreading)
    478516
    479     # Izris oblike
     517    ## Izris oblike
    480518    show_bottle(aRes)
     519
     520def draw_bottle(event=None):
     521    # Definiranje razdalj: širina, dolžina, višina
     522    myWidth = 50.0
     523    myThickness = 30.0
     524    myHeight = 70.0
     525    # Define Points
     526    define_body(myWidth, myThickness, myHeight)
     527   
     528if __name__ == '__main__':
     529    # OCC.Display.SimpleGui.init_display() returns multiple
     530    # values which are assigned here
     531    display, start_display, add_menu, add_function_to_menu = \
     532    init_display()
     533    draw_bottle() #kličemo podprogram za izris bottle
     534    start_display()
     535
    481536
    482537def draw_bottle(event=None):