Change layer color in multiple AutoCAD drawings at one time.

Change layer color in multiple AutoCAD drawings at one time.

lisa_as-caddtech
Explorer Explorer
1,238 Views
5 Replies
Message 1 of 6

Change layer color in multiple AutoCAD drawings at one time.

lisa_as-caddtech
Explorer
Explorer

I would like to change the color of 1 layer (Equipment) from black to blue in over 200 AutoCAD drawings without opening each drawing and manually changing it.  I have heard I can do that in Hurricane for AutoCAD but I have not had the time to try it out and I have not found a tutorial for it.  Does anyone have a good step-by-step instructions I could use?

TIA LSmith

0 Likes
1,239 Views
5 Replies
Replies (5)
Message 2 of 6

pendean
Community Legend
Community Legend

@lisa_as-caddtech wrote:

...Hurricane for AutoCAD but I have not had the time to try it out and I have not found a tutorial for it...


Here you go from that vendor's website:

https://74mph.com/tutorials.html

quick start:

https://www.youtube.com/watch?v=BA214zKGGJg

How to write a script (so you can write a "change a layer color in xxxx files")

https://www.youtube.com/watch?v=PHiCVg7d7eg

 

How to use LAYER command in a script to change a layer color:

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-can-i-change-a-layers-color-usin...

 

HTH

 

Message 3 of 6

Kent1Cooper
Consultant
Consultant

How about something that will change it automatically in every drawing that is opened for any reason?  It wouldn't change it in a given drawing until it is opened, but would that be enough?  A single line of code in an acaddoc.lsp file can handle that:

 

(command "_.layer" "_color" 5 "Equipment" "")

 

It won't care if a given drawing does not contain that Layer.

 

[The "from" color of the Layer doesn't matter.  But if you would only want it changed to blue if it is currently black, that could be accommodated.]

Kent Cooper, AIA
Message 4 of 6

DGCSCAD
Collaborator
Collaborator

If all drawings reside in one single directory, this should take care of it:

 

 

(defun c:Batcher (/ wutdir wutfiles scrfile n2 scrline)
(vl-load-com)
	(setq wutdir (LM:browseforfolder "Select directory to process:" "C:\\" 512));;;<--Starting Directory
	(setq wutfiles (vl-directory-files wutdir "*.dwg"))
	(setq scr_name (strcat wutdir "\\scrfile.scr"))
	(setq scrfile (open scr_name "w"))
	(close scrfile)
	(setq scrfile (open scr_name "a"))
(foreach n wutfiles
	(setq n2 (strcat "\""wutdir "\\" n "\""))
	(setq n2 (vl-string-translate "\\" "\\" n2))
	(setq scrline (strcat "open" " " n2 " " "(load\"batcher\")" " " "(batcher_layer_chgclr)" " " "qsave" " " "close"))
	(write-line scrline scrfile)
	(princ)
)
	(close scrfile)
(command "script" scr_name)
(princ "\n***Batch complete.***")
(princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;START batcher_layer_chgclr

(defun batcher_layer_chgclr ()
(command "_.layer" "_color" 5 "Equipment" "")
(princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;START LM:browseforfolder

;; THANKYOU Mr. M!
;; Browse for Folder  -  Lee Mac
;; Displays a dialog prompting the user to select a folder.
;; msg - [str] message to display at top of dialog
;; dir - [str] [optional] root directory (or nil)
;; bit - [int] bit-coded flag specifying dialog display settings
;; Returns: [str] Selected folder filepath, else nil.

(defun LM:browseforfolder ( msg dir bit / err fld pth shl slf )
    (setq err
        (vl-catch-all-apply
            (function
                (lambda ( / app hwd )
                    (if (setq app (vlax-get-acad-object)
                              shl (vla-getinterfaceobject app "shell.application")
                              hwd (vl-catch-all-apply 'vla-get-hwnd (list app))
                              fld (vlax-invoke-method shl 'browseforfolder (if (vl-catch-all-error-p hwd) 0 hwd) msg bit dir)
                        )
                        (setq slf (vlax-get-property fld 'self)
                              pth (vlax-get-property slf 'path)
                              pth (vl-string-right-trim "\\" (vl-string-translate "/" "\\" pth))
                        )
                    )
                )
            )
        )
    )
    (if slf (vlax-release-object slf))
    (if fld (vlax-release-object fld))
    (if shl (vlax-release-object shl))
    (if (vl-catch-all-error-p err)
        (prompt (vl-catch-all-error-message err))
        pth
    )
)

(princ)

 

 

I'd backup the folder first, just for safety purposes. Make sure the LISP file resides in one of your support paths, and if you need assistance in loading and running a LISP file: https://www.lee-mac.com/runlisp.html

 

Edit (my apologies, I forgot a few key notes):

  1. Copy/Paste the code into a txt file and name it Batcher.lsp.
  2. Place Batcher.lsp in a support/trusted directory.
  3. Run the code from a drawing that will not be in the directory. Start a new drawing and run it from there.
AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 5 of 6

paullimapa
Mentor
Mentor

You could also look into AutoCAD Core Console:

https://www.houseofbim.com/posts/son-of-a-batch-autocad-core-console-through-lisp/


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 6 of 6

lisa_as-caddtech
Explorer
Explorer

Thanks!  I will try all these great suggestions.

0 Likes