Autocad Help System for Lisp and VBA (ActiveX)

I have found the front end pages for Autocad programming languages VBA (ActiveX) and Lisp (Visual Lisp). Here is my big suggestion. You can copy and paste an entire page of links into Excel and the links transfer. Use Excel to collate and organize Autodesk help bookmarks.

Autodesk splits both subjects into two groups – what they call a Guide which would be teaching text and a Reference which is a page by page Index. Visual Lisp is traditional Autocad Lisp plus ActiveX. So if you are looking for Visual Lisp help, you need to include the ActiveX references in your reading list.

As of 2017, Autodesk has returned “Developer Documentation” to its front end for Autocad 2017 help,
http://help.autodesk.com/view/ACD/2017/ENU/

Following that link brings you to this page to be bookmarked,
http://help.autodesk.com/view/ACD/2017/ENU/?home=homepage_dev

If you copy and paste that page into excel, the links all transfer without any additional work. Here i have marked with asterisk the 5 critical pages.
2017-02-22_2

you can then make tabs for guide and reference for both lisp and activeX. In a few minutes you have an upper level table of contents in a single file. as you explore the pages using the live links you can make notes or mark as read. you can make a reference to the entire page at the top of your excel sheet. Pages come in formatted with good links, you might have to reset column width and row height for entire sheet.

2017-02-22_3

If you are looking for Visual Lisp code, it is in the ActiveX reference. Both groups ActiveX and Lisp have an Object Model page, but the Lisp page is not live, just a picture. The Object Model page for ActiveX has live links. You do not need to paste that page into Excel, but it also pastes with live links, and you never have to go looking for it. You can have the Object Model with links in your spread sheet. Regardless how you access it, it is a very valuable tool.

As an example of its usefulness, say you want to know how to draw a circle with Visual Lisp.

2017-02-22_4

Click on the Circle object. It takes you to Circle Object ActiveX help page. Which has the properties and methods of the object that is a circle, but it does not have the tool to draw a circle. The Addcircle is a method of ModelSpace, PaperSpace and Block. Click on ModelSpace, scroll down to the methods, and click on AddCircle. there you find the code to draw a circle in both VBA and Lisp. Code that you can paste into your VBA module or Lisp editor.

http://help.autodesk.com/view/ACD/2017/ENU/?guid=GUID-18ADF171-166F-4FF0-8ED6-5F83153A5649#GUID-18ADF171-166F-4FF0-8ED6-5F83153A5649

http://help.autodesk.com/view/ACD/2017/ENU/?guid=GUID-837C702F-91A7-445B-8713-3099B94664BE

the object model –

http://help.autodesk.com/view/ACD/2017/ENU/?guid=GUID-A809CD71-4655-44E2-B674-1FE200B9FE30

2017-02-22_6

* EDIT – unfortunately the ActiveX Reference page links did NOT copy from www into Excel. *
The other pages, lisp guide and reference, activex guide, and activex model links do work in excel.

and finally, autocad 2017 still installs two activex chm files on your hard drive with installation, the Guide and the Reference. You can also link to those.

2017-02-22_7

Advertisements

Functions

Capture_11-09-2015-1
f stands for function in the generalized notation y=f(x). The function is an equation that for any x or for any x in a specific interval returns a value for y. In the same way a vba function is a formulation that returns a value to be captured in a variable.

We have a good loop template to process equations, but whenever a new equation is to be used, the entire sub procedure is copied to a new name and just one line of code with the equation is changed. The goal is to reduce the duplicated code. If we write the equation into a function to return a value, we still need unique sub procedures to call the correct function, but putting the equations into functions to return a value then calling the function in the loop seems like a road that will go somewhere.

We will make the function name look like the equation it contains for now, but this will be too cumbersome to maintain. We will make an index using a spreadsheet to keep track of function names and their equations.

for our two rose petal equations
R = B * Sin (C*A) + D
R = B * Cos (C*A) + D

Function B_Sin_C_A_D(A_rad As Double) As Double
'R = B * Sin(C * A) + D
B_Sin_C_A_D = B * Sin(C * A_rad) + D
End Function

Function B_Cos_C_A_D(A_rad As Double) As Double
'R = B * Cos(C * A) + D
B_Cos_C_A_D = B * Cos(C * A_rad) + D
End Function

If we cannot call either of these functions from a single subprocedure, we have not reduced anything. We need a method to call a function by passing its name.

Application.Run

