Reactors - little help getting started.

Discussion in 'AutoCAD' started by Casey Roberts, Nov 11, 2004.

  1. Okay, I've been perusing the help files as well as AfraLisp.com and I'm
    still somewhat at a loss when it comes to reactors.

    My first attemt at reactors, is to have a reactor fired on an xref attach
    event that will check the current layer, and if it happens to be DEFPOINTS,
    it will alert the user or change the layer to something else.

    Perhaps I'm over my head, but this is what I have so far, but it's not
    working.

    (vl-load-com)

    (vlr-xref-reactor

    nil '(:)vlr-beginattach . checklay)))

    (defun checklay (/ )
    (setq lay (getvar "CLAYER"))
    (if (= lay "DEFPOINTS")(setvar "clayer" "0"))
    ) ;defun
     
    Casey Roberts, Nov 11, 2004
    #1
  2. Okay, its sending info along with the checklay command call, so I've changed
    the defun of the checklay to the following

    (defun checklay (var1 var2 /) ......

    and it seems to be working, I think,. The problem is that setvar will not
    work while the xref dialog box is invoked, is there another method to set
    the current layer prior to attaching the xref so it doesn't end up on
    DEFPOINTS layer?

    Thanks,

    Casey
     
    Casey Roberts, Nov 11, 2004
    #2
  3. Okay - it's working now, but I thought I'd post it for critisism.... It's
    kindof amalgamated from afralisp.com (thanx by the way) and I'm still trying
    to wrap my head around all the vla... stuff cuz this is my first time using
    it in any extensive manner.

    Let me know if / how i can improve this.....

    Thanks,

    Casey

    (vl-load-com)

    (vlr-xref-reactor

    nil
    '(:)vlr-beginattach . checklay))
    )

    (defun checklay (object objectdbase /)
    (setq lay (getvar "CLAYER"))
    (if (= lay "Defpoints")
    (progn



    (setq thedocument
    (vla-item (vla-get-documents (vlax-get-Acad-Object)) (getvar
    "dwgname"))
    )
    (setq LayerTable (vla-get-layers thedocument))
    (vla-put-activelayer thedocument (vla-item LayerTable 0))
    )
    )
    )
     
    Casey Roberts, Nov 11, 2004
    #3
  4. Casey Roberts

    Jürg Menzi Guest

    Hi Casey

    A command reactor would be the better solution:
    Code:
    ; - Initialize ActiveX support ------------------------------------------------
    (vl-load-com)
    ;
    ; - Reactors ------------------------------------------------------------------
    ; - If not set, initialize DocManager-Reactor
    (or Me:ReaDma
    (setq Me:ReaDma (VLR-DocManager-Reactor
    nil
    '(
    (:VLR-documentToBeDestroyed . MeDocToBeDestroyedCallbacks)
    )
    )
    )
    )
    ; - If not set, initialize Command-Reactor
    (or Me:ReaCom
    (setq Me:ReaCom (VLR-Command-Reactor
    nil
    '(
    (:VLR-commandWillStart . MeCommandWillStartCallbacks)
    (:VLR-commandEnded . MeCommandEndedCallbacks)
    (:VLR-commandCancelled . MeCommandCancelledCallbacks)
    (:VLR-commandFailed . MeCommandFailedCallbacks)
    )
    )
    )
    )
    ;
    ; - Notifications -------------------------------------------------------------
    ; - CommandWillStart notifications
    (defun MeCommandWillStartCallbacks (Rea Arg)
    (MeDoCmdStartStuff Arg)
    (princ)
    )
    ; - CommandEnded notifications
    (defun MeCommandEndedCallbacks (Rea Arg)
    (MeDoCmdEndStuff Arg)
    (princ)
    )
    ; - CommandCancelled notifications
    (defun MeCommandCancelledCallbacks (Rea Arg)
    (MeDoCmdEndStuff Arg)
    (princ)
    )
    ; - CommandFailed notifications
    (defun MeCommandFailedCallbacks (Rea Arg)
    (MeDoCmdEndStuff Arg)
    (princ)
    )
    ; - DocToBeDestroyed notifications
    (defun MeDocToBeDestroyedCallbacks (Rea Arg)
    (MeDoCloseStuff)
    (princ)
    )
    ;
    ; - Subs ----------------------------------------------------------------------
    ; - Command will start function
    (defun MeDoCmdStartStuff (Arg / CurCmd)
    (setq CurCmd (strcase (car Arg)))
    (cond
    ((wcmatch CurCmd "XATTACH")
    (setq Me:TmpLay (getvar "CLAYER"))
    (setvar "CLAYER" "0")
    )
    ;;; other command depend functions
    )
    (princ)
    )
    ; - Command end function
    (defun MeDoCmdEndStuff (Arg / CurCmd)
    (setq CurCmd (strcase (car Arg)))
    (cond
    ((wcmatch CurCmd "XATTACH")
    (if Me:TmpLay (setvar "CLAYER" Me:TmpLay))
    (setq Me:TmpLay nil)
    )
    ;;; other command depend functions
    )
    (princ)
    )
    ; - Reactor cleanup function
    (defun MeDoCloseStuff ( / VarLst)
    (setq VarLst (MeGetReaVars))
    (mapcar 'VLR-remove (mapcar 'eval VarLst))
    (mapcar '(lambda (l) (set l nil)) VarLst)
    (princ)
    )
    ; - Collect global reactor variables
    (defun MeGetReaVars ( / RetVal)
    (foreach memb (atoms-family 1)
    (if (wcmatch (strcase memb) "ME:REA*")
    (setq RetVal (cons memb RetVal))
    )
    )
    (mapcar 'read RetVal)
    )
    
    (princ)
    
    Cheers
     
    Jürg Menzi, Nov 12, 2004
    #4
  5. watch out for document reactors. Just when you are done and things are going smoothe, you will notice certain commands
    not working after the reactor runs.
    This is because reactors screw up entmake and entmod for any commands run right after the reactor fires.
    I have a text edit routine that will not work after reactors fire, took me forever to figure out the cause.
    Way too high a price to pay for what they can do.

    Jürg Menzi <>
    |>Hi Casey
    |>
    |>A command reactor would be the better solution:
    |>
    Code:
    |>; - Initialize ActiveX support ------------------------------------------------
    |>(vl-load-com)
    |>;
    |>; - Reactors ------------------------------------------------------------------
    
    
    James Maeding
    jmaeding at hunsaker dot com
    Civil Engineer/Programmer
     
    James Maeding, Nov 12, 2004
    #5
  6. Will this work for only when an xref is attached? I don't need it to catch
    every time the 'XREF' command is run, only when an xref is actually
    attached.


    Casey


     
    Casey Roberts, Nov 12, 2004
    #6
  7. Casey Roberts

    Jürg Menzi Guest

    Hi James
    I'm familiar with document reactors. In the given case I use this reactor to
    remove the reactors in closing the document.

    Cheers
     
    Jürg Menzi, Nov 12, 2004
    #7
  8. Casey Roberts

    Jürg Menzi Guest

    Hi Casey
    The reactor does his job only in case of XATTACH command -> see code!
    In every case the previous layer will be restored at the end of the command.

    Cheers
     
    Jürg Menzi, Nov 12, 2004
    #8
  9. Thanks Juerg... I'll look it over.

    Is there anything inherently wrong with the way I did it aside from the fact
    that it does not reset the current layer back to "Defpoints" when the
    command is done? (which isn't a big deal for me anyway)
     
    Casey Roberts, Nov 12, 2004
    #9
  10. Casey Roberts

    Jürg Menzi Guest

    Hi Casey

    Welcome...¦-)
    IMHO, the layer should be set to '0' not only if 'Defpoints' is active and...
    it's confusing for a user to have another layer set after execute a command.

    To create your reactors you should use a notification system to be more
    flexible in adding other events to the reactor(s) (see my sample).
    At the end of a drawing session, you should remove teh reactor(s).

    About the code:
    Code:
    (or Me:ReaXrf
    (setq Me:ReaXrf (VLR-Xref-Reactor
    nil
    '((:VLR-BeginAttach . checklay))
    )
    )
    )
    
    (defun checklay (object objectdbase / thedocument LayerTable)
    (if (= (getvar "CLAYER") "Defpoints")
    (progn
    (setq thedocument (vla-get-ActiveDocument (vlax-get-Acad-Object))
    LayerTable  (vla-get-layers thedocument)
    )
    (vla-put-activelayer thedocument (vla-item LayerTable "0"))
    )
    )
    )
    
    Cheers
     
    Jürg Menzi, Nov 12, 2004
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.