Author Topic: Get Layer Object from LayerName  (Read 6145 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 495
Get Layer Object from LayerName
« on: April 01, 2014, 11:56:36 PM »
How to get Layer Object from layername as string for doing various operations such as (vla-put-LayerOn Layerobj :vlax-false).

I am aware of command line method but i want to use the vla- method.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #1 on: April 01, 2014, 11:59:54 PM »

Where are Layers stored ?

How do you access an item in a collection ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get Layer Object from LayerName
« Reply #2 on: April 02, 2014, 12:36:00 AM »
Example
Code - Auto/Visual Lisp: [Select]
Note that throws an error if the layer doesn't exist, so you might have to wrap it inside a vl-catch-all-apply.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #3 on: April 02, 2014, 12:43:04 AM »
I was teaching fishing irne, not selling filleted snapper.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #4 on: April 02, 2014, 12:52:29 AM »
acadauto.chm 
for 2013 and later have VLisp examples as well as VBA

Search for LayerOn
-> Example

Visual LISP Example
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:Example_LayerOn()
  3.     ;; This example creates a new layer called "LayerOn".
  4.     ;; It then displays the status of the LayerOn property
  5.     ;; for the new layer, toggles the status of the
  6.     ;; LayerOn property, and again displays its status.
  7.     ;; After running this example, you can check the layer
  8.     ;; control on the Object Properties tool bar. It will
  9.     ;; show the new layer and the latest LayerOn status.
  10.     (setq acadObj (vlax-get-acad-object))
  11.     (setq doc (vla-get-ActiveDocument acadObj))
  12.  
  13.     ;; Create the new layer
  14.     (setq layerObj (vla-Add (vla-get-Layers doc) "LayerOn"))
  15.    
  16.     ;; Display the LayerOn status of the new layer
  17.     (if (= (vla-get-LayerOn layerObj) :vlax-true)
  18.         (alert (strcat "Layer " (vla-get-Name layerObj) " is turned on."))
  19.         (alert (strcat "Layer " (vla-get-Name layerObj) " is turned off."))
  20.     )
  21.  
  22.     ;; Toggle the status of the LayerOn property for the layer
  23.     (vla-put-LayerOn layerObj (if (= (vla-get-LayerOn layerObj) :vlax-true) :vlax-false :vlax-true))
  24.    
  25.     ;; Display the LayerOn status of the new layer
  26.     (if (= (vla-get-LayerOn layerObj) :vlax-true)
  27.         (alert (strcat "Layer " (vla-get-Name layerObj) " is turned on."))
  28.         (alert (strcat "Layer " (vla-get-Name layerObj) " is turned off."))
  29.     )
  30. )
  31.  
  32.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get Layer Object from LayerName
« Reply #5 on: April 02, 2014, 01:03:03 AM »
I was teaching fishing irne, not selling filleted snapper.
:-P
I thought I'd spoon feed a bit ... note I even attached the bib with the catch-all-apply comment  :lol:
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #6 on: April 02, 2014, 01:06:56 AM »
And some pre-digested stuff with the bib attached for the Library.
 ... you do keep a Library, don't you ?

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;;------------------------------------------------------------------
  3. ;;;------------------------------------------------------------------
  4. ;; Makes the Layer if reqd and sets active and returns Layer Object
  5. ;;; (setq layername "ST-WELDx") (kdub:assertactivelayer layername)
  6. ;;
  7. (defun kdub:assertactivelayer (layername / iacadlayer)
  8.   (if (setq iacadlayer (kdub:assertlayer layername))
  9.       iacadlayer
  10.     )
  11.   )
  12.   iacadlayer
  13. )
  14. ;;;------------------------------------------------------------------
  15. ;;;------------------------------------------------------------------
  16. ;;; Makes the Layer if reqd and returns Layer Object
  17. ;;; (kdub:assertLayer "ST25x")
  18. ;;
  19. (defun kdub:assertlayer (layername / iacadlayer)
  20.         (setq iacadlayer
  21.                (vl-catch-all-apply
  22.                  'vla-add
  23.                  (list (vla-get-layers
  24.                          (vla-get-activedocument (vlax-get-acad-object))
  25.                        )
  26.                        layername
  27.                  )
  28.                )
  29.         )
  30.       )
  31.     nil
  32.     iacadlayer
  33.   )
  34. )
  35. ;;;------------------------------------------------------------------
  36. ;;;------------------------------------------------------------------
  37. ;;;
  38.  
  39.  
