First steps into nanoCAD’s programming world

First steps into nanoCAD’s programming world

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: 
 


To get the same result we should have the following:

Tasking

There is no significant difference between programming languages you use: nanoCAD supports Java-script as well as Visual Basic Script. In this article the Visual Basic Script was used.
To demonstrate the work of VB-Script we will transform the LISP-Script to the VB-Script. The shape shown in fig.1 was drawn by using the recursive functions. Let’s create the same shape in VB-Script.


Fig. 1. The universal Sierpinski curve

Structure of the program

VB-Script in nanoCAD doesn’t have any input function. The script starts running from the first lines and works to the end. The main program module is quite simple – a single iteration. Let’s start with it:
 
Code in LISP
Code in VBScript
( setq h ( div *SquareSize* 4 ) )
( setq x0 ( div *MaxX* 2 ) )
( setq y0 ( + ( div *MaxY* 2 ) h ) )
do (( i 1 ))
     (( eql i ( + Count 1 ) ) 'Done )
     ( setq x0 ( - x0 h ) )
     ( setq h ( div h 2 ) )
     ( setq y0 ( + y0 h ) )
     ( setq Px x0 Py y0 )
     ( A i )
     ( Line 1 h )
     ( B i )
     ( Line 3 h )
     ( C i )
     ( Line 5 h )
     ( D i )
     ( Line 7 h )
     ( setq i ( + i 1 )) )

h = SquareSize \ 4
x0 = MaxX \ 2
y0 = h + MaxY \ 2

for i = 1 to count
     x0 = x0 - h   
     h = h \ 2    
     y0 = y0 + h    
     Px = x0    
     Py = y0    
     A(i)    
     Line 1, h    
     B(i)    
     Line 3, h    
     C(i)    
     Line 5, h    
     D(i)    
     Line 7, h
next


From the code we can see that the Sierpinski curve is drawn by four recursive functions: A(), B(), C(), D(). There is also the special Line (Direction, Size) function which draws a line on the necessary scale and at the required angle. Definitions of these functions are realized by VBS tools, for example, the A()-function will be described in LISP and VBS as:
 
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 ) ) 
) ) 

Sub A(k) 
    if k > 0 then 
        A(k-1) 
        Line 1, h 
        B(k-1) 
        Line 0, 2 * h 
        D(k-1) 
        Line 7, h 
        A(k-1) 
    End If 
End Sub 

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() or MsgBox()), 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:

Dim ms
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:

Dim ut
Set ut = ThisDrawing.Utility
ut.Prompt("Hello, world!")


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:

Do
    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)
Next

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: 
 
Dim ms
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")
...
case 5:
    Set oLyr = oLyrLine1
case 6:
    Set oLyr = oLyrLine2
...
oLine.Layer = oLyr.Name

The result

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 :-) 
Copyright 2017 Nanosoft. All Rights Reserved. Privacy policy  |  Terms of use  |  Forgot password?
Follow us: