Help with OpenAI ChatGPT generated Autolisp code

Help with OpenAI ChatGPT generated Autolisp code

asimjian
Explorer Explorer
7,424 Views
10 Replies
Message 1 of 11

Help with OpenAI ChatGPT generated Autolisp code

asimjian
Explorer
Explorer

Hello All,

 

Precursor: I have very limited knowledge in lisp and how to debug lisp code.

 

I was able to use the new ChatGPT implementation on the OpenAI website to write the lisp code below:

 

 

 

;;; c:EQUALSPACE - Spaces a group of blocks evenly by a specified distance
;;;
;;; Usage:
;;;   1. Start the AutoCAD program and open a drawing.
;;;   2. Type "APPLOAD" on the command line and press Enter.
;;;   3. In the "Load/Unload Applications" dialog box, select "EQUALSPACE.LSP"
;;;      from the list and click the "Load" button.
;;;   4. Type "EQUALSPACE" on the command line and press Enter.
;;;   5. Follow the prompts to select the blocks and specify the distance.

(defun c:EQUALSPACE()
  "Spaces a group of blocks evenly by a specified distance"

    ;; Ask the user to select the blocks
    (if (not (setq blocks (entsel "Select blocks: ")))
        (princ "Oops! No blocks were selected. Try again.")

      ;; Ask the user to enter the distance
      (if (setq distance (getreal "Enter the distance to space the blocks: "))

          ;; Check that at least two blocks were selected
          (if (< (setq num_blocks (length blocks)) 2)
              (princ "Oops! You need to select at least two blocks. Try again.")

            ;; Calculate the total distance and the starting point
            (setq total_distance (* distance (- num_blocks 1)))
            (setq start_point (car (car blocks)))

            ;; Iterate over the blocks and move them to the appropriate locations
            (setq current_point start_point)
            (foreach block blocks
              (setq current_point (polar current_point (cadr block) total_distance))
              (command "move" block "" start_point (car current_point) (cadr current_point) 0)
            )
            (princ "EQUALSPACE completed successfully.")
          )
        )
      )
    )
)

 

However, I am getting the errors below before I could even test if the lisp works in practice.

 

These are the errors:

 

 

Regenerating model.
AutoCAD menu utilities loaded.*Cancel*
Command:
Command:
Command:
Command: AP
APPLOAD EQUAL_SPACE.lsp successfully loaded.
Command: ; error: syntax error
Command:
Command: EQUALSPACE
Unknown command "EQUALSPACE". Press F1 for help.

 

 

Any ideas why? Please explain like I am five years old.

Accepted solutions (1)
7,425 Views
10 Replies
Replies (10)
Message 2 of 11

Sea-Haven
Mentor
Mentor

1st error found.

 

 