« Last Edit: April 02, 2014, 01:13:03 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Get Layer Object from LayerName
« Reply #7 on: April 02, 2014, 01:30:56 AM »
... you do keep a Library, don't you ?
Nah! Just throw it on the floor  :-o

Seriously though. Kerry makes a very good point. It's one of the main reasons the convention is to have each function do only one thing. That way you can have all these functions in a library and simply call them instead of copy-pasting (or worse re-writing) code again and again.

For me I'd split this LayerOn function into at least 3:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun ib:GetLayerObject  (Doc Name / result)
  3.              (setq result (vl-catch-all-apply 'vla-Item (list (vla-get-Layers Doc) Name)))))
  4.     result))
  5.  
  6. (defun ib:LayerOn (Doc Name On / layObj)
  7.   (if (setq layObj (ib:GetLayerObject Doc Name))
  8.     (vla-put-LayerOn layObj On)))
  9.  
  10. ;; Example:
  11. ;; (ib:LayerOn (ib:ActiveDocument) "MyLayer" :vlax-on)
That way the same code would even be usable if the document is an ObjectDBX document.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #8 on: April 02, 2014, 02:10:45 AM »
< .. > It's one of the main reasons the convention is to have each function do only one thing. That way you can have all these functions in a library and simply call them instead of copy-pasting (or worse re-writing) code again and again.
< ..>

Yes Irne, that's what I do with my production code.
The functions I posted actually have been modified to make them more palatable here.

Personally I attempt to design modular functions (where practical) to follow the Single Responsibility Principle
This makes testing a breeze.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Get Layer Object from LayerName
« Reply #9 on: April 02, 2014, 08:52:22 AM »
And a mishmash:  :)
Code: [Select]
(defun _layob (name / l)
  (if (setq l (tblobjname "layer" name))
    (vlax-ename->vla-object l)
  )
)
;; (_layob "0")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mailmaverick

  • Bull Frog
  • Posts: 495
Re: Get Layer Object from LayerName
« Reply #10 on: April 02, 2014, 10:05:47 AM »
RON's method is the best and fastest too.

Code: [Select]
(defun c:test ()
;;; 1.) RON's method
  (setq Start (getvar "Millisecs"))
  (repeat 25000 (setq cv (_layob "0")))
  (setq End (getvar "Millisecs"))
  (setq yu (- End Start))
  (setq yu (/ yu 1000.00))
  (princ "\nTime Taken to Run in Ron's Method : ")
  (princ yu)
  (princ " seconds \n")
;;; 2.) IRNEB's 1st method
  (setq Start (getvar "Millisecs"))
  (repeat 25000
    (setq
      cv (vla-Item (vla-get-Layers
     (vla-get-ActiveDocument (vlax-get-acad-object))
   )
   "0"
)
    )
  )
  (setq End (getvar "Millisecs"))
  (setq yu (- End Start))
  (setq yu (/ yu 1000.00))
  (princ "\nTime Taken to Run in IRNEB's 1st Method : ")
  (princ yu)
  (princ " seconds \n")
;;; 3.) IRNEB's 2nd method
  (setq Start (getvar "Millisecs"))
  (repeat 25000
    (setq cv (ib:GetLayerObject
       (vla-get-ActiveDocument (vlax-get-acad-object))
       "0"
     )
    )
  )
  (setq End (getvar "Millisecs"))
  (setq yu (- End Start))
  (setq yu (/ yu 1000.00))
  (princ "\nTime Taken to Run in IRNEB's 2nd Method : ")
  (princ yu)
  (princ " seconds \n")
  (princ)
)

(defun _layob (name / l)
  (if (setq l (tblobjname "layer" name))
    (vlax-ename->vla-object l)
  )
)

(defun ib:GetLayerObject (Doc Name / result)
  (if (not (vl-catch-all-error-p
     (setq result (vl-catch-all-apply
    'vla-Item
    (list (vla-get-Layers Doc) Name)
  )
     )
   )
      )
    result
  )
)

Results :
Time Taken to Run in Ron's Method : 1.279 seconds

Time Taken to Run in IRNEB's 1st Method : 2.496 seconds

