ChatGPT generated 'safe-command' wrapper function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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))))))
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