TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: GDF on February 21, 2018, 07:10:01 AM

Title: Creating a Layerstate Reactor
Post by: GDF on February 21, 2018, 07:10:01 AM
I'm trying to create a Layerstate reactor to run a layerstate-restore whenever a unique ctab is made current.
And, I am way over my head here...The function PM-LSR will run the restore process.

Please help me get started.

Code: [Select]
(defun PM-LSR (/ layerstatenam)
  (setvar "tilemode" 0)
  (cond
    ((= (getvar "ctab") "A1-0 SITE")(setq layerstatenam "SITE"))
    ((= (getvar "ctab") "A1-1 DRIVE-THRU")(setq layerstatenam "DRIVE-THRU"))
    ((= (getvar "ctab") "A2-0 PHOTO")(setq layerstatenam "PHOTO-FLOOR"))
    ((= (getvar "ctab") "A3-0 FLOOR")(setq layerstatenam "FP"))
    ((= (getvar "ctab") "A3-1 CEILING")(setq layerstatenam "CEILING"))
    ((= (getvar "ctab") "A4-0 EXT ELEVS")(setq layerstatenam "ELEVATIONS"))
    ((= (getvar "ctab") "K2-0 KITCHEN")(setq layerstatenam "KITCHEN"))   
  )   
  (layerstate-restore "ALL-ON" nil 255)
  (layerstate-restore layerstatenam nil 255)
  (command "regenall")
  (princ (strcat "\n* Restoring Layer States from: " layerstatenam)) 
  (princ))



(defun Layerstate:Start ()
  (or *LS-Reactor*
      (setq *LS-Reactor*
             (vlr-command-reactor
               nil
               '(
                 (:vlr-commandcancelled . LS-Reactor:CommandEnded)
                 (:vlr-commandended . LS-Reactor:CommandEnded)
                 (:vlr-commandfailed . LS-Reactor:CommandEnded)
                 (:vlr-commandwillstart . LS-Reactor:CommandWillStart)
                )
             )
      )
  )

(defun LS-Reactor:CommandEnded ()
    ?
)

(defun LS-Reactor:CommandWillStart ()
  ?
)
Title: Re: Creating a Layerstate Reactor
Post by: CAB on February 21, 2018, 10:41:56 AM
Got my hands full but years ago this was a subject in several threads.
This is only one of them:  https://www.theswamp.org/index.php?topic=3388.0
Title: Re: Creating a Layerstate Reactor
Post by: Grrr1337 on February 21, 2018, 10:54:54 AM
Maybe this will work:

Code: [Select]
(defun LSon nil
  (and (LSrtr t) (alert "\nLayer states reactor is activated, type \"LSoff\" to turn it off."))
)
(defun LSoff nil
  (and (LSrtr nil) (alert "\nLayer states reactor is disabled, type \"LSon\" to turn it on."))
)

