The QCAD 3 Scripting Interface
- Details
- Last Updated on Thursday, 24 May 2012 12:48
- Published on Thursday, 24 May 2012 11:47
QCAD 3 revolutionizes the way how QCAD can be extended and new CAD related applications can be developed on top of it.
Using the powerful QCAD 3 scripting interface, new interactive tools and user interface components can be added to QCAD 3 without having to set up a development environment or requiring a special developer license. In the same manner completely new applications can be developed using only the script interface of QCAD 3.
Extending QCAD 3
Extending QCAD 3 involves creating an ECMAScript file which does something when QCAD 3 is loaded. For example, if the extension is a tool, it might add a menu, tool button, command or keycode to launch the tool.
When the tool is launched, the same script starts to handle all user interaction to 'do something'. It might for example show a dialog, ask for some user input, allow the user to specify a position in the drawing and based on those inputs add some entities to the drawing.

Typical use cases for extending QCAD 3 through its script interface are:
- A tool that automatically generates an often used component from some user given parameters and lets the user position the component in the current drawing.
- A user interface component that displays an interactive drawing or shows information about the current drawing or selection in the drawing.
- A tool that generates a new drawing, based on the currently viewed drawing.
- Importers and exporters for new file formats.
- ...
Examples of QCAD extensions are:
- Almost all tools and user interface components of the QCAD 3 application are implemented as script plugins. Some tools implement most of their functionality in ECMAScript (e.g. SVG exporter, property editor, drawing tools, modification tools, ...) and other tools are mainly implemented in C++ but called through ECMAScript (e.g. DWG importer / exporter based on Teigha).
Creating New Applications Based on QCAD 3 Technology
A new application might for example create a drawing document, add some entities to is, save it as a DXF file and close itself. Or it might create a complex user interface which allows the user to interact and work with it. Developing scripts on top of QCAD 3 technology is just like developing in any other environment. The script interface does not impose any limits as almost all classes of Qt and QCAD are fully scriptable.

Although any type of application can be developed using this approach, applications will usually be CAD related. For example:
- GUI applications which must be able to load and display CAD drawings.
- Applications which analyze drawings.
- Applications which convert or publish drawings.
- Applications which create drawings based on user input, SQL data bases, XML files, etc.
- ...
Examples of QCAD 3 based applications are:
- The command line utilities that are included with every QCAD 3 installation: dwg2pdf, dwg2bmp and dwg2svg.
- The QCAD 3 application self.
Requirements
Any QCAD 3 user can start developing new tools and features for QCAD 3 immediately. All that is needed is a regular QCAD 3 single user, site or server license, a text editor and basic ECMAScript (JavaScript) skills.
A Teigha license is not required. Teigha provides DXF / DWG file format export and import and is included with every regular QCAD 3 user license.
A Qt license is not required. The Qt libraries and the ECMAScript interface to the Qt libraries are included with every regular QCAD 3 user license.
API Scope
The scripting interface of QCAD 3 provides full access to the Qt framework upon which QCAD is built. So if you are already familiar with Qt, you can leverage this know-how in QCAD 3 scripts. For example, creating a toolbar with the QCAD 3 scripting interface works in the same way as in C++, only with the slightly different ECMAScript syntax:
var toolBar = new QToolBar("My Tool Bar");
mainWindow.addToolBar(Qt.TopToolBarArea, toolBar);
When it comes to CAD specific functionality, you can use the QCAD 3 API, for example in this code snippet which adds a line entity to a drawing document:
var op = new RAddObjectsOperation();
var lineEntity = new RLineEntity(document, new RLineData(new RVector(0,0), new RVector(50,10)));
op.addObject(lineEntity);
op.apply(document);
The Qt API offers modules and classes for:
- GUI development
- SQL database access
- WebKit
- XML
- OpenGL
- Networking
- Internationalization / Localization
- Multimedia
The Qt API is documented at http://doc.qt.nokia.com/4.7/index.html
The QCAD 3 API adds to that all CAD specific functionality such as:
- Graphics scene and view
- CAD format import / export
- User interaction handling in graphics view
- CAD core (entity storage, entity rendering, algorithms, spatial index, ...)
The QCAD 3 API is documented at http://www.ribbonsoft.com/doc/qcad/3.0/developer/
On the same site you will find some tutorials and howtos to get started.

