TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: GDF on February 12, 2018, 01:39:41 PM

Title: Layer States
Post by: GDF on February 12, 2018, 01:39:41 PM
Anyone have any good layer state routines, or comments to mine.

Layer states named after layout tab name:
Code: [Select]
(defun C:State (/ tmp)
  (initget "I X S R D")
  (setq tmp (getkword "\n* Layer States: [Import eXport - Save Restore - Delete]"))
  (initget 7) 
  (cond ((= tmp "I")(LSImportIT))
        ((= tmp "X")(LSXportIT))
        ((= tmp "S")(LSSaveIT))
        ((= tmp "R")(LSRestoreIT))
        ((= tmp "D")(LSDeleteIT)))
  (princ)
)
;;;
(defun ARCH:State (/ tmp) 
  (setq tmp (layerstate-has (getvar "ctab")))
  (if (= tmp T)(LSRestoreIT))   
  (princ)
)
;;;
(ARCH:State);place in your acaddoc file



(defun LSImportIT  (/ tmp)
  (setq tmp (strcat (getvar 'dwgprefix)(vl-string-right-trim ".dwg" (getvar 'dwgname)) " ctab-" (getvar "ctab") ".las"))
  (if (/= tmp nil)
    (progn
      (princ (strcat "\n* Importing LayerStates to: " tmp))
      (command "mspace")
      (layerstate-import tmp)
      (command "pspace")
    )
  )
  (princ))
;;;
(defun LSXportIT  (/ tmp)
  (setq tmp (strcat (getvar 'dwgprefix)(vl-string-right-trim ".dwg" (getvar 'dwgname)) " ctab-" (getvar "ctab") ".las"))
  (princ (strcat "\n* Exporting Layer States to: " tmp))
  (command "mspace")
  (layerstate-export (getvar "ctab") tmp)
  (command "pspace")
  (princ))
;;;
(defun LSSaveIT  ()
  (princ (strcat "\n* Saving New Layer States to: " (getvar "ctab")))
  (command "mspace")
  (layerstate-save (getvar "ctab") 255 nil)
  (command "pspace")
  (princ))
;;;
(defun LSRestoreIT  ()
  (princ (strcat "\n* Restoring Layer States from: " (getvar "ctab")))
  (command "mspace")
  (layerstate-restore (getvar "ctab") nil 255)
  (command "pspace")
  (princ))
;;;
(defun LSDeleteIT  ()
  (princ (strcat "\n* Deleting Layer States from: " (getvar "ctab")))
  (command "mspace")
  (layerstate-delete (getvar "ctab"))
  (command "pspace")
  (princ))

Gary

Title: Re: Layer States
Post by: John Kaul (Se7en) on February 12, 2018, 02:21:02 PM
I don't have much of my old lisp stuff anymore but I did find this one (not much, I know, but I hope it helps).

Code - Auto/Visual Lisp: [Select]
  1. (defun DeleteLayerStates ( / )
  2.   ;; Delete all layer states
  3.   ;; Ex: (DeleteLayerStates)
  4.   (vl-catch-all-apply
  5.     '(lambda ()
  6.        (vla-remove
  7.            (vla-get-layers
  8.              (vla-get-activedocument
  9.                (vlax-get-acad-object))))
  10.          "ACAD_LAYERSTATES")))
  11.   (princ)
  12.   )
Title: Re: Layer States
Post by: MP on February 12, 2018, 02:55:16 PM
Subset of one of my fav's (used when turning over dwgs to clients):

