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.
- A open Process Model
If there is not a open Process Model this macro cannot do anything. - A output file Path and Filename
This is where the ASCII text report will be created.
Outputs
What outputs and feedback are needed by the macro?
- Done message
Message indicating the macro has completed. - ASCII Report File
This is the primary output; the ASCII text file with the summarized lengths. - Error messages
Will need error messages for any problems encountered during the running of 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.
- No Step Geometry
In this case, there is an open Process Model, however, it only contains wireframe geometry. - Output File Problems
The given output filename could be invalid, incomplete, or write protected.
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.
- Element 1 is a layer and is skipped.
- Element 2 is Step 10. Step 10 has not already been checked. The length of Element 2 is collected and saved. A small loop will now check from element 3 (Current element + 1) to element 6.
- Element 3 is Step 10, so its length is added to the running total for Step 10.
- Elements 4 through 6 are not Step 10 so they are skipped. The small inner loop is complete.
- The main loop increments to Step 3. It is Step 10, since Step 10 has already been checked, it is skipped.
- Step 4 is a layer and is skipped.
- Step 5 is Step 20. Step 20 has not been checked. So length of element 5 is added to the running total for Step 20. A small loop is setup to check from element 6 (Current element + 1) to the end of the process model. In this case a loop from 6 to 6 - or one element.
- Element 6 is checked. It is Step 20 and so the length is added to the running total for Step 20. There are no other elements to check, so the small inner loop ends.
- The main loop continues. The next element checked is Element 6. This is Step 20, which has already been checked and so it is skipped.
There are no other elements to check, so the main loop ends and the output is created.
Related Topics