Author Topic: check to see if a layer is on/off  (Read 5993 times)

0 Members and 1 Guest are viewing this topic.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
check to see if a layer is on/off
« on: March 20, 2008, 10:51:01 AM »
is it possible to check to see if a specific layer is on (in code of course)?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

T.Willey

  • Needs a day job
  • Posts: 5251
Re: check to see if a layer is on/off
« Reply #1 on: March 20, 2008, 10:59:36 AM »
Yes.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: check to see if a layer is on/off
« Reply #2 on: March 20, 2008, 11:05:20 AM »
hmm  :? , let me reiterate:
is it possible to check to see if a specific layer is on (in code of course), and if so, how?
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

T.Willey

  • Needs a day job
  • Posts: 5251
Re: check to see if a layer is on/off
« Reply #3 on: March 20, 2008, 11:13:56 AM »
Depends which way you like.

Lisp
Code: [Select]
(<
    (cdr (assoc 62 (entget (tblobjname "layer" "LayerNameHere"))))
    0
)
True if layer off^

ActiveX
Code: [Select]
(vla-get-LayerOn
    (vla-Item
        (vla-get-Layers (vla-get-ActiveDocument (vlax-get-Acad-Object)))
        "LayerNameHere"
    )
)
True if layer on^
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: check to see if a layer is on/off
« Reply #4 on: March 20, 2008, 11:33:12 AM »
Or a combination of the two  :-)

Code: [Select]
(vla-get-LayerOn
  (vlax-ename->vla-object
    (tblobjname "layer" "LayerNameHere")
  )
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: check to see if a layer is on/off
« Reply #5 on: March 20, 2008, 12:03:13 PM »
For whats its worth, the AutoLisp version will be the fastest hands down.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: check to see if a layer is on/off
« Reply #6 on: March 20, 2008, 01:13:52 PM »
SUCCESS!
exactly what i wanted, thank you thank thank you!!!

Code: [Select]
(defun c:cn()
(if
(<
    (cdr (assoc 62 (entget (tblobjname "layer" (getvar "clayer")))))
    0
)
(progn
(command "-layer" "on" (getvar "clayer") "")
(princ "\n The curren layer: ")(princ (getvar "clayer"))(princ " has been turned on!")
);progn
(progn
(command "-layer" "off" (getvar "clayer") "Y" "")
(princ "\n The curren layer: ")(princ (getvar "clayer"))(princ " has been turned off!")
);progn
);if
(princ)
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

ronjonp

  • Needs a day job
  • Posts: 7526
Re: check to see if a layer is on/off
« Reply #7 on: March 20, 2008, 01:26:39 PM »
Just some food for thought  :-)

Code: [Select]
(defun c:wheee (/ lay)
  (vl-load-com)
  (if (= (vla-get-LayerOn
   (setq lay (vlax-ename->vla-object
       (tblobjname "layer" (getvar 'clayer))
     )
   )
)
:vlax-true
      )
    (vla-put-layeron lay :vlax-false)
    (vla-put-layeron lay :vlax-true)
  )
  (princ)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: check to see if a layer is on/off
« Reply #8 on: March 20, 2008, 01:35:32 PM »
that's so nice, i have GOT to learn vlisp!
thank you
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

T.Willey

  • Needs a day job
  • Posts: 5251
Re: check to see if a layer is on/off
« Reply #9 on: March 20, 2008, 01:44:45 PM »
Or
Code: [Select]
(defun whee (lay / Num EntData)
   
    (if
        (<
            (setq Num
                (cdr
                    (assoc
                        62
                        (setq EntData (entget (tblobjname "layer" lay)))
                    )
                )
            )
            0
        )
        (entmod
            (subst
                (cons 62 (abs Num))
                (assoc 62 EntData)
                EntData
            )
        )
    )
)
Called for the current layer like
Code: [Select]
(whee (getvar 'CLayer))
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

daron

  • Guest
Re: check to see if a layer is on/off
« Reply #10 on: March 20, 2008, 02:19:49 PM »
Of course, if you really want to get into it, >>this<< is some really chewy rawhide.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: check to see if a layer is on/off
« Reply #11 on: March 20, 2008, 02:52:17 PM »
Hi,

My 2 cents

Code: [Select]
((lambda (lay-lst)
   (entmod
     (subst (cons 62 (* -1 (cdr (assoc 62 lay-lst))))
    (assoc 62 lay-lst)
    lay-lst
     )
   )
 )
  (entget (tblobjname "LAYER" (getvar "CLAYER")))
)
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: check to see if a layer is on/off
« Reply #12 on: March 20, 2008, 06:32:47 PM »
I had not read Se7en's reply in Daron's link when I posted the upper code, it looks like an imitation.

So, the same way, more like alanjt's

Code: [Select]
(defun c:cn (/ elst col)
  (setq elst (entget (tblobjname "LAYER" (getvar "CLAYER")))
col  (cdr (assoc 62 elst))
  )
  (entmod (subst (cons 62 (* -1 col)) (cons 62 col) elst))
  (princ (strcat
   "\n The current layer: "
   (getvar "CLAYER")
   " has been turned "
   (if (minusp col) "on" "off")
)
  )
  (princ)
)
Speaking English as a French Frog

FengK

  • Guest
Re: check to see if a layer is on/off
« Reply #13 on: March 21, 2008, 12:45:15 PM »
For whats its worth, the AutoLisp version will be the fastest hands down.

Seven, I'm curious how much faster is the Autolisp version? And do you need to repeat the function many times to see a meaningful (say 0.1 second) difference? Thanks.

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: check to see if a layer is on/off
« Reply #14 on: March 21, 2008, 01:30:10 PM »
Used alone, You would have to have quite a few layers to see the diff but that's not really the point; if we try to keep each procedure small and nimble, we can have faster applications overall (when we do a bunch of stuff to a bunch of stuff). 

However, take a look at the Overall process' of each kind of attack. The first and the last, reach and grab the layer from the tbl directly, the second grabs the entire tbl and does a search on it...used several times the second procedure would chew up a lot of time compared to the others. Procedures one and three are fairly close, the only loss in the two is three's need to seek outside the built in Autolisp language functions (the VL extension is just that an extension, a DLL used by VL and VBA.). SO, if you can use native Autolisp functions instead of VL extensions, do it. It could save a few seconds in a large procedure. (I hope that makes sense)

To test the speed use Benchmark (theSwamp's default A/VutoLisp speed tester application.)
[ http://www.theswamp.org/index.php?topic=3952.0 ]

Here are three (posted here) that will demonstrate common attacks most AutoLispers would take.

Code: [Select]
;; benchmark: of getting layer props (checking to see if on)

;; alisp version

(defun test-alisp ( LayerNameHere / )
  (< (cdr (assoc 62 (entget (tblobjname "LAYER" LayerNameHere)))) 0) )
(defun test-vlisp1 ( LayerNameHere / )
  (vla-get-LayerOn
    (vla-Item
      (vla-get-Layers (vla-get-ActiveDocument (vlax-get-Acad-Object)))
      LayerNameHere)) )
(defun test-vlisp2 ( LayerNameHere / )
  (vla-get-LayerOn
    (vlax-ename->vla-object
      (tblobjname "layer" LayerNameHere))) )
(benchmark
  '( (test-alisp "0")
     (test-vlisp1 "0")
     (test-vlisp2 "0")) )

Elapsed milliseconds / relative speed for 32768 iteration(s):

    (TEST-ALISP "0")......1547 / 2.05 <fastest>
    (TEST-VLISP2 "0").....2125 / 1.49
    (TEST-VLISP1 "0").....3172 / 1 <slowest>
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org