Class - Macros
Navigation: PGMs ➔ Classes
| Functions | Subroutines | Classes | |||||
|---|---|---|---|---|---|---|---|
| Defining a Function | Predefined Functions | Tag Functions | Mathematical Functions | String Functions | Defining a Subroutine | Trigger Subroutines | Defining a Class |
Related Links: User Defined Class and Functions, Example PGM Files
| Class - Introduction | Class - Defining a Class | Class - Using a Class | Class - Macros | Class - Examples |
|---|
Global Predefined Class Instances: Species Database Class, Particle Size Definition Class, Plant Model Class
Predefined Classes: Array Class, StrArray Class, Matrix Class, TagSelect Class, Noise Class, TimeClass
Class Macros
When working with multiple class instances, a number of Macros are available which will perform repetitive functions on each class instance. At load time, a macro is converted into a sequence of normal code. This saves the user from writing out loops or multiple lines of code. It also makes it very easy to add and remove class instances without having to remember to adjust code using the class instances. Macros can work with a predefined ClassList or the classes list can be provided as a parameter for the macro. Class instances can be blocked from use in macros using ExcludeClass.
| Macro | Macro Syntax | Description/Notes/Examples |
|---|---|---|
ForEachSub |
ForEachSub(classes, SubName(..))
|
The macro ForEachSub provides an efficient method to loop through all class instances from a list of classes and call a Sub within the class. Examples:ForEachSub(TankClass, Exec())
ClassList MainTanks {T1,T5,FeedTank,TankLine.MidTank,TankLine.EndTank}
ForEachSub(MainTanks, UpdateResults(AmbientT))
|
| ForEachClass | The macro function ForEachClass was the original macro implemented and the only option available before Build 139.32530. The equivalent functionality is now available as ForEachSub. | |
ForEachFn |
ForEachFn(ResultVariable, OperationMethod, Classes, FnName(..))
|
The macro ForEachFn provides an efficient method to loop through all class instances from a list of classes and call a Function within the class performing the specified operation on the result of the Function and storing the result in the specified variable. Example: |
ForEachVar |
ForEachVar(ResultVariable, OperationMethod, Classes, ClassVariable)
|
The macro ForEachVar provides an efficient method to loop through all class instances from a list of classes and using a variable within the class performing the specified operation and storing the result in the specified variable. Example: |
ForEachVarSet |
ForEachVarSet(Classes, Variable, Expression)
|
The macro ForEachVarSet provides an efficient method to loop through all class instances from a list of classes and set the value of a numeric variable (string variables not supported) within the class to be equal to the specified value (or result of an expression). Example: |
Macro Functions
A "Macro Function" can be used in expressions similar to a function, unlike a "Macro" which can only be used like a Sub.
| Macro | Macro Syntax | Description/Notes/Examples |
|---|---|---|
ForEachFnCalc |
ForEachFnCalc(OperationMethod, Classes, FunctName(...)) |
The function macro ForEachFn provides an efficient method to loop through all class instances from a list of classes and call a Function within the class performing the specified operation and return the result. Example: |
ForEachVarCalc |
ForEachVarCalc(OperationMethod, Classes, ClassVariable) |
The function macro ForEachVar provides an efficient method to loop through all class instances from a list of classes and using a variable within the class performing the specified operation and return the result. Example: |
ForEach |
ForEach(OperationMethod, Classes, FunctName(...)/ClassVariable) |
This function macro is the same as ForEachFnCalc or ForEachVarCalc where at load time it auto detects last parameter type and is then equivalent to ForEachFnCalc or ForEachVarCalc. |
ClassCount |
ClassCount(Classes) |
The function macro ClassCount returns the number of class instances in the list of classes. Examples:
|
Macro Parameters
Classes Parameter
All of the class macros include a Classes parameter which is used to define the list of class instances that need to be looped through. There are a number of different ways of defining the list of class instances to be used. If the list of classes includes any class instances marked as excluded using ExcludeClass, this are ignored and excluded from the list of class instances used in the macros.
The different methods for defining Classes to be used in macros are:
| Classes | Description | Example |
|---|---|---|
| ClassType | A named Class Type Definition. List used will be all class instances from global scope, up to this point in the code, of the specified class definition. |
ForEachSub(TankClass, Exec())
|
| ClassList | A previously defined ClassList name. List used will be from the defined ClassList used. |
ForEachSub(TanksList, Exec())
|
| {ClassInstance1,…} | A comma separated list of class instances, that may include indexed classes in class array. List used will be as specified in the {} list. |
ForEachSub({T1,T2,T3}, Exec())
|
| {ClassType1,…} | A comma separated list of class type definitions. List used will be all class instances from global scope, up to this point in the code, of all the specified class definitions. |
ForEachSub({TankClass,CSTR_Class}, Exec())
|
| ClassArray | A class array. List used will be all the classes in the Class Array. |
ForEachSub(T, Exec())
|
OperationMethod Parameter
Many of the class macros include a OperationMethod parameter which is used to define the operator method to be applied to the results of the functions or values of the variables for each class instance in the list of class instances that need to be looped through. The Operators allowed are: + - * or and bor band bxor.
For example to sum all the values of a class variable use the "+" operator. Sum = ForEach(+, TankClass, flow).
For example to test a bit flag in all classes use the "or" operator. AnyTanksFull = ForEach(or, TankClass, IsFull()).