Wednesday, October 9, 2024

Let ChatGPT Help you create new faster command

 Let ChatGPT Help you create new faster command


This scenario might sound familiar to many drafters:

You need to align multiple circles so that the center points of all selected circles are aligned either vertically or horizontally — based on the first selected circle.


If you're a typical user without any programming knowledge, you might feel stuck. You’d end up manually moving each circle, which is time-consuming and inefficient.



But now, in the age of Artificial Intelligence (AI), you’ve got a powerful assistant by your side — like the old joke “Abdul” who always had the answers to everything!


Let’s try using ChatGPT to help us generate a custom command in AutoLISP, which can be used directly in PTCAD. Here's how you can do it:


Steps to Get Help from AI

  1. Go to ChatGPT.com and register for an account.

  2. Once you're in and see the chat input field, start a conversation. You can greet the AI and explain what you're trying to do.



The AI will write the code for you.

  1. Click Copy to copy the source code.

  2. Paste the code into Notepad, then save the file with a .lsp extension, as shown below.








Loading the LISP Program in PTCAD

  1. Use the APpload command in PTCAD.

  2. Click the Add File button, change the file type to .lsp, and select your file.




  1. For example, choose the file named ACC.lsp.   Click the file and then press Load.



Running the Command

  1. Now try running the command by typing ACC.

At first, the command might not work perfectly — you may need to adjust a few parts of the code. (A working version is included below.)


Once it's working, running the ACC command will automatically align the circles like this:


Source code ==========================================================

(defun C:Acc ()

  (setq ss (ssget '((0 . "CIRCLE"))))  ; Select only circles

  (if ss

    (progn

      (initget "Vertical Horizontal")          ;; Ask user for alignment direction

      (setq alignType (getkword "\nAlign circles [Vertical/Horizontal]: ")) 


      (if alignType

        (progn

          (setq firstCircle (ssname ss 0))  ; Get the first selected circle

          (setq firstData (entget firstCircle))

          (setq firstCenter (cdr (assoc 10 firstData)))  ; Extract center point


          ;; Loop through remaining circles and move them

          (setq i 1)

          (while (< i (sslength ss))

            (setq ent (ssname ss i))

            (setq entData (entget ent))

            (setq entCenter (cdr (assoc 10 entData)))  ; Get entity's center

            

            ;; Calculate the displacement vector based on alignment type

            (cond

              ((= alignType "Vertical")  ; Align vertically (same X)

               (setq dx (- (car firstCenter) (car entCenter)))

               (setq dy 0)

              )

              ((= alignType "Horizontal")  ; Align horizontally (same Y)

               (setq dx 0)

               (setq dy (- (cadr firstCenter) (cadr entCenter)))

              )

            )

            ;; Move the circle to align

           ;; (command "MOVE" ent "" "_DISPLACEMENT" (list 0 0) (list dx dy))

(command "MOVE" ent "" "_DISPLACEMENT"  (list dx dy))


            (setq i (1+ i))  ; Increment index

          )


          (princ (strcat "\nAll selected circles aligned " alignType "."))

        )

        (princ "\nInvalid selection, please choose Vertical or Horizontal.")

      )

    )

    (princ "\nNo circles selected!")

  )

  (princ)  ; Clean exit

)



No comments:

Post a Comment

Draw mechanical details with PTCAD Plus - MEC

     Draw mechanical details with PTCAD Plus - MEC               PTCAD Plus edition contains many plug-ins  AECplus, MEC, Assetlink which ca...