nanoCAD provides several types of API for CAD application development. In order to build utilities and small application you can use scripts based on ActiveX Automation (JScript and VBScript). Below we will show how it works by creating a small application, which interacts with the user through the command line and draws lines in predefined order on nanoCAD’s drawing space automatically. Based on this example you can create your own application.
The result of the demonstration example should be similar to the image below:
|Code in LISP
|Code in VBScript|
|defun A ( k )
( cond ( ( > k 0 )
( A ( - k 1 ) )
( Line 1 h )
( B ( - k 1 ) )
( Line 0 ( * 2 h ) )
( D ( - k 1 )
( Line 7 h )
( A ( - k 1 ) )
if k > 0 then
Line 1, h
Line 0, 2 * h
Line 7, h
Place these additional five functions at the end of the main script. The following steps will introduce you to output graphic in nanoCAD and interact with the user.
The first steps
First of all, VB-Script is a text editor with the .vbs file extension, which can be run in nanoCAD by using VBS (see fig.2). It is easy, you should only specify the script path and if it was written correctly, the script works. If the script has any errors, the message about it appears in the command line.
Fig.2. Java and Visual Basic scripts are run by JS and VBS commands correspondingly
VB-syntax is completely standard, so we can use DIM, SET, FOR-NEXT, IF-THEN-ELSE,
declaration of variables, arrays and standard features (such as CStr()
), function declarations and/or integer division operations.
To get access to the current drawing use ThisDrawing. If you are going to draw in the model space (the Model tab), the ModelSpace collection will be defined through the following structure:
Set ms = ThisDrawing.ModelSpace
The access to the command line is carried out through the Utility object. The script, which is written below, outputs the classical greeting in the command line:
Set ut = ThisDrawing.Utility
With GetInteger(), GetPoint(), GetString(), GetReal(), GetEntity(), GetDistance(), GetAngle() functions and also with some others it is possible to request data from the command line. For example:
count = ThisDrawing.Utility.GetInteger("Specify a positive number (>=1)”)
Loop While count <= 0
For i = 1 to count
point = ThisDrawing.Utility.GetPoint(“0,0,0”, “Specify a point №” & i)
If you know where the data is entered and how to interact with the user, you can start to write more complicated programs.
Most crucial functions
What other functions can be used in nanoCAD? In general, you can use all functions that are defined by DWG file structure: draw a line, arc, circle, hatch, define a point, polar coordinates, open/close a file, work with layers, variables, etc. These functions are described in the SDK documentation to DWG: Autodesk
(Developer Center section), or Open Design Alliance
We will use the AddLine (startpoint, endpoint) function to create a 2D line. Also you can use the following functions:
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.
The coordinates which are transferred into nanoCAD can be three-dimensional. In this case, it has been three-dimensional array, but it is more comfortable to transfer the «x,y,z» line, where x, y, z are three-dimensional coordinates. That is like this:
Set ptStr = CStr(x)+","+CStr(y)+","+CStr(y)
Also the lines in this script are located in the different layers. To get this you should create two new layers at the beginning and then change the current layer:
Set oLyrLine1 = ThisDrawing.Layers.Add("Connections")
Set oLyrLine2 = ThisDrawing.Layers.Add("Rectangles")
Set oLyr = oLyrLine1
Set oLyr = oLyrLine2
oLine.Layer = oLyr.Name
At the end you should have the result as shown in fig.3. Of course, this result doesn’t have any particular usefulness (other than an aesthetic pleasure), but it gives some food for thought and self-improvement.
Fig.3. Result of VBScript command
The demonstration script you can download here: demoscript.zip
. Download, unzip, open using any text editor, learn and run it in nanoCAD. And a little tip: don’t set the order of curves above 4 or you will get stuck :-)