Repeating commands in macros
If you want to repeat a set of commands a number of times, for example, creating a circle at the start of every line in the model, you can use loops.
For example, if you have two feature sets, Top and Bottom, which contain holes you want to drill from the top and bottom of the model respectively, use the macro:
STRING Fset = 'Top'
INT Count = 0
WHILE Count < 2 {
ACTIVATE FEATURESET $Fset
ACTIVATE WORKPLANE FROMENTITY FEATURESET $Fset
IMPORT TEMPLATE ENTITY TOOLPATH "Drilling\Drilling.ptf"
EDIT TOOLPATH $TpName CALCULATE
$Fset = 'Bottom'
$Count = Count + 1
}
There are three loop structures:
FOREACH loops repeatedly execute a block of commands for each item in a list.
WHILE loops repeatedly execute a block of commands until its conditional test is false.
DO - WHILE loops executes a block of commands and then checks its conditional test.
FOREACH loop
A FOREACH loop repeatedly executes a block of commands for each item in a list or array.
The basic control structure is:
FOREACH item IN sequence{
Commands A
}
Commands B
where:
item
is an automatically created variable that PowerMill initialises for each iteration of the loop;
sequence
is either a list or an array.
Commands A are executed on the first item in the list.
Commands A are executed on the next item in the list. This step is repeated until there are no more items in the list.
At the end of the list, Commands B are executed.
For example,
FOREACH item IN folder("path") {
Commands A
}
Commands B
Where <path>
is a folder in the Explorer such as, Toolpath, Tool, Toolpath\Finishing.
Within FOREACH loops, you can:
Cancel the loop using the BREAK statement.
Jump directly to the next iteration using the CONTINUE statement.
You cannot create your own list variables, there are some built in functions in PowerMill that return lists (see the parameter documentation for component, and folder).
You can use one of the inbuilt functions to get a list of entities, or you can use arrays to create a sequence of strings or numbers to iterate over. For example, use the inbuilt folder function to get a list of entities.
An example of using a FOREACH loop is to batch process tool holder profiles:
FOREACH ent IN folder('Tool') {
ACTIVATE TOOL $ent.Name
EDIT TOOL ; UPDATE_TOOLPATHS_PROFILE
}
Another example is to renumber all the tools in a project:
INT nmb = 20
FOREACH t IN folder('Tool') {
$t.number.value = nmb
$t.number.userdefined = 1
$nmb = nmb + 2
}
To get the most out of these macro features, you should familiarise yourself with the inbuilt parameter functions detailed in Help > Parameters > Reference.
WHILE loop
A WHILE loop repeatedly executes a block of commands until its conditional test is false.
The basic control structure is:
WHILE condition {
Commands A
}
Commands B
If condition is true, then Commands A are executed.
While condition remains true, then Commands A are executed.
When condition is false, Commands B are executed.
Within WHILE loops, you can:
Cancel the loop using the BREAK statement.
Jump directly to the next iteration using the CONTINUE statement.
DO - WHILE loop
The DO - WHILE loop executes a block of commands and then performs its conditional test, whereas the WHILE loop checks its conditional test first to decide whether to execute its commands or not.
The basic control structure is:
DO {
Commands A
} WHILE condition
Commands B
Commands A are executed.
While condition remains true, then Commands A are executed.
When condition is false, Commands B are executed.
Within DO - WHILE loops, you can:
Cancel the loop using the BREAK statement.
Jump directly to the next iteration using the CONTINUE statement.
CONTINUE statement
The CONTINUE statement causes a jump to the conditional test of any one of the loop constructs WHILE, DO - WHILE, and FOR EACH in which it is encountered, and starts the next iteration, if any.
This example, calculates and offsets, all unlocked boundaries, outwards and inwards.
FOREACH bou IN folder('Boundary') {
IF locked(bou) {
// This boundary is locked go get the next one
CONTINUE
}
REAL offset = 1 mm
EDIT BOUNDARY $bou.Name CALCULATE
EDIT BOUNDARY $bou.Name OFFSET $offset
EDIT BOUNDARY $bou.Name OFFSET ${-offset} }
The CONTINUE statement enables the selection of the next boundary.
BREAK statement in a WHILE loop
The BREAK statement exits the WHILE loop.