2.11. Python scripting

2.11.1. Importing polyline from ASCII file

The points for polyline are stored in a text file in columns. Coordinate values for x, y and z are separated with space delimiter and are in format

x1 y1 z1
x2 y2 z2
x3 y3 z3
etc.

2.11.1.1. Importing to GEOM module

In order to input points from a file to polyline we first need to read the file and store points to a variable s of type string. Then we call the appropriate libraries and create polyline.

f = open('path_to_file') # open file with function open()
s = f.read() # store values in file as string
f.close() # close the file
data = [(float(line.split()[0]), float(line.split()[1]), \
float(line.split()[2])) for line in s.splitlines()] # split each line in string with delimiter " " and store it in a list called data
from salome.geom import geomBuilder # import the correct libraries
import salome
geompy = geomBuilder.New() # create new GEOM builder
polyline = geompy.MakePolyline([geompy.MakeVertex(data[i][0],data[i][1],data[i][2]) for i in range(len(data))]) # create polyline with function MakePolyline
geompy.addToStudy(polyline,'polyline') # add new polyline to study
salome.sg.updateObjBrowser()

2.11.1.2. Importing to SMESH module

To import points from text file to SMESH module we again need to read text files, import libraries and then create polyline with help of functions from those libraries.

f = open('path_to_file') # open file with function open()
s = f.read() # store values in file as string
f.close() # close the file
data = [(float(line.split()[0]), float(line.split()[1]), float(line.split()[2])) for line in s.splitlines()] # split each line in string with delimiter " " and store it in a list called data
from salome.smesh import smeshBuilder # import the correct libraries
import salome
smesh = smeshBuilder.New() # create new SMESH builder
mesh_name = 'polyline' # name of new polyline
myMesh = smesh.Mesh(mesh_name) # create myMesh object

node1 = myMesh.AddNode(data[0][0], data[0][1], data[0][2]) # Create and append new nodes to myMesh object
for i in range(0, len(data)):
     node2 = myMesh.AddNode(data[i][0], data[i][1], data[i][2])
     myMesh.AddEdge([node1, node2])
     node1 = node2
smeshComp = salome.myStudy.FindComponent('SMESH') # search for component SMESH in active salome study
base = smeshComp.GetID() # get ID  and search for object
tag = smeshComp.GetLastChildTag()
child = salome.myStudy.FindObjectID(base + ':' + str(tag))
child.SetAttrString('AttributePixMap', 'ICON_SMESH_TREE_MESH')
salome.sg.updateObjBrowser()

2.11.2. Scripting Geometry

For complex models Python scripting can be used to build geometry models.In this tutorial we will show four examples on how to build CAD models in GEOM module.

2.11.2.1. Initialization

Each script must contain following code, that initializes Salome. This enables us interaction with SMITER interface

import salome
salome.salome_init()

Now we have to import GEOM python library, that contains all functions used in GEOM modue that allow us to easily manipulate with geometrical objects in SMITER.

import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New()

Object geompy of class geomBuilder contains all necessary functions for geometry manipulation.

2.11.2.2. Creating a Box

Function geompy.MakeBox() takes coordinates of two points and then creates a box between them.

import salome
import GEOM
from salome.geom import geomBuilder
geompy = geomBuilder.New()
box =  geompy.MakeBox(10.0,10.0,10.0, 20, 20, 20)
# Insertion of property into study
geompy.addToStudy(box, 'Box')
salome.sg.updateObjBrowser()

With function geompy.addToStudy() we add geometrical object to SMITER study. In the last line we update object browser so that created boject is displayed in GEOM module.

2.11.2.3. Extrusion of a face

For CAD modelling in commercial CAD software (like SolidWorks, AutoCad, Catia, Siemens NX,…) the following standard procedure is used:

  • Definition of sketch surface
  • Sketch drawing on surface
  • Definition of sketch entities (dimensions and position of lines, vertices,…)
  • Definition of process for creation of 3D object (Extrude, revolve, …)
  • Definition of process parameters (Direction and height of extrusion, …)
  • Operation of extrusion is finished - full 3D model

In Python development this procedure is different. Some parameters in commercial Cad software are determined automatically, while in python scripting those parameters must be defined manually. The following modelling steps apply:

  1. Importing libraries

    In this step we import all libraries into Python script.

    import salome
    salome.salome_init()
    import GEOM
    from salome.geom import geomBuilder
    geompy = geomBuilder.New()
    gg = salome.ImportComponentGUI("GEOM")
    
  2. Vertices definition

    Now we can define points in three-dimensional space with function geompy.MakeVertex, that takes coordinates of point as input arguments.

    p1 = geompy.MakeVertex(   0.,   0.,   0.)
    p2 = geompy.MakeVertex( 100.,   0.,   0.)
    p3 = geompy.MakeVertex( 100., 100.,   0.)
    p4 = geompy.MakeVertex(   0., 100.,   0.)
    
    ../_images/pointsPrismExample.svg

    Fig. 2.28 Points on surface.

    To display objects in GEOM module, we have to add following commands to our code

    # Add points to study
    geompy.addToStudy(p1,"point1")
    geompy.addToStudy(p2,"point2")
    geompy.addToStudy(p3,"point3")
    geompy.addToStudy(p4,"point4")
    salome.sg.updateObjBrowser()
    
  3. Polyline definition

    With function geompy.MakePolyline() we can create polyline, that consists of four lines.

    polyline = geompy.MakePolyline([p1, p2, p3, p4, p1])
    # Add polyline to study
    geompy.addToStudy(polyline,"polyline")
    # Update object browser
    salome.sg.updateObjBrowser()
    
    ../_images/polylinePrismExample.svg

    Fig. 2.29 Polyline on surface.

  4. Face creation

    Now we have to create face, that is limited with previously defined polyline. We do that with function geompy.MakeFace(). This function takes closed polyline and creates face within it. Then we add this face to SMITER study.

    polyline = geompy.MakePolyline([p1, p2, p3, p4, p1])
    # Add polyline to study
    geompy.addToStudy(polyline,"polyline")
    # Update object browser
    salome.sg.updateObjBrowser()
    
    ../_images/facePrismExample.svg

    Fig. 2.30 Face on surface.

  1. Extrusion

    Extrusion of face is done with geompy.MakePrism(). This function takes three input arguments - face, point_1, point_2, where point 1 and point 2 are points that define a vector of extrusion.

    p5 = geompy.MakeVertex(0.,0.,100.)
    prism1 = geompy.MakePrism(face, p1, p5)
    # Add new extrusion to study
    geompy.addToStudy(prism1, "prism1")
    # Update tree structure
    salome.sg.updateObjBrowser()
    
    ../_images/extrudePrismExample.svg

    Fig. 2.31 Final model.

