CNC Simulator Pro

user guide

11.22. Create your own codes

One very powerful feature is the possibility to create your own G, M, and other codes directly in the CNC file. You can replace existing codes and create new custom ones. As long as it is possible to do a subprogram or a macro of what you want, you can turn it into your own code.
In the samples folder for milling machines, you can find a program called Sample24_CustomCodes_mm.cnc that illustrates how this can be done. You need to create a separate file containing the code definitions and macros for your custom codes.
In the example, we have used a file called Sample24_Includefile.cnc. You can then use the file and include it in all programs where you want to be able to use our custom codes. This is done by putting the following line at the beginning of your CNC program.
$Include "Sample24_Includefile.cnc" (or your filename)
This will run the codes in the file in the background when you simulate your program. The example shows how we can redefine the canned G12 circular drilling cycle built-in to CNC Simulator Pro to a custom rectangular slot cycle.
To define macros for custom codes, use the following two commands:
$DefineModalMacro for modal codes
$DefineNonModalMacro for non modal codes.
Here is an example that redefines G12 to a custom macro with number 4000.
$DefineModalMacro  "G12" 4000 0
The last number (0) is a debug flag. If you set it to one, you can step into the macro while running the program. If it is set to zero, the code will act like a built-in code, and the macro will run in the background.
There is also a command that we can use to create custom popup-help for our new codes. It is $CodeHelp.
Here is an example:
$CodeHelp 1 "G12|Mill rectangle|G12 width height|Cycle to mill a rectangle. Create a rectangle slot at the tool position with X width and Y height."
The first parameter is used to set the color of the code. 1 = blue and should be used for G codes. 2 = red and should be used for M codes, and 3 = gray and should be used for special codes like $-commands.
The second parameter is a long string that contains substrings separated by a vertical line character |.
The first substring is the name of the code. The second one is the description. The third is the syntax, and the last one is information about the code.
After the command above has been executed, the code help for G12 will look like this:
See the example file for more information on how to create your own codes.