The QCAD 3 Scripting Interface

QCAD version 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 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.

Extending QCAD

Extending QCAD involves creating an ECMAScript file which adds a user interface component when QCAD is loaded. For example, if the extension is a new tool, the script might add a menu, tool bar button, command or keycode to launch that 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, etc. Based on those inputs the script might for example add or modify the contents of a drawing.

Typical use cases for extending QCAD through its script interface are:

  • A tool that automatically generates a reusable component from some user given parameters and optionally 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 existing QCAD extensions are:

  • Almost all tools and user interface components of the QCAD application are implemented as script add-ons. 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).

QCAD C++ Plugins

There are situations in which extending QCAD through scripts alone is not possible. This is mostly the case, if your extension is based on an existing C or C++ library. In that case, you can create a QCAD C++ plugin that wraps your library and adds the necessary hooks for to access library functionality through the script interface. Such a plugin can be automatically loaded by QCAD on start up to add functions and classes to the script interface of QCAD. These script extensions can then be used by your script add-ons to make that functionality available as part of the application interface.

A plugin might for example offer export to a file format through an existing library for that format. The plugin registers itself as file exporter and becomes available as a format choice in the File - Save As dialog. Alternatively, it could register a script function to export a document (e.g. exportDocument(document, fileName)). This function could be called by a script add-on which is available through a new menu entry File - Export XY.

Creating New Applications Based on QCAD 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 technology is just like developing in any other environment. The script interface does not impose any limits since almost all classes of the Qt tool kit and QCAD are fully scriptable.

 

Although any type of application can be developed using this approach, QCAD based 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 existing QCAD based applications are:

  • The command line utilities that are included with every QCAD Pro installation: dwg2pdf, dwg2bmp and dwg2svg.
  • The QCAD application self.

Requirements

Any QCAD user can start developing new tools and features for QCAD immediately.

All that is needed is 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 Pro user license. If your script add-on or application relies on DXF / DWG format support through Teigha, the user(s) of your add-on will require a QCAD Pro single user, site or server license.

A Qt license is not required. The Qt libraries and the ECMAScript interface to the Qt libraries are included with every regular QCAD installation.

API Scope

The scripting interface of QCAD 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 your QCAD scripts. For example, creating a toolbar with the QCAD 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 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 qt-project.org/doc/qt-4.8/

The QCAD API adds to that all CAD specific functionality such as:

  • Graphics scene and view
  • CAD format import / export
  • User interaction handling in graphics views
  • CAD core (entity storage, entity rendering, algorithms, spatial index, ...)

The QCAD API is documented at qcad.org/doc/qcad/latest/developer/

On the same site you will also find some tutorials and howtos to get started.