Author Topic: Layer testing question  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

KOWBOI

  • Guest
Layer testing question
« on: September 01, 2010, 04:04:16 PM »
I know how to save clayer to a variable and restore it but how do I go about testing for layers being frozen, on, off, existing and so forth?

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Layer testing question
« Reply #1 on: September 01, 2010, 04:09:42 PM »
Test for existence: look into tblsearch.

Test for ON/OFF: look into DXF code 62 of the layer entity

Text for Frozen etc: look into DXF code 70 of the layer entity

Reference here:

http://autodesk.com/techpubs/autocad/acad2000/dxf/

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Layer testing question
« Reply #2 on: September 02, 2010, 07:54:56 AM »
Here is an old one you can look at, not the best example.
Code: [Select]
;;;=================[ GetLayerProp.lsp ]=======================
;;; Author: Copyright© 2007 Charles Alan Butler
;;; Version:  1.0 Oct. 23, 2007
;;; Purpose: To get list of layers matching the property
;;; Sub_Routines: -None
;;;===========================================================
;;;  Argument: property
;;;  property is a flag for which layer property to get
;;;  property may be a single string or a list of strings
;;;  valid strings are not case sensitive:
;;;   "OFF" "FROZEN" "LOCKED"   "NoPLOT" "COLOR"
;;;   "ON"  "THAWED" "UNLOCKED" "PLOT"   "ALL"
;;;  If "ALL" is not included a list of list is returned
;;;  The lists are of matching layers, nil if none
;;;  The return list is in the order of the properties quired
;;;  "COLOR" returns a list of '(layername color)
;;;  Other options return a list of layer names
;;;  "ALL" is the flag to combine into one list where the layers
;;;   must meet all the conditions
;;;   Like All layers that are ON, THAWED & LOCKED
;;;  Properties that may not be used together with "ALL" are:
;;;   "OFF"     "ON"
;;;   "FROZEN"  "THAWED"
;;;   "LOCKED"  "UNLOCKED"
;;;   "NoPLOT"  "PLOT"
;;;   "COLOR"   "ALL"
;;;   If they are used togather with ALL, nil is returned
;;;   If COLOR is used with ALL, COLOR is ignored
(defun GetLayerProp (property / bit layName Offlayers Onlayers
                     Frozenlayers Thawedlayers NoPlotlayers Plotlayers
                     Lockedlayers UnLockedlayers itm lst ColorOk
                     Colorlayers layName Result IndexList)
  (setq lst '("OFF" "FROZEN" "LOCKED" "NOPLOT" "COLOR"
              "ON" "THAWED" "UNLOCKED" "PLOT"  "ALL"))
  (cond
    ((null property)
     (prompt "\n*** ERROR: NULL properties  ***")
     nil ; return nil
    )
    ((and (not (listp property)) ; single condition
          (setq property (list property))
          nil       ; cause a fall through
     )
    )
    ((and (setq property (mapcar 'strcase property))
          (vl-remove-if '(lambda (x) (member x lst)) property))
     (prompt "\n*** Error in properties name  ***")
      nil ;  error in properties name
     )
    (t
     (if (and (vl-position "ALL" property)(vl-position "COLOR" property))
       (progn
         (setq property (vl-remove "COLOR" property))
         (prompt "\n*** ERROR: Color property removed  ***")
       )
     )

     (setq ColorOk (vl-position "COLOR" property))
     ;;  Get list of layer names
     (vlax-for layobj (vla-get-layers
                        (vla-get-activedocument (vlax-get-acad-object)))
       (setq layName (vla-get-name layobj))
       (if (= (vla-get-layeron layobj) :vlax-false)
         (setq Offlayers (cons layName Offlayers))
         (setq Onlayers (cons layName Onlayers))
       )
       (if (= (vla-get-freeze layobj) ':vlax-true)
         (setq Frozenlayers (cons layName Frozenlayers))
         (setq Thawedlayers (cons layName Thawedlayers))
       )
       (if (= (vla-get-lock layobj) ':vlax-true)
         (setq Lockedlayers (cons layName Lockedlayers))
         (setq UnLockedlayers (cons layName UnLockedlayers))
       )
       (if (= (vla-get-plottable layobj) ':vlax-false)
         (setq NoPlotlayers (cons layName NoPlotlayers))
         (setq Plotlayers (cons layName Plotlayers))
       )
       (if ColorOk
         (setq Colorlayers
                (cons (list layName (vla-get-color layobj)) Colorlayers))
         )
       )
    )
  )
  ;;  return set up
  (setq IndexList  '(("ON" Onlayers)
                     ("OFF" Offlayers)
                     ("FROZEN" Frozenlayers)
                     ("THAWED" Thawedlayers)
                     ("LOCKED" Lockedlayers)
                     ("UNLOCKED" UnLockedlayers)
                     ("NOPLOT" NoPlotlayers)
                     ("PLOT" Plotlayers)
                     ("COLOR" Colorlayers)
                    ))
  (if (vl-position "ALL" property)
    (progn ; Return names common to all list in property
      (foreach itm IndexList
        (if (vl-position (car itm) property)
          (if Result
            (setq Result (vl-remove-if-not '(lambda (x) (member x Result)) (eval (cadr itm))))
            (setq Result (eval (cadr itm)))
          )
        )
      )
      Result
     )
     (progn ; Return individual list in order of request
       (foreach itm property
         (if (setq lst (eval (cadr (assoc itm IndexList))))
           (setq Result (cons lst Result))
         )                   
       )
      (setq Result (reverse Result))
    )
  )
  Result
)



(defun c:test(/ lst)
  ;;  return a single list with all of these properties
  ;;  i.e.  Frozen and locked and on
  (setq lst '("ALL" "FROZEN" "LOCKED" "ON"))
 
  ;;  i.e.  Frozen and locked and off
  (setq lst '("ALL" "FROZEN" "LOCKED" "OFF"))
 
  ;;  i.e.  Frozen and locked, could be on or off
  (setq lst '("ALL" "FROZEN" "LOCKED"))

  ;;  return a list of off and a seperate list of locked
  (setq lst '("OFF" "locked"))
 
  ;;  return a list of off and a seperate list of color
  (setq lst '("OFF" "color"))
 
  (setq lst '("ALL" "FROZEN" "LOCKED" "ON"))
  (setq result (GetLayerProp lst))
  (princ)
  )
           
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Layer testing question
« Reply #3 on: September 02, 2010, 07:56:19 AM »
Another example. Not sure of the author.
Code: [Select]
(defun LayerLocked (lname / ent)
  (if (setq ent (tblobjname "layer" lname))
    (= 4 (logand 4 (cdr (assoc 70 (entget ent)))))
  )
)

(defun layer_status (lname / ent bit elst)
  (if (setq ent (tblobjname "layer" lname))
    (progn
      (setq elst (entget ent)
    bit (cdr (assoc 70 elst))
      )
      (prompt (strcat "\nLayer " lname " is "))
      (if (= 1 (logand 1 bit))
(princ "frozen ")
(princ "thawed ")
      )
      (if (= 2 (logand 2 bit))
(princ "(frozen in new viewport) ")
(princ "(thawed in new viewport) ")
      )
      (if (= 4 (logand 4 bit))
(princ "locked ")
(princ "unlocked ")
      )
      (if (minusp (cdr (assoc 62 elst)))
(princ "off ")
(princ "on ")
      )
      (if (zerop (cdr (assoc 290 elst)))
(princ "non-plotting")
(princ "plotting-on")
      )
      (prompt "\nLayer not found in drawing")
    ) ; end progn
  ) ; endif
) ; end defun

(defun c:ltest (/ lname)
  (if (setq lname (getstring "Enter a layer name. "))
    (layer_status lname)
  )
)

(defun layer_status2 (lname / ent bit elst state status)
  (if (setq ent (tblobjname "layer" lname))
    (progn
      (setq elst   (entget ent)
    bit    (cdr (assoc 70 elst))
    status '()
      )
      (setq status (cons (if (= 1 (logand 1 bit))
   "frozen"
   "thawed"
)
status
   )
      )
      (setq status (cons (if (= 2 (logand 2 bit))
   "NewVPfrozen"
   "NewVPthawed"
)
status
   )
      )
      (setq status (cons (if (= 4 (logand 4 bit))
   "locked"
   "unlocked"
)
status
   )
      )
      (setq status (cons (if (minusp (cdr (assoc 62 elst)))
   "off"
   "on"
)
status
   )
      )
      (setq status (cons (if (zerop (cdr (assoc 290 elst)))
   "non-plotting"
   "plotting-on"
)
status
   )
      )
    ) ; end progn
  ) ; endif
  status
) ; end defun


(defun c:ltest2 (/ lname)
  (if (setq lname (getstring "Enter a layer name. "))
    (if (setq st (layer_status2 lname))
      (progn
    (prompt "\nlayer status: ")
      (foreach x st
(prompt (strcat x " "))
      )
    )
    )
  )
  (princ)
)




;;;  Returns T if On
(defun islayeron (lname / entlst)
  (and (= (type lname) 'str)
       (setq entlst (tblsearch "layer" lname))
       (null (minusp (cdr (assoc 62 entlst))))
  )
) ; end defun


;;;  Returns T if Frozen
(defun islayerfrozen (lname / entlst)
  (and (= (type lname) 'str)
       (setq entlst (tblsearch "layer" lname))
       (= 1 (logand 1 (cdr (assoc 70 entlst))))
  )
) ; end defun


;;;  Returns T if Plot is Off
(defun islayerplotoff (lname / ent)
  (and (= (type lname) 'str)
       (setq ent (tblobjname "layer" lname))
       (zerop (cdr (assoc 290 (entget ent))))
  )
) ; end defun


;;;  Returns T if Layer is Frozen In New View Port
(defun islayerfrozeninnewvp (lname / entlst)
  (and (= (type lname) 'str)
       (setq entlst (tblsearch "layer" lname))
       (= 2 (logand 2 (cdr (assoc 70 entlst))))
  )
) ; end defun
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: Layer testing question
« Reply #4 on: September 02, 2010, 09:17:46 AM »
Here is my version that will just generate a list of layers that are visible (not off) in the drawing.

If you have several layers you need to check, instead of checking each and every one you can assemble a list (like below) and check to see if they are in the list.

However, if you need to check just a few layers use another method. That method can be gleaned from the code below (Look up the "tblobjname" function in the help files).

Code: [Select]
(defun Drawing-Visible-Layers-lst ( / mlst x get-layer)
  ;; Create a list of layers in the drawing.
  ;;
  ;; This proced will itterate thru the layers and
  ;; assemble a list of all the visible (non frozen
  ;; or off) layers in the drawing.
  ;;
  ;; 70 - Standard flags (bit-coded values):
  ;; 1  = Layer is frozen; otherwise layer is thawed
  ;; ...
  ;; 4  = Layer is locked
  ;; ...
  ;; 62 - Color number (if negative, layer is off)
  ;;
  ;;
  ;; By: John (Se7en) K
  ;;     12.20.06
    (setq get-layer '(setq x (tblnext "LAYER" (null mlst))))
    (while (eval get-layer)
      (if (and (cond
                 ((= (cdr (assoc 70 x)) 0) T)
                 ((= (cdr (assoc 70 x)) 4) T))
               (> (cdr (assoc 62 x)) 0) );_ end and
        (setq mlst (append mlst (list (cdr (cadr x)))))
        (setq get-layer '(setq x (tblnext "LAYER" nil)))))
    mlst
    )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Layer testing question
« Reply #5 on: September 02, 2010, 02:36:21 PM »
Having some fun  8-)

Code: [Select]
(defun LM:GetLayerStatus ( / FindBits List->String cAssoc IterateLayers )
  ;; © Lee Mac 2010

  (defun FindBits ( n / b )
    (if (< 0 n)
      (cons
        (setq b
          (expt 2
            (fix (/ (log n) (log 2)))
          )
        )
        (FindBits (- n b))
      )
    )
  )

  (defun List->String ( lst del )
    (if (cdr lst)
      (strcat (car lst) del (List->String (cdr lst) del))
      (car lst)
    )
  )

  (defun cAssoc ( key lst ) (cdr (assoc key lst)))

  (defun IterateLayers ( def )
    (
      (lambda ( status )
        (if def
          (cons
            (cons (cAssoc 2 def)
              (List->String
                (apply 'append
                  (cons (if (minusp (cAssoc 62 def)) (list "Off"))
                    (mapcar
                      (function
                        (lambda ( c ) (list (cAssoc c status)))
                      )
                      (FindBits (logand (cAssoc 70 def) (~ 112)))
                    )
                  )
                )
                ","
              )
            )
            (IterateLayers (tblnext "LAYER"))
          )
        )
      )
     '((1 . "Frozen")
       (2 . "Frozen in VP")
       (4 . "Locked")
      )
    )
  )

  (foreach x (IterateLayers (tblnext "LAYER" T))
    (if (cdr x)
      (print x)
    )
  )

  (princ)
)

   
« Last Edit: September 02, 2010, 06:17:49 PM by Lee Mac »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Layer testing question
« Reply #6 on: September 02, 2010, 05:34:19 PM »
Going recursive all the way... Very interesting Lee!
Small problem with xref-dependent layers: change line 39 to:
Code: [Select]
                     (FindBits (logand (cAssoc 70 def) (~ 112)))
« Last Edit: September 02, 2010, 06:00:15 PM by roy_043 »

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Layer testing question
« Reply #7 on: September 02, 2010, 06:16:59 PM »
Going recursive all the way... Very interesting Lee!

Thanks Roy  8-)

Small problem with xref-dependent layers: change line 39 to:
Code: [Select]
                      (FindBits (logand (cAssoc 70 def) (~ 112)))

Ahh yes...

Nice method, I was thinking something like:

Code: [Select]
(boole 4 112 (cAssoc 70 def))

(boole 2 (cAssoc 70 def) 112)

But I do like your method, its more obvious to read  :-)
« Last Edit: September 02, 2010, 06:28:42 PM by Lee Mac »