LSP/LISP routine that will turn on or off layers in multiple drawings

LSP/LISP routine that will turn on or off layers in multiple drawings

vsmith9HYC2
Participant Participant
4,128 Views
28 Replies
Message 1 of 29

LSP/LISP routine that will turn on or off layers in multiple drawings

vsmith9HYC2
Participant
Participant

I love LSP routines. The BFIND is my most used LSP and has saved me hours of drawing edits on each job.

Now I just need to find one that can turn on or off a specified layer in all the drawings in a folder.

I found a couple in searches but 2 of them don't have the lsp file I need. One has a great LSP file but it only works in an open drawing. I'm looking for one that will make the change to all drawings in the same folder.

Any help is appreciated. Thanks!

Ciao y'all!

0 Likes
4,129 Views
28 Replies
Replies (28)
Message 21 of 29

nrussell4CW5T
Contributor
Contributor

so where would i add this in the code? to regenerate the drawing after it thaws it?

0 Likes
Message 22 of 29

nrussell4CW5T
Contributor
Contributor

Never mind, I just added REGENALL to the end of the macro button, so now it regenerates all

0 Likes
Message 23 of 29

nrussell4CW5T
Contributor
Contributor

Nevermind, it did not work, how would i change the function to regenerate the drawing as it iterates through it?

0 Likes
Message 24 of 29

ec-cad
Collaborator
Collaborator

(command "_regen")

 

 

0 Likes
Message 25 of 29

nrussell4CW5T
Contributor
Contributor

Still does not work, it seems to only regen the current drawing that im in, and not the drawing before it iterates to the next drawing. This is the code:

