domingo, 14 de abril de 2013

Creating a spiral drilling stabilizer with Salomé - Python Script

As I promised, we are going to take a look at the Python script generated by the dump file from the generated stabilizer. But ... to make things clear ... I'm not an expert on Python but I try to make things to be reusable as much as possible. My coding can be very ugly, but I understand them on the first sight.

I would suggest you to visit the following links regarding Salomé's script:

I know this can be boring but that is why I strongly suggest you to have a problem to be solved, a driver ... something to give you a purpose, and if do so learn something from the script, it can save you lots and lots of time.


Time to go.


  1. Open Salomé and load the hdf file you had created. 
  2. Press "Ctrl" + "D" or go to "File >> Dump Study". 
  3. Name your python file the way you would like.
  4. Save it.
The generated file:

  1. # -*- coding: iso-8859-1 -*-

  2. ###
  3. ### This file is generated automatically by SALOME v6.6.0 with dump python functionality
  4. ###

  5. import sys
  6. import salome

  7. salome.salome_init()
  8. theStudy = salome.myStudy

  9. import salome_notebook
  10. notebook = salome_notebook.notebook
  11. sys.path.insert( 0, r'/media/rafael/5e8dda66-24fd-42c8-b0d6-ece1a49fa89d/Projects/CFD/Salome')

  12. ###
  13. ### GEOM component
  14. ###

  15. import GEOM
  16. import geompy
  17. import math
  18. import SALOMEDS


  19. geompy.init_geom(theStudy)

  20. O = geompy.MakeVertex(0, 0, 0)
  21. OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
  22. OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
  23. OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
  24. OD_max_point = geompy.MakeVertexWithRef(O, 0.10795, 0, 0)
  25. Vertex_1 = geompy.MakeVertexWithRef(OD_max_point, 0, 0.3, 0)
  26. OD_min_point = geompy.MakeVertexWithRef(Vertex_1, -0.0254, 0.043994, 0)
  27. Vertex_2 = geompy.MakeVertexWithRef(OD_min_point, 0, 0.6, 0)
  28. Vertex_3 = geompy.MakeVertex(0, 0.943994, 0)
  29. Line_1 = geompy.MakeLineTwoPnt(Vertex_3, O)
  30. Line_2 = geompy.MakeLineTwoPnt(O, OD_max_point)
  31. Line_3 = geompy.MakeLineTwoPnt(OD_max_point, Vertex_1)
  32. Line_4 = geompy.MakeLineTwoPnt(Vertex_1, OD_min_point)
  33. Line_5 = geompy.MakeLineTwoPnt(OD_min_point, Vertex_2)
  34. Line_1_vertex_2 = geompy.GetSubShape(Line_1, [2])
  35. Line_6 = geompy.MakeLineTwoPnt(Vertex_2, Line_1_vertex_2)
  36. Wire_1 = geompy.MakeWire([Line_1, Line_2, Line_3, Line_4, Line_5, Line_6], 1e-07)
  37. Face_1 = geompy.MakeFaceWires([Wire_1], 1)
  38. Revolution_1 = geompy.MakeRevolution(Face_1, OY, 360*math.pi/180.0)
  39. geomObj_1 = geompy.MakeCurveParametric("0.5*0.2159*cos(t+pi/2)", "t*1.2/pi", "0.5*0.2159*sin(t+pi/2)", 0, 1, 999, geompy.GEOM.Interpolation, True)
  40. [geomObj_2,geomObj_3] = geompy.ExtractShapes(geomObj_1, geompy.ShapeType["VERTEX"], True)
  41. geomObj_4 = geompy.MakeCurveParametric("0.5*0.1651*cos(t+pi/2)", "t*1.2/pi", "0.5*0.1651*sin(t+pi/2)", 0, 1, 999, geompy.GEOM.Interpolation, True)
  42. [geomObj_5,geomObj_6] = geompy.ExtractShapes(geomObj_4, geompy.ShapeType["VERTEX"], True)
  43. geomObj_7 = geompy.MakeEdge(geomObj_2, geomObj_5)
  44. geomObj_8 = geompy.MakeEdge(geomObj_6, geomObj_3)
  45. geomObj_9 = geompy.MakeWire([geomObj_1, geomObj_4, geomObj_7, geomObj_8], 1e-07)
  46. geomObj_10 = geompy.MakeFaceWires([geomObj_9], 0)
  47. geomObj_11 = geompy.MakeRevolution(geomObj_10, OY, 40*math.pi/180.0)
  48. Curve_1 = geompy.MakeCurveParametric("0.6*0.2159*cos(t+pi/2)", "t*1.2/pi", "0.6*0.2159*sin(t+pi/2)", 0, 0.9, 999, geompy.GEOM.Interpolation, True)
  49. [Vertex_4,Vertex_5] = geompy.ExtractShapes(Curve_1, geompy.ShapeType["VERTEX"], True)
  50. Curve_2 = geompy.MakeCurveParametric("0.5*0.1651*cos(t+pi/2)", "t*1.2/pi", "0.5*0.1651*sin(t+pi/2)", 0, 0.9, 999, geompy.GEOM.Interpolation, True)
  51. [Vertex_6,Vertex_7] = geompy.ExtractShapes(Curve_2, geompy.ShapeType["VERTEX"], True)
  52. Edge_1 = geompy.MakeEdge(Vertex_4, Vertex_6)
  53. Edge_2 = geompy.MakeEdge(Vertex_7, Vertex_5)
  54. Wire_2 = geompy.MakeWire([Curve_1, Curve_2, Edge_1, Edge_2], 1e-07)
  55. Face_2 = geompy.MakeFaceWires([Wire_2], 0)
  56. Revolution_2 = geompy.MakeRevolution(Face_2, OY, 80*math.pi/180.0)
  57. Cut_1 = geompy.MakeCut(Revolution_1, Revolution_2)
  58. geompy.Rotate(Cut_1, OY, 120*math.pi/180.0)
  59. Cut_2 = geompy.MakeCut(Cut_1, Revolution_2)
  60. geompy.Rotate(Cut_2, OY, 120*math.pi/180.0)
  61. Cut_3 = geompy.MakeCut(Cut_2, Revolution_2)
  62. Mirror_1 = geompy.MakeMirrorByAxis(Cut_3, OX)
  63. geompy.Rotate(Mirror_1, OY, 20*math.pi/180.0)
  64. Fuse_1 = geompy.MakeFuse(Cut_3, Mirror_1)
  65. geompy.addToStudy( O, 'O' )
  66. geompy.addToStudy( OX, 'OX' )
  67. geompy.addToStudy( OY, 'OY' )
  68. geompy.addToStudy( OZ, 'OZ' )
  69. geompy.addToStudy( OD_max_point, 'OD_max_point' )
  70. geompy.addToStudy( Vertex_1, 'Vertex_1' )
  71. geompy.addToStudy( OD_min_point, 'OD_min_point' )
  72. geompy.addToStudy( Vertex_2, 'Vertex_2' )
  73. geompy.addToStudy( Vertex_3, 'Vertex_3' )
  74. geompy.addToStudy( Line_1, 'Line_1' )
  75. geompy.addToStudy( Line_2, 'Line_2' )
  76. geompy.addToStudy( Line_3, 'Line_3' )
  77. geompy.addToStudy( Line_4, 'Line_4' )
  78. geompy.addToStudy( Line_5, 'Line_5' )
  79. geompy.addToStudyInFather( Line_1, Line_1_vertex_2, 'Line_1:vertex_2' )
  80. geompy.addToStudy( Line_6, 'Line_6' )
  81. geompy.addToStudy( Wire_1, 'Wire_1' )
  82. geompy.addToStudy( Face_1, 'Face_1' )
  83. geompy.addToStudy( Revolution_1, 'Revolution_1' )
  84. geompy.addToStudy( Curve_1, 'Curve_1' )
  85. geompy.addToStudy( Curve_2, 'Curve_2' )
  86. geompy.addToStudyInFather( Curve_1, Vertex_4, 'Vertex_4' )
  87. geompy.addToStudyInFather( Curve_1, Vertex_5, 'Vertex_5' )
  88. geompy.addToStudyInFather( Curve_2, Vertex_6, 'Vertex_6' )
  89. geompy.addToStudyInFather( Curve_2, Vertex_7, 'Vertex_7' )
  90. geompy.addToStudy( Edge_1, 'Edge_1' )
  91. geompy.addToStudy( Edge_2, 'Edge_2' )
  92. geompy.addToStudy( Wire_2, 'Wire_2' )
  93. geompy.addToStudy( Face_2, 'Face_2' )
  94. geompy.addToStudy( Revolution_2, 'Revolution_2' )
  95. geompy.addToStudy( Cut_1, 'Cut_1' )
  96. geompy.addToStudy( Cut_2, 'Cut_2' )
  97. geompy.addToStudy( Mirror_1, 'Mirror_1' )
  98. geompy.addToStudy( Cut_3, 'Cut_3' )
  99. geompy.addToStudy( Fuse_1, 'Fuse_1' )