Time Taken to Run in IRNEB's 2nd Method : 2.636 seconds
« Last Edit: April 02, 2014, 10:28:07 AM by mailmaverick »

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Get Layer Object from LayerName
« Reply #11 on: April 02, 2014, 10:07:03 AM »
< .. > It's one of the main reasons the convention is to have each function do only one thing. That way you can have all these functions in a library and simply call them instead of copy-pasting (or worse re-writing) code again and again.
< ..>

Yes Irne, that's what I do with my production code.
The functions I posted actually have been modified to make them more palatable here.

Personally I attempt to design modular functions (where practical) to follow the Single Responsibility Principle
This makes testing a breeze.

And upgrades as well.  Treat it as a black box (small "b"   ;-) ), and as long as the inputs and outputs are the same what's inside doesn't matter.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #12 on: April 02, 2014, 10:07:45 AM »
 
I think ronjonp's method is the best.


:-D
Yes, I like ron's offering.

I'm not arguing with you, but I wonder how/why you came to that determination ?
and what do you mean by "best" ?
« Last Edit: April 02, 2014, 10:19:08 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #13 on: April 02, 2014, 10:09:50 AM »
< .. > It's one of the main reasons the convention is to have each function do only one thing. That way you can have all these functions in a library and simply call them instead of copy-pasting (or worse re-writing) code again and again.
< ..>

Yes Irne, that's what I do with my production code.
The functions I posted actually have been modified to make them more palatable here.

Personally I attempt to design modular functions (where practical) to follow the Single Responsibility Principle
This makes testing a breeze.

And upgrades as well.  Treat it as a black box (small "b"   ;-) ), and as long as the inputs and outputs are the same what's inside doesn't matter.

Yes David, I agree.

Had a cackle about the (small "b"   ;-) )   ;-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Get Layer Object from LayerName
« Reply #14 on: April 02, 2014, 04:33:37 PM »
I love the way you make people work for their knowledge, Kerry. I know it's benefited me over the years.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #15 on: April 02, 2014, 04:45:32 PM »
I love the way you make people work for their knowledge, Kerry. I know it's benefited me over the years.


Nah, I'm just a nasty bastard who enjoys inflicting pain.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

BlackBox

  • King Gator
  • Posts: 3770
Re: Get Layer Object from LayerName
« Reply #16 on: April 02, 2014, 04:54:04 PM »
Example
Code - Auto/Visual Lisp: [Select]
Note that throws an error if the layer doesn't exist, so you might have to wrap it inside a vl-catch-all-apply.

FWIW -

The Add() Method will yield an existing, or create a new LayerTableRecord for the LayerName (string) passed, which is great for reactors that set a specific layer current prior to a given command execution, etc.

For the OP's situation though, the Item() Method is perfectly fine, given that the string passed is from a selected entity.

Cheers
"How we think determines what we do, and what we do determines what we get."

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #17 on: April 02, 2014, 04:58:21 PM »

< .. >  but i want to use the vla- method.

mailmaverick,
When did you change your mind about what you wanted ?

1.2 seconds time difference for 25,000 iterations is miniscule.

I probably spend longer than that every 5 minutes finding a key on the keyboard.

Just to be fair , what happens is you run each of those routines on a drawing which doesn't contain the layer you want ?



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Get Layer Object from LayerName
« Reply #18 on: April 02, 2014, 05:12:35 PM »
I love the way you make people work for their knowledge, Kerry. I know it's benefited me over the years.


Nah, I'm just a nasty bastard who enjoys inflicting pain.
lol. Well, then I enjoy watching you annoying the hell out of everyone.  :-P
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Get Layer Object from LayerName
« Reply #19 on: April 02, 2014, 05:18:39 PM »
[< .. >
For the OP's situation though, the Item() Method is perfectly fine, given that the string passed is from a selected entity.

Cheers

I didn't know that (must have missed the memo) . If so, all the rules change.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

BlackBox

  • King Gator
  • Posts: 3770
Re: Get Layer Object from LayerName
« Reply #20 on: April 02, 2014, 05:22:23 PM »
[< .. >
For the OP's situation though, the Item() Method is perfectly fine, given that the string passed is from a selected entity.

Cheers

I didn't know that (must have missed the memo) . If so, all the rules change.

I must have confused two different threads, regarding the selected entity part.  :-D
"How we think determines what we do, and what we do determines what we get."