question

Sebastien avatar image
0 Likes"
Sebastien asked Jason Lightfoot commented

How to append a string to the names of every objects ?

Hi !

I am trying to rename every object of my model. Yet I do not want to go through each object to change their name as I just want to appen "L1" to them. So I thought about doing it via Script but I don't know how to go through each object individually.

I imagined going through the model treenode but all objects are not on the same level.

Maybe there is a simple way to do it ?

FlexSim 20.2.3
flexsim 20.2.3script
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Jeanette F avatar image
0 Likes"
Jeanette F answered Jason Lightfoot commented

Hello @Sebastian,

One way you could do this is to select all of the objects that are the same and do them at once. For instance I could select all of the processors and check that I have done so in the selected Objects tab in the Properties window.

1629472074643.png

Then I can select one of the processors and right next to the name select the icon with an "ab" on it.

If you want the 1 in the L1 to increment with each object have it set like so.

1629472160827.png

If you want L1 on every processor the set it up as follows.

1629472237016.png


1629472074643.png (19.3 KiB)
1629472160827.png (9.6 KiB)
1629472237016.png (8.7 KiB)
· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ralf Gruber avatar image Ralf Gruber ♦ commented ·
@Sebastien There is a command "forobjecttreeunder()" that loops through the whole tree beginning with the container object. Look it up in the Command Help. That will allow you to rename all objects without creating groups, but you will have to be careful to exclude, e.g. by class type, what you don´t want renamed.

Good Luck

1 Like 1 ·
Jason Lightfoot avatar image Jason Lightfoot ♦ Ralf Gruber ♦ commented ·

...and if it's a blanket rename excluding the objects in "Tools" you can use this:

treenode rootnode=model().first;
while(objectexists(next(rootnode))) {
    rootnode=next(rootnode);
    rootnode.name="L1_"+rootnode.name;
    forobjecttreeunder(rootnode){
        a.name="L1_"+a.name;
    }
}
0 Likes 0 ·
Sebastien avatar image Sebastien commented ·
Thank you @Jeanette F for your response.

Unfortunately when I do it, it renames all the object with the same name, in your case Processor-L1.

Is it the normal behavior ?

In my case I would like, from 2 objects called "Processor1" and "DecisionPoint1" to become "L1_Processor1" and "L1_DecisionPoint1". Would that be achievable with the approach you described ? Or if not, with another approach ?


0 Likes 0 ·
Eric M avatar image Eric M Sebastien commented ·

Another option is to add the string "L1_" to the name of the object which works like this:

Object obj = Model.find("Processor1");
string orig_name = obj.name;
obj.name = "L1_" + orig_name;
//this will return L1_Processor1

To change multiple objects at once, just add them to a group and run a for loop that would look like this:

Group group = Group("Group1");
for (int i = 1; i <= group.length; i++){
     string orig_name = group[i].name;
     group[i].name = "L1_" + orig_name;
}

Keep in mind every time you run a loop like this it will add an extra "L1_" to the beginning, so you'd only want to do it once.

1 Like 1 ·
Sebastien avatar image Sebastien Eric M commented ·
Right !

I did not think about creating a group for the for loop.

Thank you very much ! :)

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.