Using Variables and Functions
SmartCAM's macro language really begins to shine when used in combination with macro variables and macro functions. With these two additions, it is possible to write macros that create complex geometry or automate complex tasks with relative ease. In fact, many of these activities can be done directly within SmartCAM, while the macro is being recorded, and require no additional editing.
Obround Race Track Profile
In this example, you will create a 8x4 pill-shaped obround profile. This profile can then be used as a pocket profile or stock profile in later operations. To simplify the macro creation and recording, for this exercise, the dimensions of the geometry will be hard-coded in the macro.
While the dimensions of the profile are fixed, the location of the profile should be dynamic. Therefore all the dimensions used in the macro will be based on variables. This way when the macro is run several times, simply by changing the X and Y locations of the center of the profile, it can be created at any location.
SmartCAM allows you to enter variable names, instead of fixed coordinates, for most panel inputs. When the macro is played back, the user will be prompted to provide the values for the variables and thus will be able to change the location of the generated profile.
Step One: Record the Macro
Start your SmartCAM application and select Macro - Record. Enter the full path and
filename for the macro. For purpose of this tutorial, we suggest C:\MACROS\RaceTrack.mcl
.
Press the Accept button to begin recording the macro.
- Set the Start Profile Position
The profile will be created using the SmartCAM Profile functionality. To begin, select Geometry - Profile, then select the Start tab from the Profile control panel. The Start Profile is -2 units back from the center X position and +2 units above the center Y position.
Click in the Start Prof Point: X input and enter:
#RTX-2
. Then press Enter and enter:#RTY+2
in the Start Prof Point: Y input and press Enter. The two variables used are#RTX
which is the "Race Track Center X" position and#RTY
which is the "Race Track Center Y" position.Note, that as you try to advanced through the prompts, SmartCAM will prompt you to provide a numeric value for the #RTX and #RTY variables. When prompted, enter "0" for each. This will give the SmartCAM solver enough information to continue, but the macro will still include the #RTX/Y variables.
- Create First Line
Select the Line tab on the Profile control panel. For the line, all the information that is needed is the direction and length of the line segment.
For Line Angle: enter
0
and for Length: enter4
, then press Enter. - Create the First Arc
The complete set of information needed to draw the arc will not be available during the arc's creation. So, the macro will rely upon the Profile functionality's Pending Elements solver.
Select the Arc tab on the Profile control panel. Set the Arc Dir to
CW
(clock-wise). Set the Rel (relationship) list toTangent
.To help solve the arc, profile the arc's center point. For Center Point: X enter
#RTX+2
and for the Center Point: Y enter#RTY
.This is all the information we need to give SmartCAM. Press the Advance button.
- Create the Second Line
Like the previous line, all that is needed is the line length and angle.
For Line Angle: enter
180
. For Length: enter4
and then press Enter. - Finish the profile with the Second Arc
Select the Arc tab. Make sure the arc direction is still
CW
and the arc isTangent
.Tell the arc where to end by entering End Point: X as
#RTX-2
and Enter Point: Y as#RTY+2
and then press Enter. - The obround profile has been solved and is ready to be created. Click on the Go button to commit the pending profile geometry to the process model element list.
The race track profile macro is now finished. Stop recording by selecting Macro - Stop.
Step Two: Test the Macro
A macro is not finished until it has been tested. In this instance, testing means running the macro.
Start a new Process Model, using File - New. Then use Macro - Execute to run the macro you just created. After running it once, run it a second time.
Depending upon the state of your SmartCAM application, the first test run may not have prompted
you to enter values for the #RTX
and #RTY
variables. On the second run,
it is unlikely you were prompted to enter values for these variables.
This is related to variable Scope
.
Once a variable is created it remains until it is deleted or the application is restarted. In other words,
when the user is prompted for fill in a value for a variable, even while recording a macro, the value entered is
saved with the variable. When you run the macro, it checks to see if the variables exist. If not, the user
is prompted, if so, the known values are used.
For the example macro to run correctly, and prompt for center XY values, the variables must be removed before they are first used. Use Macro - Variables to open the Macro Variables dialog box. Find RTX and RTY in the list of variables on the left. Click on the variable and select Remove; this will remove the variables from SmartCAM's variable system. Do this for both variables and then close the dialog with Done.
Now, run the example macro again. This time the user is prompted for the values of the RTX
and
RTY
variables. Use the Macro Variables dialog and remove the two variables again. Now, run the
macro again, again the user is prompted for the center XY values.
Step Three: Variable Management
Step Two showed that the macro worked, as long as the RTX
and RTY
variables
were cleared before each run. This is not optimal macro design, as it requires the user to delete
variables before the macro is used when the macro itself can delete the variables.
Open the macro into a text editor, such as Notepad. It will look similar to:
// Production_Milling 20.0.0 Windows 02/05/13 09:29:13 Macro File
// c:\MACROS\RaceTrack.mcl
// CREATED: 02/08/13
//
//
PROF_CONTEXT_OPEN[]
START_PROF[XE=#RTX-2, YE=#RTY+2, DS=4, LV=0]
LINE_PROF[TI=0, ZE=0, DS=4, AN=0, SS=2, SE=0, SP=2]
ARC_PROF[DR=0, TI=1, XC=#RTX+2, YC=#RTY, SS=2, SC=0, SE=2, SP=2,
LV=0]
LINE_PROF[TI=1, ZE=0, DS=4, AN=180, SS=2, SE=0, SP=2]
ARC_PROF[DR=0, TI=1, XE=#RTX-2, YE=#RTY+2, SS=2, SC=0, SE=0, SP=2,
LV=0]
PROF_CONTEXT_CLOSE[]
SmartCAM supports a macro command named VAR_REMOVE[]
. This macro command takes a
single parameter, the variable name, and deletes the variable from the application's variable
system. Since the macro needs to prompt the user for the Center XY values when they are first used,
the variables need to be removed prior to their first use.
Modify the macro to look as follows:
// Production_Milling 20.0.0 Windows 02/05/13 09:29:13 Macro File
// c:\MACROS\RaceTrack.mcl
// CREATED: 02/08/xx
//
// 02Feb20xx Initials - Modified to remove RTX and RTY before first use.
// So that user is prompted
// Remove variables
VAR_REMOVE[VN="RTX"]
VAR_REMOVE[VN="RTY"]
PROF_CONTEXT_OPEN[]
START_PROF[XE=#RTX-2, YE=#RTY+2, DS=4, LV=0]
LINE_PROF[TI=0, ZE=0, DS=4, AN=0, SS=2, SE=0, SP=2]
ARC_PROF[DR=0, TI=1, XC=#RTX+2, YC=#RTY, SS=2, SC=0, SE=2, SP=2,
LV=0]
LINE_PROF[TI=1, ZE=0, DS=4, AN=180, SS=2, SE=0, SP=2]
ARC_PROF[DR=0, TI=1, XE=#RTX-2, YE=#RTY+2, SS=2, SC=0, SE=0, SP=2,
LV=0]
PROF_CONTEXT_CLOSE[]
One of the first things to note is that some additional comments were added. It is always a good idea to add some simple comments at the top of the macro file when you make changes; this way, later, you have a running history of the modifications made and why.
Next, two VAR_REMOVE[]
macro commands were added to remove the two macro
variables. The variable names are passed into the VN
(Variable Name) parameter.
Run the macro several times, notice the macro now always prompts for the Center X (RTX) and Center Y (RTY) variable values.
Related Topics