question

Patrick Cloutier avatar image
0 Likes"
Patrick Cloutier asked Matthew Gillespie edited

Addressing all members of a group

Not sure if this is a bug or normal.

When I want to address all members of a group. This statement works:

  1. for (int i = 1; i <= Group("Stations").length; i++)
  2. {
  3. Group("Stations")[i].output.close();
  4. }

But when I instead use the sampler to get the group name from the tool box. I end up with this statement which doesn't work. It doesn't give any error but doesn't do anything either.

  1. for (int i = 1; i <= Model.find("Tools/Groups/Stations").subnodes.length; i++)
  2. {
  3. Model.find("Tools/Groups/Stations").subnodes[i].output.close;
  4. }

Should the 2nd method work or not?

Thanks,

FlexSim 19.0.2
groupsapi
· 1
5 |100000

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

Matthew Gillespie avatar image Matthew Gillespie ♦♦ commented ·

If there are other groups as members of the first group you should use the toFlatArray() method to give you a single list of all the objects throughout the hierarchy of the group.

  1. Array members = Group("Stations").toFlatArray();
  2. for (int i = 1; i <= members.length; i++) {
  3. members[i].as(Object).color = Color.random();
  4. }
2 Likes 2 ·

1 Answer

Matthew Gillespie avatar image
3 Likes"
Matthew Gillespie answered

No, the 2nd method doesn't work and it's not supposed to because you're not doing what you think you're doing.

Objects in a group aren't stored as subnodes of the group, those objects are stored in the model. The subnodes of a group are pointers to the objects in the model.

You'd have to modify the 2nd method like this for it to work

  1. treenode stations = Model.find("Tools/Groups/Stations");
  2. for (int i = 1; i <= stations.subnodes.length; i++) {
  3. stations.subnodes[i].find("+/~").as(Object).output.close();
  4. }

Which is exactly why we provide a Group API (the 1st method) for you to use so that you don't have to write such nasty code.


groups.png (10.9 KiB)
5 |100000

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