Create List of Locked Layers. Lock & Unlock again.

Create List of Locked Layers. Lock & Unlock again.

Anonymous
Not applicable
3,360 Views
15 Replies
Message 1 of 16

Create List of Locked Layers. Lock & Unlock again.

Anonymous
Not applicable

Hello.

I'm trying to figure out how to create a list of all locked layers in a drawing. Then unlock those layers. Then lock them again. 

 

Thank you.

0 Likes
Accepted solutions (2)
3,361 Views
15 Replies
Replies (15)
Message 2 of 16

dlanorh
Advisor
Advisor
Accepted solution

Here are my VL routines. You pass in the list created by (rh:lock_lyr_lst)

 

;; unlock all layers : requires list of locked layer objects 
(defun rh:unlock_lyrs (lst)
  (mapcar '(lambda (x) (vlax-put-property x 'lock :vlax-false)) lst)
);end_defun
  
;; relock all previously locked layers : requires list of locked layer objects  
(defun rh:relock_lyrs (lst)
  (mapcar '(lambda (x) (vlax-put-property x 'lock :vlax-true)) lst)
);end_defun

;return list of locked layer objects : requires layer collection
(defun rh:lock_lyr_list (lyrs / lst)
  (if (= "AcDbLayerTable" (vlax-get-property lyrs 'objectname))
    (vlax-map-collection lyrs  '(lambda (x) (if (= :vlax-true (vlax-get-property x 'lock)) (setq lst (cons x lst)))))
  );end_if
  lst
);end_defun

(vl-load-com)

(defun c:test (/ c_doc lyrs lock_lst)
(setq c_doc (vla-get-activedocument (vlax-get-acad-object))
c_lyrs (vla-get-layers c_doc)
lock_lst (rh:lock_lyr_list c_lyrs);;THIS IS THE LIST OF LOCKED LAYER TO PASS INTO UNLOCK/LOCK
)
(rh:unlock_lyrs lock_lst)
;.....REST OF CODE
(rh:relock_lyrs lock_lst)
);end_defun

 

I am not one of the robots you're looking for

0 Likes
Message 3 of 16

Anonymous
Not applicable

Christ... yea ok i definitely couldn't have figured that out myself 😕 

 

That seems to work great though, thanks!

 

One thing I did change was I removed the Unlock parts and replaced it with  (command "-layer" "u" "*" "").

I figured it would do the same job right? A few less lines to deal with. 

0 Likes
Message 4 of 16

dlanorh
Advisor
Advisor

It does the same think but 10x slower for a single layer, not that you'd notice.  😁

I am not one of the robots you're looking for

0 Likes
Message 5 of 16

3wood
Advisor
Advisor

You can also use wildcard:

Command: -LAYER  UNLOCK  *

 

 

0 Likes
Message 6 of 16

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Hello.

I'm trying to figure out how to create a list of all locked layers in a drawing. Then unlock those layers. Then lock them again. 

 

Thank you.


 

