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:

    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 codeVBS Code
    ( 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
        Line 1, h
        Line 3, h
        Line 5, h
        Line 7, h

    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 codeVBS Code
    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
            Line 1, h
            Line 0, 2 * h
            Line 7, h
        End If
    End Sub

    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
    Set ms = ThisDrawing.ModelSpace

    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
    Set ut = ThisDrawing.Utility
    ut.Prompt("Привет, мир!")

    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:
        count = ThisDrawing.Utility.GetInteger("Задайте положительное число (>=1)")
    Loop While count <= 0

    For i = 1 to count
        point = ThisDrawing.Utility.GetPoint("0,0,0", "Укажите точку №" & i)

    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 ( , section of the Developer Center), or the Open Design Alliance ( , 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
    Set ptStr = CStr(x)+","+CStr(y)+","+CStr(y)

    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("Связи")
    Set oLyrLine2 = ThisDrawing.Layers.Add("Квадраты")
    case 5:
        Set oLyr = oLyrLine1
    case 6:
        Set oLyr = oLyrLine2
    oLine.Layer = oLyr.Name


    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 :-)

    Also popular now: