View Full Version : VBA Statements in LISP
peter
2006-12-07, 04:52 PM
Hey Group,
Have any of you ever thought that it would be nice to encorporate
vba statements into your lisp code?
Ever thought about how you would do it?
I woke up this morning with it on my mind and was thinking....
Any thoughts?
Peter
abdulhuck
2006-12-10, 03:01 PM
Hey Group,
Have any of you ever thought that it would be nice to encorporate
vba statements into your lisp code?
Ever thought about how you would do it?
I woke up this morning with it on my mind and was thinking....
Any thoughts?
Peter
Peter,
You think very practically, but I was thinking crazy that not only VBA statements, but VBA forms also should be made available from Vlisp. We have already the facility to embed a VBA project within a VLisp project. But what about the communication between these two? It must be possible, because I have seen some example with dynamic link library (dll). If communication with VBA forms are also possible from Vlisp, the life would be much easier. Thanks for firing such a thought...
Regards,
Abdul Huck
arshiel88
2006-12-10, 07:50 PM
I don't know much about AutoLisp but I do know that It can send a standard command via 'command' statement just like a Utility.SendCommand method in VBA.
And for the fact that the standard command vbastmt do actually prompts for a VBA statement such as....
(command "vbastmt" "frmMyForm.show")
but of course it has its limitations and I assume these limitations can be done or with a substitute within the parent language, in this case AutoLisp™.
peter
2006-12-10, 07:51 PM
Yes Forms would make it nice.
There is a lot of activity in discussing the ObjectDCL forms for lisp which are similar to the VBA forms.
I wrote a simple function that converts very simple VBA statments into lisp statements and then evaluates them
Like (translateVBA "Thisdrawing.blocks.item(0).item(0)") would return the first item in modelspace.
I thought it was useful, if you are using autocomplete in the vbaide to create object calls.
Peter
see attached code.
; Written By: Peter Jamtgaard copr 2006
; This routine translates "simple" vba statements into visual list and
; evaluates them like (translatevba "thisdrawing.blocks.item(0)")
; will return the first item in the blocks collection. make sure to
; not have any spaces in the expressions.
(defun VBA (strVBAStatement)(translateVBA strVBAStatement))
(defun TranslateVBA (strVBAStatement ; String VBA Statement
/
lstStatement ; List of properties and methods
lstStatement2 ; Returned lisp expression
strItem ; String Item in list of M and P
strObject ; Base object for functions
)
(setq strVBAStatement (replaceSubStrings " " "" strVBAStatement))
(setq lstStatement (mapcar 'strcase (csvstringtolist strVBAStatement ".")))
(if (= (strcase (setq strObject (car lstStatement))) "THISDRAWING")
(setq lstStatement2 (quote (vlax-get (vlax-get-acad-object) "Activedocument")))
(setq lstStatement2 (read strObject))
)
(if (cdr lstStatement)
(foreach strItem (cdr lstStatement)
(if (or (vl-string-search "(" strItem 0)
(vlax-method-applicable-p (eval lstStatement2) strItem)
)
(setq lstStatement2 (append (list (read "vlax-invoke")) (list lstStatement2))
lstStatement2 (append lstStatement2
(if (vl-string-search "(" strItem 0)
(GetMethodArguments strItem)
(list strItem)
)
)
)
(setq lstStatement2 (append (list (read "vlax-get")) (list lstStatement2))
lstStatement2 (append lstStatement2 (list strItem))
)
)
)
)
(eval lstStatement2)
)
; Converts the aguments inside the parenthesis of the vba statement
; into a list of arguments
(defun GetMethodArguments (strItem
/
lstItems
strItems
strMethod
)
(setq lstItems (vl-string->list strItem)
strMethod (vl-list->string (reverse (cdr (member 40 (reverse lstItems)))))
strItems (vl-list->string (member 40 lstItems))
strItems (replaceSubStrings "," " " strItems)
)
(setq lstItems (append (list strMethod) (read strItems)))
)
(defun ReplaceSubStrings (strPattern strNewString strItem / intPosition)
(setq intPosition 0)
(while (setq intPosition (vl-string-search strPattern strItem intPosition))
(setq strItem (vl-string-subst strNewString strPattern strItem intPosition))
)
strItem
)
; Parsing a textstring to a list.
(defun CSVStringToList (strText strChar / intPosition lstStrings)
(while (setq intPosition (vl-string-search strChar strText 0))
(setq lstStrings (cons (substr strText 1 intPosition) lstStrings)
strText (substr strText (+ intPosition 1 (strlen strChar)))
)
)
(reverse (cons strText lstStrings))
)
(princ)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.