Have a nice week-end!
You have to reference the VL.tlb file which is located in the AutoCAd
2000 root directory.
Then you can explore the object via the Object browser. Find below a
short VBA/VL sample on how to use this server to exchange data between
Lisp and VBA
The following is a small sample where you can see
how to get 'simple' values of
Lisp symbols and how to set them (something like
(setq a 100) or (setq a
"string")). The functions are called GetLispSym
and PutLispSym.
The functions GetLispList gets a Lisp list in VBA
and put its elements into a
variant array, and PutLispList creates a new Lisp
list from a variant array and
assigns the new list to a symbol. These two
function only handles 'simple' lists
like:
(100 1.234 T nil "string" ...)
They do not handle nested lists like:
((0 . "CIRCLE") (8 . "0") ...)
Note: before you can use the Automation Interface
of VisualLisp, you have to
load it. Just type in (vl-load-com) at the AutoCAD
command line to load and to
activate it.
Here are the VBA functions (don't forget to set
the Lisp symbols before you test
them):
Dim VL As Object
Sub test()
' Get the VisualLisp Automation interface
Set VL = CreateObject("VL.Application.1")
'
' Getting symbols and lists from Lisp
'
' Get the value of the Lisp symbol 'a'.
' Use (setq a 1234) in Lisp to set it.
a = GetLispSym("a")
Debug.Print "symbol a is: " & a
' Get the Lisp list 'b'. Use
' (setq b (list 100 1.234 "string"))
' in Lisp to define it.
b = GetLispList("b")
Debug.Print "list b contains:"
For index = LBound(b) To UBound(b)
Debug.Print b(index)
Next
'
' Setting Lisp symbols and lists
'
' Set the Lisp symbol 'c' to 100.0
Dim value As Double
value = 100#
PutLispSym "c", value
' Create the list
' ("VBA" 100 1.234)
' and store it in the Lisp symbol 'd'.
Dim values(0 To 2) As Variant
values(0) = "VBA"
values(1) = 100
values(2) = 1.234
PutLispList "d", values
End Sub
' This function returns the value of
' a Lisp symbol.
' The Lisp symbol can be set in Lisp like:
' (setq x 1234)
Function GetLispSym(symbolName As String) As
Variant
Dim sym As Object
' Get the Lisp symbol 'symbolName'.
Set sym =
VL.ActiveDocument.Functions.Item("read").funcall(symbolName)
GetLispSym =
VL.ActiveDocument.Functions.Item("eval").funcall(sym)
End Function
' This function gets a Lisp list
' and put its elements into a
' Variant array.
' The list can be something like:
' (100 1.234 "string")
Function GetLispList(symbolName As String) As
Variant
Dim sym As Object
Dim list As Object
' Get the Lisp symbol 'symbolName'.
Set sym =
VL.ActiveDocument.Functions.Item("read").funcall(symbolName)
Set list =
VL.ActiveDocument.Functions.Item("eval").funcall(sym)
' Get the number of elements in this list.
items =
VL.ActiveDocument.Functions.Item("length").funcall(list)
ReDim VarItems(0 To items - 1) As Variant
' Get every item from the list.
For I = 1 To items
Item =
VL.ActiveDocument.Functions.Item("nth").funcall(I - 1, list)
VarItems(I - 1) = Item
Next
GetLispList = VarItems
End Function
' This function creates a new Lisp symbol
'symbolName'.
' It gets the value specified by 'value'.
Sub PutLispSym(symbolName As String, value As
Variant)
Dim sym As Object
Set sym =
VL.ActiveDocument.Functions.Item("read").funcall(symbolName)
VL.ActiveDocument.Functions.Item("set").funcall
sym, value
End Sub
' This function creates a new Lisp list
' and assigns it to the symbol 'symbolName'.
' The 'values' parameter has to contain
' an array of variants, and this array
' is converted to the Lisp list:
' Array: "VBA" 100 1.234
' List: ("VBA" 100 1.234)
Sub PutLispList(symbolName As String, values As
Variant)
' Initialize the list using the first value.
Dim list As Object
Dim newList As Object
Set list =
VL.ActiveDocument.Functions.Item("list").funcall(values(0))
' Append the other items
For Item = LBound(values) + 1 To UBound(values)
' Create a new list for the next item...
Set newList =
VL.ActiveDocument.Functions.Item("list").funcall(values(Item))
' ...and append it to the existing list.
Set list =
VL.ActiveDocument.Functions.Item("append").funcall(list,
newList)
Next
' Assign the new list to 'symbolName'
Dim sym As Object
Dim res As Object
Set sym =
VL.ActiveDocument.Functions.Item("read").funcall(symbolName)
Set res =
VL.ActiveDocument.Functions.Item("set").funcall(sym, list)
End Sub
Other samples are available on teh ADN WEB site...
Cheers,
Cyrille Fauvel
Developer consulting Group
Autodesk