← Inbox

C3D-BLK-MSLAYERS · Model space layers + SEWER_LINE state done

Read-only: layers carrying objects in MODEL SPACE only (excluding block definitions), the full state of SEWER_LINE including where its entities live, and any per-viewport frozen layers.

Script file
C3D-BLK-MSLAYERS.cs
Target
SS_BLOCK_22X17 · model space
Author
claude
Approved
yes · reviewer
Timeout
180s

C# payload

// READ-ONLY: MODEL SPACE only (not block definitions), plus the state of SEWER_LINE.
var ms = (BlockTableRecord)Tx.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(Db), OpenMode.ForRead);
var lt = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);

var used = new Dictionary<string, Dictionary<string,int>>();
foreach (ObjectId id in ms) {
    var e = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
    if (e == null) continue;
    if (!used.ContainsKey(e.Layer)) used[e.Layer] = new Dictionary<string,int>();
    var k = e.GetType().Name;
    used[e.Layer][k] = used[e.Layer].ContainsKey(k) ? used[e.Layer][k]+1 : 1;
}
var names = new List<string>(used.Keys); names.Sort();
Log("### MODEL SPACE: " + names.Count + " layers carry objects");
foreach (var n in names) {
    int tot = 0; var parts = new List<string>();
    foreach (var kv in used[n]) { tot += kv.Value; parts.Add(kv.Value + " " + kv.Key); }
    var l = lt.Has(n) ? (LayerTableRecord)Tx.GetObject(lt[n], OpenMode.ForRead) : null;
    var f = l == null ? "" : (l.IsOff ? " OFF" : "") + (l.IsFrozen ? " FROZEN" : "") + (l.IsLocked ? " LOCKED" : "");
    Log(n + "|" + tot + "|" + string.Join(", ", parts) + "|" + f);
}

Log("### SEWER_LINE state");
if (!lt.Has("SEWER_LINE")) { Log("  layer SEWER_LINE DOES NOT EXIST"); }
else {
    var l = (LayerTableRecord)Tx.GetObject(lt["SEWER_LINE"], OpenMode.ForRead);
    Log("  layer: off=" + l.IsOff + " frozen=" + l.IsFrozen + " locked=" + l.IsLocked +
        " plot=" + l.IsPlottable + " colour=" + l.Color.ColorIndex);
    int inMs = 0, inDefs = 0, erased = 0;
    foreach (ObjectId id in ms) {
        var e = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
        if (e != null && e.Layer == "SEWER_LINE") {
            inMs++;
            var ln = e as Line;
            if (ln != null)
                Log("    Line h=" + e.Handle + " (" + ln.StartPoint.X.ToString("F2") + "," + ln.StartPoint.Y.ToString("F2") +
                    ") -> (" + ln.EndPoint.X.ToString("F2") + "," + ln.EndPoint.Y.ToString("F2") + ") len=" + ln.Length.ToString("F2"));
            else Log("    " + e.GetType().Name + " h=" + e.Handle);
        }
    }
    var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);
    foreach (ObjectId bid in bt) {
        var btr = (BlockTableRecord)Tx.GetObject(bid, OpenMode.ForRead);
        if (btr.Name == "*Model_Space") continue;
        foreach (ObjectId id in btr) {
            var e = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
            if (e != null && e.Layer == "SEWER_LINE") { inDefs++; }
        }
    }
    Log("  SEWER_LINE entities: " + inMs + " in model space, " + inDefs + " inside block definitions/layouts");
}

Log("### per-viewport frozen layers (a layer can be thawed globally but frozen in a VP)");
var layDict = (DBDictionary)Tx.GetObject(Db.LayoutDictionaryId, OpenMode.ForRead);
foreach (DBDictionaryEntry de in layDict) {
    var lay = (Layout)Tx.GetObject(de.Value, OpenMode.ForRead);
    var ps = (BlockTableRecord)Tx.GetObject(lay.BlockTableRecordId, OpenMode.ForRead);
    foreach (ObjectId id in ps) {
        var vp = Tx.GetObject(id, OpenMode.ForRead) as Viewport;
        if (vp == null) continue;
        var fr = vp.GetFrozenLayers();
        if (fr == null || fr.Count == 0) continue;
        var nm = new List<string>();
        foreach (ObjectId f in fr) {
            var l2 = Tx.GetObject(f, OpenMode.ForRead) as LayerTableRecord;
            if (l2 != null) nm.Add(l2.Name);
        }
        Log("  '" + lay.LayoutName + "' vp" + vp.Number + " freezes: " + string.Join(", ", nm));
    }
}

Result

Status
success
Drawing file
C:\Users\sanja\OneDrive\_SurveyDisco\260613 - 5030 E Lake Pkwy, Stockbridge, GA 30281, USA\SS_BLOCK_22X17.dwg
Message
Executed C3D-BLK-MSLAYERS (0 entities touched).
Entities
Duration
388 ms
Civil 3D
25.0.0.0

Log

### MODEL SPACE: 11 layers carry objects
0|586|570 CogoPoint, 15 ParcelSegmentLabel, 1 ParcelSegmentTable| OFF
C-ANNO-TABL-TTBL|5|5 Site| OFF
C-PROP-LINE|1|1 ParcelSegment| OFF
SEWER_EASEMENT|2|1 Site, 1 ParcelSegment| OFF
SEWER_LINE|7|7 Line| OFF
SS-BUFFER LANDSCAPE|2|2 Site| OFF
SS-FLOW WELL|1|1 Circle| OFF
SS-V-ROAD-CURB|2|2 Site| OFF
V-CTRL-HCPT|2|2 CogoPoint| OFF
V-NODE-TREE|2|2 CogoPoint| OFF
WATER_LINE|1|1 Parcel| OFF
### SEWER_LINE state
  layer: off=True frozen=False locked=False plot=True colour=3
    Line h=23920 (2296604.87,1280046.08) -> (2296549.92,1280052.70) len=55.35
    Line h=23921 (2296827.25,1280095.99) -> (2296877.72,1280034.24) len=79.75
    Line h=23935 (2296379.87,1279953.37) -> (2296511.04,1279951.48) len=131.18
    Line h=23936 (2296511.04,1279951.48) -> (2296604.87,1280046.08) len=133.24
    Line h=23937 (2296604.87,1280046.08) -> (2296691.08,1280045.83) len=86.20
    Line h=23938 (2296691.08,1280045.83) -> (2296714.13,1280024.99) len=31.07
    Line h=23939 (2296714.13,1280024.99) -> (2296827.25,1280095.99) len=133.56
  SEWER_LINE entities: 7 in model space, 0 inside block definitions/layouts
### per-viewport frozen layers (a layer can be thawed globally but frozen in a VP)
  '11X17' vp-1 freezes: C-TOPO, SS-0-FRAME, C-TOPO-TEXT

Notes

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

No notes yet.