Originally Posted by
Frank Dux
Code:
(defun c:MyLayFrz (/ e cmdecho)
(setq cmdecho (getvar 'cmdecho))
(setvar 'cmdecho 0)
(tblnext "layer" T)
(while (setq e (tblnext "layer"))
(if (wcmatch (strcase (cdr (assoc 2 e))) "*STRUCTURE*")
(if (= (cdr (assoc 70 e)) 0)
(command "._-layer" "_f" (cdr (assoc 2 e)) "")
(command "._-layer" "_t" (cdr (assoc 2 e)) "")
)
)
)
(setvar 'cmdecho cmdecho)
(princ)
)
Just some observations on your code logic, if I may....
If you're going to change user setting(s), then it is prudent to include error handling in order to restore one's settings, even on failure.
There's no need to iterate the LayerTable, as the -LAYER Command support WCMATCH functionality; as coded you iterate the LayerTable, and for each-and-every-single LayerTableRecord that WCMATCHes "*STRUCTURE*" you call the -LAYER Command separately... If 100 LayerTableRecords match, then you've effectively called the Command 100 separate times, which is not very efficient as you could instead simply supply "*STRUCTURE*" to the -LAYER Command once, and yield the same result.
Here's a quick example, with a drawing that has 100 LayerTableRecords that include "*STRUCTURE*" in the name, bench-marking your approach, an approach that iterates the LayerTable to build a list of strings and then only calls the -LAYER Command once vs. what I've posed here using WCMATCH from the outset:
Sub-functions tested:
Code:
(vl-load-com)
(defun fdux:FreezeStructureLayers (/ e layerName)
(tblnext "layer" T)
(while (setq e (tblnext "layer"))
(if (wcmatch (strcase (setq layerName (cdr (assoc 2 e)))) "*STRUCTURE*")
(command "._-layer" "_f" layerName "")
)
)
(princ)
)
(defun fdux:FreezeStructureLayers2 (/ e layerName layersToFreeze)
(tblnext "layer" T)
(while (setq e (tblnext "layer"))
(if (wcmatch (strcase (setq layerName (cdr (assoc 2 e)))) "*STRUCTURE*")
(setq layersToFreeze (cons layerName layersToFreeze))
)
)
(if layersToFreeze
(command
"._-layer"
"_f"
(vl-string-right-trim
","
(apply
'strcat
(mapcar (function (lambda (x) (strcat x ","))) layersToFreeze)
)
)
""
)
)
(princ)
)
(defun bbox:FreezeStructureLayers (/ layers)
(command "._-layer" "_f" "*structure*" "")
(princ)
)
Results from console, performing each sub-function 10 times only:
Code:
_$ (bench '(fdux:FreezeStructureLayers fdux:FreezeStructureLayers2 bbox:FreezeStructureLayers) '() 10)
FDUX:FREEZESTRUCTURELAYERS
Elapsed: 1498
Average: 149.8000
FDUX:FREEZESTRUCTURELAYERS2
Elapsed: 141
Average: 14.1000
BBOX:FREEZESTRUCTURELAYERS
Elapsed: 16
Average: 1.6000
; 5 forms loaded from #<editor "<Untitled-0> loading...">
_$
Lastly, you're checking each matching layer's current state, which allows for user to freeze all using this routine, then modify some of them before attempting to toggle, which potentially results in a mixed state where some "*STRUCTURE*" layers are thawed, and some are frozen.
Cheers