Code Generator Variables

A variable is a place-holder, a symbol that represents a quantity. It is an object that holds data and is uniquely named by the programmer. It holds the data that is assigned to it, until the data is changed or the variable is removed. Variables offer a way to indirectly store and pass data to Code Generator functions or commands.

Variables are the cornerstone of all computer languages. It does not matter what programming language you are using, there will always be variables.

Before a user defined variable can be used, it must be declared. When a variable is declared it is created in computer memory, a name is given to it, and the type of data it will hold is defined. Since a variable must be declared before it is used the declaration statement must precede the first use of the variable name in a CGT file.

When declaring a variable, you define the type of data that variable will contain.

Variable declarations are performed in the CGT file @DECLARE section.

Data Types

The following are the available SmartCAM variable declarations.

A #DEC variable contains a numeric that will be output using the F_Decimal format. An #INT variable contains a numeric that will be output using the F_Integer format. A #STR contains a series of text characters such as a message to output to the G-Code file as a display to the machine operator. A string variable can hold a maximum of 254 characters. A #TBL is a numeric that will look up an associated string in a matching @TABLE section. A #CALCDEC outputs a decimal value that is returned via #RETNUM() from a matching calculational @$Section. A #CALCINT outputs an integer value that is returned via #RETNUM() from a matching calculational @$Section. A #CALCSTR outputs a string that is returned via #RETSTR() from a matching calculational @$Section.

The declaration contains two parts, the data type, and the variable name list which is one of more comma separated variable names. All SmartCAM variable names begin with a pound (#) character. This # character is a designator that tells SmartCAM that the item being referenced is handled differently and may be a variable.

Here is an example of a @DECLARE CGT file section.

@DECLARE
#STR #message, #msgList[20]
#INT #part_count, #chg_count, #toolsUsed[40]
#TBL #batchCount
#CALCDEC #xposXfo, #yposYfo
@

The matching #TBL @Section name would be @batchCount. The matching #CALCDEC @$Section names would be @$xposXfo and @$yposYfo.

Variable Names

When a variable is declared, it is given a name. This name is the handle used to access the data stored in the variable. There are a couple of restrictions on what can be used as a variable name.

Additionally, while these are not language rules, the following are some variable naming recommendations.

Using Variables

There are several ways to use variables in the Code Generator system. A value can be passed from the JOS system to the Code Generator system and stored in a variable. A value can be extracted from the JOS system using the JOS(tag name) function for extracting numeric data and the JOS_STR(tag name) function for extracting string data. A table showing these JOS Data Tags is available under Using SmartCAM - Data Tags and Section Numbers - Job Operation Setup Data Tags.

A variable can be passed via a User Command that is an element in the model. A variable can simply be assigned a value with the #EVAL command. Ultimately the variable will have to be used in some way. The basic use is to output the value to the G-Code file. This is done by simply adding the variable in a line of the CGT file unpreceded by commands or functions. The most rudimentary way is to simply add the variable on a line by itself.

Here is an example of a @START CGT file section that uses the variables from the @DECLARE section, above.

@START
#EVAL(#message=Parts to cut = )
#EVAL(#part_count = 12)
#message#part_count
#EVAL(#batchCount = #part_count)
Batch Count = #batchCount
@

@batchCount
0,One
10,Two
20,Three
30,Four
40,Five or More
@

When processed, this message will be output to the top of the G-Code file:

Parts to cut = 12

Batch Count = Two

In this example, the first command, #EVAL, sets the string variable #message equal to the string 'Parts to cut = ' and the second #EVAL sets the integer variable #part_count equal to 12. The next line simply outputs both variable values to the G-Code file. Then the #batchCount table variable is set to the #part_count and that value is used to lookup a batch count string in the matching @batchCount table

Here is an example of a @TOOLCHG CGT file section that uses the #EVAL command to increment the #chg_count variable in order to track how many tool changes have occurred and marks the corresponding index in the #toolsUsed array for each tool number that was used in the job. It is assumed that the variables are already properly declared.

@TOOLCHG
#EVAL(#chg_count=#chg_count+1)
Tool change number #chg_count
#EVAL(#toolsUsed[#TOOL] = #YES)
@

When processed, this message will be output at the beginning of each tool change sequence, incremented by one:

Tool change number 12

In this example when the @TOOLCHG section is called the #EVAL command instructs the Code Generator to make the value of the variable #chg_count equal to one more than it was. On the next line the Code Generator will output the literal string of characters 'Tool change number ' followed by the value of the newly incremented #chg_count.

Here is an example of a how decimal calculation words can be used when outputting local plane offset values for all #XPOS and #YPOS values.

@RAP
<#MOV><X#xposXfo><Y#yposYfo>
@

@$xposXfo
#UPDATE(#XPOS)
#RETNUM(#XPOS + #XFO)
@

@$yposYfo
#UPDATE(#YPOS)
#RETNUM(#YPOS + #YFO)
@

When @RAP is processed, the decimal calculation words #xposXfo and #yposYfo will automatically call the matching @$ Sections, @$xposXfo and @$yposYfo, which will return the position value plus the plane offset amount, which then will be output if they have changed.

Numeric Format

You control the format used when a numeric word is output with the #FMT() command. See the Numeric Formats page.