Examples



      Interfaces:

      SALOMEDS::Study
      SALOMEDS::StudyBuilder
      SALOMEDS::StudyManager
      SALOMEDS::SObject
      SALOMEDS::SComponent
      SALOMEDS::SComponentIterator
      SALOMEDS::ChildIterator
      SALOMEDS::AttributeComment
     



      SALOMEDS::Study interface



SComponent FindComponent( in string aComponentName )

Find GEOMETRY component in the opened study by its name:


    str= os.getenv("TmpDir")
    if str == None:
        str = "/tmp"
    file = str+"/test.hdf"

    openedStudy=batchmode_geompy.myStudyManager.Open(file)

    father = openedStudy.FindComponent("GEOM")
    if father is None:
         raise  RuntimeError, "Geom component is not found!  Wrong study is opened."


SObject FindObject ( in string anObjectName )

Find the SObject of the box by its NameAttribute "box":

box = openedStudy.FindObject("box")
if box is None :
    raise  RuntimeError, "box was not found! Wrong study is opened."


SObject FindObjectID ( in ID aObjectID )
#result: "/User data/Case1".
Find the SObject of the box by its ID "0:1:1:2":

box =openedStudy.FindObjectID("0:1:1:2")
if box is None :
    raise  RuntimeError, "box was not found! Wrong ID is used."


SObject FindObjectIOR ( in ID  aObjectIOR )

Find the SObject of the result on imported MED file by it's IOR:

theResult = myVisu.ImportFile(medFile)
aSObj = myStudy.FindObjectIOR(theResult.GetID())


SObject FindObjectByPath ( in string thePath )

Find SObject by path to it:

# create new auxiliary componen
t
aComponent = myStudyBuilder.NewComponent("Virtual Component")

# create auxiliary subtree
aPath = "/Virtual Component/Case1"
myStudyBuilder.AddDirectory(aPath)

aSObj = myStudy.FindObjectByPath(aPath)


void SetContext ( in string thePath) / string GetContext ()

Set context of the study to the created case and get it for printing:

aComponent = myStudyBuilder.NewComponent("User data")
anAttr = aBuilder.FindOrCreateAttribute(aComponent, "AttributeName")
anAttrName = anAttr._narrow(SALOMEDS.AttributeName)
anAttrName.SetValue("User data")

#Add a new case 'Case1' to the component 'User data'
aBuilder.AddDirectory("/User data/Case1")

#Set a study context to '/User data/Case1'
aStudy.SetContext("/User data/Case1")

#Print the current study context
print aStudy.GetContext()

#result: "/User data/Case1".



ChildIterator NewChildIterator ( in SObject aSO )

Import med file and print all mesh names that this file includes (mesh is a child of the result of imported file):

# define file name
aFileName = datadir + "fra.med"

# import file in visu module and get result
theVisu = batchmode_visu.myVisu
aResult = theVisu.ImportFile(aFileName)
if aResult is None : raise RuntimeError, "Error"
else : print "OK"
 
# get current study and its' SObject       
myLocalStudy = theVisu.GetCurrentStudy()
aSObj = myLocalStudy.FindObjectIOR(aResult.GetID())
if aSObj is None : raise RuntimeError, "Error"
else : print "OK"

# create iterator by SObject of the current study
aMeshIter = myLocalStudy.NewChildIterator(aSObj);

# iterating in the current study (with the help of created iterator) to find all mesh names  
while aMeshIter.More() :
        aMeshSObj = aMeshIter.Value()
        aMeshIter.Next()
        anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
        if anAttr is None :
            aMeshSObj = aMeshIter.Value()
            aMeshIter.Next()
            anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
        anAttr = anAttr._narrow(SALOMEDS.AttributeName);
        aMeshName = anAttr.Value()
        print "  ", aMeshName


SComponentIterator NewComponentIterator ()

Find the number an names of all components in the study:

aCompItr = myStudy.NewComponentIterator()

compNb = 0
while aCompItr.More():
    aComp = aCompItr.Value()
    aName = aComp.ComponentDataType()
    print "Component name = ", aName
    compNb += 1
    aCompItr.Next()


StudyBuilder NewBuilder ()

