Personal tools
You are here: Home Forum How to use Salome? Get the number of sub-objects of a SObject in a script

Get the number of sub-objects of a SObject in a script

Up to How to use Salome?

Get the number of sub-objects of a SObject in a script

Posted by William Tougeron at February 01. 2013

Hi all,

I would like to get all the sub-SObjets being present into a SObject (or a SComponent).

I know it is possible to get a sub-SObject thanks to the function sobject1.FindSubObject(i), but it requires to know the index i of the sub-object.

Examples of output of the FindSubObject() function can be:

(True, <SALOMEDS._objref_SObject instance at 0x7f1b246179a0>;)

(False, None)

The output is a tuple containing True if the sub-SObject exists, False else, and the sub-SObject itself.

Knowing this, I tried to make a while loop with a i iteration, the condition of which being that the first element of the tuple has to be True.

But the problem is that it seems that sometimes, this loop stops before the end of the sub-object list... It seems that there can be "holes" in the sub-object list, like this:

i sobject1.FindSubObject(i)[0]
1 True
2 True
3 True
4 False <-- my loop breaks here
5 True
6 True
7 False
8 False
9 False
... ...
... ...

Here the sub-objects 5 and 6 are ignored by my loop because there is no sub-object for i=4...

Does someone know how to overcome this problem? How to get all the sub-SObjects of a SObject? Or how I can fix the above method to make it work properly?

Thank you very much,

William

Re: Get the number of sub-objects of a SObject in a script

Posted by Saint Michael at February 01. 2013

Hi William

Here is the code to get all sub-SObjects:

iter = salome.myStudy.NewChildIterator( SObj )
while ( iter.More() ):
     so = iter.Value()
     iter.Next()

The iterator returned by NewChildIterator() iterates by default over SObjects of one hierarchical level. To make it iterate through all levels, it's possible to call iter.InitEx( True )

St.Michael

Re: Get the number of sub-objects of a SObject in a script

Posted by Vadim SANDLER at February 01. 2013

Hello William,

Actually, previous example is not quite right.

In SALOME, when SObject is removed from the study (e.g. with RemoveObject() or RemoveObjectWithChildren() method of StudyBuilder), it is not physically removed from the tree - only all associated attributes are cleared. Moreover, StudyBuilder allows creating SObject passing the tag (position in the parent) manually. The ChildIterator will return all SObjects that were ever created in a data tree. To check that the object is "alive", you can use the following approach - check if there are any attributes assigned to it or no. See below example, that demonstrates this technique:

import salome
s = salome.myStudy
b = s.NewBuilder()

c = b.NewComponent("C")
c.SetAttrString("AttributeName", "C")
for i in range(10):
    o = b.NewObject(c)
    o.SetAttrString("AttributeName", "O %d" % (i+1))

o = b.NewObjectToTag(c, 15)

it = s.NewChildIterator(c)
while it.More():
    o = it.Value()
    if o.GetAllAttributes():
        print "Found object:", o.GetID()
    it.Next()

o = s.FindObjectID("0:1:1:5")
b.RemoveObjectWithChildren(o)

print "---"

it = s.NewChildIterator(c)
while it.More():
    o = it.Value()
    if o.GetAllAttributes():
        print "Found object:", o.GetID()
    it.Next()

Regards,
Vadim.

Re: Get the number of sub-objects of a SObject in a script

Posted by William Tougeron at February 01. 2013

Thank you, thank you, thank you! :)

So my final code is the one of Saint Michael improved thanks to Vadim advices:

iter = salome.myStudy.NewChildIterator( SObj )

iter.InitEx( True ) # Yes, I want it to be recursive :)

while ( iter.More() ):

  so = iter.Value()

  if so.GetAllAttributes():

    # Do something :)

  iter.Next()

Thank you very much! :) It is always a pleasure to learn about scripting with Salome :)

Have a nice day, all :)

Best regards

William

Powered by Ploneboard
Document Actions