Personal tools
You are here: Home Forum Use Launching SALOME from Python and manipulating the geometry objects, all in the same script

Launching SALOME from Python and manipulating the geometry objects, all in the same script

Up to Use

Launching SALOME from Python and manipulating the geometry objects, all in the same script

Posted by Roman R at December 01. 2021
Hi all,
 
I'm struggling with this issue for days and really hope you guys can help with any useful advice.
 
I'm trying to write a Python script that does some things outside SALOME (collects user-specified information, parses some input files, performs some other irrelevant opearions), then starts SALOME, then, after it's ready, inputs some Python code to create geometrical objects and mesh, then saves it all into hdf file, and then closes SALOME. And I want all these stages to be issued ćonsequently, one after another, in the same script
 
So, I used some code from "How to write a launcher" from SALOME KERNEL documentation (here: https://docs.salome-platform.org/latest/tui/KERNEL/salome_command.html#write_launcher), and after some effort I'm able to make SALOME run.  
 
The  script as of now looks like this:
 
def main(args):

#### LAUNCHER PART

    print('__file__ is: ' + __file__)
    currentPath = os.path.dirname( os.path.abspath( __file__ ) )
    launcherFile = os.path.basename(__file__)
    
    print('currentPath is:  ' + currentPath)
    print('launcherFile is: ' + launcherFile)
    
    
    appliskelPath = '/home/dell/salome_meca/V2020.0.1_universal_universal/modules/KERNEL_V9_4_0/bin/salome/appliskel'      salomePath = '/home/dell/salome_meca/V2020.0.1_universal_universal/modules/KERNEL_V9_4_0/bin/salome'
    
    print('appliskelPath is:  ' + appliskelPath)
    print('salomePath is:  ' + salomePath)
    
    sys.path.append(appliskelPath)
    sys.path.append(salomePath)
    
    print('This is sys.path[]: ' + str(sys.path))
    
    from salome_starter import initialize
    initialize(currentPath, launcherFile)
    
    from salomeContextUtils import getConfigFileNames
    #I found that getConfigFileNames from salomeContextUtils returns three (not four!) values (no extraEnv returned), so I will just bypass this extraEnv
    #configFileNames, extraEnv, args, unexisting = getConfigFileNames(args, checkExistence=True)

    configFileNames, args, unexisting = getConfigFileNames(args, checkExistence=True)

    #
    # #context
    import salomeContext
    context = salomeContext.SalomeContext(configFileNames)
    #
    # #Initializing extra environment variables parsed from batch files:
    extraEnv = False #bypass extraEnv issue
    if extraEnv:
        for key,val in extraEnv.items():
             context.addToVariable(key,val)
    
    #Run SALOME
    out, err, returncode = context.runSalome(args)

#### BASIC GEOM USAGE PART
  
    import salome
    salome.init()
    from salome.geom import geomBuilder
    geompy = geomBuilder.New()
    
    
    pnt = [None]*4
    line = [None]*4
    
    pnt[0] = geompy.MakeVertex(10, 0, 0)
    pnt[1] = geompy.MakeVertex(10, 10, 0)
    pnt[2] = geompy.MakeVertex(0, 10, 0)
    pnt[3] = geompy.MakeVertex(0, 0, 0)
    
    
    line[0] = geompy.MakeEdge(pnt[0],pnt[1])
    line[1] = geompy.MakeEdge(pnt[1],pnt[2])
    line[2] = geompy.MakeEdge(pnt[2],pnt[3])
    line[3] = geompy.MakeEdge(pnt[3],pnt[0])
    
    area = geompy.MakeFaceWires(line)
    geompy.addToStudy(area, 'this is a square') 
 
The problem is that after the line:
 
out, err, returncode = context.runSalome()
 
SALOME is running, but the execution of the code after that line is not happening at all. The geom construction part ic executed only after I quit SALOME. I tried using context.runSalome(["shell"]) or even context.runSalome(["connect"]) to connect to pre-launched SALOME instance, but still it then just opens a new shell or Python console for new command input, which is not what I want, as I want further lines of my script to be executed right after launching SALOME.
 
Is there a way to achieve it? or may be a workaround? Any advisc is highly appreciated!
 
P.S: I am on Ubuntu 20.04 (not a virtual machine) and using Salome Meca 2020 which is based on SALOME 9.4.0
 
Many thanks!
-Roman
Powered by Ploneboard
Document Actions