Restoration of Stack

The function transDepth() yields the current depth of the stack. The function transRestore(depth) removes transformations from the stack until the given depth is reached. Typically, the two functions are combined to save and restore the state of the transformation stack.

It is good programming style to do this saving and restoring in the context of userdefined ST-functions.

Example:

Initially, within the following function the depth of the stack is stored in the variable depth. At the end of the function the initial state is restored by transRestore. Note that restoration only works properly if the stack depth does not fall below depth within the function. Instead of using transDepth() and transRestore() the stack depth could also be restored using transPop(). However, it may become cumbersome to keep pushing and popping of transformations synchronous, especially if transformations are pushed conditionally.

{
FUNCTION draw
VAR
    depth : UINT;
END_VAR
    depth := transDepth();
transTranslate(10,0,0);
    // … G-Code …
    transRotZ(45);
    // … G-Code …
    transMirrorX();
    // … G-Code …
    transRestore(depth);
END_FUNCTION
}