ChatGPT generated 'safe-command' wrapper function

ChatGPT generated 'safe-command' wrapper function

hencoop
Advisor Advisor
596 Views
4 Replies
Message 1 of 5

ChatGPT generated 'safe-command' wrapper function

hencoop
Advisor
Advisor

I initially sought some help with aeccoreconsole.exe being used unnecessarily.  Apparently, that executable is for handling command-line input regardless of whether AutoCAD or the user invokes the command-line.  ChatGPT's assessment agrees with the basic idea that it is best to avoid COMMAND calls in our programs if possible.

This SAFE-COMMAND function makes doing that easy.  I haven't tested every command included but I have tested UNDO, LINE, ARC and they worked flawlessly.
These were tested by exercising calls from within a program:

  • (safe-command '(".undo" "begin"))
  • (safe-command '(".arc" "C" ipt tp16 tp17))
  • (safe-command '(".line" tp7 tp16 ""))
  • (safe-command '(".undo" "end"))

Also, additional commands are easily added in (SETQ command-list ...).  It is necessary to also add its handler function (defun handle-<command> (args) ...)

Enjoy!
P.S. If you find errors, ask ChatGPT to debug and refactor the code and post the results here.

IF you need help with new handler functions, ChatGPT can write and debug those for you as well.

 

 

(defun handle-undo (args)
  ;; Handles the .UNDO command safely.
  (if (member (car args) '("BEGIN" "END"))
      (setvar "UNDO" (car args))
      (princ "\nInvalid UNDO argument. Use \"BEGIN\" or \"END\" only.")))

(defun handle-zoom (args)
  ;; Handles the .ZOOM command safely.
  (if (equal (car args) "EXTENTS")
      (vlax-invoke (vla-get-ActiveDocument (vlax-get-acad-object)) 'ZoomExtents)))

(defun handle-layer (args)
  ;; Handles the .LAYER command safely.
  (cond
    ((equal (car args) "SET") (setvar "CLAYER" (cadr args)))
    ((equal (car args) "OFF")
     (vla-put-LayerOn
       (vla-item
         (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
         (cadr args))
       :vlax-false))))

(defun handle-text (args)
  ;; Handles text creation safely.
  (vla-addText
    (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-acad-object)))
    (caddr args) ;; Text string
    (vlax-3d-point (car args)) ;; Insertion point
    (cadr args))) ;; Text height

(defun handle-line (args)
  ;; Handles LINE safely using entmakex.
  (entmakex (list '(0 . "LINE") (cons 10 (car args)) (cons 11 (cadr args)))))

(defun handle-circle (args)
  ;; Handles CIRCLE safely using entmakex.
  (entmakex (list '(0 . "CIRCLE") (cons 10 (car args)) (cons 40 (cadr args)))))

(defun handle-arc (args)
  ;; Handles ARC safely using entmakex.
  (entmakex (list '(0 . "ARC") (cons 10 (car args)) (cons 40 (cadr args)) (cons 50 (caddr args)) (cons 51 (cadddr args)))))

(defun handle-ellipse (args)
  ;; Handles ELLIPSE safely using entmakex.
  (entmakex (list '(0 . "ELLIPSE") (cons 10 (car args)) (cons 11 (cadr args)) (cons 40 (caddr args)))))

(defun handle-insert (args)
  ;; Handles INSERT safely using ActiveX.
  (vla-InsertBlock
    (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-acad-object)))
    (vlax-3d-point (cadr args)) ;; Insertion point
    (car args) ;; Block name
    (caddr args) (caddr args) (caddr args) ;; Scale
    (cadddr args))) ;; Rotation

(defun handle-polyline (args)
  ;; Handles POLYLINE safely using entmakex.
  (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbPolyline") '(90 . 0)) 
                    (mapcar (lambda (p) (cons 10 p)) args))))

(defun safe-command (args)
  ;; Ensure ActiveX functions are available
  (vl-load-com)
  ;; Ensure `args` is a list and extract the command
  (setq cmd (car args)
        args (cdr args)) ;; Extract command name and separate arguments

  ;; Define a list of commands and their handlers
  (setq command-list
    '((".UNDO" . handle-undo)
      (".ZOOM" . handle-zoom)
      (".LAYER" . handle-layer)
      (".TEXT" . handle-text)
      (".LINE" . handle-line)
      (".CIRCLE" . handle-circle)
      (".ARC" . handle-arc)
      (".ELLIPSE" . handle-ellipse)
      (".INSERT" . handle-insert)
      (".POLYLINE" . handle-polyline)))

  (cond
    ;; ----- Handle Commands in the command-list -----
    ((assoc cmd command-list)
     (apply (cdr (assoc cmd command-list)) (list args)))

    ;; ----- FALLBACK (If no match, runs the original command correctly) -----
    (t
     (apply 'command (cons cmd (mapcar 'eval args))))))

 

 

 

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
0 Likes
597 Views
4 Replies
Replies (4)
Message 2 of 5

Kent1Cooper
Consultant
Consultant

I don't know....

I would think, for example, that "handling the Layer command" would allow for more of the command's many options than just two.  I guess the idea is that more of them could be added as further (cond)itions.  But it's too simple: for the SET option, it should first THAW the Layer -- it will run into a problem as written if the Layer is frozen.  And maybe it should also turn the Layer ON, since presumably you would want it on if you're making it current, but (setvar 'clayer) will not do so if it's off, while a Layer command in a (command) function will.  And it seems cumbersome to have to construct the whole business individually for each Layer option you need to invoke, whereas a Layer command in a (command) function can incorporate as many of them as needed within one command.

Is the assumption that everything you ever want to draw with these functions is always to be on the current Layer?

"Handling text creatioin" needs to involve hugely more elements.

My biggest objection:  Not all Text and Block insertions go in Model space.

Kent Cooper, AIA
0 Likes
Message 3 of 5

ec-cad
Collaborator
Collaborator

I fail to see how adding yet another layer of code, just to do simple stuff is the goal.

As far as being 'Safe ? What makes that Safe.. no Error trapping, no messages.. etc ?

I totally agree with Kent, that set of code would have to be expanded to encompass all

possible combinations of existing Command functions. Why re-invent the (whole) wheel ?

How about drawing that ARC,, when you feed the (arg) the wrong stuff ?

 

ECCAD

0 Likes
Message 4 of 5

Sea-Haven
Mentor
Mentor

For some object creation steps can be removed if I type c123 I get asked "pick point" a circle of radius 123 is drawn, I have offset and fillet also. Other object creation could be added. It uses a reactor checking what error occurred. Using shorthand defuns may be much simpler way of approaching task.

 

0 Likes
Message 5 of 5

hencoop
Advisor
Advisor

@Kent1Cooper@Sea-Haven , @ec-cad 

 

The reason I broached the subject with ChatGPT is that I have written lots of programs that make extensive use of COMMAND statements to create lines, arcs, insert blocks, etc.  Given the trouble I've been having with aeccoreconsole.exe burning up all my available memory, It seemed important to minimize use of the COMMAND functions since they call aeccoreconsole.exe.  For simple things like lines, arcs and blocks it works very well for me... within my existing programs... because everything except the command to place the object is accounted for ahead of the command in the program. Some things are reset after the command.  All I needed was a command wrapper that would make refactoring lots of COMMAND function usage much easier.  With this program, I can search and replace
"(COMMAND " with
"(safe-command '("
then add a closing parenthesis to it and be done.

 

I expected folks may have problems with it or suggestions for improvement but I hoped to be clear that I wasn't looking to handle any of them.

This is one reason why I said "If you find errors, ask ChatGPT to debug and refactor the code and post the results here.  If you need help with new handler functions, ChatGPT can write and debug those for you as well."  Another reason is so folks would be encouraged to see what ChatGPT can do for them as they write (or learn to write) programs.

 

To give you an idea of how much programming I am considering for refactoring,  I've attached the help file for one of my applications and a links to the help files of two others.  I have lots of programs and large multi-function-defining programs in which I use the COMMAND function extensively:
ParaPIPE Help FIle;  ParaPOINT Help FIle

The attached file is the Help file for another application, GPDGN.

 

Enjoy, 

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
0 Likes