C3D-0068 · Rebuild all three layer filters from current state [FILTERS-REBUILD] done
Recounts entities per layer live and rebuilds USED - NO PDF/C- (33), USED C- (6) and PDF LAYERS (76) from that count, so none of the three can be stale after the water and CL point moves. Replaces existing filters of those names and reads all three back from the database to verify.
C# payload
// Rebuild all three layer filters from CURRENT drawing state.
//
// Membership is derived HERE, live, by counting entities per layer across model space and the
// layouts - not from a hardcoded list that can go stale (the earlier filters already had, after
// the water and CL moves). Each layer is classified by name:
// PDF* -> "PDF LAYERS"
// C-* -> "USED C-"
// else -> "USED - NO PDF/C-"
// Only layers that actually hold entities are included, in all three.
//
// TRAPS: Autodesk.AutoCAD.LayerManager is not in ScriptRunner's imports (fully qualify), and
// Db.LayerFilters returns a STRUCT copy that must be assigned back or nothing persists.
// Idempotent: each filter replaces any existing one of the same name.
var lt = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);
var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);
var counts = new Dictionary<string,int>();
foreach (ObjectId btrId in bt) {
var btr = (BlockTableRecord)Tx.GetObject(btrId, OpenMode.ForRead);
if (!btr.IsLayout) continue;
foreach (ObjectId id in btr) {
Autodesk.AutoCAD.DatabaseServices.DBObject o;
try { o = Tx.GetObject(id, OpenMode.ForRead); } catch { continue; }
var ent = o as Autodesk.AutoCAD.DatabaseServices.Entity;
if (ent == null) continue;
string lay; try { lay = ent.Layer; } catch { continue; }
if (!counts.ContainsKey(lay)) counts[lay] = 0;
counts[lay]++;
}
}
var keep = new List<ObjectId>(); var cdash = new List<ObjectId>(); var pdf = new List<ObjectId>();
int nk = 0, nc = 0, np = 0;
foreach (ObjectId lid in lt) {
var l = (LayerTableRecord)Tx.GetObject(lid, OpenMode.ForRead);
if (!counts.ContainsKey(l.Name) || counts[l.Name] == 0) continue;
string u = l.Name.ToUpper();
if (u.StartsWith("PDF")) { pdf.Add(l.ObjectId); np++; }
else if (u.StartsWith("C-")) { cdash.Add(l.ObjectId); nc++; }
else { keep.Add(l.ObjectId); nk++; }
}
var root = Db.LayerFilters;
var parent = root.Root;
Action<string, List<ObjectId>> build = (name, ids) => {
Autodesk.AutoCAD.LayerManager.LayerFilter dup = null;
foreach (Autodesk.AutoCAD.LayerManager.LayerFilter f in parent.NestedFilters)
if (f.Name == name) { dup = f; break; }
if (dup != null) parent.NestedFilters.Remove(dup);
var grp = new Autodesk.AutoCAD.LayerManager.LayerGroup();
grp.Name = name;
foreach (ObjectId oid in ids) grp.LayerIds.Add(oid);
parent.NestedFilters.Add(grp);
};
build("USED - NO PDF/C-", keep);
build("USED C-", cdash);
build("PDF LAYERS", pdf);
Db.LayerFilters = root; // struct - REQUIRED or nothing persists
// verify by re-reading from the database
var check = Db.LayerFilters;
foreach (Autodesk.AutoCAD.LayerManager.LayerFilter f in check.Root.NestedFilters) {
var g = f as Autodesk.AutoCAD.LayerManager.LayerGroup;
if (g == null) continue;
if (g.Name == "USED - NO PDF/C-" || g.Name == "USED C-" || g.Name == "PDF LAYERS")
Log(" \"" + g.Name + "\" -> " + g.LayerIds.Count + " layers (verified)");
}
Log("");
Log("built from live counts: keep=" + nk + " cdash=" + nc + " pdf=" + np);
Ed.Regen();
Result
Log
"USED - NO PDF/C-" -> 33 layers (verified) "USED C-" -> 6 layers (verified) "PDF LAYERS" -> 76 layers (verified) built from live counts: keep=33 cdash=6 pdf=76
Notes
What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.
No notes yet.