Monday, July 24, 2006

Autocad programming in visual lisp.

Autocad visual lisp has made available all (or maybe most) functionality of autocad exposed through the activex and vba interface. However the interface so provided exposes a lot of internal data structures probably for the sake of efficiency but make the API less productive for direct use. One should build more lisp-like abstraction on the top of the visual lisp API, so that lists can be used for most things (instead of idiosyncratic "safe-arrays" and "variants"). There is a nice book titled "Visual Lisp: a guide to artful programming" by Phil Kreiker that presents such an abstraction layer. The software accompanying the book implements the abstraction layer.
Following are some usage examples of the artful programming API
;; setting and getting a variable that would be persistent with the drawing file.

(defun setp (varname value)
(eval (list 'setq varname (list 'quote value) ))
(ap-putdict varname))

(defun getp (varname)
(ap-getdict varname))

;; getting and setting a external string-valued property associated with an object

(defun set-prop (obj prop propvalue)
(regapp prop)
(apa-SetXData obj (list (cons 1001 prop) (cons 1000 propvalue))))

(defun get-prop (obj prop / xdata)
(setq xdata (apa-GetXData obj prop))
(cdr (assoc 1000 xdata)))
;;; aforementioned are two important primitives for building custom applications.

0 Comments:

Post a Comment

<< Home