AutoLISP / Visual LISP

HomeHome BlogBlog TwitterTwitter YouTubeYouTube ContactContact
   
 

Free AutoLISP and Visual LISP code snippets for AutoCAD

Please feel free to be inspired, cut&paste or if you have any feedback, questions or looking for an AutoLISP programmer for small or large projects go here. If you want some customization or anything else that you can come up with that we might help you with you're welcome to contact us.

Want to learn LISP for AutoCAD? The Visual LISP Developer's Bible, 2011 Edition

AutoLISP / Visual LISP

AutoLISP is based on the LISP programming language.  Because AutoCAD has a built-in LISP interpreter, you can enter AutoLISP code at the command prompt or load AutoLISP code from external files. Visual LISP (VLISP) is a software tool designed to expedite AutoLISP program development. AutoLISP and Visual LISP is often used mutually but AutoLISP is only a subset of Visual LISP. Visual LISP was incorporated into AutoCAD 2000 released in March 1999, as a replacement for AutoLISP.

Visual LISP technology is a tool for code creation in the AutoCAD software application. It is a full-featured, interpretive programming language that you can use to call AutoCAD commands, system variables, and dialog boxes. Visual LISP offers a complete development environment, including

  • Reduced development time using the integrated development environment (IDE), which makes it easier and faster for users and developers alike to create, debug, and deliver AutoLISP-based applications
  • Access to Microsoft ActiveX objects and event reactors
  • Source code protection against theft and alteration
  • Operating system file-operation functions
  • LISP function extensions for list processing

Miscellaneous tips and trix for AutoCAD AutoLISP

; AutoLISP function to convert degrees to radians
(defun dtr (a)
  (* pi (/ a 180.0))
)

; AutoLISP function to convert radians to degrees
(defun rtd (r)
  (* 180.0 (/ r pi))
)

; Example on how to remove the last element or item in a list
(setq lst '(1 2 3 4))
(setq lst (reverse (cdr (reverse lst))))
; result is (1 2 3)

; Example on how to right rotate a list 1 step
(setq lst '(1 2 3 4))
(setq lst (append (list (last lst)) (reverse (cdr (reverse lst)))))
; result is (4 1 2 3)

; Example on how to left rotate a list 1 step
(setq lst '(1 2 3 4))
(setq lst (append (cdr lst) (list (car lst))))
; result is (2 3 4 1)

Visual LISP example how to delete all empty layouts (tabs). If the layout has been activated it will contain a viewport object and will not be deleted by this function.

In previous version (= nil (ssget "x" (list (cons 410 (vla-get-name layout))))) was used but did not catch layouts where the viewport object has been deleted. vla-get-count will return 0 for non activated layouts and 1 for a layout with no objects.

(defun c:DeleteEmptyLayouts (/ layouts layout i)
  (vl-load-com)
  (setq i 0 layouts (vla-get-Layouts (vla-get-activedocument (vlax-get-acad-object))))
  (if (> (vla-get-count layouts) 2)
    (vlax-for layout layouts
      (if (= (vla-get-ModelType layout) :vlax-false)
        (if (< (vla-get-count (vla-get-block layout)) 2)
          (progn
            (princ (strcat "\n" (vla-get-name layout) " deleted"))
            (setq i (1+ i))
            (vla-delete layout)
          )
        )
      )
    )
  )
  (cond
    ((> i 1) (princ (strcat "\n" (itoa i) " layouts deleted")))
    ((= i 0) (princ "\nNo layouts deleted"))
  )
  (princ)
)

Purge layer filters / delete layer filters in AutoCAD. You get it here at my blog or here.

; To set the a custom scale, first set the StandardScale property to acVpCustomScale,
; then use the property CustomScale to define the custom scale value.
; Code examples to play with follow


(setq al (vla-get-ActiveLayout (vla-get-activedocument (vlax-get-acad-object)))
(vla-get-CustomScale al)
(vla-put-StandardScale al acVpCustomScale)
(vla-put-StandardScale al acVpScaleToFit)
(vla-put-StandardScale al ac1_10)
(setq numerator 1)
(setq denominator 25)
(vla-setCustomScale al numerator denominator)
(vla-getCustomScale al 'numerator 'denominator)
(princ numerator)
(princ denominator)

; The is how you can do a PGP file reinitialization (reload)awing template file used by QNEW
;;; (ax:GetQnewPath)
(defun ax:GetQnewPath (/ prof key)
  (setq prof (vla-get-ActiveProfile (vla-get-profiles (vla-get-preferences (vlax-get-Acad-Object)))))
  (setq key (strcat "HKEY_CURRENT_USER\\" (vlax-product-key) "\\Profiles\\" prof "\\General"))
  (vl-registry-read key "QnewTemplate")
)

;;; Set the drawing template file used by QNEW
;;; This writes it to the registry but is later dismissed and overwritten by AutoCAD
;;; (ax:SetQnewPath "M:\\CAD\\ACADISO.DWT")
(defun ax:SetQnewPath (QnewPath / prof key)
  (setq prof (vla-get-ActiveProfile (vla-get-profiles (vla-get-preferences (vlax-get-Acad-Object)))))
  (setq key (strcat "HKEY_CURRENT_USER\\" (vlax-product-key) "\\Profiles\\" prof "\\General"))
  (vl-registry-write key "QnewTemplate" QnewPath)
)

;; Gets current template
(getenv "QnewTemplate")
"c:\\my files\\templates\\qnew.dwt"

and

;; Sets a different template
(setenv "QnewTemplate" "c:\\my files\\templates\\qnew.dwt")

or another approach because the above one is not working in newer versions of AutoCAD:

Using VBA where QnewPath is the Template File Name: ThisDrawing.Application.Preferences.Files.QNewTemplateFile = QnewPath

Using LISP: (vla-put-QNewTemplateFile (vla-Get-Files (vla-Get-Preferences (vlax-get-acad-object))) QnewPath)

This is how you can preset the Path type in the xref attach dialog box
;;; Sets the Xref Path type used in the xref attach dialog box
;;; Absolute Path: (SetPathType 0)
;;; Relative Path: (SetPathType 1)
;;; No Path: (SetPathType 2)

(defun SetPathType (v)
  (vl-load-com)
  (vl-registry-write
    (strcat
      "HKEY_CURRENT_USER\\"
      (vlax-product-key)
      "\\Profiles\\"
      (vla-get-activeprofile
	(vla-get-profiles
	  (vla-get-preferences (vlax-get-acad-object))
	)
      )
      "\\Dialogs\\XattachDialog"
    )
    "PathType"
    v
  )
)

;;; Gets the Xref Path type used in the xref attach dialog box
;;; 0 = Absolute Path
;;; 1 = Relative Path
;;; 2 = No Path

(defun GetPathType ()
  (vl-load-com)
  (vl-registry-read
    (strcat
      "HKEY_CURRENT_USER\\"
      (vlax-product-key)
      "\\Profiles\\"
      (vla-get-activeprofile
	(vla-get-profiles
	  (vla-get-preferences (vlax-get-acad-object))
	)
      )
      "\\Dialogs\\XattachDialog"
    )
    "PathType"
  )
)

 
© 2001-2014 JTB World. All rights reserved.