Plugins
Available plugins
The following plugins are accessible via Mesh > SMESH plugins menu:
MeshCut plugin - allows to cut a mesh constituted of linear tetrahedrons by a plane.
Get min or max value of control - a sample plugin whose sources are located in ${GUI_ROOT_DIR}/share/salome/plugins/gui/demo directory (files minmax_plugin.py, minmax_ui.py and smesh_plugins.py).
You can find a detailed description of how to create your own plugin in documentation: Help > User’s Guide > GUI module > How-To’s and Best Practices > Extend SALOME gui functions using python plugins.
Dump Python plugins instructions in SALOME script
It is possible to dump text in the SALOME Python dump from the SMESH Python plugins using an instance of smeshBuilder.smeshBuilder:
from salome.smesh import smeshBuilder
smesh = smeshBuilder.New()
smesh.AddToPythonScript("# A comment added in the dumped script from SALOME")
smesh.AddToPythonScript("Some instructions ...")
Some of the methods of the smeshBuilder.smeshBuilder class automatically add content to the python script when dumping from SALOME. For example, if you use smesh.CreateMeshesFromMED() method in your Python plugin, many intermediate instructions will be added to your dumped script. To avoid this behaviour and dump only the instructions you want to be dumped, call smesh.PausePythonDumpRecording() and smesh.ResumePythonDumpRecording() around the methods that produce the unwanted instructions.
In the following example, only the instructions specified in smesh.AddToPythonScript() will be recorded :
medFilePath = "somePathToMedFile"
from salome.smesh import smeshBuilder
smesh = smeshBuilder.New()
try:
# Block the dumping of instructions in python script until call to smesh.ResumePythonDumpRecording()
smesh.PausePythonDumpRecording()
# The following instructions will not dump anything to the python script
(outputMesh, status) = smesh.CreateMeshesFromMED(medFilePath)
finally :
# Resume the dumping of python instructions
smesh.ResumePythonDumpRecording()
# This will be added to the Python dump, whether it is between Pause/Resume methods or not
smesh.AddToPythonScript("# A comment added in the dumped script from SALOME")
smesh.AddToPythonScript("Some instructions ...")
To be able to add the name that is automatically generated by the study, you can use the salome.ObjectToSObject() method to get the SObject associated with the python object, and especially its ID. Here is an example :
import salome
from salome.smesh import smeshBuilder
smesh = smeshBuilder.New()
try:
# Block the dumping of instructions in python script until call to smesh.ResumePythonDumpRecording()
smesh.PausePythonDumpRecording()
# The following instructions will not dump to the python script
medFilePath = "somePathToMedFile"
(inputMesh, status) = smesh.CreateMeshesFromMED(medFilePath)
inputMesh = inputMesh[0]
inputName = "InputMesh"
smesh.SetName(inputMesh.GetMesh(), inputName)
finally :
# Resume the dumping of python instructions
smesh.ResumePythonDumpRecording()
outputMesh = myPluginMethod(inputMesh) # creates some mesh based on the input mesh
outputName = "OutputMesh"
smesh.SetName(outputMesh.GetMesh(), outputName)
# Get the study ID of the input and output objects
input_id = salome.ObjectToSObject(inputMesh.GetMesh()).GetID()
output_id = salome.ObjectToSObject(outputMesh.GetMesh()).GetID()
# An alternative to get the IDs when the object is not available (through loading an HDF study for example)
# maStudy = salome.myStudy
# smesh.UpdateStudy()
# input_id = maStudy.FindObjectByName(inputName,"SMESH")[0].GetID()
# output_id = maStudy.FindObjectByName(outputName,"SMESH")[0].GetID()
# This will be added to the Python dump
smesh.AddToPythonScript(f"import myPlugin")
smesh.AddToPythonScript(f"{output_id} = myPlugin.myPluginMethod({input_id})") # ids will be replaced by their generated name when dumping the SALOME study (Mesh_1, Mesh_X ...)
In this last example, myPluginMethod() can be some plugin core function that would be accessible through the myPlugin package (plugin API). This way, when loading a Python script that uses the plugin in SALOME, the right function call will be made.
Warning
When dumping a study object ID using
smesh.AddToPythonScript()method, be sure that there is at least a trailing space or a character after the ID.