Jump to content
Lee Chu Chu

Command Reactor inside function

Recommended Posts

Lee Chu Chu

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?

Share this post


Link to post
Share on other sites
7o7

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.

Share this post


Link to post
Share on other sites
Tharwat

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)
  )
)

Share this post


Link to post
Share on other sites
Lee Chu Chu

@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.

Share this post


Link to post
Share on other sites
Lee Chu Chu

@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?

Share this post


Link to post
Share on other sites
Tharwat

If you do not explain your idea clearly , you would keep on receiving guesses and not useful replies .

Share this post


Link to post
Share on other sites
Lee Chu Chu

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

Share this post


Link to post
Share on other sites
7o7

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.

Share this post


Link to post
Share on other sites
Lee Chu Chu

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.

Share this post


Link to post
Share on other sites
MSasu

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.

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...