📜 ⬆️ ⬇️

The first steps into the world of programming under nanoCAD

When free nanoCAD 2.0 was released last November, we said that the release version already has application development tools. In particular, for the majority 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 seize this opportunity? :-)
I propose to make joint first steps into the world of programming under nanoCAD and create a small program that interacts with the user via the command line and automatically draws lines in a given order on the working drawing nanoCAD. Starting from this script and giving free rein to your imagination, you can make 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/

Formulation of the problem


In what language is it not important to write the executed script: nanoCAD supports both Java script and Visual Basic Script. For this article, I chose VB, since lately I have been using it more often.
When I reflected on the basis of what to demonstrate the work of the VB script, I had a desire to translate any LISP script to a VB script, demonstrating in such a way that this is possible. Searching the Internet, I came across a course project “ Developing a LISP program for constructing Sierpinski’s i-th order curves ”. It contains a fairly detailed algorithm that explains how to use the recursive functions to construct the figure shown in Figure 1. Let's do the same, but on VBS.
Figure 1. The 4th order Sierpinski curve, by the example of which we consider the principles of programming under nanoCAD.


Program structure


VBS-program under nanoCAD does not imply any input function - the script starts to run 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
A(i)
Line 1, h
B(i)
Line 3, h
C(i)
Line 5, h
D(i)
Line 7, h
next


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 function Line (Direction, Size), which simply draws a regular line at the desired scale and angle. The definitions of these functions are performed by conventional means of VBS — for example, function A () will look like this on LISP and VBS:
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
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


These additional five functions are placed 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.

The first steps


First, the VB script is a plain text file with the .vbs extension. It can be invoked in the nanoCAD environment using the VBS command (see Figure 2, respectively, the Java script using the JS command). Everything is simple - we specify the path to the script and, if it is written without errors, then it is executed. If errors are made 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 feel free to 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 especially I will not teach it :-)
To access the current open drawing, use ThisDrawing. Moreover, if we plan to draw in the model space (ie, in the main drawing, the Model tab), then we define the ModelSpace collection through the following structure:
Dim ms
Set ms = ThisDrawing.ModelSpace

Access to the command line is via the Utility object. In particular, the following script 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:
Do
count = ThisDrawing.Utility.GetInteger(" (>=1)")
Loop While count <= 0

For i = 1 to count
point = ThisDrawing.Utility.GetPoint("0,0,0", " №" & i)
Next

Knowing where to display the 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 defined by the DWG file structure: creating a line, arc, circle, hatching, obtaining a point, polar coordinates, operations with opening / closing a file, working with layers, variables, etc. These functions are usually described in the DWG SDK documentation: either Autodesk ( www.autodesk.com , Developer Center section), or the Open Design Alliance ( http://www.opendwg.org/ , DWGdirect section).
We will actively use the AddLine (startpoint, endpoint) function, 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:
Dim ms
Set ptStr = CStr(x)+","+CStr(y)+","+CStr(y)

Even in my script, I decided to lay out the lines in 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

Result


This is what we get as a result - see figure 3. Of course, this result does not give practical utility (except aesthetic pleasure), but gives 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, on C: \, open in a text editor, study and run for execution in nanoCAD 2.0. And a little advice: do not set the order of the curves above 4 - hang :-)

Source: https://habr.com/ru/post/86970/


All Articles