Designing the Macro

This section shows the development process of a macro, in broad brush strokes. Fundamentally, the programmer needs to discover what inputs are required for the macro to work, what outputs are expected, what issues may crop up, and then create a high-level description of how the macro will work.

Inputs

What information does the macro need to be successful? Since this macro will be stepping through an open Process Model file looking for Step geometry, then summarizing the Step geometry lengths into an output file, we have two of the requirements described.

Outputs

What outputs and feedback are needed by the macro?

Issues

Consider what types of issues may be encountered while running the macro. Figure out ahead of time how they will be handled.

Macro Design

Before starting to code, it is a good idea to have an idea of how the macro will work. There are many ways to present and organize this information. This tutorial will use a process called Pseudocode.

Pseudocode is based on the terms psuedo and code and it means fake code. It is a compact and informal high-level description of a program using a similar structure to the final program, but without following all the syntax and semantic conventions of the language. It is a natural language description of the program flow.

Pseudocode allows the programmer to work through the macro solution before attempting to code it. If a programmer cannot describe the solution, they cannot code the solution. Additionally, once written, the programmer can imagine how the macro will work in practice and test to see whether the design is correct and will work.

For example, the following pseudocode:

IF counter is larger than total number of elements in database
   Display an error: Element counter exceeded
ELSE
   Add 1 to the counter
ENDIF

This pseudocode could be translated into the following actual MCL statements.

IF (#COUNTER > TOTEL())
   PAUSE[TX="Element counter exceeded."]
ELSE
   #COUNTER = #COUNTER + 1
ENDIF

The following pseudocode describes the macro to be developed. This is a high-level description of the solution. There are many technical details that need to be addressed and they will be in the next topic, the topic where the macro is actually written. Additionally, this is just one of many possible methods that could be used to get the same end result. This approach was chosen because it is conceptually easy to understand and easy to code.

When creating the pseudocode for a macro, always refer to your list of Inputs, Outputs, and Issues. If these are not all addressed in the pseudocode, they likely won't be addressed in the macro itself.

// Check that a process model is loaded. If not error out
IF last element number is 0, no file is loaded
   Display error: Must have an open PM model with geometry
   Exit macro
ENDIF
			
// Make sure the output file path is valid and usable. Otherwise no point in continuing
IF outputfile can not be opened for Writing
   Display error: Output path invalid or read-only. Try another path
   Exit macro
ENDIF

// The main routine.
LOOP through entire database, starting at element 1
   Is current element a step?
      IF Yes:
         Get Step number and description
		 
         // Don't check the same Step number more than once
         Is Step number already checked?
         IF No,
            Add Step and Description to list of checked steps
			
            // From the current Element position, check each remaining element and see if it is the
            // same step number as current
            LOOP from current element to End of Elements
               Check each element
               IF Layer
                  skip
               ENDIF
               IF Step
                  Get Step number
                  IF Step matches current Step
                     Get length and add to running total
                  ENDIF
               ENDIF
            END LOOP
         ELSE
            // Step is already checked, skip it and move to the next element
         ENDIF
   ELSE If No
      Nothing to do, move to next element
   ENDIF
END LOOP

// Output the results
OPEN the output file for writing
LOOP through the steps found
   Write the Step, Description, and accumulated length for step to output file
   Add Step accumulated length to total accumulated length
END LOOP
Write Total Accumulated length to output file
CLOSE the output file

// Macro finished
Display message: Macro complete.

The concept for this macro is simple. Starting at the first element in the database, check each element until the end of the database is reached. For each element, ignore it if it is a layer. If it is a Step, get the Step ID and Description. Keep a running list of Steps that have already been checked. If the newly found Step is in the list, skip it and move to the next element in the database.

If the Step has not already been checked, create an inner loop (a loop nested in the primary loop) that checks the database from the current position to the end of the file. For every element that is a the same Step, get the length and add to the total for that Step.

The following will help explain this algorithm. It is a simple representation of a small Process Model database.

El. 1 - Layer 1
El. 2 - Step 10
El. 3 - Step 10
El. 4 - Layer
El. 5 - Step 20
El. 6 - Step 20

The macro will create a main loop that checks each element from 1 to 6.

There are no other elements to check, so the main loop ends and the output is created.

Related Topics

Coding the Macro

From The Beginning Overview

SmartCAM Automation Overview