← Inbox

C3D-0100 · ERASE all PDF layers and their 5824 entities [PDF-ERASE] done

User explicitly chose erase over freeze. Thaws, unlocks and turns on the 7 PDF* layers, erases every entity on them (5824 expected), then removes the layer records themselves. Only layers whose name starts with PDF are touched. Verifies no PDF layers remain.

Script file
C3D-0100.cs
Target
active document · model space
Author
claude
Approved
yes · reviewer
Timeout
300s

C# payload

// ERASE every entity on a PDF* layer, then remove the layers themselves.
// User explicitly confirmed erase over freeze: "ERASE I DONT NEED ANY OF THESE LAYERS".
//
// Expected from C3D-0099: 7 layers, 5824 entities.
//   PDF2_INTER_CONTOURS 5365, PDF2_E GRAVEL 427, PDF2_SWM 14, PDF2_E IPF-SET 10,
//   PDF2_C-DEMO 4, PDF2_P-ESMT 3, PDF2_V-BNDY-ANNO 1
//
// Only layers whose name starts with PDF are touched. Frozen/locked layers are thawed and
// unlocked first, otherwise the erase silently fails on them.
var lt = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForWrite);
var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);
Log("drawing: " + Db.Filename);

// collect the PDF layer names + ids
var pdfNames = new HashSet<string>();
var pdfIds = new List<ObjectId>();
foreach (ObjectId lid in lt) {
    var l = (LayerTableRecord)Tx.GetObject(lid, OpenMode.ForRead);
    if (!l.Name.ToUpper().StartsWith("PDF")) continue;
    pdfNames.Add(l.Name);
    pdfIds.Add(lid);
}
Log("PDF layers found: " + pdfNames.Count);

// thaw + unlock so nothing blocks the erase
foreach (ObjectId lid in pdfIds) {
    var l = (LayerTableRecord)Tx.GetObject(lid, OpenMode.ForWrite);
    if (l.IsFrozen) l.IsFrozen = false;
    if (l.IsLocked) l.IsLocked = false;
    if (l.IsOff)    l.IsOff = false;
}

// erase the entities
int erased = 0;
foreach (ObjectId btrId in bt) {
    var btr = (BlockTableRecord)Tx.GetObject(btrId, OpenMode.ForRead);
    var doomed = new List<ObjectId>();
    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 (pdfNames.Contains(lay)) doomed.Add(id);
    }
    foreach (ObjectId id in doomed) {
        try {
            var w = Tx.GetObject(id, OpenMode.ForWrite);
            w.Erase();
            erased++;
        } catch (System.Exception ex) { Log("  could not erase " + id.Handle + ": " + ex.Message); }
    }
}
Log("entities erased: " + erased);

// now remove the layers (a layer still holding anything, or in use, will refuse)
int removed = 0, kept = 0;
foreach (ObjectId lid in pdfIds) {
    var l = (LayerTableRecord)Tx.GetObject(lid, OpenMode.ForWrite);
    string nm = l.Name;
    try { l.Erase(); removed++; Log("  layer removed: " + nm); }
    catch (System.Exception ex) { kept++; Log("  layer KEPT: " + nm + " - " + ex.Message); }
}
Log("layers removed: " + removed + "   kept: " + kept);

// verify
var lt2 = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);
int stillPdf = 0;
foreach (ObjectId lid in lt2) {
    var l = (LayerTableRecord)Tx.GetObject(lid, OpenMode.ForRead);
    if (!l.IsErased && l.Name.ToUpper().StartsWith("PDF")) stillPdf++;
}
Log("PDF layers remaining: " + stillPdf + " (verified)");
Ed.Regen();

Result

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

Log

drawing: C:\Users\sanja\OneDrive\_SurveyDisco\260613 - 5030 E Lake Pkwy, Stockbridge, GA 30281, USA\ACAD-260612 - CSTORE WATER ABS 30.dwg
PDF layers found: 7
entities erased: 5824
  layer removed: PDF2_C-DEMO
  layer removed: PDF2_E GRAVEL
  layer removed: PDF2_V-BNDY-ANNO
  layer removed: PDF2_E IPF-SET
  layer removed: PDF2_INTER_CONTOURS
  layer removed: PDF2_SWM
  layer removed: PDF2_P-ESMT
layers removed: 7   kept: 0
PDF layers remaining: 0 (verified)

Notes

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

No notes yet.