(defun LSrtr ( flg )
  (foreach rtr (cdar (vlr-reactors :VLR-SysVar-reactor)) (if (= "LSrtr" (vlr-data rtr)) (vlr-remove rtr)) )
  (if flg (vlr-SysVar-Reactor "LSrtr" '((:VLR-sysVarChanged . CB:LSrtr)))) t
)

(defun CB:LSrtr ( rtr args )
  (and (equal '("TILEMODE" T) args)
    (setq args
      (cdr
        (assoc
          (getvar 'ctab)
          '(
            ("A1-0 SITE"       . "SITE")
            ("A1-1 DRIVE-THRU" . "DRIVE-THRU")
            ("A2-0 PHOTO"      . "PHOTO-FLOOR")
            ("A3-0 FLOOR"      . "FP")
            ("A3-1 CEILING"    . "CEILING")
            ("A4-0 EXT ELEVS"  . "ELEVATIONS")
            ("K2-0 KITCHEN"    . "KITCHEN")
          )
        )
      )
    )
    (vl-catch-all-error-p (setq rtr (vl-catch-all-apply 'eval '((progn (layerstate-restore "ALL-ON" nil 255) (layerstate-restore args nil 255))))))
    (alert (strcat "\nError occured: \n" (vl-catch-all-error-message rtr)))
  )
  (princ)
)
(vl-load-com) (princ)
Title: Re: Creating a Layerstate Reactor
Post by: Lee Mac on February 21, 2018, 01:46:23 PM
I would suggest:
Code - Auto/Visual Lisp: [Select]
  1. (defun layerstate-callback ( rtr arg / las )
  2.     (if
  3.         (setq las
  4.             (cdr
  5.                 (assoc (strcase (car arg))
  6.                    '(
  7.                         ("A1-0 SITE"       . "SITE")
  8.                         ("A1-1 DRIVE-THRU" . "DRIVE-THRU")
  9.                         ("A2-0 PHOTO"      . "PHOTO-FLOOR")
  10.                         ("A3-0 FLOOR"      . "FP")
  11.                         ("A3-1 CEILING"    . "CEILING")
  12.                         ("A4-0 EXT ELEVS"  . "ELEVATIONS")
  13.                         ("K2-0 KITCHEN"    . "KITCHEN")
  14.                     )
  15.                 )
  16.             )
  17.         )
  18.         (progn
  19.             (layerstate-restore "ALL-ON" nil 255)
  20.             (layerstate-restore las      nil 255)
  21.             (vla-regen (layerstate-doc) acallviewports)
  22.         )
  23.     )
  24.     (princ)
  25. )
  26.  
  27. (   (lambda ( )
  28.         (vl-load-com)
  29.             (if (= "layerstate-reactor" (vlr-data rtr))
  30.                 (vlr-remove rtr)
  31.             )
  32.         )
  33.             (vlr-miscellaneous-reactor "layerstate-reactor" '((:vlr-layoutswitched . layerstate-callback)))
  34.             'active-document-only
  35.         )
  36.         (eval (list 'defun 'layerstate-doc nil (vla-get-activedocument (vlax-get-acad-object))))
  37.         (princ)
  38.     )
  39. )
Title: Re: Creating a Layerstate Reactor
Post by: Grrr1337 on February 21, 2018, 02:09:40 PM
BTW the subfunctions in CAB's code are handy, as they provide alternative for the layerstate-restore (which is a floating sand <EXRXSUBR>).
Impressive work CAB, (considering its from 2004).
Title: Re: Creating a Layerstate Reactor
Post by: JohnK on February 21, 2018, 02:40:00 PM
BTW the subfunctions in CAB's code are handy, as they provide alternative for the layerstate-restore (which is a floating sand <EXRXSUBR>).
Impressive work CAB, (considering its from 2004).

*blink-blink* ...How about just: "...<snip>... Impressive work, CAB.".
Title: Re: Creating a Layerstate Reactor
Post by: Grrr1337 on February 21, 2018, 03:00:51 PM
*blink-blink* ...How about just: "...<snip>... Impressive work, CAB.".

His code looks solid, the 'impressive' is because he wrote it 14 years ago.
Now one can find examples/references/similar codes anywhere...

Didn't mean to sound "its a nice code but its old" :sad:
Hope now its clear what I ment.
Title: Re: Creating a Layerstate Reactor
Post by: JohnK on February 21, 2018, 03:59:00 PM
Ah, well okay, that's "situation normal" for TheSwamp. You've heard the saying: "We all stand on the shoulders of giants." Well, here be where they roam--or, in some cases, were but you'll know they were here by their foot prints-. This place has taught quite a few giants.

Take for example a recent event (which I know you've been around long enough to witness):
1. How many other forums do you know of that fixed up an old code syntax highlighter mod--made it theirs--and created a custom help file (put in a huge effort to make the code you post that much more presentable and just plain awesome-er). That was fourth time we've fixed that code highlighter mod thing gone to extra effort for our members.

EDIT:
And, you should also just accept CABs code is solid (be shocked when it's not).
Title: Re: Creating a Layerstate Reactor
Post by: GDF on February 21, 2018, 04:05:09 PM
Thanks guys

My head hurts...
Title: Re: Creating a Layerstate Reactor
Post by: Grrr1337 on February 21, 2018, 05:07:04 PM
Ah, well okay, that's "situation normal" for TheSwamp. You've heard the saying: "We all stand on the shoulders of giants." Well, here be where they roam--or, in some cases, were but you'll know they were here by their foot prints-. This place has taught quite a few giants.

This forum is full of members, that are giants -
Literally when you have a problem with your code, there are so many guys that could help you so you could not list their nicknames without forgetting someone.
Even you can't include in there the members you never knew exist and how skilled they are.
So the feeling is like playing poker - you don't know who what cards will reveal.

And the special thing I find about it (theswamp) is the type of threads - they help you to develop in knowledge.
See, in the other forums the people just ask for a complete routine that does bla-bla-bla and bla.
Someone helps out (usually a giant from theswamp) and thats it - if you are a start lisper you could learn something from that suggestion (if you want to)
But if you are rather experienced, then theres a small chance to find something new for you.
While here:

And, you should also just accept CABs code is solid (be shocked when it's not).

I guess we all like to revise someone else's suggestions (whatever the reason - learn/doubt/check), and point out if there are any possible problems (so we'll all know).
Title: Re: Creating a Layerstate Reactor
Post by: GDF on February 22, 2018, 09:33:23 AM
@Lee

Is there a wall using you routine to run a regenall? This is what is missing to show the effects of the layerstate restore.
I have tried the different settings in "layout regen" in options, and without success.

Gary
Title: Re: Creating a Layerstate Reactor
Post by: Lee Mac on February 22, 2018, 01:12:29 PM
Is there a wall way using your routine to run a regenall? This is what is missing to show the effects of the layerstate restore.

I have updated my earlier code.  :-)
Title: Re: Creating a Layerstate Reactor
Post by: GDF on February 22, 2018, 03:12:47 PM
WOW, THANK YOU SIR!!!

It works like a charm...I've never played around much with layer states. Doing work for a new client, and the use a lot of layer states.
I tied to use a command in your routine...duh, of course it did not work.

Gary

Title: Re: Creating a Layerstate Reactor
Post by: Lee Mac on February 22, 2018, 04:28:49 PM
You're welcome Gary, glad it helps.
Title: Re: Creating a Layerstate Reactor
Post by: GDF on February 22, 2018, 04:32:57 PM
I add:
(vla-zoomextents (vlax-get-acad-object))

Now it will do everthing in my wish list.

Thanks again
Title: Re: Creating a Layerstate Reactor
Post by: GDF on February 22, 2018, 07:04:40 PM
OOPS

In BricsCAD I get this message:

Error: bad argument type <NIL> ; expected ENTITYNAME at [layerstate-restore]
; LISP execution error : inside <LAYERSTATE-CALLBACK> at [VLR-Miscellaneous-Reactor:vlr-layoutSwitched]
Title: Re: Creating a Layerstate Reactor
Post by: Lee Mac on February 23, 2018, 07:55:28 AM
OOPS

In BricsCAD I get this message:

Error: bad argument type <NIL> ; expected ENTITYNAME at [layerstate-restore]
; LISP execution error : inside <LAYERSTATE-CALLBACK> at [VLR-Miscellaneous-Reactor:vlr-layoutSwitched]

This is due to BricsCAD's implementation of the layerstate-restore function - evidently the viewport argument is not considered optional in BricsCAD.

This will require a support request to BricsCAD to resolve.
Title: Re: Creating a Layerstate Reactor
Post by: roy_043 on February 23, 2018, 08:38:21 AM
... In the interim: use the BricsCAD vl-layerstates-restore function.
Title: Re: Creating a Layerstate Reactor
Post by: GDF on February 23, 2018, 11:44:42 AM
Thanks

I posted this error to the BricsCAD forum.
Title: Re: Creating a Layerstate Reactor
Post by: GDF on April 15, 2018, 11:13:59 AM
Will the following work?

(setq program (substr (getvar "acadver") 6 (- (strlen (getvar "acadver")) 1)))
(cond
             ((= program "BricsCAD")(vl-layerstates-restore (getvar "ctab") 6 NIL 0))
             ((/= program "BricsCAD")(layerstate-restore las      nil 255)))
Title: Re: Creating a Layerstate Reactor
Post by: GDF on April 15, 2018, 05:38:28 PM
Made some other changes...it now works.

(setq program (substr (getvar "acadver") 6 (- (strlen (getvar "acadver")) 1))) 
(if (= (getvar "program") "BRICSCAD")
              (vl-layerstates-restore (getvar "ctab") 6 NIL 0)
              (layerstate-restore las      nil 255)
            ) 
Title: Re: Creating a Layerstate Reactor
Post by: ronjonp on April 16, 2018, 09:54:07 AM
Gary,

You could also do something like this:
Code - Auto/Visual Lisp: [Select]
  1. (if vl-layerstates-restore
  2.   (vl-layerstates-restore (getvar "ctab") 6 nil 0)
  3.   (layerstate-restore las nil 255)
  4. )