Reactors - Page II
Another Editor Type of reactor is the
"VLR-Command-Reactor". This reactor notifies us of a Command
Event. In this instance we will make use of the "vlr-commandEnded"
reactor event which returns a event parameter list containing a string
identifying the command that has been cancelled.
Every time a drawing is plotted, our call back function will first, save
the drawing, and secondly save the drawing to a backup directory, namely
C:/Backup. Let's have a look at the coding :
(prompt " \nLoad Only....Do NOT Run...")
(vl-load-com)
;*******************************************************
(vlr-command-reactor
"Backup After Plot" '((:vlr-commandEnded . endPlot)))
;*******************************************************
(defun endPlot (calling-reactor endcommandInfo /
thecommandend drgName newname)
(setq thecommandend (nth 0 endcommandInfo))
(if (= thecommandend "PLOT")
(progn
(setq acadDocument (vla-get-activedocument
(vlax-get-acad-object)))
(setq drgName (vla-get-name acadDocument))
(setq newname (strcat "c:\\backup\\" drgName))
(vla-save acadDocument)
(vla-saveas acadDocument newname)
);progn
);if
(princ)
);defun
;*********************************************************
(princ)
A word of warning!!! Did you notice how I used ActiveX statements and
functions to "Save" and "SaveAs". You
cannot use interactive functions from within a reactor as AutoCAD may
still be processing a command at the time the event is triggered.
Therefore, avoid the use of input-acquisition methods such as "getPoint",
"ensel", and "getkword", as well as
"selection set" operations and the "command"
function.
Here's another interesting command event reactor :
When a user adds Text or Hatch to the drawing, the layer will
automatically change to Layer "4" or Layer "6"
respectively. When the command is completed, or cancelled, the user is
returned to the original Layer he was on before he started the command.
Save the file as "LayMan.lsp", BUT please remember, that as this
routine contains reactors, you must only Load it and NOT Run it. If you
want to use this routine on a permanent basis, you'll have to ensure that
it is loaded at startup. There is also no checking to ensure that the
layers exist, are frozen, switched off, etc. and no other form of error
checking. I've got to leave something for you to do!!!
(prompt " \nLoad Only....Do NOT Run...")
(vl-load-com)
;****************************************
(vlr-command-reactor
nil '((:vlr-commandWillStart . startCommand)))
(vlr-command-reactor
nil '((:vlr-commandEnded . endCommand)))
(vlr-command-reactor
nil '((:vlr-commandCancelled . cancelCommand)))
;******************************************************
(defun startCommand (calling-reactor startcommandInfo /
thecommandstart)
(setq OldLayer (getvar "CLAYER"))
(setq thecommandstart (nth 0 startcommandInfo))
(cond
((= thecommandstart "TEXT") (setvar "CLAYER" "4"))
((= thecommandstart "MTEXT") (setvar "CLAYER" "4"))
((= thecommandstart "DTEXT") (setvar "CLAYER" "4"))
((= thecommandstart "HATCH") (setvar "CLAYER" "6"))
((= thecommandstart "BHATCH") (setvar "CLAYER" "6"))
);cond
(princ)
);defun
;****************************************************
(defun endCommand (calling-reactor endcommandInfo /
thecommandend)
(setq thecommandend (nth 0 endcommandInfo))
(cond
((= thecommandend "TEXT") (setvar "CLAYER" OldLayer))
((= thecommandend "MTEXT") (setvar "CLAYER" OldLayer))
((= thecommandend "DTEXT") (setvar "CLAYER" OldLayer))
((= thecommandend "HATCH") (setvar "CLAYER" OldLayer))
((= thecommandend "BHATCH") (setvar "CLAYER" OldLayer))
);cond
(princ)
);defun
;********************************************************
(defun cancelCommand (calling-reactor cancelcommandInfo /
thecommandcancel)
(setq thecommandcancel (nth 0 cancelcommandInfo))
(cond
((= thecommandcancel "TEXT") (setvar "CLAYER" OldLayer))
((= thecommandcancel "MTEXT") (setvar "CLAYER" OldLayer))
((= thecommandcancel "DTEXT") (setvar "CLAYER" OldLayer))
((= thecommandcancel "HATCH") (setvar "CLAYER" OldLayer))
((= thecommandcancel "BHATCH") (setvar "CLAYER" OldLayer))
);cond
(princ)
);defun
;*********************************************************
(princ)
Did you notice that this application used three command reactors with
three different command events. We could have incorporated all three
reactor events and call back functions under one command reactor type, but
I prefer to leave them separate for clarity and ease of debugging.
O.K. that's enough of command reactors. Let's have a look at Object
Reactors.
See you on Page III |