question

Ryusuke T avatar image
0 Likes"
Ryusuke T asked Ryusuke T commented

How to edit background color with script

Hi,

I wanted to edit the background color and ran the script below.

-----------------------------------------------------------------------------------------------------

/**Custom Code*/

Object current = param(1);

treenode activity = param(2);

Token token = param(3);

treenode processFlow = ownerobject(activity);


for (int i = 1; i <= Model.find("ModelBackground1").find(">visual/shape").subnodes.length; i++) {

Model.find("ModelBackground1").find(">visual/shape").subnodes[i].subnodes[1].subnodes[1].value = 0;

Model.find("ModelBackground1").find(">visual/shape").subnodes[i].subnodes[1].subnodes[2].value = 0;

Model.find("ModelBackground1").find(">visual/shape").subnodes[i].subnodes[1].subnodes[3].value = 0;

}

-----------------------------------------------------------------------------------------------------

The tree information and UI are changing correctly, but the actual background color remains the same. Am I missing some settings?

image001.png

image002.png

I've confirmed that setting the color manually can change it.


Thanks in advance.

FlexSim 23.0.9
backgroundchange color
image001.png (70.8 KiB)
image002.png (65.7 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.

1 Answer

·
Matthew Gillespie avatar image
1 Like"
Matthew Gillespie answered Ryusuke T commented

You're missing the function that updates the DWG.

applicationcommand("updatedwglayerlist", background, shapeNode);

Here's an example script of it all working

Object background = Model.find("ModelBackground1");
treenode shapeNode = background.find(">visual/shape");
Color newColor = Color.blue;

for (int i = 1; i <= shapeNode.subnodes.length; i++) {
    treenode colorNode = shapeNode.subnodes[i].first;
    colorNode.value = 3;
    colorNode.subnodes[1].value = newColor.r * 255;
    colorNode.subnodes[2].value = newColor.g * 255;
    colorNode.subnodes[3].value = newColor.b * 255;
}

applicationcommand("updatedwglayerlist", background, shapeNode);

Note, that while this script works now, it is relying on the current tree structure and there's no guarantee that this tree structure will remain the same going forward.

· 5
5 |100000

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

Ryusuke T avatar image Ryusuke T commented ·

@Matthew Gillespie ,

Thank you for your reply.

When I applied the sample code you advised to the attached dwg file, the color of "Equipment Layer" was changed, but the other colors were not changed.

Is there a way to change other items as well?

Or is there information about "updatedwglayerlist" documented somewhere?

ER_small.dwg

0 Likes 0 ·
er-small.dwg (243.9 KiB)
Julie Weller avatar image Julie Weller Ryusuke T commented ·

Hey @Ryusuke T! I noticed when I ran it that only the equipment layer changed but if I changed all the layers to a different color and then ran the script it changed all of them... could be a bug. I'm going to try running it with a few more dwg files and see if the same thing happens.

Here is the coding reference to the command applicationcommand

https://docs.flexsim.com/en/23.2/Reference/CodingInFlexSim/CommandReference/Commands.html#applicationcommand which is just acting on the tree node "updatedwglayerlist"

0 Likes 0 ·
Julie Weller avatar image Julie Weller Julie Weller commented ·
Yeah no matter the dwg file I use, it updates the colors in the ModelBackground pop up but doesn't in the model, however if I change all the colors once and then run the script it works. Interestingly when I delete the background and add it again after doing all that, it is still able to be changed. @Matthew Gillespie Do you think this is a bug or is there just something that needs to be called first?
0 Likes 0 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Ryusuke T commented ·

Sorry, I tested my script in a model where I'd already changed the colors manually so it seemed to all be working. It looks like that script needs to set the value of the colorNode itself too. I updated the script above with the extra line.

updatedwglayerlist is not documented because it's a function that was not designed as a public API. It's just some code that is called by the properties window. This is not a feature request that has come up before and so we haven't designed and exposed a public API.

0 Likes 0 ·
Ryusuke T avatar image Ryusuke T Matthew Gillespie ♦♦ commented ·

@Matthew Gillespie ,

I have verified that all shapes are updated with the updated script code.

Thank you for your support.

I would like to use it recognizing that it is not permanently guaranteed.

0 Likes 0 ·

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.