Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

change a layer within all blocks

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Anonymous
3236 Views, 8 Replies

change a layer within all blocks

Hi, I want to be able to change the layer of all objects within all blocks that were drawn in a certain layer to another one -  does anyone have a lisp for that...? (if it werent in a block I would choose using QS & change it all at once but i cant choose within blocks unless i go into each...)

Thanks!

8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

Is the target layer always the same?
Message 3 of 9
Anonymous
in reply to: Anonymous

usually yes but i want to be able to choose the laye ( i could change the neme in the lisp too perhaps...) Thanks!!

Message 4 of 9
Anonymous
in reply to: Anonymous

I use the following code to change all entities within a block to layer 0. If there are attributes within the block then command ATTSYNC must be done afterwards (I use another lisp to ATTSYNC  all blocks in the drawing).

 

I cannot remember where I got the code originally, I didn't write it.

 

;Searches all block sub-entities in the block table, and converts them to layer 0
(defun C:BL0 (/ eBlk ss cnt i ent count bln_Change)
	;eBlk: block from block definition table
	(princ "Specify blocks that want to change all their entities to layer 0: ")
	(setq ss (ssget '((0 . "INSERT")))) ;Select inserted entities into a selection set
	(if ss ;This SSet is not nil -> contains items
		(progn
			(setq cnt (sslength ss) ;Number of entities found in this SSet
			      i 0 ;Begin the counter
						count 0
			)
			(while (< i cnt) ;Search all items in the SSet
				(setq ent (entget (ssname ss i))) ;Definition data of a selected entity
				(setq eBlk (tblsearch "block" (cdr (assoc 2 ent)))) ;Get block with specified name (assoc 2 ent)
				(setq bln_Change nil) ;Reset the changed flag
				(ZeroBlock eBlk) ;Change to layer 0 of all entities in the specified block
				(if (= T bln_Change) ;Change is found from the routine (ZeroBlock)
					(setq count (+ 1 count)) ;Increase the counter to mark this change
				)
				(setq i (+ 1 i)) ;Increase the counter
			);while
			(command ".regen") ;Regen the screen to update changes
			;Display result information after changing
			(ResultInfo count)
		);progn
	);if this SSet contains items
	(princ) ;Finish silently
)

;Change to layer 0 of all entities in the specified block
(defun ZeroBlock(eBlk / ent data)
	(setq ent (cdr (assoc -2 eBlk))) ;1st sub entity in this block (eBlk)
	(while ent ;Search all sub entities in the block
		(setq data (entget ent)) ;Entity definition data of this sub entity
		(if (assoc 8 data) ;Layer name exists
			(if (/= "0" (cdr (assoc 8 data))) ;Entity layer is not 0 -> change it to 0
				(progn
					(entmod (setq data (subst (cons 8 "0") (assoc 8 data) data))) ;Change layer to 0
					(setq bln_Change T) ;Flag the change
				)
			);if
		);if
		(setq ent (entnext ent)) ;Move to the next entity in this block
	);while
)

;Change all blocks to layer 0
(defun ZeroAllBlocks(/ eBlk)
	(setq eBlk (tblnext "BLOCK" T)) ;Get the 1st block
	(while eBlk ;Search all blocks
		(ZeroBlock eBlk) ;Zero layers of this block in the block definition table
		(setq eBlk (tblnext "BLOCK")) ;Move to the next block in the block definition table
	);while
	(command ".regen") ;Regen the screen to update changes
)

;Display result information
(defun ResultInfo(count)
	(if (= 0 count)
		(princ "No block has changed its entities to layer 0. ")
		;Else -> count >= 1
		(if (= 1 count)
			(princ "1 selected block has changed its entities to layer 0. ")
			(princ (strcat (itoa count) " selected blocks have changed their entities to layer 0. "))
		)
	);if
)

 

 

Change the "0"to "hannaslayer" (or whatever) in the following two lines:

 

 

(if (/= "0"

and

 

cons 8 "0")

You could also pick a layer to choose it to replace the existing layer(s) by using an entsel method.

 

Message 5 of 9
Anonymous
in reply to: Anonymous

Thanks! it works great - but doesnt go into a block within another block - which i would really want it to do (i wanna make sure ALL my layers are O.K) - is there a way to make that happen?

Also  - what do you mean by "You could also pick a layer to choose it to replace the existing layer(s) by using an entsel method." ? i dont know what entsel means...

Thank you so much for your help

Message 6 of 9
3wood
in reply to: Anonymous

You can try CHZ20, which is able to change nested object layers according to a rule file.

Settings as below:

chz20_1.JPG

 

Here attached a rule file example, you can modify it to suit your needs (add more layer matching pairs etc.)

 

Message 7 of 9
miroko
in reply to: Anonymous

Hi

 

I use such a lisp:

It goes through all blocks and nested blocks.

Probably you can also edit the "0" and change it to other i.e. "mylayer" - but did not try this.

 

 

 

;;; CADALYST 07/07  Tip 2225: brp.lsp Block Repair (c) 2007 Michael Kolomiyets

;;;   original name of file: brp.lsp
;;;   Block redefinition program
;;;   version 1.0
;;;   Created 5/03/06 by Michael Kolomiyets
;;;
;;;                   DESCRIPTION
;;;    This program allows users to redefine either
;;;   a simple block (a block with no attributes) or
;;;   a block with attributes. Also it gives an option
;;;   to make a global redefinition which means updating
;;;   any entities of a block (including attribute entities)
;;;   in one shot.
;;;   The user can create either a general block selection
;;;   in a selection set or a selection set of all blocks
;;;   in the database.
;;;    All insertions of a block are updated. Each entity
;;;   of a block changes its properties such as color and
;;;   linetype by layer. The program gives the user an option
;;;   to move entities of a block to layer "0". 


(defun err (s)
 (if(= s "Function cancelled")
  (prompt "\Function cancelled by user\n")
  (prompt (strcat "\nerror: " s "\n"))
 )
 (resetting)
 (prompt "SYSVARS have been reset\n")
 (textpage)
 (princ)
)

(defun setting ()
 (setq old-error *error*)
 (setq *error* err)
 (setq old-cmd (getvar "cmdecho"))
 (setvar "cmdecho" 0)
 (setq old-blp (getvar "blipmode"))
 (setvar "blipmode" 0)
 (setq opt-yes nil)
 (setq opt-no nil)
 (princ)
);end defun

(defun resetting ()
 (setvar "cmdecho" old-cmd)
 (setvar "blipmode" old-blp)
 (setq *error* old-error)
 (princ)
)


(defun C:bylayer ()
 (setting)
 (prompt "\nBlock redefinition program loaded...")
 (initget "Blocks Attrib Global")
 (setq kw (getkword
 "\nEnter an option for block redefinition:
  \nBlocks/Attribute Blocks/Global <Blocks>"
 )
)
 (cond
  ((= kw "Attrib") (attributes))
  ((= kw "Global") (global))
  ((= kw nil) (blocks))
  ((= kw "Blocks") (blocks))
 );end cond
);end defun

(defun blocks ()
 (initget "Selectblocks")
  (if(= (getkword
       "\nAll blocks/Select blocks <All blocks>")
       "Selectblocks"
        )
   (progn
    (if(setq ss (ssget'((-4 . "<AND")
          (0 . "INSERT")
   (66 . 0)
   (-4 . "AND>")
                )
                )
       )
     (progn
      (prompt "Blocks are being redefined...")
      (blklst ss)
      (reports)
     );end progn
     (progn
      (prompt "No inserts detected!")
      (princ)
     );end progn
    );end if
   );end progn
   (progn
    (if (setq ss (ssget"X"'((-4 . "<AND")
       (0 . "INSERT")
       (66 . 0)
       (-4 . "AND>")
      )
          )
        )
     (progn
      (prompt "Blocks are being redefined...")
      (blklst ss)
      (reports)
     );end progn
     (progn
      (prompt "No inserts detected!")
      (princ)
     );end progn
    );end if
   );end progn
  );end if 
  (princ)
);end defun blocks

(defun blklst (ss / ins-name ins-list blk-data ins-cur k c b i n)
 (setq ins-name (cdr (assoc 2 (entget (ssname ss 0)))))
 (setq ins-list (list ins-name))
 (setq i 1)
  (while
   (<= i (1- (sslength ss)))
   (setq k 0)
    (while
     (and
      (<= k (1- (length ins-list)))
      (/= (setq ins-cur (cdr (assoc 2 (entget (ssname ss i)))))
          (cdr (assoc 2 (entget (ssname ss k))))
      )
     )
     (setq k (1+ k))
    );end of k while
   (if (= (1- k) (1- (length ins-list)))
    (setq ins-list (append ins-list (list ins-cur)))
   );end of if
   (setq i (1+ i))
  );end of i while
  (setq c 0
        b 0
        n 0
  )
  (while
   (<= c (1- (length ins-list)))
   (setq ins-elem (nth c ins-list))
   (setq blk-data (tblsearch "block" ins-elem))
   (getblk blk-data)
   (setq c (1+ c))
  );end while
);end of function

(defun attrproc (ss / z ss-ent)
  (setq z 0)
  (while
   (<= z (1- (sslength ss)))
   (setq ss-ent (ssname ss z))
   (if (equal (assoc 66 (entget ss-ent)) '(66 . 1))
    (attrmod ss-ent)
   );end of if
   (setq z (1+ z))
  );end while
);end of attribproc
 
(defun attrmod (ss-ent)
 (while
  (and
   (setq ss-ent (entnext ss-ent))
   (/= (cdr (assoc 0 (entget ss-ent))) "SEQEND")
  );end and
  (lay-colr-ltyp (entget ss-ent))
 );end while
);end of attrmod

(defun attributes (/ ss-attr)
 (initget "Selectblocks")
  (if(= (getkword "\nAll blocks/Select blocks <All blocks>"
        )
                 "Selectblocks"
     )
   (progn
    (if (setq ss-attr (ssget '((-4 . "<AND")
          (0 . "INSERT")
          (66 . 1)
          (-4 . "AND>")
         )
        )
 )
     (progn
      (prompt "Blocks are being redefined...")
      (attrproc ss-attr)
      (blklst ss-attr)
      (reports)
     );end progn
     (progn
      (prompt "No inserts detected!")
      (princ)
     );end progn
    );end if               
   );end progn
   (progn
    (if (setq ss-attr (ssget "X" '((-4 . "<AND")
       (0 . "INSERT")
       (66 . 1)
       (-4 . "AND>")
      )
        )
 )
     (progn
      (prompt "Blocks are being redefined...")
      (attrproc ss-attr)
      (blklst ss-attr)
      (reports)
     );end progn
     (progn
      (prompt "No inserts detected!")
      (princ)
     );end progn
    );end if                   
   );end progn
  ); end if
  (princ)
);end defun attributes

