TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: alanjt on March 20, 2008, 10:51:01 AM

Title: check to see if a layer is on/off
Post by: alanjt 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)?
Title: Re: check to see if a layer is on/off
Post by: T.Willey on March 20, 2008, 10:59:36 AM
Yes.
Title: Re: check to see if a layer is on/off
Post by: alanjt 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?
Title: Re: check to see if a layer is on/off
Post by: T.Willey 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^
Title: Re: check to see if a layer is on/off
Post by: ronjonp 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")
  )
)
Title: Re: check to see if a layer is on/off
Post by: JohnK on March 20, 2008, 12:03:13 PM
For whats its worth, the AutoLisp version will be the fastest hands down.
Title: Re: check to see if a layer is on/off
Post by: alanjt 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)
)
Title: Re: check to see if a layer is on/off
Post by: ronjonp 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)
)
Title: Re: check to see if a layer is on/off
Post by: alanjt on March 20, 2008, 01:35:32 PM
that's so nice, i have GOT to learn vlisp!
thank you
Title: Re: check to see if a layer is on/off
Post by: T.Willey 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))
Title: Re: check to see if a layer is on/off
Post by: daron on March 20, 2008, 02:19:49 PM
Of course, if you really want to get into it, >>this<< (http://www.theswamp.org/index.php?topic=21246.msg257616#msg257616) is some really chewy rawhide.
Title: Re: check to see if a layer is on/off
Post by: gile 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")))
)
Title: Re: check to see if a layer is on/off
Post by: gile 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)
)
Title: Re: check to see if a layer is on/off
Post by: FengK 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.
Title: Re: check to see if a layer is on/off
Post by: JohnK 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>
Title: Re: check to see if a layer is on/off
Post by: FengK on March 21, 2008, 02:09:11 PM
Seven, thanks for the test and pointing me to the Benchmarking utility.