For loop (SDL script)
A For loop can iterate through linked items or items in a list:
• If you specify a link type for the current item, the For loop iterates through each item that is linked to the current item through that link type. For example, if the current item is a Class, you can iterate through its Attributes by specifying the Attribute link type.
• If you use the %getlist or %getlocal list keyword and specify a list, the For loop iterates through each item in that list.
The current item is derived as follows:
• ACS calls the top-level Generate.sdl generation template for each item (Model, Package or Class) that is selected either directly or indirectly for generation. On entering the Generate.sdl generation template, the selected Model, Package or Class is the current item.
• The current item remains as the selected Model, Package or Class until a For loop iterates a collection of linked items or items in a list. During a For loop, the current item is the item that is currently being iterated.
• After leaving a For loop, the current item reverts to the item that was the current item before entering the For loop.
For loops can be nested.
The For loop uses the following keywords:
• %for - starts the For loop and specifies a link type for the current item or a list.
A link type is specified through the automation interface name of the link type that links the current item to other items. For more information about the automation interface names of link types, see Object Attributes and associations under the Automation Interface chapter of the PTC Modeler Help, and then click the required item type. The link types are listed in the Associations section.
• %separator - optional keyword to specify text for delimiting the output of each iteration. If you do not want a separator generated to the code with each iteration of the For loop, do not include this keyword.
• %exit - optional keyword for exiting the For loop without processing all iterations. The %exit keyword is typically used with the evaluation of a condition.
• %endfor - ends the For loop
Syntax
For a collection of items:
%for "<link type>" [%separator "<separator characters>"]
<set of statements>
[%exit]
%endfor
For a global list:
%for %getlist "<list name>" [%separator "<separator characters>"]
<set of statements>
[%exit]
%endfor
For a local list:
%for %getlocallist "<list name>" [%separator "<separator characters>"]
<set of statements>
[%exit]
%endfor
The following examples are provided:
• For loop processing linked items
• For loop processing list
• For loop using %exit keyword
• For loop using the %separator keyword
Example 1. For loop processing linked items
This example works in the context of an Operation and uses the 'Parameter' automation interface association to process the Operation's Parameters. For each Parameter, it loads the Parameter.sdl template.
(
%for "Parameter"
%load "Parameter.sdl"
%endfor
)
Example 1. For loop processing list
This example runs a set of statements against each item in the 'IncludedObjects' list.
%for %getlist "IncludedObjects"
$ calculate and output the #include
%if %inlist "sysCurrentObject" %then
$ self so don't include
%else
%load "OutputInclude.sdl"
%setlocalvar "nGeneratedDependency" = 1
%endif
%endfor
Example 1. For loop using %exit keyword
This example works in the context of a Class and uses the 'Attribute' automation interface association to see if any of the Attributes owned by the Class have On Instance storage. If any Attributes have On Instance storage, the text '-- On instance attributes' is generated, and the For loop is exited because of the %exit keyword.
%for "Attribute"
%if %custom "Storage" == "on instance" %then
\t\t\t
"-- On instance attributes"
\n
%exit
%endif
%endfor
Example 1. For loop using the %separator keyword
This example works in the context of an Operation and uses the 'Parameter' automation interface association to process the Operation's Parameters. For each Parameter, it loads the Parameter.sdl template. The text that is returned by the Parameter.sdl template for each Parameter is delimited by the specified comma and space.
(
%for "Parameter" %separator ", "
%load "Parameter.sdl"
%endfor
)