(defun _Unlocked (layers / _LockedlayerState)
  (vlax-for itm	layers
    (if	(minusp (vlax-get itm 'Lock))
      (progn
	(setq _LockedlayerState (cons itm _LockedlayerState))
	(vlax-put itm 'Lock 0)
      )
    )
  )
  _LockedlayerState
)

(defun demo (/ c_doc lyrs lock_lst)
  (setq aDoc (vla-get-activedocument (vlax-get-acad-object))
        clayrs (vla-get-layers aDoc)		
        L_locked (_Unlocked clayrs)
  )
  
  ;<<< Do you thing here >>>
  
  (foreach itm L_locked	;<-- if not nil
    	(vlax-put itm 'Lock -1))
)

Unlocked the layer while in the process of creating the locked layer list.  

 

Message 7 of 16

Anonymous
Not applicable

Ok dlanorh, thanks for clearing that up. And yes that's right 3wood, that's what I was suggesting to dlanorh 👍

 

pbejse - I popped in the rest of my code into your routine & tried it out as well. It seems to work perfectly!

 

Thank you all for you're help, I really appreciate it. 

 
 
 
 
0 Likes
Message 8 of 16

Kent1Cooper
Consultant
Consultant

If you don't really need them as a list specifically, but a comma-delimited string will do, the re-locking becomes simpler:

(defun C:GLL (/ lay); = Get Locked Layers
  (while (setq lay (tblnext "layer" (not lay)))
    (if (= (logand (cdr (assoc 70 lay)) 4) 4); locked
      (setq LockedLayers (strcat (cond (LockedLayers) ("")) (cdr (assoc 2 lay)) ","))
    ); if
  ); while
); defun

(defun C:UAL (); = Unlock All Layers
  (command "_.layer" "_unlock" "*" "")
); defun

(defun C:RLL () ; = Relock [formerly] Locked Layers
  (command "_.layer" "_lock" LockedLayers "")
); defun
Kent Cooper, AIA
Message 9 of 16

azri_kun
Explorer
Explorer

This is very great code. It resolved what I'm looking for everywhere while other solution provided longer routine to somehow achieve it.

 

@Kent1Cooper Further to this routine, if i want to not only save the lock state of layers but all states (on/off, frozen/thawed, lock/unlock), how to go about to achieve this? I don't have any idea on how to modified the lisp around the dxf code. 

0 Likes
Message 10 of 16

Kent1Cooper
Consultant
Consultant

@azri_kun wrote:

.... if i want to not only save the lock state of layers but all states (on/off, frozen/thawed, lock/unlock), how to go about to achieve this? .... 


You've got the right word in there -- with a Layer STATE.  This is just what they're for.  -LAYER command, stAte option, Save -- give it a name, check that it's showing "Yes" on all the properties you want it to know about, and Enter a few times to back out.  Then when you've done whatever you needed to change Layer conditions for, and you want it all back the way it was, use the -LAYER / stAte / Restore option.

Kent Cooper, AIA
0 Likes
Message 11 of 16

azri_kun
Explorer
Explorer

Many thanks. Really appreciate you taking time to reply especially with the formatings.

 

I tried to use command as you mentioned, however during selection of states that I want to pick, the yes no option is globally save where it option will switch between yes and no and I have no way to know if user current setting is yes or no. Which mean I could flip the no to yes and yes to no. 

 

Is there any way to achieve this concretely to set yes or no for each states?

0 Likes
Message 12 of 16

john.uhden
Mentor
Mentor

@Kent1Cooper wrote "...but a comma-delimited string will do,..."

Not if the string exceeds 256 characters.

John F. Uhden

0 Likes
Message 13 of 16

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

@Kent1Cooper wrote "...but a comma-delimited string will do,..."

Not if the string exceeds 256 characters.


Not true.  An individual Layer's name cannot exceed 255 characters according to Help, but a comma-delimited string of Layer names can.  I just verified that using 6 Layer names, each 50 characters long, for a total string length of 305 characters including the commas.  A -LAYER command happily made them all in the New option, and LOcked them all, using that string.

Kent Cooper, AIA
0 Likes
Message 14 of 16

john.uhden
Mentor
Mentor

@Kent1Cooper 

Maybe the rules have changed over the years.  I was basing my claim on conditions in ACAD2002.

Is the same as you say true for Freeze and Thaw options?

John F. Uhden

0 Likes
Message 15 of 16

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

 

....

Is the same as you say true for Freeze and Thaw options?


Yes, and Color, and Unlock.  I didn't try all the other options, but I assume they'll all accept that kind of string.

Kent Cooper, AIA
0 Likes
Message 16 of 16

john.uhden
Mentor
Mentor

@Kent1Cooper 

Thanks for that verification, Kent.  It will undoubtedly change any future code I write having to deal with lots of layers.  Of course if the old ones still work, then why bother fixing them?

John F. Uhden

0 Likes