Personal tools
You are here: Home Forum How to use Salome? Use python script to export a mesh group

Use python script to export a mesh group

Up to How to use Salome?

Use python script to export a mesh group

Posted by Benoit Hutié at August 10. 2017

Hi everyone,

 

im trying to use python console to export a mesh group in DAT. I found some lines in documentation here to do so in MED :

           http://docs.salome-platform.org/7/gui/SMESH/tui_creating_meshes_page.html#tui_export_mesh

          

but it is then impossible to find the exported file on disk (as it is a tempfile ?).

First question : is it possible to save the tempfile on disk ?

Second question : is there another way to export the mesh or a mesh group ?

Thanks in advance for your replies :)

Re: Use python script to export a mesh group

Posted by Saint Michael at August 10. 2017

Hi

The script actually writes a group into a temporary file and removes it at the end:

medFile = tempfile.NamedTemporaryFile(suffix=".med").name
...
os.remove( medFile )

You are free to modify the script to write into a file you need:

medFile = "/my_favorite_path/group.med"
...
# os.remove( medFile )

St.Michael

Re: Use python script to export a mesh group

Posted by Benoit Hutié at August 10. 2017

Hi Saint Michael thx for the reply :)

i succeded to get the file i wanted copying the tempfile in the location i want with a new name. But i faced some issues when i tried to implement the method for a study already created thx to GUI environment and not through a script.

In fact im using these lines :

    import tempfile
    import os
    chemin = tempfile.NamedTemporaryFile(suffix=".dat",delete=False).name
    Mesh_1.ExportDAT( chemin, meshPart=Beam )
    dest = '/home/salome/Documents/Validation_1D2D/AFT_Floor_Grid/Exports/Beam'
    os.rename(chemin,dest)

assuming 'Beam' is the group i want to export in DAT. But it seems like it doesnt know whats the 'Beam' group (in the example it was clear cause the group was defined through a GroupOnGeom function).

My question is : how define a mesh group to export through reading (and not creating) it in the study tree ? im trying to do so with GetObject functions but either the exported file is empty or the it raises errors

Re: Use python script to export a mesh group

Posted by Benoit Hutié at August 10. 2017

Btw i dont know your nationality but i can speak english or french as you like :)

Re: Use python script to export a mesh group

Posted by Saint Michael at August 10. 2017

I attach a script that retrieves all geom and mesh objects from a study to namesake python objects.

St.Michael

Attachments

Re: Use python script to export a mesh group

Posted by Benoit Hutié at August 10. 2017

Wow many thanks to you sir for your help :o its exactly what i needed :)

it raises an error though but its just because it misses the '.igs_1' at line 18 when you do the 'exec( "%s = go"%so.GetName() )'

I dont know why given as the previous print gives the right string 'AFT_Floor_Grid.igs_1'

       Traceback (most recent call last):
          File "<input>", line 1, in <module>
          File "/home/salome/Documents/Validation_1D2D/AFT_Floor_Grid/Exports/Export.py", line 18, in <module>
              exec( "%s = go"%so.GetName() )
          File "<string>", line 1, in <module>
      NameError: name 'AFT_Floor_Grid' is not defined

Do you see how to add the '.igs_1' ? Im currently trying to do so myself

Re: Use python script to export a mesh group

Posted by Saint Michael at August 10. 2017

There is the error because 'AFT_Floor_Grid.igs_1' is not a valid name for a python variable. Solution is to either rename AFT_Floor_Grid.igs_1 to remove '.' from it, or to modify the script to fix names that can't be used as python names.

St.Michael

Re: Use python script to export a mesh group

Posted by Benoit Hutié at August 10. 2017

Ok i see you mean its not valid cause the '.' is meant to be followed by an attribute. I changed the named as you suggested and error disappeared :)

Geometry part now works well, i did the same (rename) for mesh parts to remove same kind of error by adding '_' everywhere and it works well too :)

I got 1 or 2 errors for the export part but i managed to solve that and it works perfectly fine thanks to you !! :) you did the most important part of the job for me sir and i cant be more grateful !!

Just one last question : is there a documentation for all the python commands in Salome ?

Im attaching the full script just in case other people would do the same operation

 

Attachments

Re: Use python script to export a mesh group

Posted by Saint Michael at August 10. 2017

is there a documentation for all the python commands in Salome ?

Each module has documentation on its Python API. E.g. doc on smeshpy commands is here http://docs.salome-platform.org/latest/gui/SMESH/smeshpy_doc/namespacesmeshBuilder.html. Here the commands are grouped http://docs.salome-platform.org/latest/gui/SMESH/smeshpy_doc/modules.html

St.Michael

Re: Use python script to export a mesh group

Posted by Benoit Hutié at August 10. 2017

Thx i found a lot of interesting things in there :)

i spotted something wrong in the previous script i attached : it works perfectly fine for the whole mesh but it raises an error when i try to do the same with a mesh group as it says : 'ExportDAT() got an unexpected keyword argument 'meshPart'

i dont understand cause in the documentation, this keyword is explicitly mentionned as a way to export a sub-mesh or a group instead of the whole mesh. Is there something i missed ?

Re: Use python script to export a mesh group

Posted by Saint Michael at August 10. 2017

The matter is that Mesh_1 is not a Python wrapper defined in smeshBuilder module but an object that is stored in the study. Fix it by adding the following line after retrieving objects from the study:

Mesh_1 = smeshpy.Mesh( Mesh_1 )

St.Michael

Re: Use python script to export a mesh group

Posted by Benoit Hutié at August 11. 2017

It works perfectly fine now :)

Im attaching the final script

Huge thanks to you St.Michael i couldnt do it without your help

Attachments
Powered by Ploneboard
Document Actions