(defun global ()
 (initget "Selectblocks")
 (if(= (getkword "\nAll blocks/Select blocks <All blocks>"
       )
          "Selectblocks"
    )
  (progn
   (if (setq ss (ssget '((0 . "INSERT"))))
    (progn
     (prompt "Blocks are being redefined...")
     (attrproc ss)
     (blklst ss)
     (reports)
    );end progn
    (progn
     (prompt "No inserts detected!")
     (princ)
    );end progn
   );end if
  );end progn
  (progn
   (if (setq ss (ssget "X" '((0 . "INSERT"))))
    (progn
     (prompt "Blocks are being redefined...")
     (attrproc ss)
     (blklst ss)
     (reports)
    );end progn
    (progn
     (prompt "No inserts detected!")
     (princ)
    );end progn
   );end if
  );end progn
 );end if
 (princ)
);end defun global   

(defun getblk (blk-data / ent-blk ent-data)
 (if (null (assoc 1 blk-data))
  (progn
   (setq ent-blk (cdr (assoc -2 blk-data)))
    (while ent-blk
     (setq ent-data (entget ent-blk))
      (if (= (cdr (assoc 0 ent-data)) "INSERT")
       (progn
 (if (equal (assoc 66 ent-data) '(66 . 1))
  (attrmod ent-blk)
 );end if 
 (setq ent-data
  (tblsearch "block"(cdr (assoc 2 ent-data)))
 );end setq
 (getblk ent-data)
       );end progn
      );end if
      (lay-colr-ltyp ent-data)
      (setq ent-blk (entnext ent-blk))
    );end while
  );end progn
 );end if
 (princ)
);end defun getblk

(defun choice ()
 (initget "Yes")
 (if(= (getkword
        (strcat
         "\nDo you want to move all"
         " entities to layer \"0\"(Yes/No)<No>"
        )
       )
 "Yes"
    )
   (setq opt-yes T)
   (setq opt-no T)
 );end if
);end defun choice

(defun reports ()
 (setvar "cmdecho" 0)
 (prompt "\nRedefinition complete\n ")
 (command "Regen")
);end defun

(defun lay-colr-ltyp (ent-data /)
 (if(/= (cdr (assoc 8 ent-data)) "0")
  (progn
   (if(and
       (not opt-yes) (not opt-no))
       (choice)
      )
    (if opt-yes
     (progn
      (setq ent-data
       (subst
        (cons 8 "0")
        (assoc 8 ent-data)
         ent-data
       );end subst
      );end setq 
     );end progn
    );end if
  );end progn
 );end if 
 (if (assoc 62 ent-data)
  (progn
    (setq ent-data
     (subst
      (cons 62 256)
      (assoc 62 ent-data)
       ent-data
     );end subst
    );end setq
   );end progn           
  );end if
  (if (assoc 6 ent-data)
   (progn
    (setq ent-data
     (subst
      (cons 6 "BYLAYER")
      (assoc 6 ent-data)
       ent-data
     );end subst
    );end setq
   );end progn     
  );end if
  (entmod ent-data)
);end defun 

 

 

 

Message 8 of 9
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

.... I want to be able to change the layer of all objects within all blocks that were drawn in a certain layer to another one ....


IF there is not anything else on that Layer [that is not in Block definitions], so that you can take the entire Layer and move it all into the other one:  LAYMRG.  That will get everything in nested Blocks, too.

Kent Cooper, AIA
Message 9 of 9
Anonymous
in reply to: Kent1Cooper

thank you all for your kind help! sorry for the delay,,,, kent1cooper's LAYMRG is what i was looking for - thanks!!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report