TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: SPDCad on October 12, 2004, 02:14:23 PM

Title: Command Reactor
Post by: SPDCad on October 12, 2004, 02:14:23 PM
I wrote this lisp to use with autocad 2002 and it worked great thru to Acad2004, but for some reason with 2005, the layer will not switch back to the orginal layer when the command is ended, or canceled.  Did they change something in 2005, and not tell anyone?


Code: [Select]
;Programme to be loaded only, not run!
(setq CHK (tblsearch "Layer" "Xref"));
(if (= CHK Nil)(command "_.Layer" "M" "Notes" "" "c" "9" ""));

(vl-load-com)

(vlr-command-reactor nil '((:vlr-commandWillStart . startCommand)))
(vlr-command-reactor nil '((:vlr-commandEnded . endCommand)))
(vlr-command-reactor nil '((:vlr-commandCancelled . cancelCommand)))
(vlr-command-reactor nil '((:vlr-commandFailed . failedCommand)))

(defun startCommand (calling-reactor startcommandInfo / BGN)
(setq OLD (getvar "CLAYER"))
(setq BGN (nth 0 startcommandInfo))
(cond
((= BGN "TEXT") (setvar "CLAYER" "Notes"))
((= BGN "MTEXT") (setvar "CLAYER" "Notes"))
((= BGN "DTEXT") (setvar "CLAYER" "Notes"))
);cond
);defun

(defun endCommand (calling-reactor endcommandInfo / EXT)
(setq EXT (nth 0 endcommandInfo))
(cond
((= EXT "TEXT") (setvar "CLAYER" OLD))
((= EXT "MTEXT") (setvar "CLAYER" OLD))
((= EXT "DTEXT") (setvar "CLAYER" OLD))
);cond
);defun

(defun cancelCommand (calling-reactor cancelcommandInfo / CNL)
(setq CNL (nth 0 cancelcommandInfo))
(cond
((= CNL "TEXT") (setvar "CLAYER" OLD))
((= CNL "MTEXT") (setvar "CLAYER" OLD))
((= CNL "DTEXT") (setvar "CLAYER" OLD))
);cond
);defun

(defun failedCommand (calling-reactor failedcommandInfo / CNL)
(setq FLD (nth 0 failedcommandInfo))
(cond
((= FLD "TEXT") (setvar "CLAYER" OLD))
((= FLD "MTEXT") (setvar "CLAYER" OLD))
((= FLD "DTEXT") (setvar "CLAYER" OLD))
);cond
);defun


Any help would be greatly appreciated.  :) [/code]
Title: Command Reactor
Post by: Serge J. Gianolla on October 12, 2004, 09:54:45 PM
Ciao SPD,
How's things?
Your code as is would not work on 2002/2004 here. Made a few changes:
(command "_.Layer" "_N" "Notes" "_c" "9" "Notes" "")
Make option creates a new layer and sets it current. New option creates it, and since you are using reactors to switch layers...

(setq OLD (getvar 'CLAYER))

and finally
(defun failedCommand (calling-reactor failedcommandInfo / FLD)

Code: [Select]
(setq CHK (tblsearch "Layer" "Xref"))  ;
(if (= CHK Nil)
  (command "_.Layer" "_N" "Notes" "_c" "9" "Notes" "")
)  ;

(vl-load-com)

(vlr-command-reactor nil '((:vlr-commandWillStart . startCommand)))
(vlr-command-reactor nil '((:vlr-commandEnded . endCommand)))
(vlr-command-reactor nil '((:vlr-commandCancelled . cancelCommand)))
(vlr-command-reactor nil '((:vlr-commandFailed . failedCommand)))

(defun startCommand (calling-reactor startcommandInfo / BGN)
  (setq OLD (getvar 'CLAYER))
  (setq BGN (nth 0 startcommandInfo))
  (cond
     
    ((= BGN "TEXT") (setvar "CLAYER" "Notes"))
     
    ((= BGN "MTEXT") (setvar "CLAYER" "Notes"))
     
    ((= BGN "DTEXT") (setvar "CLAYER" "Notes"))
  )  ;cond
)  ;defun

(defun endCommand (calling-reactor endcommandInfo / EXT)
  (setq EXT (nth 0 endcommandInfo))
  (cond
     
    ((= EXT "TEXT") (setvar "CLAYER" OLD))
     
    ((= EXT "MTEXT") (setvar "CLAYER" OLD))
     
    ((= EXT "DTEXT") (setvar "CLAYER" OLD))
     
  )  ;cond
)  ;defun

(defun cancelCommand (calling-reactor cancelcommandInfo / CNL)
  (setq CNL (nth 0 cancelcommandInfo))
  (cond
     
    ((= CNL "TEXT") (setvar "CLAYER" OLD))
     
    ((= CNL "MTEXT") (setvar "CLAYER" OLD))
     
    ((= CNL "DTEXT") (setvar "CLAYER" OLD))
     
  )  ;cond
)  ;defun

(defun failedCommand (calling-reactor failedcommandInfo / FLD)
  (setq FLD (nth 0 failedcommandInfo))
  (cond
     
    ((= FLD "TEXT") (setvar "CLAYER" OLD))
     
    ((= FLD "MTEXT") (setvar "CLAYER" OLD))
     
    ((= FLD "DTEXT") (setvar "CLAYER" OLD))
     
  )  ;cond
)  ;defun

She no longer chucks a wobbly here!! HTH 8)
Title: Command Reactor
Post by: Kerry on October 12, 2004, 11:47:01 PM
I understood there was an issue with using Command and/or setvar inside a reactor callback.
Title: Command Reactor
Post by: Serge J. Gianolla on October 13, 2004, 02:19:41 AM
Hey again happy Kanook!

You can streamline the code for TEXT, DTEXT, MTEXT in ((= BGN "TEXT") ...  by ((wcmatch BGN "*TEXT")
Title: Command Reactor
Post by: CAB on October 13, 2004, 07:36:05 AM
Quote from: Serge J. Gianolla
Hey again happy Kanook!

You can streamline the code for TEXT, DTEXT, MTEXT in ((= BGN "TEXT") ...  by ((wcmatch BGN "*TEXT")

Will RTEXT be a problem?

Edited.....
How about.
Code: [Select]
(member BGN '("TEXT" "DTEXT" "MTEXT"))
Title: Command Reactor
Post by: daron on October 13, 2004, 08:22:35 AM
Quote from: Kerry Brown
I understood there was an issue with using Command and/or setvar inside a reactor callback.


I too understood that to be the case. The first reactor I ever did was trying to change ltscale when switching from model tab to any layout tab. I didn't know about the issue, but found that the reactor would work going to any and moving between any layout tab, but would fail when returning to the model tab.
Title: Command Reactor
Post by: SMadsen on October 13, 2004, 08:23:44 AM
Here's one more take. Commented in code.
Code: [Select]
(setq CHK (tblsearch "Layer" "Xref"))   ;
(if (= CHK Nil)
  (command "_.Layer" "_N" "Notes" "_c" "9" "Notes" "")
)                                       ;

(vl-load-com)

;; No need for separate reactors, one can handle all
;; Also, to avoid creation of more reactors, you
;; might want to check if it exists already by
;; assigning a global
(if (not *myCommandRectors*)
  (setq *myCommandRectors*
         (vlr-command-reactor
           nil
           '((:vlr-commandWillStart . startCommand)
             (:vlr-commandEnded . endCommand)
             (:vlr-commandCancelled . cancelCommand)
             (:vlr-commandFailed . failedCommand)
            )
         )
  )
)

;; Layer could have been purged in the meanwhile,
;; so it would probably be good to check if it exists
(defun startCommand (calling-reactor startcommandInfo / BGN)
  (setq OLD (getvar 'CLAYER))
  (setq BGN (nth 0 startcommandInfo))
  (if (tblsearch "LAYER" "Notes")
   (cond
     ((= BGN "TEXT") (setvar "CLAYER" "Notes"))
     ((= BGN "MTEXT") (setvar "CLAYER" "Notes"))
     ((= BGN "DTEXT") (setvar "CLAYER" "Notes"))
   )                                     ;cond
  )
)                                       ;defun

;; Just a precaution to check for global variable
;; before using it. To make it more failsafe it
;; should check for the OLD layer but .. let's not
;; get carried away?
(defun endCommand (calling-reactor endcommandInfo / EXT)
  (setq EXT (nth 0 endcommandInfo))
  (and (= (type OLD) 'STR)
   (cond
     ((= EXT "TEXT") (setvar "CLAYER" OLD))
     ((= EXT "MTEXT") (setvar "CLAYER" OLD))
     ((= EXT "DTEXT") (setvar "CLAYER" OLD))
   )
  )                                     ;cond
)                                       ;defun

(defun cancelCommand (calling-reactor cancelcommandInfo / CNL)
  (setq CNL (nth 0 cancelcommandInfo))
  (and (= (type OLD) 'STR)
   (cond
     ((= CNL "TEXT") (setvar "CLAYER" OLD))
     ((= CNL "MTEXT") (setvar "CLAYER" OLD))
     ((= CNL "DTEXT") (setvar "CLAYER" OLD))
   )
  )                                     ;cond
)                                       ;defun

(defun failedCommand (calling-reactor failedcommandInfo / FLD)
  (setq FLD (nth 0 failedcommandInfo))
  (and (= (type OLD) 'STR)
   (cond
     ((= FLD "TEXT") (setvar "CLAYER" OLD))
     ((= FLD "MTEXT") (setvar "CLAYER" OLD))
     ((= FLD "DTEXT") (setvar "CLAYER" OLD))
   )                                     ;cond
  )
)                                       ;defun
Title: Command Reactor
Post by: SPDCad on October 13, 2004, 09:52:51 AM
Thanx Serge, and SMadsen.
Greatly appreciated.
     :D