TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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?
;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]
-
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)
(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)
-
I understood there was an issue with using Command and/or setvar inside a reactor callback.
-
Hey again happy Kanook!
You can streamline the code for TEXT, DTEXT, MTEXT in ((= BGN "TEXT") ... by ((wcmatch BGN "*TEXT")
-
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.
(member BGN '("TEXT" "DTEXT" "MTEXT"))
-
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.
-
Here's one more take. Commented in code.
(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
-
Thanx Serge, and SMadsen.
Greatly appreciated.
:D