(defun c:batchthaw ( / lay lst )
(if (and (setq lay (getlayernames "Select Layer(s) to Thaw"))
(setq lay (mapcar 'strcase lay))
(setq lst (LM:getfiles "Select drawings to process" nil "dwg"))
)
(progn
(LM:odbx
(function
(lambda ( doc )
(vlax-for x (vla-get-layers doc)
(if (member (strcase (vla-get-name x)) lay)
(vl-catch-all-apply 'vla-put-freeze (list x :vlax-false))
(command "._regen")
)
)
)
)
lst t
)
(princ (strcat "\n" (itoa (length lst)) " drawing(s) processed."))
)
)
(princ)
)

0 Likes
Message 26 of 29

JamesMaeding
Advisor
Advisor

@nrussell4CW5T 

Your code does not open the other drawings, is part of it missing?

If you open "dbx document" style, it will run really fast, but its not a full open so you can't see it or regen.

The disadvantage of that is the dwg will not have xref layer added since last full open.

I'm trying to solve that but doubt it can be. You may or may not care.

If you want full open, you have to use a scripter as closing the current document ends the lisp.

If scripting, its full open so you can plot and anything you want.

Which are you trying to do?


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes
Message 27 of 29

nrussell4CW5T
Contributor
Contributor

Fixed it, put this code infront and then added a line after its done thawing the layers to call this function which will then iterate through every opened drawing and regenerate the the drawing. 

 

;; Function to run REGENALL on all open drawings
(defun regen-all-open-docs ( / acad-app docs )
(setq acad-app (vlax-get-acad-object)) ; Get AutoCAD application object
(setq docs (vlax-get acad-app 'Documents)) ; Get all open documents
(vlax-for doc docs
(vla-Regen doc acAllViewports) ; Regenerate each document
)
)

0 Likes
Message 28 of 29

JamesMaeding
Advisor
Advisor

@nrussell4CW5T 

oh, loop through open drawings in session.

I much prefer a scripter like:

https://www.cadfx.com/batchscr.html 


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes
Message 29 of 29

ec-cad
Collaborator
Collaborator

Here's a Lisp to add to a Script builder, Freezes / Thaws layers, if found.

Sets to Layer "0" and filters that out from layer list. You need to edit the routine,

to setup your Freeze / Thaw list, and add a (load .. to your script is all.

 

ECCAD

;; LayerOn_Off.lsp
;; Written by: ECCAD for AutoDesk Forum example
;; Run with a script builder only.
;; Overall Function C:GO is called at end of load.
;; Just edit the Freeze list and Thaw list
;;
;; To use in a script, do
;; Open dwgname
;;  (load "C:\\MYLisp\\LayerOn_Off.lsp")); where 'C:\\MYLisp\\' is where you placed this lisp.
;; Qsave
;;  .. next drawing

 (defun C:GO ()
;; Setup Layer Names to Freeze or Thaw

;; Freeze List
  (setq Flist (list
    "freeze1"
    "freeze2"
    "freeze3"
  )); setq/list

;; Thaw List
  (setq Tlist (list
    "thaw1"
    "thaw2"
    "thaw3"
  )); setq/list

;; Define DXF function
;;
 (defun dxf (code elist)
  (cdr (assoc code elist))
 ); end function dxf

;;
;; Define function tdlist
;;
 (defun tdlist (tbname / tdata)
  (while (setq tdata (tblnext tbname (not tdata)))
   (setq tblist (append tblist (list tdata)))
  ); end while
 ); end function tdlist
;; 
;; make layer list for this file...
;;
(defun LL ()
 (setq tblist nil)
 (foreach tbdata (tdlist "layer")
  (setq lname (dxf 2 tbdata))
  (if (and (/= (strcase lname) "DEFPOINTS")(/= lname "0"))
   (progn
     (if (not (member lname lay_lst))
      (setq lay_lst (cons lname lay_lst))
     ); end if
   ); end progn
  ); end if
 ); end foreach
); end function LL

;;
;; Function to grab an item 'name' from a collection
  (defun layexist (collection item / rslt)
   (if
     (not
       (vl-catch-all-error-p 
         (setq rslt
           (vl-catch-all-apply 'vla-item
             (list collection item)
           );trap error
         );end setq
       );return T if successful, else nil
     );end not
     rslt; return object or nil
   );end if
  );end defun

;;
;; Function to obtain Layer Status for Layer Chosen
 (defun get_layer_status ( layer / ado lyo lay )
  (setq ado (vla-get-ActiveDocument (vlax-get-acad-object)));get pointer to activeDocument
  (setq lyo (vla-get-layers ado))
  (if layer
   (progn
    (setq lay (layexist lyo layer))
    (if lay
     (progn
      (setq l_on 1
        l_freeze 0
        l_locked 0
      ); setq defaults
      (if (= (vla-get-LayerOn lay) :vlax-false); is layer off?
         (setq l_on 1)
      );end if
      (if (= (vla-get-Freeze lay) :vlax-true); is layer frozen?
         (setq l_freeze 1)
      );end if
      (if (= (vla-get-Lock lay) :vlax-true); is layer locked?
         (setq l_locked 1)
      );end if
     ); progn
    ); if
   ); progn
  ); if
  (if ado (vlax-release-object ado))
  (if lyo (vlax-release-object lyo))
  (if lay (vlax-release-object lay))
  (princ)
 ); end function get_layer_status
;;

;;
;; *************** MAIN ****************
;;
 (LL) ; Get Layer List of Current Drawing
 (setvar 'clayer "0") ; safest place to be

;; Do Freeze / Thaw
 (foreach layer lay_lst
  (get_layer_status layer)
;; First, Unlock layer if Locked..so it can be Frozen
  (if (= l_locked 1)
   (command "._layer" "U" layer ""); unlock it
  ); if
;; Next, Freeze the layers named in Flist
  (if (member layer Flist)
    (command "._layer" "F" layer "")
  ); if
;; Next, Thaw the layers named in Tlist
  (if (member layer Tlist)
    (command "._layer" "ON" layer "T" layer "")
  ); if
 ); foreach
;;
 (gc)
); Function GO
;; And fire it up for batching
(C:GO)
(princ); silent load
0 Likes