Create a new StudyBuilder (uses to add or modify an object in the study ):

myBuilder = myStudy.NewBuilder()


AttributeStudyProperties GetProperties ()

Get the attribute, which contains the properties of the study, and change properties of the study by changing it:

aProperties = myStudy.GetProperties()
if aProperties == None :
    raise  RuntimeError, "Can't create AttributeStudyProperties attribute"
aProperties = aProperties._narrow(SALOMEDS.AttributeStudyProperties)

A = aProperties

# print stydy properties
print "A.GetUserName()= ", A.GetUserName()
res,mm,hh,dd,mnth,yy=A.GetCreationDate()
print "A.GetCreationDate() = ", mm,hh,dd,mnth,yy
print "A.GetCreationMode() = ", A.GetCreationMode()
print "A.IsModified() = ", A.IsModified()
print "A.IsLocked() = ", A.IsLocked()

# change the properties of the study
if A.IsLocked() == 0 :
    A.SetUserName("tester"); print 'A.SetUserName("tester"), A.GetUserName() = ', A.GetUserName()
    A.SetCreationDate(11,11,11,11,2002); print 'A.SetCreationDate(11,11,11,11,2002), A.GetCreationDate() =', A.GetCreationDate()
    print "A.IsModified() = ", A.IsModified()
A.SetLocked(1)


boolean IsModified ()

Find if study is modified:

IsModified = myStudy.IsModified()

if IsModified == 1:
    print "The study is modified and not saved"


boolean IsEmpty ()

Find if study is empty:

IsEmpty = myStudy.IsEmpty()

if IsEmpty == 1:
    print "The study is empty"




SALOMEDS::StudyBuilder interface



SComponent NewComponent ( in string ComponentDataType )

Create Geometry SComponent:

myBuilder = myStudy.NewBuilder()
father = myBuilder.NewComponent("GEOM")


void DefineComponentInstance ( in SComponent aComponent, in Object ComponentIOR )


Define the instance to the created geometry component:

# find geom component
myLCC = batchmode_salome.lcc
geom = myLCC.FindOrLoadComponent("FactoryServer", "Geometry")
geom = geom._narrow(GEOM.GEOM_Gen)
geom.GetCurrentStudy(myStudyId)

myBuilder = myStudy.NewBuilder()

father = myBuilder.NewComponent("GEOM")
myBuilder.DefineComponentInstance(father,geom)


SObject NewObject ( in SObject theFatherObject )

Create box and add it to study:

from batchmode_geompy import *

# create a box
box = geom.MakeBox(0,0,0,100,100,150)

ior = orb.object_to_string(box)
box._set_Name(ior)     

# create Geometry SComponent
father = myBuilder.NewComponent("GEOM")
A1 = myBuilder.FindOrCreateAttribute(father, "AttributeName");
FName = A1._narrow(SALOMEDS.AttributeName)
FName.SetValue("Geometry")
myBuilder.DefineComponentInstance(father,geom)

# add box to Study
myBuilder.NewCommand()
newObj = myBuilder.NewObject(father)
A1 = myBuilder.FindOrCreateAttribute(newObj, "AttributeIOR");
ObjIOR = A1._narrow(SALOMEDS.AttributeIOR)
ObjIOR.SetValue(ior)
A2 = myBuilder.FindOrCreateAttribute(newObj, "AttributeName");
ObjName = A2._narrow(SALOMEDS.AttributeName)
ObjName.SetValue("Common_operation")
id = newObj.GetID()
box._set_StudyShapeId(id)
myBuilder.CommitCommand()


void RemoveObject ( in SObject anObject )

# Remove CutPlanes SObject from the StudyBuilder (delete cutplanes):

SObj=myStudy.FindObjectIOR(cutplanes.GetID())
myBuilder = newStudy.NewBuilder()
myBuilder.RemoveObject(SObj)


void LoadWith ( in SComponent sco, in Driver Engine ) raises (SALOME::SALOME_Exception)


# Load Visu component:

myBuilder = openedStudy.NewBuilder()
SCom=openedStudy.FindComponent("VISU")
myBuilder.LoadWith(SCom ,myVisu)


