0
0
SolidworksHow-ToBeginner · 4 min read

How to Create Custom Command in AutoCAD Quickly

To create a custom command in AutoCAD, use the CUI (Customize User Interface) editor to define a new command and assign it a name and macro. Alternatively, write an AutoLISP script and load it to run custom commands with specific actions.
📐

Syntax

Creating a custom command in AutoCAD involves two main methods:

  • CUI Editor: Define a command name, description, and macro that runs AutoCAD commands.
  • AutoLISP: Write a function with (defun) that performs actions, then load it and call the function as a command.

In CUI, the macro syntax uses ^C^C to cancel current commands and then runs commands with ; to separate them.

lisp
;; AutoLISP function syntax
(defun c:MyCommand ()
  (command "LINE" (getpoint "Start point: ") (getpoint "End point: ") "")
  (princ)
)
💻

Example

This example shows how to create a simple custom command named MyLine using AutoLISP that draws a line between two points.

After loading the script, typing MyLine in the command line will prompt for start and end points and draw the line.

lisp
(defun c:MyLine ()
  (command "LINE" (getpoint "Start point: ") (getpoint "End point: ") "")
  (princ)
)

; To load this, save as MyLine.lsp and use APPLOAD command in AutoCAD.
Output
User is prompted: Start point: (user clicks) User is prompted: End point: (user clicks) A line is drawn between the two points.
⚠️

Common Pitfalls

  • Forgetting to prefix AutoLISP commands with c: in the function name means the command won't be recognized in AutoCAD.
  • Not loading the LISP file before running the command causes "undefined command" errors.
  • In CUI macros, missing ^C^C at the start can cause commands to run unexpectedly if another command is active.
  • Using incorrect syntax in macros or LISP functions leads to errors or no action.
lisp
(defun MyLine () ; Missing c: prefix
  (command "LINE" (getpoint "Start point: ") (getpoint "End point: ") "")
  (princ)
)

; Correct way:
(defun c:MyLine ()
  (command "LINE" (getpoint "Start point: ") (getpoint "End point: ") "")
  (princ)
)
📊

Quick Reference

StepDescription
Open CUI EditorType CUI in command line and press Enter
Create New CommandRight-click Commands > New Command
Name CommandGive a unique name and description
Set MacroEnter macro like ^C^C_LINE; to run commands
Add to Ribbon/MenuDrag command to desired location
Save and TestSave changes and run command by name
AutoLISPWrite (defun c:CmdName () ...), load with APPLOAD

Key Takeaways

Use the CUI editor to create commands with macros for simple automation.
Write AutoLISP functions with the c: prefix to define custom commands.
Always load your AutoLISP scripts before running custom commands.
Start macros with ^C^C to cancel any active commands safely.
Test your commands carefully to avoid syntax errors and unexpected behavior.