Using ChatGPT to Write AutoCAD LISP Routines: From Idea to Execution

Applies to:
Civil 3D

Issue:
Creating AutoCAD LISP routines can save significant time in drafting and design workflows, but writing them from scratch requires programming knowledge not all CAD users possess. Whether it's automating repetitive tasks, modifying geometry, or creating custom commands, many engineers and designers are unsure how to start—or what to do when something goes wrong.

Manually learning LISP programming is the ideal path, but can be time-consuming, and online examples are often overly generic or outdated. This is where ChatGPT can offer a fast and iterative solution to prototype, generate, and troubleshoot custom LISP code.

 

Solution:
Using ChatGPT to assist in writing LISP routines provides a guided, conversational way to build automation scripts even if you're not a programmer. Here’s a step-by-step example of how to go from concept to implementation:

 

Step 1 – Define Your Task Clearly

Let’s say you want to create a LISP that places blocks at all COGO point locations in a Civil 3D drawing.

Start with a clear, detailed prompt:

“Write an AutoLISP routine that loops through all COGO points in the drawing and inserts a custom block (named ‘PointSymbol’) at each point location, using the point’s elevation as the block's Z value.”

 

Step 2 – Generate Code Using ChatGPT

ChatGPT might return something like:

lisp

CopyEdit

(defun c:InsertBlocksAtPoints (/ acadApp doc points point blkName)

  (vl-load-com)

  (setq acadApp (vlax-get-acad-object))

  (setq doc (vla-get-ActiveDocument acadApp))

  (setq blkName "PointSymbol")

 

  (defun get-COGO-points ()

    (vlax-for obj (vla-get-ModelSpace doc)

      (if (and (= (vla-get-ObjectName obj) "AeccDbCogoPoint"))

        (cons obj points)

      )

    )

    points

  )

   (setq points (get-COGO-points))

   (foreach pt points

    (setq insPt (vlax-get pt 'Location))

    (command "_-insert" blkName insPt 1 1 1 0)

  )

  (princ)

)

 

Step 3 – Test in AutoCAD

  1. Open AutoCAD and use the APPLOAD command to load your .lsp file.
  2. Run the command by typing InsertBlocksAtPoints in the command line.
  3. If successful, blocks will appear at COGO point locations.

 

Step 4 – Troubleshoot

If you receive an error like:
; error: bad argument type: lentityp nil

You can copy the error and paste it back into ChatGPT with the original LISP code and say:

“I’m getting this error when I run the routine: bad argument type: lentityp nil. What could be wrong?”

ChatGPT may respond:

“It looks like the points list is nil because the get-COGO-points function isn’t building the list correctly. Try initializing the points list before the loop.”

Then ChatGPT will provide a corrected version:

lisp

CopyEdit

(defun get-COGO-points ()

  (setq points '()) ; initialize the list

  (vlax-for obj (vla-get-ModelSpace doc)

    (if (= (vla-get-ObjectName obj) "AeccDbCogoPoint")

      (setq points (cons obj points))

    )

  )

  points

)

 

Step 5 – Iterate for Improvement

You can also ask ChatGPT to:

  • Add error handling if no points are found.
  • Prompt the user for block name.
  • Scale block by point description or elevation.

 

Previous Article
Fix Civil 3D PDF plotting issues with Profile reset
Fix Civil 3D PDF plotting issues with Profile reset

Users sometimes encounter problems when plotting to PDF in Civil 3D.

Next Article
"Reference Text" Feature in Civil 3D Labels
"Reference Text" Feature in Civil 3D Labels

Elevate Label Clarity with the "Reference Text" Feature in Civil 3D Labels

Civil 3D Classes Now Available

Learn More