in the loop where we used to have
‘R = B * Sin(C * A_rad) + D
The only value on the right side of the equation that is changing each time through the loop is A_rad. To simplify things we make the other variables global so we do not have to pass them. A_rad is the radian conversion of A degrees, which is our changing interval variable since we are doing a polar equation. It corresponds to x in a rectangular equation.

we can now have
R = Application.Run(funcname, A_rad)

Sub petal(funcname As String)
Call connect_acad

Dim R As Double, A As Integer
Dim X As Double, Y As Double
Dim A_rad As Double
Dim i As Integer, numpts As Integer
Dim plineobj As AcadLWPolyline
Dim pt() As Double

numpts = (Amax - Amin) / A_inc 'num of lines
numpts = numpts + 1
ReDim pt(1 To numpts * 2)

For i = 1 To numpts
A = Amin + ((i - 1) * A_inc)
A_rad = deg2rad(A)

R = Application.Run(funcname, A_rad)

X = R * Cos(A_rad)
Y = R * Sin(A_rad)
pt(i * 2 - 1) = X: pt(i * 2) = Y
Next i

Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt)
Update
End Sub

The upper level calling program can now look like a recipe. This will change when we integrate this into a form. The variables that go into an actual graph are all made globals – Amin, Amax, A_inc, B, C, D, and funcname.

Sub call_petal()
'R = B * Sin(C * A) + D
funcname = "B_Sin_C_A_D"
Amin = 0
Amax = 360
A_inc = 1
B = 3
C = 5
D = 2

Call petal(funcname)
End Sub

This makes the whole structure more complicated, and detracts from the core graphing code, but something along this line seems necessary to reduce the multiplication of subprocedures which only differ by the function being graphed.

Capture_11-08-2015

First Daisy

Capture_11-05-2015

Spirals are drawn as either individual lines or a connected polyline. A polyline in autocad vba requires an array of points in the form (x1,y1,x2,y2,x3…). This is constructed with a loop that runs once for each point and adds an x and y value to an array each time through the loop. The polyline is drawn outside the loop with a single statement.

The spiral consisting of individual lines is drawn with a loop that runs once for each line. It stores values for two points x1,y1 and x2,y2 each time through the loop. It draws one line segment inside the loop each time through.

The fermat spiral is sometimes used to draw plant simulations using a bit of geometry at each node rather than a straight line. Instead of calling the line subroutine, any other geometry can be called inside the loop. These are sometimes called daisies or sunflowers.


Sub fermat_spiral_daisy1()
'R = B A^1/2
Call init_polar
Dim B As Double, C As Double
Dim i As Integer
Dim numlines As Integer
Dim R1 As Double, R2 As Double
Dim A1 As Integer, A2 As Integer
Dim A1_rad As Double, A2_rad As Double
Dim X1 As Double, X2 As Double
Dim Y1 As Double, Y2 As Double

B = frm_polar.txt_b8.Value
C = 0.5

numlines = (Amax - Amin) / A_inc 'num of lines

For i = 1 To numlines
A1 = Amin + ((i - 1) * A_inc)
A2 = Amin + (i * A_inc)

A1_rad = deg2rad(A1)
A2_rad = deg2rad(A2)

'this is the function
R1 = B * (A1_rad ^ C)
R2 = B * (A2_rad ^ C)

X1 = R1 * Cos(A1_rad)
Y1 = R1 * Sin(A1_rad)
X2 = R2 * Cos(A2_rad)
Y2 = R2 * Sin(A2_rad)

'this would be the regular spiral
'Call line(X1, Y1, X2, Y2)

 Call polygon3(R2, A2)

Next i

Update
End Sub


Sub polygon3(R As Double, A As Integer)
Call connect_acad

Dim X As Double, Y As Double
Dim plineobj As AcadLWPolyline
Dim pt() As Double
Dim t As Integer, numpts As Integer

numpts = 7
ReDim pt(1 To numpts * 2) 'to store both x and y for one pt

Dim AA As Double
Dim RR As Integer
RR = 3

For t = 1 To 7
AA = 2 * pi * t / 6 + deg2rad(A)

X = RR * Cos(AA) + R * Cos(A)
Y = RR * Sin(AA) + R * Sin(A)
pt(t * 2 - 1) = X: pt(t * 2) = Y
Next t

Set plineobj = acadApp.ActiveDocument.ModelSpace.AddLightWeightPolyline(pt)
plineobj.Closed = True
Update
End Sub

Polar Coordinates

