Open Source 2D CAD
Creating a Drawing

With the QCAD Application Framework it is not only possible to create interactive tools but also to construct entire drawings based on user input (dialog, command line arguments, ...), mathematical formulas, data bases, files or other sources.

This tutorial focuses on creating objects with the QCAD Application Framework and adding them to a drawing document. The example in this tutorial creates a drawing document from scratch, adds various objects to it and saves the document as a DXF file. No user interface is used for this example, so it can be run as a command line application without first starting the QCAD Application user interface.


Some initialization is required before we get started. We need to give the application a name (used among other things as ID for XDATA in DXF / DWG).

1 // init application name:
2 qApp.applicationName = "MyApplication";

Creating a Document

A drawing in the QCAD Application Framework is represented by a document (RDocument). The document is backed by a storage which stores the actual drawing data (RStorage) and is usually not accessed directly by script developers. Further, the document also owns a spatial index (RSpatialIndex) for fast entity lookup based on location. Script developers only need to worry about the spatial index to create a document.

This code snippet creates a new, empty drawing document (RDocument) that is backed by a memory storage (RMemoryStorage) and a high performance spatial index implementation (RSpatialIndexNavel):

1 var storage = new RMemoryStorage();
2 var spatialIndex = new RSpatialIndexNavel();
3 var document = new RDocument(storage, spatialIndex);
5 // create document interface (needed for file export functionality):
6 var di = new RDocumentInterface(document);

The new document already contains some default objects like a layer '0', the model space block and line type definitions. The document has no graphics views attached, so there is no visual representation of any kind. Graphics scenes and views could be attached to the document interface.


Every change to a document (including adding entities) is wrapped in a transaction (RTransaction) in the QCAD Application Framework. Transactions are a low level concept and usually don't have to be used directly by script developers.

To control which changes belong to one transaction, the QCAD Application Framework offers a higher level concept of operations (ROperation). An operation essentially wraps a transaction with a user friendly interface. Operations can be applied to documents in way that the various graphics views and other user interface components are being updated automatically.

The following example code adds three line entities in the shape of a regular triangle to a document:

1 // create an operation:
2 var operation = new RAddObjectsOperation();
4 // corner point of the triangle::
5 var p1 = new RVector(0, 0);
6 var p2 = new RVector(10, 0);
7 var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);
9 // add line entity to operation:
10 operation.addObject(new RLineEntity(document, new RLineData(p1, p2)));
11 operation.addObject(new RLineEntity(document, new RLineData(p2, p3)));
12 operation.addObject(new RLineEntity(document, new RLineData(p3, p1)));
14 // apply operation to document:
15 operation.apply(document);

Note that we have not indicated a layer or any attributes (color, line type, line weight) for our lines. The lines are added to the current layer (layer '0') with the default attributes (color, line type and line weight inherited from layer).

DXF / DWG Export

To store the drawing document to a DXF file, we can use the document interface:

1 // export document to DXF, version 2000:
2 di.exportFile("example.dxf", "DXF 2000");

Complete Code

Here's the complete code of our example again:

1 //! [init]
2 // init application name:
3 qApp.applicationName = "MyApplication";
4 //! [init]
6 //! [document]
7 var storage = new RMemoryStorage();
8 var spatialIndex = new RSpatialIndexNavel();
9 var document = new RDocument(storage, spatialIndex);
11 // create document interface (needed for file export functionality):
12 var di = new RDocumentInterface(document);
13 //! [document]
15 //! [operation]
16 // create an operation:
17 var operation = new RAddObjectsOperation();
19 // corner point of the triangle::
20 var p1 = new RVector(0, 0);
21 var p2 = new RVector(10, 0);
22 var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);
24 // add line entity to operation:
25 operation.addObject(new RLineEntity(document, new RLineData(p1, p2)));
26 operation.addObject(new RLineEntity(document, new RLineData(p2, p3)));
27 operation.addObject(new RLineEntity(document, new RLineData(p3, p1)));
29 // apply operation to document:
30 operation.apply(document);
31 //! [operation]
33 //! [export]
34 // export document to DXF, version 2000:
35 di.exportFile("example.dxf", "DXF 2000");
36 //! [export]

To run the example, save it as a file (e.g. 'example.js') and launch QCAD, using this script as autostart script:

./qcad -autostart example.js