Code: [Select]
(defun _JennyCraig ( doc )
    ;;  Remove all layer states, layer filters and groups from the
    ;;  document (which may be an ObjectDBX document) but leave the
    ;;  layer's xdict intact in case it's used to store other data.
    ;;  Return T if any of the undesirables nuked.
    (   (lambda ( remove layers keys / owner result )
            (and
                (eq :vlax-true (vla-get-hasextensiondictionary layers))
                (setq owner (vla-getextensiondictionary layers))
                (foreach key keys (remove owner key))
            )
            result                 
        )
        (lambda ( owner key / lst offender )   
            (and
                (setq lst (list owner key))
                (setq offender (vl-catch-all-apply 'vla-item lst))
                (eq 'vla-object (type offender))
                (vl-catch-all-apply 'vla-remove lst)
                (vl-catch-all-error-p (vl-catch-all-apply 'vla-item lst))
                (setq result t) ;; lexical global
            )       
        )
        (vla-get-layers doc)
       '("ACAD_LAYERSTATES" "ACAD_LAYERFILTERS" "ACLYDICTIONARY")         
    )
)
Title: Re: Layer States
Post by: GDF on February 12, 2018, 03:53:20 PM
Thanks guys
Title: Re: Layer States
Post by: Ben Clark on February 13, 2018, 05:59:21 PM
Subset of one of my fav's (used when turning over dwgs to clients):

Code: [Select]
(defun _JennyCraig ( doc )
    ;;  Remove all layer states, layer filters and groups from the
    ;;  document (which may be an ObjectDBX document) but leave the
    ;;  layer's xdict intact in case it's used to store other data.
    ;;  Return T if any of the undesirables nuked.
    (   (lambda ( remove layers keys / owner result )
            (and
                (eq :vlax-true (vla-get-hasextensiondictionary layers))
                (setq owner (vla-getextensiondictionary layers))
                (foreach key keys (remove owner key))
            )
            result                 
        )
        (lambda ( owner key / lst offender )   
            (and
                (setq lst (list owner key))
                (setq offender (vl-catch-all-apply 'vla-item lst))
                (eq 'vla-object (type offender))
                (vl-catch-all-apply 'vla-remove lst)
                (vl-catch-all-error-p (vl-catch-all-apply 'vla-item lst))
                (setq result t) ;; lexical global
            )       
        )
        (vla-get-layers doc)
       '("ACAD_LAYERSTATES" "ACAD_LAYERFILTERS" "ACLYDICTIONARY")         
    )
)

I need to draw attention to your function name. Hilarious.
Title: Re: Layer States
Post by: MP on February 13, 2018, 06:38:44 PM
Consequence of 3 decades writing LISP.  :whistling:
Title: Re: Layer States
Post by: ronjonp on February 13, 2018, 10:32:10 PM
Subset of one of my fav's (used when turning over dwgs to clients):

Code: [Select]
(defun _JennyCraig ( doc )
    ;;  Remove all layer states, layer filters and groups from the
    ;;  document (which may be an ObjectDBX document) but leave the
    ;;  layer's xdict intact in case it's used to store other data.
    ;;  Return T if any of the undesirables nuked.
    (   (lambda ( remove layers keys / owner result )
            (and
                (eq :vlax-true (vla-get-hasextensiondictionary layers))
                (setq owner (vla-getextensiondictionary layers))
                (foreach key keys (remove owner key))
            )
            result                 
        )
        (lambda ( owner key / lst offender )   
            (and
                (setq lst (list owner key))
                (setq offender (vl-catch-all-apply 'vla-item lst))
                (eq 'vla-object (type offender))
                (vl-catch-all-apply 'vla-remove lst)
                (vl-catch-all-error-p (vl-catch-all-apply 'vla-item lst))
                (setq result t) ;; lexical global
            )       
        )
        (vla-get-layers doc)
       '("ACAD_LAYERSTATES" "ACAD_LAYERFILTERS" "ACLYDICTIONARY")         
    )
)

I need to draw attention to your function name. Hilarious.
MP has tidbits of funnies all throughout his comments in code. Always made me chuckle. 😁
Title: Re: Layer States
Post by: MP on February 14, 2018, 08:12:16 AM
Glad to hear Iíve brought lolz here and there. :laugh:
Title: Re: Layer States
Post by: BIGAL on February 15, 2018, 03:19:16 AM
Just me I a moving away from getkword where you have multiple choice say more than 3 to either a list dcl choice or something like Grrr's matrix dcl which only require a couple of lines to execute, as they use a library routine.