Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP CHANGE COLOR LAYER XREF

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
Edwin.Saez
4399 Views, 16 Replies

LISP CHANGE COLOR LAYER XREF

Greetings to all,

I found a good lisp in a forum, but I need to ask for your help to modify it, so that instead of being able to change the color to objects, I can change in color to the layer that I select from an xref.

 

 

(defun c:blcc () (pl:block-color) (princ))
(defun c:encc () (pl:block-ent-color) (princ))
;;;get from  Alaspher  http://forum.dwg.ru/showthread.php?t=1036
;;; http://forum.dwg.ru/showpost.php?p=166220&postcount=18
(vl-load-com)
(defun pl:block-ent-color (/ adoc blocks color ent lays)
    (setq adoc  (vla-get-activedocument (vlax-get-acad-object))
          lays  (vla-get-layers adoc)
          color (acad_colordlg 256)
    )
    (if color
        (progn (setvar "errno" 0)
               (vla-startundomark adoc)
               (while (and (not (vl-catch-all-error-p
                                    (setq ent (vl-catch-all-apply
                                                  (function nentsel)
                                                  '("\nSelect entity <Exit>:")
                                              )
                                    )
                                )
                           )
                           (/= 52 (getvar "errno"))
                      )
                   (if ent
                       (progn (setq ent (vlax-ename->vla-object (car ent))
                                    lay (vla-item lays (vla-get-layer ent))
                              )
                              (if (= (vla-get-lock lay) :vlax-true)
                                  (progn (setq layloc (cons lay layloc))
                                         (vla-put-lock lay :vlax-false)
                                  )
                              )
                              (vl-catch-all-apply (function vla-put-color) (list ent color))
                              (vla-regen adoc acallviewports)
                       )
                       (princ "\nNothing selection! Try again.")
                   )
               )
               (foreach i layloc (vla-put-lock i :vlax-true))
               (vla-endundomark adoc)
        )
    )
    (princ)
)

