/src/examples/Tools/InteractiveViewer/scripts/Bottle.py

http://pythonocc.googlecode.com/ · Python · 125 lines · 83 code · 22 blank · 20 comment · 1 complexity · 874e90f7d6a6d6e4a59c1bef7da1614d MD5 · raw file

  1. #!C:/Python24/Python.exe
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Bottle example converted to work with the viewer
  5. '''
  6. __author__ = "Andrew Haywood"
  7. __date__ = "15 November 2007"
  8. # --------------------------------------------------
  9. import os
  10. import os.path
  11. import math
  12. # --------------------------------------------------
  13. display.EraseAll()
  14. # --------------------------------------------------
  15. myWidth = 50
  16. myHeight = 70
  17. myThickness = 30
  18. # Profile : Define Support Points
  19. aPnt1 = gp_Pnt(-myWidth / 2. , 0 , 0)
  20. aPnt2 = gp_Pnt(-myWidth / 2. , -myThickness / 4. , 0)
  21. aPnt3 = gp_Pnt(0 , -myThickness / 2. , 0)
  22. aPnt4 = gp_Pnt(myWidth / 2. , -myThickness / 4. , 0)
  23. aPnt5 = gp_Pnt(myWidth / 2. , 0 , 0)
  24. # Profile : Define the Geometry
  25. aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4)
  26. aSegment1 = GC_MakeSegment(aPnt1 , aPnt2)
  27. aSegment2 = GC_MakeSegment(aPnt4 , aPnt5)
  28. # Profile : Define the Topology
  29. aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
  30. aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
  31. aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
  32. aWire = BRepBuilderAPI_MakeWire(aEdge1.Edge() , aEdge2.Edge() , aEdge3.Edge())
  33. # Complete Profile
  34. xAxis = gp().OX()
  35. aTrsf = gp_Trsf()
  36. aTrsf.SetMirror(xAxis)
  37. aBRepTrsf = BRepBuilderAPI_Transform(aWire.Shape() , aTrsf)
  38. aMirroredShape = aBRepTrsf.Shape()
  39. aMirroredWire = TopoDS().Wire(aMirroredShape)
  40. mkWire = BRepBuilderAPI_MakeWire()
  41. mkWire.Add(aWire.Wire())
  42. mkWire.Add(aMirroredWire)
  43. myWireProfile = mkWire.Wire()
  44. # Body : Prism the Profile
  45. myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
  46. aPrismVec = gp_Vec(0 , 0 , myHeight)
  47. myBody = BRepPrimAPI_MakePrism(myFaceProfile.Shape() , aPrismVec)
  48. # Body : Apply Fillets
  49. mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape())
  50. aEdgeExplorer = TopExp_Explorer(myBody.Shape() , TopAbs_EDGE)
  51. while aEdgeExplorer.More():
  52. aEdge = TopoDS().Edge(aEdgeExplorer.Current())
  53. mkFillet.Add(myThickness / 12. , aEdge)
  54. aEdgeExplorer.Next()
  55. myBody = mkFillet.Shape()
  56. # Body : Add the Neck
  57. neckLocation = gp_Pnt(0 , 0 , myHeight)
  58. neckNormal = gp().DZ()
  59. neckAx2 = gp_Ax2(neckLocation , neckNormal)
  60. myNeckRadius = myThickness / 4.
  61. myNeckHeight = myHeight / 10.
  62. MKCylinder = BRepPrimAPI_MakeCylinder(neckAx2 , myNeckRadius , myNeckHeight)
  63. myNeck = MKCylinder.Shape()
  64. myBody = BRepAlgoAPI_Fuse(myBody , myNeck)
  65. # Threading : Create Surfaces
  66. neckAx2_bis = gp_Ax3(neckLocation , neckNormal)
  67. aCyl1 = Geom_CylindricalSurface(neckAx2_bis , myNeckRadius * 0.99)
  68. aCyl2 = Geom_CylindricalSurface(neckAx2_bis , myNeckRadius * 1.05)
  69. # Threading : Define 2D Curves
  70. aPnt = gp_Pnt2d(2. * math.pi , myNeckHeight / 2.)
  71. aDir = gp_Dir2d(2. * math.pi , myNeckHeight / 4.)
  72. aAx2d = gp_Ax2d(aPnt , aDir)
  73. aMajor = 2. * math.pi
  74. aMinor = myNeckHeight / 10.
  75. anEllipse1 = Geom2d_Ellipse(aAx2d , aMajor , aMinor)
  76. anEllipse2 = Geom2d_Ellipse(aAx2d , aMajor , aMinor / 4)
  77. aArc1 = Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse1) , 0 , math.pi)
  78. aArc2 = Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse2) , 0 , math.pi)
  79. aArc1_handle = Handle_Geom2d_Curve(aArc1)
  80. aArc2_handle = Handle_Geom2d_Curve(aArc2)
  81. anEllipsePnt1 = anEllipse1.Value(0)
  82. anEllipsePnt2 = anEllipse1.Value(math.pi)
  83. aSegment = GCE2d_MakeSegment(anEllipsePnt1 , anEllipsePnt2)
  84. # Threading : Build Edges and Wires
  85. aEdge1OnSurf1 = BRepBuilderAPI_MakeEdge( aArc1_handle , Handle_Geom_Surface(aCyl1))
  86. aEdge2OnSurf1 = BRepBuilderAPI_MakeEdge( aSegment.Value() , Handle_Geom_Surface(aCyl1))
  87. aEdge1OnSurf2 = BRepBuilderAPI_MakeEdge( aArc2_handle , Handle_Geom_Surface(aCyl2))
  88. aEdge2OnSurf2 = BRepBuilderAPI_MakeEdge( aSegment.Value() , Handle_Geom_Surface(aCyl2))
  89. threadingWire1 = BRepBuilderAPI_MakeWire(aEdge1OnSurf1.Edge() , aEdge2OnSurf1.Edge())
  90. threadingWire2 = BRepBuilderAPI_MakeWire(aEdge1OnSurf2.Edge() , aEdge2OnSurf2.Edge())
  91. BRepLib().BuildCurves3d(threadingWire1.Shape())
  92. BRepLib().BuildCurves3d(threadingWire2.Shape())
  93. # Create Threading
  94. aTool = BRepOffsetAPI_ThruSections(True)
  95. aTool.AddWire(threadingWire1.Wire())
  96. aTool.AddWire(threadingWire2.Wire())
  97. aTool.CheckCompatibility(False)
  98. myThreading = aTool.Shape()
  99. display.DisplayShape(myBody.Shape())
  100. display.DisplayShape(myThreading)
  101. display.View_Iso()
  102. display.Zoom_FitAll()
  103. # --------------------------------------------------