Personal tools
You are here: Home Forum Use Open HDF file from python file???

Open HDF file from python file???

Up to Use

Open HDF file from python file???

Posted by Fabio Furlan at November 19. 2012

Hi everybody,

for an Optimization loop I need to open hdf file from python file. What is the command please? I just tried the command

study = salome.myStudyManager.OpenStudy("/home/user/MyStudy.hdf") 

but it doesn't work.

Thank you all for the time and consideration.

FF

Re: Open HDF file from python file???

Posted by SMESH expert at November 19. 2012

Hi Fabio

I usually use such code to open a study and then use SMESH commands on it

salome.myStudy     = salome.myStudyManager.Open( file_name )
salome.myStudyId   = salome.myStudy._get_StudyId()
salome.myStudyName = salome.myStudy._get_Name()
meshCompSO         = salome.myStudy.FindComponent("SMESH")
b                  = salome.myStudy.NewBuilder()
b.LoadWith( meshCompSO, smesh.smesh )

Regards
Edward

Re: Open HDF file from python file???

Posted by William Tougeron at November 19. 2012

Hi Fabio,

I don't know on Windows, but on Linux, I simply type:

salome Study1.hdf

in a console and Salome loads automatically the HDF file after opening.

So, maybe you could just use a "os.system" command to do it from your Python script:

import os

fileName = "Study1.hdf"

os.system( "salome " + fileName )

I hope it's what you were looking for.

Best regards,

William

PS: My "salome" command is an alias of:

source /opt/salome/salome_6.5.0/KERNEL_6.5.0/salome.sh

/opt/salome/salome_6.5.0/KERNEL_6.5.0/bin/salome/runSalome

I installed my Salome version into my /opt/salome/ folder, but on your computer (or server), it can be different, of course :)

Re: Open HDF file from python file???

Posted by Fabio Furlan at November 21. 2012

Thanks for the replies. It finally works. Another question: I create a compound mesh (call compound_mesh) and I have to extrude it (from python file) and export it (in UNV format, from python file too). How can I do it?

Thank you

 

Re: Open HDF file from python file???

Posted by Saint Michael at November 21. 2012

    ## Generates new elements by extrusion of the elements which belong to the object
    #  @param theObject the object which elements should be processed.
    #                   It can be a mesh, a sub mesh or a group.
    #  @param StepVector vector, defining the direction and value of extrusion for one step (the total extrusion length will be NbOfSteps * ||StepVector||)
    #  @param NbOfSteps the number of steps
    #  @param MakeGroups forces the generation of new groups from existing ones
    #  @param  IsNodes is True if elements which belong to the object are nodes
    #  @return list of created groups (SMESH_GroupBase) if MakeGroups=True, empty list otherwise
    #  @ingroup l2_modif_extrurev
    def ExtrusionSweepObject(self, theObject, StepVector, NbOfSteps, MakeGroups=False, IsNodes=False):

 

 

    ## Exports the mesh in a file in UNV format
    #  @param f the file name
    #  @param meshPart a part of mesh (group, sub-mesh) to export instead of the mesh
    #  @ingroup l2_impexp
    def ExportUNV(self, f, meshPart=None):

 

 

St.Michael

 

Re: Open HDF file from python file???

Posted by William Tougeron at January 24. 2014

Previously SMESH expert wrote:

Hi Fabio

I usually use such code to open a study and then use SMESH commands on it

salome.myStudy     = salome.myStudyManager.Open( file_name )
salome.myStudyId   = salome.myStudy._get_StudyId()
salome.myStudyName = salome.myStudy._get_Name()
meshCompSO         = salome.myStudy.FindComponent("SMESH")
b                  = salome.myStudy.NewBuilder()
b.LoadWith( meshCompSO, smesh.smesh )

Regards
Edward

Hi all

Does anyone know how to do the same with the Geometry module ?...

I tried:

salome.myStudy     = salome.myStudyManager.Open( file_name )
salome.myStudyId   = salome.myStudy._get_StudyId()
salome.myStudyName = salome.myStudy._get_Name()
geomCompSO         = salome.myStudy.FindComponent("GEOM")
b                  = salome.myStudy.NewBuilder()
b.LoadWith( geomCompSO, geompy.geompy )

But I have the error:

AttributeError: 'module' object has no attribute 'geompy'

Best regards,

William

Re: Open HDF file from python file???

Posted by SMESH expert at January 24. 2014

Hello William

b.LoadWith( geomCompSO, geompy.geompy )

After recent changes, this line should be changed.

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

b.LoadWith( geomCompSO, geompy)

Best regards

Edward

Re: Open HDF file from python file???

Posted by William Tougeron at January 24. 2014

Hello,

Thank you for response.

I don't know if I am doing something wrong, but I am not able to open an existing HDF file from a script and to add some geometry in the Geometry module without deleting all previous work in this module...

I enclose an archive in which there is a HDF file (Study_1.hdf) in which there is a cube and a python script (addVertex.py) in which I put your code and which tries to add a vertex in the HDF file.

I type:

runSalome -t addVertex.py

The script save the study under a new name "Study_1.hdf.hdf".

When I open it, the cube is no more here and only the vertex is visible...

The script:

#!/apl/salome/salome-7.3.0/KERNEL_7.3.0/bin/salome/runSalome -t

hdfFile='Study_1.hdf'

import os

import sys

from killSalomeWithPort import killMyPort

import salome

from salome.geom import geomBuilder

geompy=geomBuilder.New(salome.myStudy)

# Open the study

study = salome.myStudyManager.Open(sys.path[0]+'/'+hdfFile)

salome.myStudyId=salome.myStudy._get_StudyId()

salome.myStudyName=salome.myStudy._get_Name()

geomCompSO=salome.myStudy.FindComponent("GEOM")

b=salome.myStudy.NewBuilder()

b.LoadWith(geomCompSO,geompy)

# Add a vertex

vertex=geompy.MakeVertex(0,0,0)

geompy.addToStudy(vertex,'v')

# Save the study

salome.myStudyManager.SaveAs(sys.path[0]+'/'+hdfFile+'.hdf',salome.myStudy,False)

# Close the study

salome.myStudyManager.Close(salome.myStudy)

# Close the Salome session

killMyPort(os.getenv('NSPORT'))

Any help would be very appreciated...

Best regards,

William

Attachments

Re: Open HDF file from python file???

Posted by SMESH expert at January 24. 2014

Dear William

I think it's necessary to initialize geompy with a just opened study:

# Add a vertex

geompy.init_geom( study )

vertex=geompy.MakeVertex(0,0,0)

 

Best regards

Edward

Re: Open HDF file from python file???

Posted by William Tougeron at January 24. 2014

Previously SMESH expert wrote:

Dear William

I think it's necessary to initialize geompy with a just opened study:

# Add a vertex

geompy.init_geom( study )

vertex=geompy.MakeVertex(0,0,0)

 

Best regards

Edward

Hi Edward,

This time neither the cube nor the vertex is saved...

:(

William

Re: Open HDF file from python file???

Posted by William Tougeron at January 24. 2014

Here is the script with your suggestion.

Attachments

Re: Open HDF file from python file???

Posted by William Tougeron at January 24. 2014

OOoops, I enclosed a wrong file :)

Attachments

Re: Open HDF file from python file???

Posted by Saint Michael at January 27. 2014

Hello William

I attach a working script, where I replaced all "study" with "salome.myStudy".

St.Michael

Attachments

Re: Open HDF file from python file???

Posted by William Tougeron at January 27. 2014

Saint Michael !

I don't know how to thank you :)

Thank you very very much! :)

Best regards,

William

Powered by Ploneboard
Document Actions