Creating Macros in
Part 2: Adding Variables and Functions
Overview
Welcome back!
In our last post, we looked at the fundamentals of macros. In this post, we’ll be looking at more advanced ways to use macros that continue to increase your productivity by automating:
- Family of parts – Cases where the you create similar parts with slight variations.
- Common operations - Cases where the you run the same operations over and over again.
- New functionality - Gives the SmartCAM application the ability to do something that it cannot already do.
- Integration - Integrates SmartCAM with other applications.
- Simplification - Makes existing functionality easier to use.
In this post on creating macros, we’ll look at the following so you can add user input and return values, text etc:
- Variables
- Functions
Variables
If you aren’t familiar with variables, they are really just place holders. You are defining an empty thing to be filled in either when you create it or later. Below is some information about variables.
The following are the three SmartCAM data types and the three related variable declarations. “#VARNAME” is the name you will give your variable for example #LAYER_NUM or #ELMT_TYPE.
- DECIMAL:#VARNAME
- INTEGER:#VARNAME
- STRING:#VARNAME
You can use any VARNAME (variable names) you want but there are some things to consider.
- Your variable name can only have 12 characters. This is a strict rule so you will need to ensure your variable names stay at 12 characters or less (this includes underscores (_) like the examples given.
- Variables must start with a letter.
- We suggest using a consistent prefix for variable names in your macros. So let’s say you were making a set of macros specific to layers:
o An example variable might be #LY_NUM. This variable name tells you that it is specific to a layer (LY) and the last section gives more detail; in this case, the layer number. In that same set, you might also have #LY_COLOR. Again, the prefix tells you it is specific to layers (LY) and COLOR tells you its specific to the layer color. By using naming and prefixes, you can use wildcards to view and remove them.
- Check out this document for additional notes and caveats.
In our previous post, we showed you how to make a simple macro that selected Layer 2 then Zoomed in. Now let’s say you want the user to be able to add in different variables because your user doesn’t always want it to be Layer 2 or the same magnification. Let’s add some variables to the code we looked at last time so the user can put in the data they want.
In the macro code below, we have used INTEGER: #LY_NUM and INTEGER:#LY_MF as our variables. These will allow the user to input the Layer # and Multiplication Factor when they run the macro. So here is that code (Notice how we have commented out the first line of each section to describe what we are doing? This is akin to the pseudocode we discussed last time):
// Remove the entire list of variables with a wildcard using LY_ per the discussion above.
VAR_REMOVE_LIST [LST="LY_*"]
//Notice that in removing the variable, we just use its name and not include #. And by having a common prefix for your variables, you can use this function with a wildcard (*) and it will remove all variables that have that prefix.
// Define the new variables. The # character is a designator that tells SmartCAM that the item being referenced is a variable. Notice how they both start with LY_.
//Since we are not setting these variables to a specific value, the user will be prompted for input.
INTEGER: #LY_NUM
INTEGER: #LY_MF
// Clear any selected groups, then hide all elements
FILTER_USE[ON=1]
SELECT_ALL[]
HIDE_SELECTED[]
// Show the desired layer and zoom details. Notice we only used variables for the Layer# and the Magnification Factor – not the XY coordinates of the zoom
SHOW_LAYER[LY=#LY_NUM]
ZOOM[X1=1089, Y1=595, MF=#LY_MF]
Here is some information about the commands in this exercise:
ZOOM[X1 and Y1 where you want the center of the zoom. MF is the Multiplication Factor.
Try it!
- Grab the above code.
- Put it in notepad and save it as a .mcl file. Remember to store your custom files in \ProgramData\SmartCAM\Common\ICON. (If you have issues with the macro and you copy/pasted into notepad, try typing in the code manually. Sometimes copy/paste adds characters we can’t see but cause the macro to fail)
- Run your new macro from the Macro > Execute menu item.
Functions
Macro Functions return a value from a command. This could be arithmetic, text or a call for something else. Basically, a Command tells SmartCAM to do something and Function asking for a response (this response may or may not be visible, and may just be asking for something that will be used later in the macro).
Functions types
- Math Functions: These are built in operations, and include basic arithmetic as well as trigonometry.
- Branching, Looping and Controls: Some examples of these are IF/THEN statements as well as HALT which stops the macro for use while testing.
- Element Properties: There is a myriad of data you can pull from the elements in your model using predefined functions.
- Profile Properties: Supported profiles can be built from lines, arcs, and polylines.
Let's try it!
We will be creating an mcl that looks at two elements and calculates the total area combined. Here is the code:
// Remove the variables – again, you will want to do this to ensure there are not old variables being used from a previous macro/variable/usage.And you can use the list remove and a wildcard to remove all variables with this prefix.
VAR_REMOVE_LIST[LST="AR_*"]
// Define the new variables
INTEGER: #AR_ELEM1
INTEGER: #AR_ELEM2
//Since we are not setting these variables to a specific value, the user will be prompted for input.
// Define the area profile variables and get the profile area of the two elements using the Function AREA(elem)
DECIMAL: #AR_PROF1 = AREA(#AR_ELEM1)
DECIMAL: #AR_PROF2 = AREA(#AR_ELEM2)
// Define the variable that will store the total then use the two variables above and arithmetic to total the two areas
DECIMAL: #AR_TOTAL = (#AR_PROF1 + #AR_PROF2)
// Define the variable that will store the type of unit being used and use the Function UNITS() to determine the type of units used in the current model. This will be useful for our output string.
INTEGER: #AR_UNIT = UNITS()
//Define the variable that will be the string you will be adding to the conditional text below
STRING:#AR_SUM
//Set the variable decimal from above as a string. FMT formats the decimal to string and "D9.2" tells the string to use 9 digits before of the decimal and 2 digits after.
#AR_SUM=FMT(#AR_TOTAL,"D9.2")
//Use conditional branching to determine the appropriate message to display with the total area
IF (#AR_UNIT = 0)
PAUSE[TX="The total area is " + #AR_SUM + " inch(es)."]
ELSE
PAUSE[TX="The total area is " + #AR_SUM + " mm."]
ENDIF
- Copy and paste the above code into a text file. Name it something meaningful to you and put it in \ProgramData\SmartCAM\Common\ICON. (Again, if you have issues with the macro and you copy/pasted into notepad, try typing in the code manually. Sometimes copy/paste adds characters we can’t see but cause the macro to fail.)
- Create a new project with at least 2 elements. Or you can download this file. It has two arcs. Like in our previous post, you will need to uncompress the file and you may get an error opening it in SmartCAM. You can say OK and this will open the file in all SmartCAM products.
You will notice that there are 2 elements in the example file:
If you created your own, you will want to ensure you know which elements you want to use for this example.
3. Go to Macro > Execute.
4. Click the File Select button and open the .mcl file you just created.
5. Click Accept.
6. For our example, you will input “1” and “2” into the requested fields. If you are using your own model, input the desired element numbers.
You should get a message that tells you the approximate area of the two elements you selected and the type of units our are using.
There you go! You have just created and used Variables and Functions in SmartCAM. Like we said last time, the examples given here are pretty simple. Just like anything you can do in SmartCAM, you can make these as simple or complex as you need.
In our next post, we’ll show you all the ways you can execute a macro because who wants to have a handy macro that does the work if you have to add steps to use it? In the meantime, we hope you starting building and using macros to help you and your team be more efficient.
No comments:
Post a Comment