Pin It

QCAD/CAM can export CAD drawings to various formats used in CAM (computer-aided manufacturing). These formats can be dialects of G-Code or other, completely different formats. Each supported CAM format is defined in a configuration file (or "postprocessor") which is an ECMAScript (JavaScript) file that implements the format details. All available configurations are stored inside the directory postprocessors. New format implementations can be added to the same location.

Basic Structure

Each configuration file contains an ECMAScript class with the same name as the script file. If the script file is called MyGCode.js, the class defined inside the script must be called MyGCode.

The ECMAScript class must derive directly or indirectly from class CamExporterV2, which is defined by QCAD/CAM. CamExporterV2 defines the basic structure of CAM exporter configuration classes. Various methods in CamExporterV2 are called to export the file header, the various entities in the CAD drawing and the footer. CamExporterV2 also takes care of the various features of QCAD/CAM, for example tool radius compensation, lead in / lead out, cutting inner contours before outer contours, path optimization, etc.

CamExporterV2 does not output anything by itself, but would produce an empty file on its own. CamExporterV2 offers a text stream (QTextStream) to write to the exported file. This text stream is exposed through the member variable this.stream.

If the desired output is a G-Code dialect (e.g. G1 X10 Y20 for a linear movement), the configuration can be derived from the existing configuration for G-Code called GCodeBase, which is defined in file GCodeBase.js. This is the case for most machine controllers. One such example configuration for outputting G-Code in Millimeters is defined in file GCodeMM.js and available in the user interface as the configuration called G-Code (G41/G42) [mm].

Deriving a Configuration from GCodeBase

Create a file with a unique name of your choice. For this example, we call our new configuration MyGCode and store it in a file with the same name, i.e. MyGCode.js under directory postprocessors of your QCAD/CAM installation.

You can use your favourite plain text editor to create and write the file, for example Notepad under Windows, TextEdit under macOS or vim, Emacs, etc. under Linux systems.

MyGCode.js must define a class called MyGCode to be a valid configuration for QCAD/CAM. While there are strictly speaking no classes in ECMAScript, we can create a similar concept by attaching functions to the prototype object of a constructor function. How this works exactly is beyond the scope of this documentation. However, there is a straightforward template you can use for all configuration files.

For our example, the definition of the class MyGCode looks like this (note that lines starting with // are comments and are not required):

// Include GCodeBase.js which contains the class 'GCodeBase', used as base for our own configuration:
include("GCodeBase.js");

// Constructor of our class:
function MyGCode(documentInterface, newDocumentInterface) {
// Call constructor of base class:
GCodeBase.call(this, documentInterface, newDocumentInterface);
}

// Derive our class from class GCode:
MyGCode.prototype = new GCodeBase();

// Display name shown in user interface:
MyGCode.displayName = "My G-Code";

Now, we have a created a new, fully working configuration called "My G-Code" which is ready to be used in QCAD/CAM.

Configuration Preferences

The constructor we defined above calls the constructor of our base class GCodeBase. In the constructor, we can set some parameters for our configuration such as the desired output unit or if we want to use G41/G42 for tool radius compensation or calculate the center of the tool path.

To configure the output unit (that's the unit expected by your machine or controller), we can set the member variable unit to Inch or Millimeter, for example:

this.unit = RS.Inch;

or

this.unit = RS.Millimeter;

Other settings that can be configured in the constructor are:

Variable: Possible Values: Default: Description:

this.outputOffsetPath

true or false false Set to true to calculate tool radius compensation instead of using G41/G42.

this.lineNumber

Number 10  Initial number used for line numbering (N...).

this.lineNumberIncrement

Number 10 Line number incrementation from line to line.

A complete constructor might look like this:

function MyGCode(documentInterface, newDocumentInterface) {
GCode.call(this, documentInterface, newDocumentInterface);
    this.unit = RS.Inch;
this.outputOffsetPath = true;
this.lineNumber = 1;
this.lineNumberIncrement = 1;
}

Changing Header / Footer

To add G-Codes or M-Codes or other information to the header and footer, a configuration can override the member variables header and footer:

this.header = [ "[N] G0 Z100" ];
this.footer = [
"[N] G0 Z100",
"[N] M30"
];

Changing the Output File Extension

The output file extension is chosen by the user during export. The set of extensions the user can choose from in the file export dialog can be defined in member variable fileExtensions. For example:

this.fileExtensions = [ "nc", "cnc" ];