Just ugly ... I used to get terrified by coding but, as I mentioned ... you'll save time. You can take this time and enjoy in a nerdish way by improving the code, or the tool, I don't know ... just choose something. 

I would like to explain from the line 1 to 28, but I'm here to be practical so, don't touch them. 

Why do we need to take a look on this? Well, mainly because I would like to understand what is happening and also, because, we can draw any type of stabilizer. But, as heads up, If you want to create any sort of STB you probably are going to debug the code or create more code. Why? A Spiral STB is a tortuous tool and it depends on the precision ... sometimes the cutting action over the virgin tool, won't work, that is when you need to debug the code/design.

From now on I'll try to comment the coding.

29 - From the previous Salomé, I believe, we had to create the origin point. Salomé 6.6 takes care of this by creating the O vertex at (0,0,0). That is what this line of code does, calls the geompy package and asks for MakeVertex function.

30-32 - From these lines is possible to observe that it was created the unit vectors by calling the MakeVector function. Again, this a good thing and shouldn't be touched.

33-36 - As we did with the first steps with stabilizer creation. We used a reference to create our vertexes . The MakeVertexWithRef function asks for the Reference point, "x" axis walk, "y" walk and "z" axis walk all related to a reference point. Pay attention on the values we are inputting. Do they mean anything to you?

