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 or questions 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&trix for AutoCAD AutoLISP

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

(defun c:DeleteEmptyLayouts (/ layout)
  (vl-load-com)
  (vlax-for layout (vla-get-Layouts (vla-get-activedocument (vlax-get-acad-object)))
    (if (= (vla-get-ModelType layout) :vlax-false)
      (if (= nil (ssget "x" (list (cons 410 (vla-get-name layout)))))
        (vla-delete layout)
      )
    )
  )
  (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-2012 JTB World. All rights reserved.