Code Generator Commands and Functions
The SmartCAM CGT (Code Generator Template) uses CGT Words to perform tasks. These are generally in the form of commands and functions. A command instructs the Code Generator to do something and is usually in the form of #COMMAND(), where #COMMAND is the name of the command and the data inside the parenthesis is what to do or what to act upon. Some examples of this are:
#EVAL(#V1=#V1-#U2)
#CALL(section)
#IF(#V1<#U2)
#RESET(#XPOS)
Another type of command, or control, are the conditional brackets. <> These control the output of the contents based on whether it has already been output or not. It tells the Code Generator to output the contents only if the value of the contents have changed since the last time they were output. See the Conditional Brackets content below.
Functions are words that contain information or will get information. They differ from variables in that they are dynamically updated and the Code Generator progresses through the element data base. Some examples are:
#XPOS
#COOLNT
#DATE
#TOOL
The #FMT() command is used to specify the format for a CGT word or class of CGT words. See the Numeric Formats page.
The Code Generator system can also take advantage of many Macro functions with the use of the #EVAL command. Some examples are:
#EVAL(#V1=TOTEL())
#EVAL(#V1=TLEN(#curstep))
#EVAL(#S1=STRSUB(#string, #start, #length))
#EVAL(#V1=STRLEN(#tflags))
See the CGT Word Reference for a full list of CGT Words.
Building logic
Conditional Brackets (<>)
Conditional brackets are used to set up conditional tests within a CGT file. The conditional brackets test the contents of the keyword and only output the value if the content differs from the last time that SmartCAM output the value.
As an example, the CGT word #XPOS outputs the current X-axis position. If your controller does not require that the coordinate value be output each time, but only when it is different, then you would place the word in conditional brackets: <X#XPOS>.
If you want spaces between your words, in lines with conditional brackets, be sure that the spaces are inside the brackets. If the space characters are outside of the brackets and none of the conditions for output are met, a line of spaces is output.
Multiple CGT words can be included in a single conditional expression. If a change occurs to any of the words the content of all the words is output.
The following control words can be added to conditional brackets, with multiple CGT words, to change the evaluation and output:
- #OR is the default logic operation for multiple words. If any change occurs then the condition tests as True and is output.
- #EXC excludes any following CGT words from the testing.
- #AND requires that all of the words, which follow, must be changed for the condition to test true and be output.
Examples of using the control words:
< #MOV#OR X#XPOS > - If either #MOV or #XPOS changes both words are output
< #DCOMP#EXC D#DOFF > - #DOFF is excluded from the test, if #DCOMP changes both are output.
< X#XPOS#AND Y#YPOS > - only if both #XPOS and #YPOS are changed is the line output.
You can include the #ELSE modifier to output different text if a CGT word has not changed. #ELSE must follow the initial conditional test. The CGT words within the #ELSE statement are not checked to see if they are changed, they are always output. Example:
<T#NTOOL>#ELSE< T0>
In this example, if #NTOOL has changed, then T#NTOOL is output. If #NTOOL has not changed then the ELSE block is used and T0 is output.
Use the #IFCHG test to see if a CGT word has changed.
Nested Conditionals
Conditionals can be nested so that, for example, if the first expression is false (will not be output), then skip the rest of the tests. However, if the first expression is true (will be output) then test the next expression. Continue testing each 'next' expression as long as the current expression is true. Here are some examples.
< #CYCLE <X#XPOS>< Y#YPOS>>
If #CYCLE = True, consider BOTH #XPOS and #YPOS. If #CYCLE = False, then skip the remainder of the tests and move to the next command.
< #CYCLE <X#XPOS>< Y#YPOS>>
If either #XPOS or #YPOS are True, output the ones that are.
< #CYCLE <X#XPOS>>< Y#YPOS>
If #CYCLE = True, output it and consider #XPOS for output.
< #CYCLE <X#XPOS>>< Y#YPOS>
But because #YPOS is outside of the conditional test, consider #YPOS whether #CYCLE is true or not.
< #CYCLE <X#XPOS <Y#YPOS>>>
If #CYCLE = True, output it and consider the next expression (#XPOS).
< #CYCLE <X#XPOS <Y#YPOS>>>
If #XPOS = True, output it and consider #YPOS.
< #CYCLE <X#XPOS <Y#YPOS>>>
If #YPOS is true, output it.
So if #CYCLE = True, output it and consider #XPOS.
And if #XPOS = True, output it and consider #YPOS.
And if #YPOS = True, output it.
However, if at the beginning, #CYCLE = False, do nothing more and move to the next command.
IF statements
SmartCAM supports logic operations that enable you to test for certain conditions and trigger different actions based on the results of the test. Unlike the Conditional Brackets, these logic operators are not limited to testing whether a CGT word has changed.
The most common logic statement is #IF(test)<output>; where (test) is the test operator and <output> are the CGT words to be output if the test is true. You can add #ELSE<output> after the #IF()<> block to output a different set of data if the test is false.
There are three types of IF statements, each checking a different type of data. #IF is used for numeric comparisons, #IFSTR is used for string comparisons, and #IFCHG is used to check whether a CGT word has changed.
Numeric comparisons
#IF(#V1>3)<output>#ELSE<output2> is an example of a numeric comparison. If the variable #V1 is greater than 3, then output is coded, otherwise output2 is processed.
String comparisons
#IFSTR(#S1=G00)<output>#ELSE<output2> is an example of a string comparison. If #S1 is equal to the string "G00" then output is processed, else output2 is used.
CGT Word comparisons
#IFCHG(#XPOS)<output>#ELSE<output2> is an example of a CGT word comparison. If #XPOS has changed, then output is processed, else output2 is used.
Multiple Comparisons
When doing numeric comparisons, you can combine multiple tests by using the #OR and #AND operators. For example:
#IF(#V2>3,#OR#V2<-1)
The IF statement evaluates true is #V2 is greater than 3 or less than -1. Notice the comma between the first expression and the #OR command.
The IF commands can also use AND and OR operators. There are three forms of IF commands:
Comparison Operators
The following comparison operations can be used in IF statements:
= | equal to |
<> | not equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
Exit a CGT section
Use the #EXIT statement to leave a CGT @ section early and not process any remaining commands. Example:
#IF(#V9=1)<#EXIT>
If #V9 is set to 1, the #EXIT command is used and the current @ section is exited.
Use the #EXITC statement to leave a manually called CGT @ section early, not process any remaining commands, and return to the #CALL statement. Example:
@LINE
#IF(#var1<#var2)<#CALL(SAMPLE)>
return here if #EXITC is encountered
...
@
@SAMPLE
#IF(#var2<#var3)<#EXITC>
...
If #var1 is less than #var2 in the @LINE section, #CALL the @SAMPLE section. If #var2 is less than #var3 in the @SAMPLE section then return to the line following the #CALL statement from the @LINE section
Further examples of #IF logic
These commands work as follows: If the contents in the parenthesis () are true, then perform the tasks within the brackets <>. For example:
#IF(#var1<#var2)<#RESET(#XPOS,#YPOS)>
If the value of #var1 is less than the value of #var2, then reset #XPOS and #YPOS.
An Else operator can be added to an If command. If the contents in the parenthesis () are true, then perform the tasks within the following set of brackets <>, if not then perform the tasks in the next set of brackets <>. This takes the form:
#IF(#var1<#var2)<#RESET(#XPOS,#YPOS)>#ELSE<#UPDATE(#XPOS,#YPOS)>
If the value of #var1 is less than the value of #var2, then reset #XPOS and #YPOS. If not, then update #XPOS and #YPOS.
Two conditions can be tested with the use of #AND and #OR, for example:
#IF(#var1<#var2,#AND#U2=1)<#RESET(#XPOS,#YPOS)>#ELSE<#UPDATE(#XPOS,#YPOS)>
If the value of #var1 is less than the value of #var2, and the value of #U2 is one, then reset #XPOS and #YPOS. If not, then update #XPOS and #YPOS.
#IF(#var1<#var2,#OR#U2=1)<#RESET(#XPOS,#YPOS)>#ELSE<#UPDATE(#XPOS,#YPOS)>
If the value of #var1 is less than the value of #var2, or the value of #U2 is one, then reset #XPOS and #YPOS. If not, then update #XPOS and #YPOS.
Looping
The SmartCAM Code Generator uses the #REPEAT() command for looping (FOR, NEXT loops, WHILE, ENDW loops, etc.). Very simply, its only form is:
#REPEAT(integer value)
The integer value can be an actual number or an integer declared variable.
Here is an example of how a #REPEAT() might be used to incrementally increase power while piercing the material. #upPower is an integer variable set earlier.
@PIERCE
#REPEAT(#upPower)
<
#EVAL(#cut_power=#cut_power*1.05)
S#cut_power
>
@
When this @PIERCE CGT section is called it will loop through the two brackets <> following the #REPEAT command the number of times assigned to #upPower. Each loop through #cut_power is incremented by 1.05, the output to the G-Code file as a series of increasing 'S' words.
Tables
A table is a list of values that the #TABLE() function can choose from. #TABLE has two components within the parenthesis (). First is the table section to call and second is which item from the list to return. Another way to use a table is to declare a variable as a table variable (#TBL) and have a table section with the same name. Each time the table variable is to be output, it first retrieves its data from the table of the same name.
For the first example:
@START
#TABLE(myTable, 2)
@
@myTable
0,Off
1,Low
2,Med
3,High
@
This example would output Med at the top of the G-Code file. When the #TABLE function is encountered it will find the first parameter section name (@myTable) and parameter number two of that section, then output that to the code file. Note that it does not capture the first number and comma, these are ignored and are provided for readability. Only the data to the right of the comma is considered.
For the second example:
@DECLARE
#TBL#myTable
@
@START
#EVAL(#myTable=3)
#myTable
@
@myTable
0,Off
1,Low
2,Med
3,High
@
This example would output High at the top of the G-Code file. The @DECLARE sets #myTable as a #TBL, which lets the Code Generator know that it is a table variable. The @START section assigns the integer value of 3 to #myTable. The following line asks to output #myTable to the code file, but because the Code Generator knows it is a table variable it will search the CGT file for a section of the same name (@myTable) and use the integer value to return item 3 from the section. If no table exists by that name, nothing is output.