Skip to main content
  1. ✍️ Blog/

Writing AutoLISP With ChatGPT

·1 min
civ-eng chatgpt lisp autocad
Table of Contents

Prompt #

Write code for autocad lisp command for total length

Code Output #

(defun c:totallength (/ ss len)
  (setq ss (ssget))
  (setq len 0.0)
  (if ss
      (setq nents (sslength ss))
      (repeat nents
        (setq ent (ssname ss (setq i (1- i))))
        (setq len (+ len (vla-get-length 
        (vlax-ename->vla-object ent))))
      (princ (strcat "\nTotal length: " (rtos len)))
    (princ "\nNo objects selected.")

Instructions from ChatGPT #

This command defines a new function called totallength and then defines two variables: ss to store the selection set and len to store the total length of the selected objects.

The function first uses the ssget function to prompt the user to select objects. If objects are selected, it then loops through each object in the selection set and adds the length of each object to the len variable. Finally, it prints the total length of the selected objects using the princ function.

To use this command, simply load the Lisp file in AutoCAD and type totallength at the command prompt.