GenericAttribute FindOrCreateAttribute ( in SObject anObject,  in string aTypeOfAttribute)

Create AttributeName attribute for created component an set value to it:

myBuilder = myStudy.NewBuilder()
aComponent = myBuilder.NewComponent("User data")

anAttr = myBuilder.FindOrCreateAttribute(aComponent, "AttributeName")

anAttrName = anAttr._narrow(SALOMEDS.AttributeName)
anAttrName.SetValue("User data")


boolean FindAttribute ( in SObject anObject, out GenericAttribute anAttribute, in string aTypeOfAttribute )

Find AttributeName attribute of the field and print the field name:

aFieldSObj  = myStudy.FindObject("Head, -")

myStudyBuilder.FindAttribute( aFieldSObj, anAttr, "AttributeName")
if res == 0:
    raise  RuntimeError, "Error:  Attribute not found"

anAttr = anAttr._narrow(SALOMEDS.AttributeName);
aFieldName = anAttr.Value()
print "      ", aFieldName


void RemoveAttribute ( in SObject anObject, in string aTypeOfAttribute )

Remove AttributeSelectable attribute of the field SObject:

aFieldSObj  = myStudy.FindObject("Head, -")

myStudyBuilder.RemoveAttribute( aFieldSObj, "AttributeSelectable")


void Addreference ( in  SObject anObject, in  SObject theReferencedObject )


Create a reference between created SObject and the existing field:

aFieldSObj  = myStudy.FindObject("Head, -")
aNewSObj = myBuilder.NewObject(myVisu)

myBuilder.Addreference(aFieldSObj, aNewSObj)


void NewCommand ()

Create new command wich containes actions for changing the properties of the study:

A = myStudy.GetProperties()
A = aProperties._narrow(SALOMEDS.AttributeStudyProperties)

myBuilder = myStudy.NewBuilder()

myBuilder.NewCommand() # creates a new command

# change the properties of the study
if A.IsLocked() == 0 :
    A.SetUserName("tester"); print 'A.SetUserName("tester"), A.GetUserName() = ', A.GetUserName()
    A.SetCreationDate(11,11,11,11,2002); print 'A.SetCreationDate(11,11,11,11,2002), A.GetCreationDate() =', A.GetCreationDate()
    print "A.IsModified() = ", A.IsModified()
A.SetLocked(1)

myBuilder.CommitCommand() # commits all actions declared within the created command


void CommitCommand()

See the end of the previous example


void AbortCommand ()


Create new command wich containes actions for changing the properties of the study, cancel all declared actions:

A = myStudy.GetProperties()
A = aProperties._narrow(SALOMEDS.AttributeStudyProperties)

myBuilder = myStudy.NewBuilder()

myBuilder.NewCommand() # creates a new command

# change the properties of the study
if A.IsLocked() == 0 :
    A.SetUserName("tester"); print 'A.SetUserName("tester"), A.GetUserName() = ', A.GetUserName()
    A.SetCreationDate(11,11,11,11,2002); print 'A.SetCreationDate(11,11,11,11,2002), A.GetCreationDate() =', A.GetCreationDate()
    print "A.IsModified() = ", A.IsModified()
A.SetLocked(1)

myBuilder.AbortCommand() # abort all actions declared within the created command


void Undo () raises (LockProtection) ,
void Redo () raises (LockProtection)

Create new command wich containes actions for changing the properties of the study,
cancel all declared actions and then redo it with the help of undo/redo mechanism:


A = myStudy.GetProperties()
A = aProperties._narrow(SALOMEDS.AttributeStudyProperties)

myBuilder = myStudy.NewBuilder()

myBuilder.NewCommand() # creates a new command

# change the properties of the study
if A.IsLocked() == 0 :
    A.SetUserName("tester"); print 'A.SetUserName("tester"), A.GetUserName() = ', A.GetUserName()
    A.SetCreationDate(11,11,11,11,2002); print 'A.SetCreationDate(11,11,11,11,2002), A.GetCreationDate() =', A.GetCreationDate()
    print "A.IsModified() = ", A.IsModified()
A.SetLocked(1)

myBuilder.CommitCommand() # commits all actions declared within the created command

myBuilder.Undo() # cancels all actions of the command

