AutoLISP: Blocks to BYLAYER

Here is another LISP gem…

I know that the same can be accomplished with the command SETBYLAYER, but it is still fun nonetheless. And it allows you to change the color to other colors – not just to bylayer.

If you’ve ever selected blocks in your drawing and changed their layer, but they didn’t change color – this one will help you to change the blocks to any color number or to “BYLAYER” which is #256.

To do this:

  • BBL <enter> to start
  • Select the blocks that you wish to change <enter>
  • Specify color # <enter> If you do not know what the color number is, type a question mark in at the command line and a few of the colors and their numbers are provided. 256 = bylayer

That’s it.

 

 

 

 

 

 

 

 

 

 

 

 


;****************************************************************************************

;	UPDATE BLOCK COLOR (updblkcl.lsp)

;	PRE-INSERTED BLOCK DEFINITION CLEAN-UP UTILITY

;

;	This routine is especially usefull to redefine pre-inserted blocks whose

;	entity colors need to be changed to BYLAYER.

;

;       This routine allows the user to update the color of

;	all entities within a block to a single color (exam: color=BYLAYER)

;	without the user having to explode the symbol.  By default the layer name of

;	all entities are NOT changed. The routine changes the original

;	definition of the block within the current drawing.

;

;	To use this routine the user is asked to specify a single

;	color to place all entities of a selected block(s).

;

;	The user is next prompted to select one or more blocks to update. The routine

;	then redefines all entities of the block to the color specified.

;

;	When the user regenerates the drawing she/he will find that all

;	occurances of the block have been redefined.  This is because the

;	original definition of the block is changed!!!

;

;       by CAREN LINDSEY, July 1996

;****************************************************************************************

;

;INTERNAL ERROR HANDLER

(defun err-ubc (s)				; If an error (such as CTRL-C) occurs

; while this command is active...

(if (/= s "Function cancelled")

(princ (strcat "\nError: " s))

)

(setq *error* olderr)			; Restore old *error* handler

(princ)

);err-ubc

(DEFUN C:BBL (/ BLK CBL CBL2 C ACL ALY NLY NCL)

(setq olderr *error* *error* err-ubc)

(initget "?")

(while

(or (eq (setq C (getint "\nEnter new color number/<?>: ")) "?")

(null C)

(> C 256)

(< C 0)

);or

(textscr)

(princ "\n                                                     ")

(princ "\n                 Color number   |   Standard meaning ")

(princ "\n                ________________|____________________")

(princ "\n                                |                    ")

(princ "\n                       0        |      <BYBLOCK>     ")

(princ "\n                       1        |      Red           ")

(princ "\n                       2        |      Yellow        ")

(princ "\n                       3        |      Green         ")

(princ "\n                       4        |      Cyan          ")

(princ "\n                       5        |      Blue          ")

(princ "\n                       6        |      Magenta       ")

(princ "\n                       7        |      White         ")

(princ "\n                    8...255     |      -Varies-      ")

(princ "\n                      256       |      <BYLAYER>     ")

(princ "\n                                               \n\n\n")

(initget "?")

);while

(PROMPT "\nPick blocks to update. ")

(SETQ SS (SSGET '((0 . "INSERT"))))

(SETQ K 0)

(WHILE (< K (SSLENGTH SS))

(setq CBL (tblsearch "BLOCK" (CDR (ASSOC 2 (ENTGET (SETQ BLK (SSNAME SS K)))))))

(SETQ CBL2 (CDR (ASSOC -2 CBL)))

(WHILE (BOUNDP 'CBL2)

(SETQ EE (ENTGET CBL2))

;Update layer value

(SETQ NCL (CONS 62 C))

(SETQ ACL (ASSOC 62 EE))

(IF (= ACL nil)

(SETQ NEWE (APPEND EE (LIST NCL)))

(SETQ NEWE (SUBST NCL ACL EE))

);if

(ENTMOD NEWE)

(SETQ CBL2 (ENTNEXT CBL2))

);end while

(ENTUPD BLK)

(SETQ K (1+ K))

);end while

(setq *error* olderr)

(princ)

);end updblkcl

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Blocks, TIPS. Bookmark the permalink.

4 Responses to AutoLISP: Blocks to BYLAYER

  1. Jafferi says:

    Hello Sir, Please help me somebody send me the drawing autocad drawing he made all the plans on 0 layer now I have to make seperate layers, but he use different colors I try ssx it is not working at all , I would appreciate if you pls help me with lisp how i can change from color to layers
    Brgds

    • AutoCAD Tips says:

      You can try the below routine. It lets you pick an object with the color not “bylayer” and then create a new layer name. Object and all of the other object with the same color will be on this layer too. One quirky thing though. The new color of the layer is white and the objects still have their forced color applied to them. I would use this routine to define the new layers and then when you are done, open up the layer properties manager and assign the colors to the layers and then use the SETBYLAYER command to have the object’s properties be defined by the layer…

      (DEFUN C:CLC ()
      (PROMPT “\n*CHANGE LAYER BY COLOR* “)
      (SETQ SCEN (CAR (ENTSEL “\nSelect entity for color: “)))
      (SETQ SCENL (ENTGET SCEN))
      (SETQ SC (CDR (ASSOC 62 SCENL)))
      (SETQ NL (GETSTRING T “\nEnter new layer: “))
      (COMMAND “LAYER” “M” NL “”)
      (SETQ NENC (CONS 8 NL))
      (SETQ FLTR (LIST (CONS 62 SC)))
      (SETQ CES (SSGET “X” FLTR))
      (SETQ CESL (SSLENGTH CES))
      (SETQ CT (- CESL 1))
      (SETQ LP 1)
      (WHILE LP
      (SETQ EN (SSNAME CES CT))
      (SETQ CT (- CT 1))
      (SETQ ENL (ENTGET EN))
      (SETQ ENC (ASSOC 8 ENL))
      (SETQ NENL (SUBST NENC ENC ENL))
      (ENTMOD NENL)
      (IF (< CT 0) (SETQ LP NIL))
      );END LP
      (PRINC)
      );END CLC

  2. Liz says:

    This lisp is beautiful, versatile, and hugely time saving. Thank you!

  3. Nice post thaanks for sharing

Leave a comment