← Inbox

C3D-0103 · ERASE layers 9, 26, 27, 34, 35, 53 and the three empty ones [LAYER-ERASE] done

Erases every entity on BOUNDARY, SEWER_EASEMENT POLY, SEWER_EASEMENT_20, SS-0-FRAME, SS-BOUNDARY, WATER_LINE_PTS, ADDRESS_TEXT, V-CTRL-LINE-SHOT and V-CTRL-TEXT, then removes the layer records. Matched by exact name; frozen and locked layers are thawed first so the erase cannot silently skip them. Verifies the remaining layer count.

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

C# payload

// ERASE the layers the user selected by number from C3D-0102, and everything on them.
// User confirmed: "I SAVED THE FILE IM TIRED OF FKC WITH THIS JUST DO IT".
//
//   9  BOUNDARY               4 paper
//   26 SEWER_EASEMENT POLY    2 model
//   27 SEWER_EASEMENT_20      1 model
//   34 SS-0-FRAME             21 model + 4 blockdef
//   35 SS-BOUNDARY            2 model  (frozen)
//   53 WATER_LINE_PTS         25 model + 50 blockdef
//   all-zero layers: 2 ADDRESS_TEXT, 43 V-CTRL-LINE-SHOT, 44 V-CTRL-TEXT
//
// Layers are matched by EXACT name - no wildcards - so nothing adjacent is caught.
// Frozen/locked/off layers are thawed first or the erase silently fails on them.
string[] TARGETS = new string[] {
    "BOUNDARY",
    "SEWER_EASEMENT POLY",
    "SEWER_EASEMENT_20",
    "SS-0-FRAME",
    "SS-BOUNDARY",
    "WATER_LINE_PTS",
    "ADDRESS_TEXT",
    "V-CTRL-LINE-SHOT",
    "V-CTRL-TEXT",
};

var lt = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);
var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);
Log("drawing: " + Db.Filename);

var want = new HashSet<string>();
var ids = new List<ObjectId>();
foreach (string nm in TARGETS) {
    if (!lt.Has(nm)) { Log("  NOT FOUND: " + nm); continue; }
    want.Add(nm);
    ids.Add(lt[nm]);
}
Log("layers targeted: " + want.Count + " of " + TARGETS.Length);

// thaw / unlock / turn on so nothing blocks the erase
foreach (ObjectId lid in ids) {
    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;
}

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 (want.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);

int removed = 0, kept = 0;
foreach (ObjectId lid in ids) {
    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);

var lt2 = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);
int total = 0;
foreach (ObjectId lid in lt2) {
    var l = (LayerTableRecord)Tx.GetObject(lid, OpenMode.ForRead);
    if (!l.IsErased) total++;
}
Log("layers remaining in drawing: " + total + " (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-0103 (0 entities touched).
Entities
Duration
428 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
layers targeted: 9 of 9
entities erased: 109
  layer removed: BOUNDARY
  layer removed: SEWER_EASEMENT POLY
  layer removed: SEWER_EASEMENT_20
  layer removed: SS-0-FRAME
  layer removed: SS-BOUNDARY
  layer removed: WATER_LINE_PTS
  layer removed: ADDRESS_TEXT
  layer removed: V-CTRL-LINE-SHOT
  layer removed: V-CTRL-TEXT
layers removed: 9   kept: 0
layers remaining in drawing: 47 (verified)

Notes

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

No notes yet.