myBuilder.Redo() # redoes all actions of the command



SALOMEDS::StudyManager interface



Study NewStudy ( in string study_name )

Create the study with the name "Test_Study":


myNewStudy = myStudyManager.NewStudy("Test_Study")


Study Open ( in URL aStudyUrl ) raises (SALOME::SALOME_Exception)

Open the study saved in the HDF file:

file = 'saved_study.hdf'

openedStudy=myStudyManager.Open(file)

if openedStudy == None:
    raise  RuntimeError, "Can't open saved study!"


void Save (in Study aStudy, in boolean theMultifile )

Open study, import med file into it and save with the old path and filename:

file = "saved_study.hdf"
myMedFile ="medfile.med"

openedStudy=myStudyManager.Open(file)

myVisu.SetCurrentStudy(openedStudy)
myResult = myVisu.ImportFile(myMedFile)

myStudyManager.Save(openedStudy, 0)


void SaveAs ( in URL aUrl, in  Study aStudy,  
in boolean theMultifile )

Open study from the file and resave it in several files (using Multifile option while saving)

file = "saved_study.hdf"
newfile = "resaved_study.hdf"

openedStudy=myStudyManager.Open(file)
myStudyManager.SaveAs(newfile, openedStudy, 1)


void Close ( in  Study aStudy )


Close just opened study:

file = "saved_study.hdf"

openedStudy=myStudyManager.Open(file)
myStudyManager.Close(openedStudy)



SALOMEDS::SObject interface



ID GetID ()

Create new SObject and get its ID:

mySObj = myBuilder.NewObject(myFather)

myID =  mySObj.GetID()


SComponent GetFatherComponent ()

Get father component of the SObject:

myFather = mySObj.GetFatherComponent();


boolean FindAttribute ( out GenericAttribute anAttribute, in string aTypeOfAttribute )

Find the AttributeName attribute of the field:

aFieldSObj  = myStudy.FindObject("Head, -")

res = aFieldSObj.FindAttribute( anAttr, "AttributeName")
if res == 0:
    raise  RuntimeError, "Error:  Attribute not found"


ListOfAttributes GetAllAttributes ()

Get list of all attributes of the SObject, find the number of attributes:

attrs = mySObj.GetAllAttributes()
aLen = len(attrs) # number of attributes




SALOMEDS::SComponent interface



string ComponentDataType ()

Print names of all components wich the study contains:

aCompItr = myStudy.NewComponentIterator()

while aCompItr.More():
    aComp = aCompItr.Value()
    aName = aComp.ComponentDataType()
    print "Component name = ", aName
    aCompItr.Next()


Other methods are inherited.




SALOMEDS::SComponentIterator interface



boolean More (), void Next (), SComponent Value ()

See another example

  

SALOMEDS::ChildIterator interface



boolean More () , void Next (), SObject Value ()

Print all mesh names of imported MED file with the help of ChildIterator:

aResult = myVisu.ImportFile("MedFile.med")
       
myStudy = theVisu.GetCurrentStudy()
aSObj = myLocalStudy.FindObjectIOR(aResult.GetID())

aMeshIter = myLocalStudy.NewChildIterator(aSObj);  # creating new child iterator

while aMeshIter.More() :                                                # check if one more child level exists.
        aMeshSObj = aMeshIter.Value()                            # returns the SObject corresponding to the current object found by the iterator.  
        aMeshIter.Next()                                                    # passes the iterator to the next level.
        anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
        if anAttr is None :
                aMeshSObj = aMeshIter.Value()
                aMeshIter.Next()
                anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
        anAttr = anAttr._narrow(SALOMEDS.AttributeName);
        aMeshName = anAttr.Value()
        print "  ", aMeshName



SALOMEDS::AttributeComment interface


string Value (),  void SetValue ( in string value )

Find the AttributeComment attribute of the "Head" field in the study, print it, then change it to "My Comment" string:

aFieldSObj  = myStudy.FindObject("Head, -")
anAttr = aFieldSObj.FindAttribute("AttributeComment")[1]
                    anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
                    aFieldComment = anAttr.Value()

print "AttributeComment", anAttr

anAttr.SetValue ("My Comment")