A foray into AutoLISP

I was recently asked by a friend to write a couple of AutoLISP routines that would offset a polyline in both directions, and change a few styles along the way. The problem is easiest defined visually:


This was to be my second experience writing lisp (after my experience with GIMP scripts), so I knew the language concepts and syntax already.  Initially I tried to get away with command macros; unfortunately these have no ability to store objects (as far as I could tell) and if you can't do what you want by running a sequence of AutoCAD commands it seems you're pretty much out of luck.

The entire function is as follows:

;;double offset with offset line style change
(defun C:doffset(/ pickEnts pickEnt offset i n)
  (if (setq pickEnts (ssget '((0 . "LWPOLYLINE"))))       ;select polylines
    (setq offset(getreal "\n Offset: "))                  ;store offset
    (setq i 0 n (sslength pickEnts))
    (while (< i n)
     (setq  pickEnt (ssname pickEnts i))                  ;get the next polyline from the selection
     (setq i (1+ i))
     (setq pickObj (vlax-EName->vla-Object pickEnt))   ;convert entity to object
     (vla-Offset pickObj offset)                          ;offset in one direction
     (command "_change" "l" "" "p" "LT" "ZIGZAG" "")
     (vla-Offset pickObj (- offset))                      ;offset in the other direction
     (command "_change" "l" "" "p" "LT" "ZIGZAG" "")
     (command "_change" pickEnt "" "p" "LT" "ACAD_ISO07w100" "")
     (command "_pedit" pickEnt "w" (* offset 2) "")       ;change the pline width (twice the original offset)
   )                                                      ;progn
   (princ "No object selected!\n")                      
  )                                                       ;endif
  (princ)                                                 ;clean exit (supresses echo)

All in all this lisp business seems to be a quick and easy way to automate some of the more tedious AutoCAD jobs.

| July 12th, 2014 | Posted in Software |

Leave a Reply