38-44 - After point creation, what we did? We had created lines to connect all the points. That is what is happening in here. Pay attention on line 43. We can see two inputs: Line_1 and [2]. The function geompy.GetSubShape(Line_1, [2]) takes Line_1 as the source of the SubShapes (it can be a vertex, edge, line, face, etc ...) and wants the "[2]", for this case, [2], is the first variable when you created the Line_1, so, it would be Vertex_3. If you put [3] it is going to return the second variable, O, and [1] would be the line itself.

Uhhhmmm ... I believe I'm made my point here. I don't want to be boring and you can see the rest of the code is pretty much self explanatory.

So ... What do we want? We want to have this code to be reusable and with those numbers ... just can't. Your brain can decode easily names, not numbers. We must put everything on a easy way. This is my code and for sure can be improved.

Cheers!!!


  1. import GEOM
  2. import geompy
  3. import math
  4. import SALOMEDS

  5. OD_max = 8.5 # in
  6. OD_min = 6.5 # in
  7. a = 20 # deg
  8. WaC = 0.6 # m - Wall Contact
  9. SmSpa = (0.5*(OD_max-OD_min)*0.0254)/math.tan(a*math.pi/180) # m 
  10. FN = 0.6 # m - Tem que ser igual ao Body Neck BN
  11. BN = 0.6 # m
  12. Total_Length = WaC + 2*SmSpa + FN + BN
  13. geompy.init_geom(theStudy)

  14. O = geompy.MakeVertex(0, 0, 0)
  15. OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
  16. OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
  17. OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
  18. Flow = geompy.MakeVectorDXDYDZ(0, 0, 1)

  19. #Creating the points before making the lines and closing the wire
  20. OD_max_point = geompy.MakeVertexWithRef(O, 0, 0.5*OD_max*0.0254, 0)
  21. Half_WaC = geompy.MakeVertexWithRef(OD_max_point, 0,  0 , WaC/2)
  22. OD_min_point = geompy.MakeVertexWithRef(Half_WaC,0, -0.5*(OD_max-OD_min)*0.0254, SmSpa)
  23. FN_point = geompy.MakeVertexWithRef(OD_min_point, 0, 0 , FN)
  24. FN_point_2 = geompy.MakeVertexWithRef(FN_point, 0, -0.5*OD_min*0.0254, 0)
  25. #

  26. #Connecting the points
  27. Line_1 = geompy.MakeLineTwoPnt(O, OD_max_point)
  28. Line_2 = geompy.MakeLineTwoPnt(OD_max_point, Half_WaC)
  29. Line_3 = geompy.MakeLineTwoPnt(Half_WaC, OD_min_point)
  30. Line_4 = geompy.MakeLineTwoPnt(OD_min_point, FN_point)
  31. Line_5 = geompy.MakeLineTwoPnt(FN_point, FN_point_2)
  32. Line_6 = geompy.MakeLineTwoPnt(FN_point_2, O)
  33. #

  34. #Creating the wireframe and the face
  35. Wire_1 = geompy.MakeWire([Line_1, Line_2, Line_3, Line_4, Line_5, Line_6], 1e-07)
  36. Face_1 = geompy.MakeFaceWires([Wire_1], 1)
  37. #

  38. #Creating the solid. The stabilizer basis
  39. Revolution_1 = geompy.MakeRevolution(Face_1, OZ, 360*math.pi/180.0)
  40. #

  41. #Creating the cutting tool
  42. Curve_1 = geompy.MakeCurveParametric("0.55*OD_max*0.0254*cos(t+pi/2)", "0.55*OD_max*0.0254*sin(t+pi/2)", "t*1.2/pi", 0, (0.5*WaC + SmSpa)*math.pi/1.2, 999, geompy.GEOM.Interpolation, True)
  43. [Vertex_1,Vertex_2] = geompy.ExtractShapes(Curve_1, geompy.ShapeType["VERTEX"], True)

  44. Curve_2 = geompy.MakeCurveParametric("0.5*OD_min*0.0254*cos(t+pi/2)", "0.5*OD_min*0.0254*sin(t+pi/2)", "t*1.2/pi", 0, (0.5*WaC + SmSpa)*math.pi/1.2, 999, geompy.GEOM.Interpolation, True)
  45. [Vertex_3,Vertex_4] = geompy.ExtractShapes(Curve_2, geompy.ShapeType["VERTEX"], True)

  46. Edge_1 = geompy.MakeEdge(Vertex_1, Vertex_3)
  47. Edge_2 = geompy.MakeEdge(Vertex_2, Vertex_4)
  48. Wire_2 = geompy.MakeWire([Curve_1, Curve_2, Edge_1, Edge_2], 1e-07)
  49. Face_2 = geompy.MakeFaceWires([Wire_2], 0)
  50. Revolution_2 = geompy.MakeRevolution(Face_2, OZ, 20*math.pi/180.0)
  51. #

  52. #Cutting, Rotating, Mirroring
  53. Cut_1 = geompy.MakeCut(Revolution_1, Revolution_2)
  54. geompy.Rotate(Cut_1, OZ, 120*math.pi/180.0)
  55. Cut_2 = geompy.MakeCut(Cut_1, Revolution_2)
  56. geompy.Rotate(Cut_2, OZ, 120*math.pi/180.0)
  57. Cut_3 = geompy.MakeCut(Cut_2, Revolution_2)
  58. Mirror_1 = geompy.MakeMirrorByAxis(Cut_3, OY)
  59. geompy.Rotate(Mirror_1, OZ, -40*math.pi/180.0)
  60. Stabilizer = geompy.MakeFuse(Cut_3, Mirror_1)

  61. geompy.addToStudy( Stabilizer, 'Stabilizer' )

  62. geompy.addToStudy( Revolution_1, 'Revolution_1' )
  63. geompy.addToStudy( Revolution_2, 'Revolution_2' )

  64. if salome.sg.hasDesktop():
  65.   salome.sg.updateObjBrowser(1)

Nenhum comentário:

Postar um comentário