Jump to content

Command Reactor inside function


Lee Chu Chu

Recommended Posts

So I want to be able to set things inside functions when the command is called up in the command line. I tried using a command reactor. It works fine when it is outside of the function that I wanted for it to be activated but when I put it inside the function, it doesnt seem to be getting the values of the variables before the command is called up. so I have a simple function used for texting.

 

(defun c:trec()
  (reactoron)
  (command "rectang")
)

 

This is not the actual function I want to use in autocad but using this as my test. So this is my reactor which lives in reactoron.

 

(defun reactoron()

(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 "TREC") (setvar "CLAYER" "4"))
)
(princ)
)

(defun endCommand (calling-reactor endcommandInfo / 
	   thecommandend)
(setq thecommandend (nth 0 endcommandInfo))
(cond
 ((= thecommandend "TREC") (setvar "CLAYER" OldLayer)) 
)
(vlr-remove-all)
(princ)
)

(defun cancelCommand (calling-reactor cancelcommandInfo / 
	      thecommandcancel)
(setq thecommandcancel (nth 0 cancelcommandInfo))
(cond
 ((= thecommandcancel "TREC") (setvar "CLAYER" OldLayer))
)
(vlr-remove-all)
(princ)
)

)

I should thank afralisp for the code sample which this code here is based from. So someone help me find a way to be able to activate this command reactor inside my function TREC?

Link to comment
Share on other sites

Command reactors are used only for CAD command, not user defined command.

In this case the name of command is "rectang", not "trec".

If you want to use reactor in your case, use a lisp reactor.

And why do you want to put reactor inside your command? It may make a nest reactor.

Link to comment
Share on other sites

Try this .

 

(defun _rectang:started (reac data)
 (if (and (wcmatch (car data) "*RECTANG") (tblsearch "LAYER" "4"))
   (progn (setq *oldlayer* (getvar 'CLAYER)) (setvar 'CLAYER "4"))
 )
 (princ)
)
;;                    ;;
(defun _rectang:ended (reac data)
 (if (and (wcmatch (car data) "*RECTANG") (tblsearch "LAYER" *oldlayer*))
   (setvar 'CLAYER *oldlayer*)
 )
 (princ)
)
;;                    ;;
(vlr-command-reactor
 "My Reactor"
 '((:VLR-commandWillStart . _rectang:started)
   (:VLR-commandEnded . _rectang:ended)
   (:VLR-commandCancelled . _rectang:ended)
   (:VLR-commandFailed . _rectang:ended)
  )
)

Link to comment
Share on other sites

@707, I have used a lisp reactor however when I have placed my lisp reactor into my huge lisp file, the reactor doesnt seem to want to cancel at all even though I keep pressing the esc button to cancel the command. The lisp reactor does start but never cancels nor does it end.

Link to comment
Share on other sites

@Tharwat, I will have to try this out tomorrow morning and see if it works. But just so that I can understand what you have written, what does this part of the code mean/ do?

(if (and (wcmatch (car data) "*RECTANG") (tblsearch "LAYER" "4"))

 

Edit: Actually, now I am not sure if it will work in my case when I try to implement this code for my actual functions that I want to personalise. Is there any way to use these reactors for a user-defined function? If not, how would I use a lisp reactor as 707 suggested?

Link to comment
Share on other sites

I'm sorry but I am trying my best to explain things. I am not good at explaining nor am I good at programming. But my question to you know is with the code you wrote above, will it work on user defined functions?

Edited by Lee Chu Chu
Link to comment
Share on other sites

When you press esc, the first function to be triggered is *error* function, not reactor. So if you want to handle with it, you'd better redefine the *error* function instead of reactors.

Link to comment
Share on other sites

How would I do this? I have no understanding of this *error* function thingo even after reading stuff from Lee-Macs website and the stuff from afralisp.

Link to comment
Share on other sites

Basically the *ERROR* is a function that is executed by AutoLISP interpreter each time an error occurs (a code logical error or when user press ).

Even if the name is reserved, the programmer is allowed to define its content. So, you need to add there everything you want to be executed when an error occurs.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...