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 which is an ECMAScript (JavaScript) file that implements the format details. All available configurations are stored inside the directory scripts/Cam/CamConfigurations. 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 CamExporter, which is defined by QCAD/CAM. CamExporter defines the basic structure of CAM exporter configuration classes. Various methods in CamExporter are called to export the file header, the various entities in the CAD drawing and the footer. CamExporter also takes care of the various features of QCAD/CAM, for example to add ramp on / ramp off, cut inner contours before outer contours, etc.

CamExporter does not output anything by itself, but would produce an empty file on its own. CamExporter 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 exiting configuration for G-Code called GCode, which is defined in file GCode.js. This is the case for most machine controllers.

Deriving a Configuration from GCode 

Create a file with a unique name of your choice. For this example, we call our new configuration MyGCode and store it in file MyGCode.js.

We start by creating a new empty text file MyGCode.js inside the directory scripts/Cam/CamConfigurations of your QCAD/CAM installation. You can use your favourite plain text editor for this task, for example Notepad under Windows, TextEdit under Mac OS X or vim / Emacs under Linux systems.

MyGCode.js must contain 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 if beyond the scope of this documentation.

In practice, this looks like this (note that lines starting with // are comments and are not required):

// Include GCode.js which contains the class 'GCode', on which we base our configuration:
include("GCode.js");

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

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

Now, we have a fully working configuration, which is identical to the existing GCode configuration but called 'MyGCode'.

Configuration Preferences

The constructor we defined above calls the constructor of our base class 'GCode'. There, the default values for some global options are set. We can override these options in our constructor if desired. We can for example increase the number of decimals used by our configuration from 3 (default) to 4, so that a coordinate with value 1.23456789 will be exported as 1.2345 instead of 1.235. To do this, we set the class variable decimals to '4' (or whatever value is desired):

this.decimals = 3;

We can also configure the output unit here. That's the unit expected by your machine or controller. Drawings that are exported to G-Code using this configuration, are automatically converted to this unit. 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.alwaysWriteGCode

true or false false Set to true to always repeat G-Codes (G1, G2, G3, ...), even if the code is the same as the one that was last used.

this.lineNumber

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

this.absoluteIJ

true or false  false  Set to true to use absolute coordinates for I and J for circular moves (G2, G3) 

A complete constructor might look like this:

function MyGCode(documentInterface, newDocumentInterface) {
GCode.call(this, documentInterface, newDocumentInterface);
    this.unit = RS.Inch;
this.decimals = 4;
}

Changing Header / Footer

To add G-Codes or M-Codes or other information to the header and footer, a configuration can override the functions writeHeader and writeFooter.

MyGCode.prototype.writeHeader = function() {
    this.writeLine("G20");
this.writeLine("G90");

// Call implementation for writeHeader of 'GCode'. This moves to the Z safety level as configured by the user during conversion.
GCode.prototype.writeHeader.call(this);
};

MyGCode.prototype.writeFooter = function() {
    // Make sure, the tool is lifted up, no longer cutting:
this.writeToolUp();
// Move to safety level:
this.writeRapidZMove(this.getSafetyZLevel());
this.toolPosition = GCode.ToolPosition.Clear;
// Write some additional, alternative codes to end the program:
this.writeLine("M2");
};

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 function getFileExtensions. The function is expected to return an array of allowed file extensions for this exporter:

MyGCode.prototype.getFileExtensions = function() {
return ["ngc", "nc"];
};

Enabling / Disabling Line Numbers

All lines that are exported, including the ones written using this.writeLine in the writeHeader and writeFooter functions, are prefixed by a line number code (Nxxx). By default, the line number is incremented by 10 for each new line. To change that behaviour or to disable line numbers altogether, a configuration can override function getLineNumberCode:

MyGCode.prototype.getLineNumberCode = function() {
return "";
};

Changing Tool Up / Tool Down Codes

The GCode configuration by default uses the Z axis to move the tool down for cutting and up to the clearance level. If you are for example using a LASER cutting machine, you will have to change this behaviour to enable / disable the LASER, using the appropriate codes (for example an M code to switch the LASER on / off).

MyGCode.prototype.writeToolDown = function() {
if (isNull(this.onCuttingLevel)) {
// move to Z1, 2, ... (LASER cutting level) only on the first call:
GCode.prototype.writeToolDown.call(this);
this.onCuttingLevel = true;
}
    this.writeLine("M8");
this.toolIsDown();
};

MyGCode.prototype.writeToolUp = function() {
this.writeLine("M9");
this.toolIsUp();
};

Adding Global Parameters

This is work in progress.

Adding Layer Parameters

This is work in progress.

Deriving a Configuration from CamExporter

This is work in progress.