← Inbox

C3D-PDFPURGE · Purge empty PDF_* layers done

The strip erased 8,579 entities but left ~39 now-empty PDF_* layers cluttering the layer list. Purges any PDF_* layer with zero references anywhere (model, paper, or block defs). Keeps PDF_IMPORT_ASBUILT and any layer still holding geometry.

Script file
C3D-PDFPURGE.cs
Target
C:\Users\sanja\OneDrive\_SurveyDisco\260613 - 5030 E Lake Pkwy, Stockbridge, GA 30281, USA\CSTORE WATER ABS-260612_SHEET.dwg · model space
Author
claude
Approved
yes · sanjay (batch-approved: HCWA migration)
Timeout
60s

C# payload

// Purge the now-empty PDF_* layers left behind after the strip. A layer is only removed if
// nothing references it anywhere (model space, paper space, or any block definition).
var lt = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);
var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);

// count references per layer across EVERY block record (includes model/paper space + block defs)
var used = new Dictionary<string,int>();
foreach (ObjectId bid in bt) {
    var b = (BlockTableRecord)Tx.GetObject(bid, OpenMode.ForRead);
    foreach (ObjectId eid in b) {
        var e = Tx.GetObject(eid, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
        if (e == null) continue;
        used[e.Layer] = used.TryGetValue(e.Layer, out var n) ? n+1 : 1;
    }
}

var kill = new List<ObjectId>();
var names = new List<string>();
foreach (ObjectId id in lt) {
    var l = (LayerTableRecord)Tx.GetObject(id, OpenMode.ForRead);
    if (!l.Name.ToUpperInvariant().StartsWith("PDF_")) continue;
    if (l.Name == "PDF_IMPORT_ASBUILT") continue;          // the block ref itself lives here
    if (used.ContainsKey(l.Name)) continue;                 // still has geometry - keep
    if (l.IsUsed) continue;                                 // AutoCAD's own in-use check
    kill.Add(id); names.Add(l.Name);
}
Log($"empty PDF_* layers to purge: {kill.Count}");
int gone = 0, failed = 0;
for (int i = 0; i < kill.Count; i++) {
    try {
        var l = (LayerTableRecord)Tx.GetObject(kill[i], OpenMode.ForWrite);
        l.Erase();
        gone++;
    } catch (System.Exception ex) { failed++; Log($"  could not purge '{names[i]}': {ex.Message}"); }
}
// verify
var lt2 = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);
int left = 0, total = 0;
foreach (ObjectId id in lt2) {
    var l = (LayerTableRecord)Tx.GetObject(id, OpenMode.ForRead);
    total++;
    if (l.Name.ToUpperInvariant().StartsWith("PDF_")) left++;
}
Log($"PURGED {gone} layers ({failed} failed). PDF_* layers remaining: {left}. Total layers now: {total} (verified).");

Result

Status
success
Drawing file
C:\Users\sanja\OneDrive\_SurveyDisco\260613 - 5030 E Lake Pkwy, Stockbridge, GA 30281, USA\CSTORE WATER ABS-260612_SHEET.dwg
Message
Executed C3D-PDFPURGE (0 entities touched).
Entities
Duration
754 ms
Civil 3D
25.0.0.0

Log

empty PDF_* layers to purge: 0
PURGED 0 layers (0 failed). PDF_* layers remaining: 77. Total layers now: 165 (verified).

Notes

What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.

No notes yet.