Tuto

Introduction to AutoLISP: Creating a Custom Line Drawing Command in AutoCAD

To learn AutoLISP, you can use theses programs as a starting point. In this tutorial, you can learn the following concepts:

  • Defining functions: Functions are the basic building blocks of AutoLISP programs. They allow you to encapsulate code into reusable units.
  • Declaring variables: Variables are used to store data in AutoLISP programs.
  • Prompting the user for input: The getpoint function can be used to prompt the user for input.
  • Drawing objects: The command function can be used to draw objects in AutoCAD.

Example 1 of a simple AutoLISP program

Here’s a simple AutoLISP program that will draw a line in AutoCAD from user-specified points:

(defun c:DrawMyLine (/ p1 p2)
   (princ “\nSelect first point: “)
   (setq p1 (getpoint))

  (princ “Select second point: “)
  (setq p2 (getpoint p1))

  (command “line” p1 p2 “”)

  (princ)
)

Explanation of the program, line by line:

  1. (defun c:DrawMyLine (/ p1 p2)
    This defines a new function named DrawMyLine. The c: prefix means that this function can be called directly from the AutoCAD command line. The (/ p1 p2) part is a declaration of local variables p1 and p2.
  2. (princ “\nSelect first point: “)
    This line outputs the string “Select first point: ” to the AutoCAD command line. The \n is a newline character to make the output look cleaner.
  3. (setq p1 (getpoint))
    This prompts the user to select a point in the drawing. The selected point’s coordinates are stored in the p1 variable.
  4. (princ “Select second point: “)
    This outputs the string “Select second point: ” to the AutoCAD command line.
  5. (setq p2 (getpoint p1))
    This prompts the user to select a second point in the drawing. The function getpoint can take a previous point as an argument, which gives the user a rubber band line effect from the first point to the current cursor position. The coordinates of the second point are stored in the p2 variable.
  6. (command “line” p1 p2 “”)
    This calls the built-in line command of AutoCAD to draw a line from p1 to p2. The "" at the end acts as pressing “Enter” to finish the command.
  7. (princ)
    This is a neat way to exit the function quietly without returning any message.

To use this program:

  1. Load the AutoLISP code into AutoCAD.
  2. Type DrawMyLine into the AutoCAD command line and press Enter.
  3. Follow the prompts to select two points.
  4. A line will be drawn between the two selected points.

This is a basic introduction to creating a function in AutoLISP for AutoCAD. As you delve deeper into AutoLISP, you’ll discover more powerful and complex functionalities you can implement to automate and extend AutoCAD’s capabilities.

Example 2 of a simple AutoLISP program

Here’s a second simple AutoLISP program that draws a line between two points:

(defun c:drawline (/ x1 y1 x2 y2)

   (setq x1 (getpoint “Enter start point X: “))
   (setq y1 (getpoint “Enter start point Y: “))
   (setq x2 (getpoint “Enter end point X: “))
   (setq y2 (getpoint “Enter end point Y: “))

   (command “line” x1 y1 x2 y2)

   (princ “Line drawn”)

)

Explanation line by line:

  • Line 1: This line defines the function drawline. The c: prefix indicates that this is a command that can be invoked from the AutoCAD command line.
  • Line 2: This line declares the local variables x1, y1, x2, and y2. These variables will be used to store the coordinates of the start and end points of the line.
  • Lines 3-6: These lines use the getpoint function to prompt the user for the coordinates of the start and end points of the line.
  • Line 7: This line uses the command function to draw the line. The line argument specifies the type of line to draw. The x1, y1, x2, and y2 arguments specify the coordinates of the start and end points of the line.
  • Line 8: This line prints a message to the AutoCAD command line indicating that the line has been drawn.

Conclusion:

AutoLISP offers a robust platform for extending and customizing AutoCAD’s native capabilities. As seen with our simples DrawMyLine and drawline function, even beginners can start making AutoCAD work more closely to their specific needs.

The world of customization with AutoLISP is vast, and this is just a stepping stone. As you continue to explore and master AutoLISP, you’ll unlock even greater automation and customization potentials, optimizing your CAD workflows and elevating your design projects. Happy coding!