(defun c:EQUALSPACE()
  "Spaces a group of blocks evenly by a specified distance"

(defun c:EQUALSPACE()
 ; "Spaces a group of blocks evenly by a specified distance"

 

Suggestion use \n for new line

 

(princ "\nEQUALSPACE completed successfully. ")

 

Suggestion 2 when using appload then run 1st time on load just add this as last line.

(c:EQUALSPACE)

  

0 Likes
Message 3 of 11

asimjian
Explorer
Explorer

Code modified as described by Sea Haven below:

;;; c:EQUALSPACE - Spaces a group of blocks evenly by a specified distance
;;;
;;; Usage:
;;;   1. Start the AutoCAD program and open a drawing.
;;;   2. Type "APPLOAD" on the command line and press Enter.
;;;   3. In the "Load/Unload Applications" dialog box, select "EQUALSPACE.LSP"
;;;      from the list and click the "Load" button.
;;;   4. Type "EQUALSPACE" on the command line and press Enter.
;;;   5. Follow the prompts to select the blocks and specify the distance.

(defun EQUALSPACE()
  ;"Spaces a group of blocks evenly by a specified distance"

    ;; Ask the user to select the blocks
    (if (not (setq blocks (entsel "Select blocks: ")))
        (princ "Oops! No blocks were selected. Try again.")

      ;; Ask the user to enter the distance
      (if (setq distance (getreal "Enter the distance to space the blocks: "))

          ;; Check that at least two blocks were selected
          (if (< (setq num_blocks (length blocks)) 2)
              (princ "Oops! You need to select at least two blocks. Try again.")

            ;; Calculate the total distance and the starting point
            (setq total_distance (* distance (- num_blocks 1)))
            (setq start_point (car (car blocks)))

            ;; Iterate over the blocks and move them to the appropriate locations
            (setq current_point start_point)
            (foreach block blocks
              (setq current_point (polar current_point (cadr block) total_distance))
              (command "move" block "" start_point (car current_point) (cadr current_point) 0)
            )
          )
        )
      )
  (princ "\nEQUALSPACE completed successfully. ")
  )

;; Call the c:EQUALSPACE function to initialize the EQUALSPACE command
(c:EQUALSPACE)

 

 

Still getting a syntax error and command not found:

 

Regenerating model.
AutoCAD menu utilities loaded.*Cancel*
Command:
Command:
Command:
Command: AP
APPLOAD EQUAL_SPACE.lsp successfully loaded.
Command: ; error: syntax error
Command:
Command: EQUALSPACE
Unknown command "EQUALSPACE".  Press F1 for help.

 

0 Likes
Message 4 of 11

Sea-Haven
Mentor
Mentor
Accepted solution

Ok I should have read the code more, lots of problems 

;; Check that at least two blocks were selected

Your only selecting 1 block

 

I think what is best is post a dwg explaining what your trying to do as the code does not make much sense.

 

Is it this

;; Call the c:EQUALSPACE function to initialize the EQUALSPACE command

(defun c:EQUALSPACE( / x distance blk ins ss)
(prompt "\nSelect blocks ")
(if (and (setq blocks (ssget '((0 . "INSERT"))))
    (> (sslength blocks) 1)
    )
    (progn
    (setq distance (getreal "Enter the distance to space the blocks: "))
    (setq start_point (getpoint "\nPick start point "))
    (repeat (setq x (sslength blocks))
      (setq blk (ssname blocks (setq x (1- x))))
      (setq ins (cdr (assoc 10 (entget blk))))
      (command "move" blk "" ins start_point)
      (setq start_point (mapcar '+ start_point (list distance 0.0 0.0)))
    )
   )
   (princ "\nOops! a min of 2 blocks need to be selected. Try again.")
)
(princ "\nEQUALSPACE completed successfully. ")
)
(c:EQUALSPACE)
0 Likes
Message 5 of 11

asimjian
Explorer
Explorer

Look like the AI algorithm writes the code all wonky and unusable. Appreciate the time spent on this experiment @Sea-Haven.

0 Likes
Message 6 of 11

Sea-Haven
Mentor
Mentor

Does it meet your needs ? There are smarter ways to display and order blocks. 

0 Likes
Message 7 of 11

martti.halminen
Collaborator
Collaborator

Seems the fundamental problem here is that the system doesn't know the syntax and semantics of IF, so it doesn't know when to close the parentheses, and when to use PROGN to group commands for the correct semantics.

 

-- 

 

0 Likes
Message 8 of 11

Kent1Cooper
Consultant
Consultant

As already noted, ChatGPT doesn't know AutoLisp well enough yet.

 

The (entsel) function is for a single object.  For more than one, you would need to use (ssget) instead, but it does not allow you to spell out a prompt as (entsel) does.  You would need to add a (prompt) before it, and it will prompt with "Select objects: ".

 

The (if) function at line 22 has its 'then' argument on the next line, but lines 25 through 35 need to be "wrapped" in a (progn) function to make it all into a single 'else' argument for the (if) in line 22.

 

The (foreach) function [line 31] is for stepping through a list, not a selection set such as (ssget) returns.  There are ways to turn the selection set into a list of entity names first, or you can do it by stepping through the selection set with (repeat) or (while) and (ssname).

 

The Move command at line 33 needs the last three elements [XYZ coordinates of the displacement end point] wrapped into a (list).

Kent Cooper, AIA
0 Likes
Message 9 of 11

Sea-Haven
Mentor
Mentor

ChatGP just does not meet the grade it makes obvious mistakes, like the select multiple as singles. Did you read what I posted ? 

 

It would be best if you describe what it is your trying to achieve by posting a dwg. If its a legend showing blocks there are better ways to do it.

0 Likes
Message 10 of 11

lionelmoen4
Observer
Observer

Can you explain the primary differences between Visual LISP and AutoLISP in the context of AutoCAD customization?

0 Likes
Message 11 of 11

_gile
Consultant
Consultant

Hi,

Please don't offend the natural intelligence of the people who provide help here by posting AI-generated code (the Stack Overflow site has banned generative AI).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes