
The first steps into the world of programming under nanoCAD
When the free nanoCAD 2.0 was released last November, we said that the released version already had tools for developing applications. In particular, for the bulk of users, it is possible to quickly write utilities and small applications using scripts (scripts) based on ActiveX Automation (Java-script or VB-script). But who knows how to take this opportunity? :-) I
suggest taking the first steps together in the world of programming for nanoCAD and creating a small program that interacts with the user through the command line and automatically draws lines in the specified order on the nanoCAD working drawing. Based on this script and giving free rein to fantasy, you can create your own application, which, for example, automatically makes a diploma for you :-)
In this demo, we should get something like this: And we need the following for this:

By the way, are you looking for an interesting job related to programming? We have open vacancies: http://habrahabr.ru/company/nanosoft/vacancies/
In what language it is generally not important to write an executable script: nanoCAD supports both Java-script and Visual Basic Script. For this article, I chose VB, since recently I use it more often.
When I considered on the basis of what to demonstrate the operation of a VB script, I had a desire to transfer some LISP script to a VB script, thus demonstrating that this is possible. After searching the Internet, I came across a course project " Development of a program in the LISP language for building Sierpinski curves of the i-th order ". It contains a fairly detailed algorithm that explains how to use the recursive functions to build the figure shown in Figure 1. Let's do the same, but on VBS.
VBS-program under nanoCAD does not imply any input function - the script starts to be executed from the first lines to the end. The basic procedure of the program is quite simple - one cycle. Let's start with it:
From the code (and from the description of the course project) we see that the Sierpinski curve is constructed by four recursive functions: they are called A (), B (), C () and D () functions. Plus, there is a specialized Line function (Direction, Size) that simply draws a regular line at the right scale and at the right angle. The definitions of these functions are carried out by the usual VBS tools - for example, this is how the function A () on LISP and on VBS looks like:
We place these additional five functions at the end of the main script. It remains only to learn how to display graphics in nanoCAD and how to interact with the user.
Firstly, a VB script is a plain text file with the extension .vbs. It can be called in the nanoCAD environment using the VBS command (see Figure 2, respectively, the Java script using the JS command). Everything is simple - we indicate the path to the script and, if it is written without errors, then it is executed. If there are errors in the script, a message about this will appear on the command line.
VB syntax is absolutely standard, so we can safely use DIM, SET, FOR-NEXT, IF-THEN-ELSE, variable declarations, arrays, standard functions (such as CStr () or MsgBox ()), function declarations and / or integer division operations . Everything should work and I won’t especially teach this :-)
To access the current open drawing, use ThisDrawing. Moreover, if we plan to draw in model space (i.e., in the main drawing, the Model tab), then we define the ModelSpace collection through this construction:
Access to the command line is through the Utility object. In particular, the script below displays a classic greeting on the command line:
Using the functions GetInteger (), GetPoint (), GetString (), GetReal (), GetEntity (), GetDistance (), GetAngle () and some others, you can request data from the command line. For example, like this:
Knowing where to display data and how to interact with the user, we can begin to create a more meaningful program.
What other functions can be used in the program under nanoCAD? In general, all those that are determined by the structure of the DWG file: creating a line, an arc, a circle, hatching, obtaining a point, polar coordinates, operations with opening / closing a file, working with layers, variables, etc. These features are typically described in the DWG SDK documentation: either Autodesk ( www.autodesk.com , section of the Developer Center), or the Open Design Alliance ( http://www.opendwg.org/ , section of DWGdirect).
We will actively use the AddLine function (startpoint, endpoint), which creates a regular 2D line. Also in your scripts you can use:
All coordinates that you transmit to nanoCAD can be three-dimensional. In this case, initially it should be a three-dimensional array, but it is more convenient to transfer a line of the form “x, y, z”, where - x, y, z are three-dimensional coordinates. Those. like this:
Even in my script, I decided to lay out the lines on different layers. To do this, I created two new layers at the beginning of the program, and then changed the active layer:
Here is what we get as a result - see Figure 3. Of course, this result does not give practical utility (except for aesthetic pleasure), but it provides food for thought and self-improvement.
The demo script can be downloaded here: demoscript.rar . Download, unzip, for example, to C: \, open in a text editor, study and run it in nanoCAD 2.0. And a little tip: do not set the order of the curves above 4 - you will freeze :-)
suggest taking the first steps together in the world of programming for nanoCAD and creating a small program that interacts with the user through the command line and automatically draws lines in the specified order on the nanoCAD working drawing. Based on this script and giving free rein to fantasy, you can create your own application, which, for example, automatically makes a diploma for you :-)
In this demo, we should get something like this: And we need the following for this:

- nanoCAD 2.0 (can be downloaded from nanocad.ru or
torrents.rurutracker.org ); - any text editor (I use Notepad ++ ).
By the way, are you looking for an interesting job related to programming? We have open vacancies: http://habrahabr.ru/company/nanosoft/vacancies/
Formulation of the problem
In what language it is generally not important to write an executable script: nanoCAD supports both Java-script and Visual Basic Script. For this article, I chose VB, since recently I use it more often.
When I considered on the basis of what to demonstrate the operation of a VB script, I had a desire to transfer some LISP script to a VB script, thus demonstrating that this is possible. After searching the Internet, I came across a course project " Development of a program in the LISP language for building Sierpinski curves of the i-th order ". It contains a fairly detailed algorithm that explains how to use the recursive functions to build the figure shown in Figure 1. Let's do the same, but on VBS.
![]() |
Figure 1. Sierpinski curve of the 4th order, on the example of which we consider the principles of programming for nanoCAD. |
Program structure
VBS-program under nanoCAD does not imply any input function - the script starts to be executed from the first lines to the end. The basic procedure of the program is quite simple - one cycle. Let's start with it:
LISP code | VBS Code |
---|---|
( setq h ( div *SquareSize* 4 ) ) | h = SquareSize \ 4 |
From the code (and from the description of the course project) we see that the Sierpinski curve is constructed by four recursive functions: they are called A (), B (), C () and D () functions. Plus, there is a specialized Line function (Direction, Size) that simply draws a regular line at the right scale and at the right angle. The definitions of these functions are carried out by the usual VBS tools - for example, this is how the function A () on LISP and on VBS looks like:
LISP code | VBS Code |
---|---|
defun A ( k ) | Sub A(k) |
We place these additional five functions at the end of the main script. It remains only to learn how to display graphics in nanoCAD and how to interact with the user.
First steps
Firstly, a VB script is a plain text file with the extension .vbs. It can be called in the nanoCAD environment using the VBS command (see Figure 2, respectively, the Java script using the JS command). Everything is simple - we indicate the path to the script and, if it is written without errors, then it is executed. If there are errors in the script, a message about this will appear on the command line.
![]() |
Figure 2. Java and Visual Basic scripts are invoked by the JS and VBS commands, respectively. |
VB syntax is absolutely standard, so we can safely use DIM, SET, FOR-NEXT, IF-THEN-ELSE, variable declarations, arrays, standard functions (such as CStr () or MsgBox ()), function declarations and / or integer division operations . Everything should work and I won’t especially teach this :-)
To access the current open drawing, use ThisDrawing. Moreover, if we plan to draw in model space (i.e., in the main drawing, the Model tab), then we define the ModelSpace collection through this construction:
Dim ms |
Access to the command line is through the Utility object. In particular, the script below displays a classic greeting on the command line:
Dim ut |
Using the functions GetInteger (), GetPoint (), GetString (), GetReal (), GetEntity (), GetDistance (), GetAngle () and some others, you can request data from the command line. For example, like this:
Do |
Knowing where to display data and how to interact with the user, we can begin to create a more meaningful program.
Basic functions
What other functions can be used in the program under nanoCAD? In general, all those that are determined by the structure of the DWG file: creating a line, an arc, a circle, hatching, obtaining a point, polar coordinates, operations with opening / closing a file, working with layers, variables, etc. These features are typically described in the DWG SDK documentation: either Autodesk ( www.autodesk.com , section of the Developer Center), or the Open Design Alliance ( http://www.opendwg.org/ , section of DWGdirect).
We will actively use the AddLine function (startpoint, endpoint), which creates a regular 2D line. Also in your scripts you can use:
- AddCircle (center, radius) - circle;
- AddArc (center, radius, startangle, endangle) - arc;
- AddEllipse (center, majoraxis, radiusratio) - ellipse;
- AddPolyline (verticeslist) - polyline;
- AddText (textstring, insertionpoint, height) - single line text;
- AddMText (insertionpoint, width, text) - multi-line text;
All coordinates that you transmit to nanoCAD can be three-dimensional. In this case, initially it should be a three-dimensional array, but it is more convenient to transfer a line of the form “x, y, z”, where - x, y, z are three-dimensional coordinates. Those. like this:
Dim ms |
Even in my script, I decided to lay out the lines on different layers. To do this, I created two new layers at the beginning of the program, and then changed the active layer:
Set oLyrLine1 = ThisDrawing.Layers.Add("Связи") |
Result
Here is what we get as a result - see Figure 3. Of course, this result does not give practical utility (except for aesthetic pleasure), but it provides food for thought and self-improvement.
![]() |
Figure 3. Java and Visual Basic scripts are invoked by the JS and VBS commands, respectively. |
The demo script can be downloaded here: demoscript.rar . Download, unzip, for example, to C: \, open in a text editor, study and run it in nanoCAD 2.0. And a little tip: do not set the order of the curves above 4 - you will freeze :-)