Capture_10-28-2015

Capture_10-27-2015

in process


Sub star_polygon()
Call connect_acad

Dim i As Integer, m As Integer
Dim R1 As Double, R2 As Double
Dim A1 As Integer, A2 As Integer
 
Dim n As Integer 'number of line segments

R1 = 50
R2 = 50
n = 11
m = 7
 
For i = 1 To n
A1 = 360 * (i - 1) * m / n
A2 = 360 * i * m / n

Call line_polar(R1, A1, R2, A2)
Update
Next

Update
End Sub


Sub spiral_1()
Call connect_acad

Dim i As Integer
Dim R1 As Double, R2 As Double
Dim A1 As Integer, A2 As Integer

Dim A_min As Integer 'start in degrees
Dim A_max As Integer 'finish in degrees
Dim A_inc As Integer 'degree increment
Dim n As Integer 'number of line segments

A_min = 0
A_max = 1800
A_inc = 3
n = (A_max - A_min) / A_inc  'number of line segments
 
For i = 1 To n
A1 = A_min + ((i - 1) * A_inc)
A2 = A_min + (i * A_inc)
R1 = 0.125 * A1
R2 = 0.125 * A2
Call line_polar(R1, A1, R2, A2)
Next

Update
End Sub


Sub line_polar(R1 As Double, A1 As Integer, R2 As Double, A2 As Integer)
'a POLAR line wrapper to draw a line with one line of code
'A1 and A2 are input in degrees integer only for now
Dim lineobj As AcadLine
Dim pt1(0 To 2) As Double
Dim pt2(0 To 2) As Double

pt1(0) = R1 * Cos(deg2rad(A1))
pt1(1) = R1 * Sin(deg2rad(A1))
pt1(2) = 0

pt2(0) = R2 * Cos(deg2rad(A2))
pt2(1) = R2 * Sin(deg2rad(A2))
pt2(2) = 0

Set lineobj = acadDoc.ModelSpace.AddLine(pt1, pt2)
End Sub


Function deg2rad(deg As Integer) As Double
deg2rad = deg * pi / 180
End Function

Capture_10-27-2015-2

Cartesian Graphing

Locating points on a plane using a horizontal and vertical number line, giving each point a location with two numbers, then drawing lines between points, is called graphing with the Cartesian coordinate system. It is a link between algebra and geometry. Its named for Rene Descartes (“I think therefore I am”) who developed one of the first ideas along this line about 1637. Autocad is a Cartesian coordinate graphing system, although normally the lines are not input with algebraic equations.

Two more additions to our function library before we begin with a polar coordinate system.

Capture_10-25-2015-4

Capture_10-25-2015-2

Sub add_sin(a As Double, b As Double, c As Double, d As Double)
'Xmin, Xmax, X_inc already set
'Y = Sin(a * X) + Sin(b * X) + Sin(c * X) + d
Dim X As Double, Y As Double
Dim plineobj As AcadLWPolyline
Dim pt() As Double
Dim i As Integer, numpts As Integer

numpts = (Xmax - Xmin) / X_inc
numpts = numpts + 1
ReDim pt(1 To numpts * 2)

For i = 1 To numpts
X = Xmin + ((i - 1) * X_inc)
Y = Sin(a * X) + Sin(b * X) + Sin(c * X) + d
pt(i * 2 - 1) = X: pt(i * 2) = Y
Next i

Set plineobj = acadDoc.ModelSpace.AddLightWeightPolyline(pt)
Update
strLabel = "Y= Sin " & a & "X + Sin " & b & "X + Sin " & c & "X + " & d
End Sub

Capture_10-25-2015-5

Capture_10-25-2015-3

Sub exp_curve(Xmin As Double, Xmax As Double, a As Double, X_inc As Double)
'y = a^x
Dim X As Double, Y As Double
Dim plineobj As AcadLWPolyline
Dim pt() As Double
Dim i As Integer, numpts As Integer

numpts = (Xmax - Xmin) / X_inc 'number of line segments
numpts = numpts + 1  'one more pt than line segment
ReDim pt(1 To numpts * 2) 'store x and y for one pt

For i = 1 To numpts
X = Xmin + ((i - 1) * X_inc)
Y = a ^ X
pt(i * 2 - 1) = X: pt(i * 2) = Y
Next i

Set plineobj = acadApp.ActiveDocument.ModelSpace.AddLightWeightPolyline(pt)
Update
strLabel = "Y= " & a & "^X"
End Sub