(defun pl:block-color (/ adoc blocks color ins lays)
    (setq adoc   (vla-get-activedocument (vlax-get-acad-object))
          blocks (vla-get-blocks adoc)
          lays   (vla-get-layers adoc)
          color  (acad_colordlg 256)
    )
    (if color
        (progn (setvar "errno" 0)
               (vla-startundomark adoc)
               (while (and (not (vl-catch-all-error-p
                                    (setq ins (vl-catch-all-apply
                                                  (function entsel)
                                                  '("\nSelect block <Exit>:")
                                              )
                                    )
                                )
                           )
                           (/= 52 (getvar "errno"))
                      )
                   (if ins
                       (progn (setq ins (vlax-ename->vla-object (car ins)))
                              (if (= (vla-get-objectname ins) "AcDbBlockReference")
                                  (if (vlax-property-available-p ins 'path)
                                      (princ "\nThis is external reference! Try pick other.")
                                      (progn (_pl:block-color blocks ins color lays)
                                             (vla-regen adoc acallviewports)
                                      )
                                  )
                                  (princ "\nThis isn't block! Try pick other.")
                              )
                       )
                       (princ "\nNothing selection! Try again.")
                   )
               )
               (vla-endundomark adoc)
        )
    )
    (princ)
)

(defun _pl:block-color (blocks ins color lays / lay layfrz layloc)
    (vlax-for e (vla-item blocks (vla-get-name ins))
        (setq lay (vla-item lays (vla-get-layer e)))
        (if (= (vla-get-freeze lay) :vlax-true)
            (progn (setq layfrz (cons lay layfrz)) (vla-put-freeze lay :vlax-false))
        )
        (if (= (vla-get-lock lay) :vlax-true)
            (progn (setq layloc (cons lay layloc)) (vla-put-lock lay :vlax-false))
        )
        (vl-catch-all-apply (function vla-put-color) (list e color))
        (if (and (= (vla-get-objectname e) "AcDbBlockReference")
                 (not (vlax-property-available-p e 'path))
            )
            (_pl:block-color blocks e color lays)
        )
        (foreach i layfrz (vla-put-freeze i :vlax-true))
        (foreach i layloc (vla-put-lock i :vlax-true))
    )
)

(progn
(princ "\BLCC - Changes color of the chosen blocks")
(princ "\nENCC - Changes color of the chosen objects (may be  element of the block)")
(princ))

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

16 REPLIES 16
Message 2 of 17
Edwin.Saez
in reply to: Edwin.Saez

The color to be changed has to be only in the active viewport

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

Message 3 of 17
john.uhden
in reply to: Edwin.Saez

I am responding from the context of 2002, but I don't think you can change layer colors on a per-viewport basis.  I think you would need some kind of reactor to change a layer's color depending on the active viewport.  Then again, what's to prevail when you plot?

John F. Uhden

Message 4 of 17
Edwin.Saez
in reply to: john.uhden

@john.uhden,

I appreciate you replying,

 

What I need is that you can change the "COLOR VP" of the layer that I select in each viewport of an external reference.

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

Message 5 of 17
Anonymous
in reply to: Edwin.Saez

This is one of my office's favorite commands, see attached. Always helps to prepare any references with SETBYLAYER.

Tags (1)
Message 6 of 17
Edwin.Saez
in reply to: Anonymous

@Anonymous, @john.uhden,

 

The lisp works, but it changes the color in all viewport.
I would like lisp to only change the color, only in the active viewport it has.

Thanks for the help.

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

Message 7 of 17
ВeekeeCZ
in reply to: Edwin.Saez

This simple routine changes VP Color of selected (nested) layers.

 

(defun c:xVPcolor ( / col lay ent)

  (if (and (or (/= 1 (getvar 'CVPORT))
               (prompt "\nProgram works inside of VP only!"))
           (setq col (getint "VP Color number: "))
           (setq lay "")
           )
    (progn
      (while (setq ent (car (nentsel "\nSelect layer: ")))
        (print (setq lay (strcat (cdr (assoc 8 (entget ent))) ","))))
      (if (/= "" lay)
        (command "_.VPLAYER" "_Color" col lay "" ""))))
  (princ)
)
Message 8 of 17
Edwin.Saez
in reply to: ВeekeeCZ

@ВeekeeCZ,

 

Thank you very much, it works perfect.
Would it be too much to ask that instead of asking for a color, it unfolds a panel where you can select the color?

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

Message 9 of 17
ВeekeeCZ
in reply to: Edwin.Saez

That should not be a problem. 

 

(defun c:xVPcolor ( / col lay ent)
  
  (if (and (or (/= 1 (getvar 'CVPORT))
	       (prompt "\nProgram works inside of VP only!"))
	   (setq lay ""
		 col (acad_colordlg 0))
	   )
    (progn
      (while (setq ent (car (nentsel "\nSelect layer: ")))
	(print (setq lay (strcat (cdr (assoc 8 (entget ent))) ","))))
      (if (/= "" lay)
	(command "_.VPLAYER" "_Color" col lay "" ""))))
  (princ)
)
Message 10 of 17
Edwin.Saez
in reply to: ВeekeeCZ

@ВeekeeCZ,

 

You may have encountered an error. When I try to change the color of a reference with a name that contains "@", it does not change the color.

 

Could you solve this please?

 

 

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

Message 11 of 17
john.uhden
in reply to: Edwin.Saez

I wonder if reverse quoting the "@" might help as it indicates taking the following character literally.

 

(setq lay (vl-string-subst "`@" "@" lay))

Plus, if the string does not contain the "@" character then it returns the string unchanged.

John F. Uhden

Message 12 of 17
Edwin.Saez
in reply to: john.uhden

@john.uhden,

 

thanks for the reply,

Could you help me, in which part of the whole code, I put it?

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

Message 13 of 17
john.uhden
in reply to: Edwin.Saez

Pardon my rearranging a little.  It's just sort of a style I learned from Stephan Koster.

I won't .guarantee that it will fix your "@" problem, but it won't hurt.

 

(defun c:xVPcolor ( / col lay ent)
  (and
    (or
      (/= 1 (getvar 'CVPORT))
      (prompt "\nProgram works inside of VP only!")
    )
    (setq lay "" col (acad_colordlg 0))
    (while (setq ent (car (nentsel "\nSelect layer: ")))
      (print (setq lay (strcat (cdr (assoc 8 (entget ent))) ",")))
      (/= "" lay)
      (setq lay (vl-string-subst "`@" "@" lay))
      (command "_.VPLAYER" "_Color" col lay "" "")
    )
  )
  (princ)
)

Sorrry if soe letters got screwed up.  My desk light wenrt ouy.

John F. Uhden

Message 14 of 17

Hello, I have a problem, I need to change the color and layer of the blocks. I would like to know if there is an automatic way, since I have about 800 blocks in each file. these one by one I have to trigger the command "refedit" to enter the block and change the layer and color. Another important point, the layer has to be 0 and the color must be ByLayer and the blocks are and different colors and layers ...

Thank you very much in advance!!!

Message 15 of 17


@silvestresallesjunior4 wrote:

Hello, I have a problem, I need to change the color and layer of the blocks. I would like to know if there is an automatic way, since I have about 800 blocks in each file. these one by one I have to trigger the command "refedit" to enter the block and change the layer and color. Another important point, the layer has to be 0 and the color must be ByLayer and the blocks are and different colors and layers ...

Thank you very much in advance!!!


 

 

SETBYLAYER Changes the property overrides of selected objects to ByLayer.

 

or via code

 

 

(defun c:Backtolayerzero (/ a)
  (while (setq a (tblnext "BLOCK" (null a)))
    (vlax-for
	      itm
		 (vla-item
		   (vla-get-blocks
		     (vla-get-activedocument
		       (vlax-get-acad-object)
		     )
		   )
		   (cdr (assoc 2 a))
		 )
      (if
	(vlax-write-enabled-p itm)
	(progn
	 (vla-put-layer itm "0")
	 (vla-put-color itm 256)
	 )
      )
    )
  )
)

 

command: Backtolayerzero

 

 

 

 

 

Message 16 of 17
Anonymous
in reply to: john.uhden

This works perfectly and is saving me so much time! Thank you all! 

Message 17 of 17
Khughe1991
in reply to: john.uhden

Hi this Xvpcolor lisp saves me so much time and I really appreciate your help creating it. I am running into a problem with blocks where the internal line work etc. are set to layer 0. Like a spot elevation tic symbol. The xvpcolor lisp does not work for these type of blocks. Do you know anyway around that? I've tried to set the layers to "by block" and "by layer" but that does not change anything. I need to manually change the vp layer of the block to get the colors to be the way I want which can be time consuming if there are a lot of different blocks I'm changing. Thanks in advanced! 

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