Avoid Spiderwebs with Direct References in Scripts

Flownex Friday Tech Tips!

It’s officially Spooky Season! In between episodes of Squid Game and re-watching Hubie Halloween I thought I could make things a little less scary by demonstrating how to get rid of the spiderwebs in our projects (AKA Data Transfer Links). We use these often with scripts, which can be very powerful, but as our networks get more complex we may want to avoid the clutter/attracting spiders.

Quick Script vs Full Script

If we’re not scared of a little C# we may decide to start with a full script. I, personally, like to use a quick script so that I can use the table inputs to define my script inputs and results.

Calling a Specific Component

We will identify the component we want to read/write to by the unique Identifier. This can be found in the input properties for the component. In this example we’ll use a pipe component. By default this would typically be “Pipe – 0” or “Pipe” followed by some other integer. This field is editable so you can change it, but it must remain unique to that specific component.

We can either specify the identifier as a string explicitly in the script:

//Referencing component "Pipe - 0"
IPS.Core.Component Pipe = Project.GetComponent("Pipe - 0");

Or we can have this as an text-type input to the script so that we can change the reference more easily. In this example we have a text variable “ComponentIdentifier”:

//Referencing component which is defined by input "ComponentIdentifier"
IPS.Core.Component Pipe = Project.GetComponent(ComponentIdentifier.ToString());

Note that we are using the ToString function to convert our text variable to a string.

Reading from Component

Now that we have our Pipe component referenced in the script we can read any of the results into our script via their display identifier using the following syntax:

//Reading from flow component
IPS.Properties.Double T_1 = Pipe.GetPropertyFromFullDisplayName("{Flow Element Results,Generic}Total temperature
") as IPS.Properties.Double;
Pipe_Temp.Value = T_1;

Note that we must use a internal variable. in this case “T_1”, to read the temperature before we assign that to the value of our script result “Pipe_Temp”.

A quick trick to determine the display name is to right-click on the result you’re interested in and select “Copy display identifiers to clipboard”.

Writing to Component

The process of modifying a component input with values from the script is very similar. We can use the above trick on our Inputs for the component to find the display identifier for the pipe diameter in this example.

//Write to component
IPS.Properties.Double diameter = Pipe.GetPropertyFromFullDisplayName("{Geometry,Inlet}Diameter") as IPS.Properties.Double;
diameter.Value = Pipe_Diameter;

Summary

Here is how we’ve set up the quick script, as you can see it is really not that scary:

And the snippet of code from the quick script:

//Identify Component
IPS.Core.Component Pipe = Project.GetComponent(ComponentIdentifier.ToString()); 

//Read from component
IPS.Properties.Double T_1 = Pipe.GetPropertyFromFullDisplayName("{Flow Element Results,Generic}Total temperature") as IPS.Properties.Double;
Pipe_Temp.Value = T_1;

//Write to component
IPS.Properties.Double diameter = Pipe.GetPropertyFromFullDisplayName("{Geometry,Inlet}Diameter") as IPS.Properties.Double;
diameter.Value = Pipe_Diameter;

Happy Friday and Happy Coding!