Useful AutoCAD LISP Routines
Here are some handy AutoCAD LISP routines that can save you time and improve your efficiency while working with AutoCAD. Copy the code for each function, paste it into your LISP editor, and use them to enhance your AutoCAD experience.
1. Layer Manager: Create Layers Quickly
This routine can create a new layer and apply certain properties, such as color and line type, without going through the Layer Properties Manager.
(defun c:CreateLayer ()
(setq layerName (getstring "\nEnter the layer name: "))
(setq colorChoice (getint "\nEnter color number (1-255): "))
(setq linetypeChoice (getstring "\nEnter line type (e.g., Continuous): "))
(command "._LAYER" "M" layerName "C" colorChoice "L" linetypeChoice "") ; Create the layer with color and linetype
(princ (strcat "\nLayer " layerName " created with color " (itoa colorChoice) " and line type " linetypeChoice))
(princ)
))
This will create a new layer with a specified name, color, and line type. You can modify the parameters to suit your preferences.
2. Quick Dimensioning (Auto-dimension)
Automatically applies dimensions to the selected objects. This is useful for quick drafts where precise manual dimensioning is not a priority.
(defun c:QuickDim ()
(setq obj (ssget '((0 . "LINE")))) ; Select only lines
(command "_DIMLINEAR" obj)
(princ "\nDimensions applied to selected objects.")
(princ)
))
This routine will dimension all selected lines quickly. It can be adjusted for more complex dimensioning styles or other object types.
3. Quick Zoom to Selected Object
A simple routine that zooms to the selected object, making it easier to focus on a specific part of the drawing.
(defun c:ZoomToObject ()
(setq obj (ssget))
(if obj
(progn
(command "ZOOM" "W" (vlax-curve-getstartpoint (ssname obj 0)) (vlax-curve-getendpoint (ssname obj 0)))
(princ "\nZoomed to selected object."))
(princ "\nNo object selected."))
(princ)
))
This zooms the view to fit around the selected object, helping you focus on your area of interest quickly.
4. Purge Unused Layers
Sometimes drawings accumulate unnecessary layers. This routine purges layers that are not in use to clean up the drawing and make it more manageable.
(defun c:PurgeUnusedLayers ()
(setq layers (tblsearch "LAYER" "*"))
(foreach layer layers
(if (not (tblsearch "LAYER" (cdr (assoc 2 layer)))) ; If layer is not used
(command "_-LAYER" "D" (cdr (assoc 2 layer)))) ; Delete unused layer
)
(princ "\nUnused layers purged.")
(princ)
))
This cleans up any unused layers from your drawing.
5. Add Prefix to Block Names
This will add a specific prefix to selected block names in the drawing, which can help organize or standardize your drawing elements.
(defun c:AddPrefixToBlocks ()
(setq prefix (getstring "\nEnter the prefix: "))
(setq ss (ssget '((0 . "INSERT")))) ; Select all blocks
(if ss
(progn
(setq n (sslength ss))
(repeat n
(setq blk (ssname ss (setq i (1+ i))))
(setq blkName (vla-get-Name (vlax-ename->vla-object blk)))
(vla-put-Name (vlax-ename->vla-object blk) (strcat prefix blkName))
)
(princ (strcat "\nAdded prefix '" prefix "' to all block names."))
)
(princ "\nNo blocks selected.")
)
(princ)
))
This is useful for renaming blocks with a standard naming convention.
6. Create a Rectangle from 2 Points
A quick way to create a rectangle from two user-selected points. This can save time when you need to create basic rectangular shapes.
(defun c:CreateRectangle ()
(setq pt1 (getpoint "\nSelect first corner of rectangle: "))
(setq pt2 (getpoint "\nSelect opposite corner of rectangle: "))
(command "_RECTANGLE" pt1 pt2)
(princ "\nRectangle created.")
(princ)
))
This quickly creates a rectangle between two user-defined points.
7. Insert a Block with Scale and Rotation Options
Insert a block at a specific point, with a custom scale and rotation.
(defun c:InsertBlockCustom ()
(setq blockName (getstring "\nEnter the block name: "))
(setq insertPoint (getpoint "\nPick insertion point: "))
(setq scaleFactor (getreal "\nEnter scale factor: "))
(setq rotationAngle (getangle "\nEnter rotation angle: "))
(command "_INSERT" blockName insertPoint scaleFactor scaleFactor rotationAngle)
(princ "\nBlock inserted with custom settings.")
(princ)
))
This lets you insert a block at any point, applying a custom scale and rotation.
8. Quick Block Cleanup
Automatically cleans up or removes duplicate blocks or lines, helping to reduce clutter in the drawing.
(defun c:CleanBlocks ()
(setq blockSelection (ssget '((0 . "INSERT"))))
(if blockSelection
(progn
(setq numBlocks (sslength blockSelection))
(repeat numBlocks
(setq currentBlock (ssname blockSelection (setq i (1+ i))))
(if (tblsearch "BLOCK" currentBlock) ; Check if the block exists in the drawing
(command "DELETE" currentBlock)
)
)
(princ "\nCleaned up duplicate blocks."))
(princ "\nNo blocks found to clean up."))
(princ)
))
This helps to quickly delete duplicate blocks or clutter from a drawing.