Author Topic: Quick Text Reactor  (Read 307 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Bull Frog
  • Posts: 398
Quick Text Reactor
« on: March 17, 2017, 06:43:35 pm »
Hi guys,
Its me again - annoying as hell.  :evillaugh: :idea:
I continued to practice with reactors - and the following idea hit me:

Code - Auto/Visual Lisp: [Select]
  1. ; Reactor that whatever unknown command is typed, prompts for a text insertion point
  2.  
  3. (defun QuickTxt:callback ( rtr arg / cLyr acDoc p txt )
  4.  (cond
  5.    ( (not (eq 'STR (type (car arg)))) )
  6.    ( (eq (vla-get-Lock cLyr) :vlax-true) )
  7.    ( (eq (vla-get-Freeze cLyr) :vlax-true) )
  8.    ( (eq (vla-get-LayerOn cLyr) :vlax-false) )
  9.    ( (setq p (getpoint (strcat "\nSpecify point for \"" (car arg) "\" the text: ")))
  10.      (cond
  11.        ( (setq txt (vla-AddMText (vla-get-Block (vla-get-ActiveLayout acDoc)) (setq p (vlax-3d-point p)) 1. (car arg) ))
  12.          (vla-put-AttachmentPoint txt acAttachmentPointMiddleCenter)
  13.          (vla-Move txt (vla-get-InsertionPoint txt) p)
  14.        )
  15.      ); cond
  16.    )
  17.  ); cond
  18.  (princ)
  19. ); defun QuickTxt:callback
  20.  
  21.  
  22. (defun C:QuickTxtOn nil ; Define the function that turns on the reactor
  23.  (cond
  24.    ( (not QuickTxt:callback) (alert "\nQuickTxt:callback function not defined!") )
  25.    (T
  26.      (cond
  27.        ( (vl-some '(lambda (r) (= "QuickTxt" (vlr-data r))) (cdar (vlr-reactors :vlr-command-reactor))) ) ; check if the reactor already exist
  28.        (T (vlr-command-reactor "QuickTxt" '((:vlr-UnknownCommand . QuickTxt:callback))) ) ; else include the new reactor
  29.      ); cond
  30.      (alert "\nQuick Text Reactor enabled!")
  31.      (defun C:QuickTxtOff nil ; Define the function that turns off the reactor
  32.        (vl-some ; check if that reactor exist in the command reactors and remove it (used vl-some instead of foreach)
  33.          '(lambda (r) (if (= "QuickTxt" (vlr-data r)) (vlr-remove r)))
  34.        ); vl-some
  35.        (alert "\nQuick Text Reactor disabled!") (princ)
  36.      ); defun C:QuickTxtOff
  37.    )
  38.  ); cond
  39.  (princ)
  40. ); defun C:QuickTxtOn

Might be useful for someone, so just leaving it here.
« Last Edit: March 18, 2017, 11:09:25 am by Grrr1337 »

Tharwat

  • Bull Frog
  • Posts: 500
  • Hypersensitive
Re: Quick Text Reactor
« Reply #1 on: March 18, 2017, 10:39:04 am »
Hi,

I think there is no need to remove the reactor and recreate it once again if a user invoked the command Quicktxton more than once since its already available/created.

Code - Auto/Visual Lisp: [Select]
  1. (if (vl-some '(lambda (r) (= "QuickTxt" (vlr-data r))) (cdar (vlr-reactors :vlr-command-reactor)))
  2.     (alert "\nQuick Text Reactor is already enabled!")
  3.     (progn (vlr-command-reactor "QuickTxt" '((:vlr-unknowncommand . quicktxt:callback)))
  4.       (alert "\nQuick Text Reactor enabled!"))
  5.    )
  6.  

You also need to check if the current layer is Locked or Off.

Grrr1337

  • Bull Frog
  • Posts: 398
Re: Quick Text Reactor
« Reply #2 on: March 18, 2017, 11:05:49 am »

Hi Tharwat, :)
Thanks for the remarks, I've modified the original code.

Although one question remains:

I've seen this being used by Lee:
Code: [Select]
(foreach rtr ; check if that reactor exist in the command reactors and remove it
  (cdar (vlr-reactors :vlr-command-reactor)) (if (= "QuickTxt" (vlr-data rtr)) (vlr-remove rtr))
); foreach

But wouldn't (vl-some) make more sence? The reactor's data should be unique afterall? :
Code: [Select]
(vl-some ; check if that reactor exist in the command reactors and remove it
  '(lambda (r) (if (= "QuickTxt" (vlr-data r)) (vlr-remove r)))
  (cdar (vlr-reactors :vlr-command-reactor))
); vl-some

Lee Mac

  • Seagull
  • Posts: 11800
  • AutoCAD 2015 Windows 7 London, England
Re: Quick Text Reactor
« Reply #3 on: March 18, 2017, 11:24:10 am »
wouldn't (vl-some) make more sence? The reactor's data should be unique afterall?

I don't like relying on 'should'  :lol:

I use foreach to ensure that there will only ever be a single instance of the reactor running in the active drawing session, and also to ensure that I can confidently identify the reactor corresponding to the application by the reactor data.



Tharwat

  • Bull Frog
  • Posts: 500
  • Hypersensitive
Re: Quick Text Reactor
« Reply #4 on: March 18, 2017, 11:31:42 am »
I've seen this being used by Lee:

You'd better know where and why he used that and what's the wisdom behind that otherwise playing with reactors can crash your AutoCAD easily.  :wink:

But wouldn't (vl-some) make more sence? The reactor's data should be unique afterall? :
If you have any doubt about the data of another reactor may running on the same document then might add more codes to search for your own created reactor.

Grrr1337

  • Bull Frog
  • Posts: 398
Re: Quick Text Reactor
« Reply #5 on: March 18, 2017, 11:51:47 am »
Ah, I see now:
Code - Auto/Visual Lisp: [Select]
  1. (nil "fillet-reactor" "grip-reactor")
  2. _$ (repeat 5
  3.  (vlr-command-reactor "QuickTxt" '((:vlr-UnknownCommand . QuickTxt:callback)))
  4. )
  5. #<VLR-Command-Reactor>
  6. ("QuickTxt" "QuickTxt" "QuickTxt" "QuickTxt" "QuickTxt" nil "fillet-reactor" "grip-reactor")
  7. _$
The reactor's data can stack!
So this means that you have to be aware of the other reactors you use, so the data won't duplicate,
else you could have multiple callbacks (for every duplicated reactor) or might erase other reactor (that has different purpose, but the same data).
Atleast thats my theory.  :roll: