Here's a quick adaptation of this old post, which accepts a WCMATCH string (i.e., the layers you want to match), and a file path for the desired CSV as arguments.
Please note that the 'extract' and 'trim' sub-functions use "," (comma) as delineator; be sure to change to "/t" if you use tab delineation instead.
Code:
(vl-load-com)
(defun MatchCSV->Layers (char path / f *error* extract trim acDoc l
layerTable layerName layerItem layerDescription
layerColor layerLinetype layerLineweight
layerPlottable layerFreeze
)
;; Exampe: (MatchCSV->Layers "C-ROAD*" "Z:\\my_layers_folder\\my_layers.csv")
;; Error handler
(defun *error* (msg)
(if f
(close f)
)
(if acDoc
(vla-endundomark acDoc)
)
(cond
((not msg)) ; Normal exit
((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit)
((princ (strcat "\n** Error: " msg " ** ")))
) ; Fatal error, display it
(princ)
)
;; Line extraction sub-function
(defun extract (l /)
(substr l 1 (vl-string-search "," l))
)
;; Line trim sub-function
(defun trim (v /)
(vl-string-subst "" (strcat v ",") l)
)
;; Main code
(if (and (findfile path)
(setq f (open path "r"))
)
(progn
(vla-startundomark
(setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
)
(read-line f) ; <- Skip first line (headers)
(setq layerTable (vla-get-layers acDoc))
;; Linetype check
;; <- Add linetype import code here, to avoid errors
(while (/= nil (setq l (read-line f)))
(if (wcmatch (setq layerName (extract l)) char)
(progn
;; Get or create layer
(setq layerItem (vla-add layerTable layerName))
;; Layer settings
(setq l (trim layerName))
(vla-put-description
layerItem
(setq layerDescription (extract l))
)
(setq l (trim layerDescription))
(if (/= 7 (setq layerColor (extract l)))
(vla-put-color layerItem layerColor)
)
(setq l (trim layerColor))
(vla-put-linetype
layerItem
(setq layerLinetype (extract l))
)
(setq l (trim layerLinetype))
(if
(= "BYLAYER" (strcase (setq layerLineweight (extract l))))
(vla-put-lineweight layerItem aclnwtbylayer)
)
(setq l (trim layerLineweight))
(if (/= "YES" (strcase (setq layerPlottable (extract l))))
(vla-put-plottable layerItem :vlax-false)
)
(setq l (trim layerPlottable))
(if (/= "NO" (strcase (setq layerFreeze (extract l))))
(vla-put-freeze layerItem :vlax-true)
)
)
)
)
(setq f (close f))
)
)
(*error* nil)
)
Some examples of how one might use this:
Code:
(defun c:ImportRoadLayers () (MatchCSV->Layers "C-ROAD*" "Z:\\my_layers_folder\\my_layers.csv"))
(defun c:ImportUtilLayers () (MatchCSV->Layers "C-UTIL*" "Z:\\my_layers_folder\\my_layers.csv"))
(defun c:ImportWaterLayers () (MatchCSV->Layers "C-UTIL-WATR*" "Z:\\my_layers_folder\\my_layers.csv"))
Cheers