2.11.3. First Wall Panel 4 modelling

In this section we will show examples of creation of curves in GEOM module and extrusion of these curves. The final result is shell of first wall panel 4 in ITER fusion reactor. In text file data.txt coordinates of points x, y and z are defined. An interpolated curve will be created through those points that will present first right side of wall shape in toroidal direction. This curve has to be mirrored in the y-z plane to define also the left side. Later on, this interpolated curve will be extruded along polyline path to create a shell.

../_images/panelPanelExample.svg

Fig. 2.32 Final model.

Coordinates in text file are in the following format:

x1 y1 z1
x2 y2 z2
x3 y3 z3
xn yn zn

We prepare several modelling steps.

  1. Importing libraries

    In this step we import all libraries into python script.

    import salome
    import numpy as np
    import GEOM
    from salome.geom import geomBuilder
    geompy = geomBuilder.New()
    
  2. Reading data from file

    Coordinates in columns can be read from file in many different ways. In this tutorial we can import them with function loadtxt() from library np.array() with names coord_x and coord_y. Then we can mirror these points in the y-z plane to obtain points also for left side of panel. These mirror points are saved into properties coord_x_mirror and coord_y_mirror.

    # Load data
    data = np.loadtxt('/pathtofile/study/tutorial/fw4_profile_poloidal.dat')
    # convert coordinates to mm
    coord_x = data[:,0]*(1000)
    coord_y = data[:,1]*(1000)
    # mirror x coordinates in the yz - plane
    coord_x_mirror = coord_x[::-1]*(-1)
    coord_y_mirror = coord_y[::-1]
    
  3. Creation of B-spline curve

    Then we can generate B-spline curve with the help of method geompy.MakeInterpol(), that takes list of points as input argument. Then it makes interpolation on those points and result is B-spline curve, based on input points. In the end this new curve is added to study.

    # Create B-Spline curve on the set of points with
    # geompy.MakeInterpol() and add curve to Salome study.
    b_spline = geompy.MakeInterpol(points_x)
    b_splineID = geompy.addToStudy(b_spline,"b_spline_ft")
    b_spline_mirror = geompy.MakeInterpol(points_y)
    b_splineID_mirror = geompy.addToStudy(b_spline_mirror,\
                         "b_spline_ft_mirrored")
    

    Now we have to define line (in yellow in image) between both curves (original one and mirrored one).

    # Create line between both curves.
    ml_point1 = geompy.MakeVertex(-coord_x[0],coord_y[0],0)
    ml_point2 = geompy.MakeVertex(coord_x[0],coord_y[0],0)
    middle_line = geompy.MakeLineTwoPnt(ml_point1,ml_point2)
    middle_lineID = geompy.addToStudy(middle_line,"middle_line")
    
    ../_images/bsplinePanelExample.svg

    Fig. 2.33 Final model.

    Now we have to merge all three components into one compound, that will be extruded into space. We do that with method geompy.MakeWire(), that takes list of lines and curves as input argument.

    # Create wire out of all three components
    wire = geompy.MakeWire([b_spline_mirror,middle_line,b_spline])
    wireID = geompy.addToStudy(wire,"wire")
    
  4. Creation of extrusion curve

    Now we can create extrusion curve, that is defined by 6 points. Those points are stored in text file dataExtrusion.txt. Again we load these points in python.

    # Load data for extrusion
    dataExtrusion = \
    np.loadtxt('/path/to/study/tutorial/fw4_profile_toroidal.dat')
    # Read data for extrusion
    coord_xe = dataExtrusion[:,0]
    coord_ye = dataExtrusion[:,1]
    coord_ze = dataExtrusion[:,2]
    

    With function geompy.MakeVertex() we can make geompy points. Then we can generate polyline.

    # Convert data to geompy point and store it in a list.
    points_e = []
    for point_e in range(len(coord_xe)):
        point_e = geompy.MakeVertex(coord_xe[point_e],\
                    coord_ye[point_e],coord_ze[point_e])
        points_e.append(point_e)
    # Create polyline for extrusion and add it to study.
    polylineVert = geompy.MakePolyline(points_e)
    polylineVertID = geompy.addToStudy(polylineVert,"b_spline_ft_vert")
    
    ../_images/polylinePanelExample.svg

    Fig. 2.34 Final model.

  5. Extrusion of a curve

    The final step is to extrude a wire to create a shell.

    # Create panel shape
    panel_FW = geompy.MakePipe(polylineVert, wire)
    geompy.addToStudy(panel_FW,'Panel')
    salome.sg.updateObjBrowser()