Running Salome script without graphics

馋奶兔 提交于 2019-12-05 10:03:12

I had similar wishes to you but after much searching I ended up concluding that what we both want to do is not completely possible.

In order to run a salome script on the command line without the GUI use

salome -t python script.py or simply salome -t script.py

In order to run a salome script you must call it using the salome executable. It seems that you cannot use the salome libraries (by importing them into a python script that is then called with python script.py) without the compiled program. The executables that salome uses contain much of what the platform needs to do its job.

This frustrated me for a long time, but I found a workaround; for a simple example, if you have a salome script you can call the salome executable from within another python program with os.system("salome -t python script.py")

But now you have a problem; salome does not automatically kill the session so if you run the above command a number of times your system will become clogged up with multiple instances of running salome processes. These can be killed manually by running killSalome.py, found in your salome installation folder. But beware! This will kill all instances of salome running on your computer! This will be a problem if you are running multiple model generation scripts at once or if you also have the salome GUI open.

Obviously, a better way is for your script to kill each specific instance of salome after it has been used. The following is one method (the exact paths to the executable etc will need to change depending on your installation):

# Make a subprocess call to the salome executable and store the used port in a text file:
subprocess.call('/salomedirectory/bin/runAppli -t python script.py --ns-port-log=/absolute/path/salomePort.txt', shell=True)

# Read in the port number from the text file:
port_file = open('/absolute/path/salomePort.txt','r')
killPort = int(port_file.readline())
port_file.close()

# Kill the session with the specified port:
subprocess.call('/salomedirectory/bin/salome/killSalomeWithPort.py %s' % killPort,shell=True)

EDIT: Typo correction to the python os command.

EDIT2: I recently found that problems with this method are met when the port log file (here "salomePort.txt" but can be named arbitrarily) is given with only its relative path. It seems that giving it with its full, absolute path is necessarily for this to work.

If you are working with salome on windows platform, use following

salome_folder\WORK>run_salome.bat -t script_file.py

According to Salome FAQ:

To launch SALOME without GUI, use “runSalome –t” command: only the necessary servers are launched (without GUI). To start an interactive Python console then (for example, to be able to load TUI scripts), you will need to use --pinter option

To launch Salome with only chosen modules:

To launch a group of chosen SALOME modules, use the command “runSalome –-modules=XXX,YYY”, where XXX and YYY are modules' names. You can use –h command to display help of runSalome script.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!