C3D-0367 · Kill the PDF2 layers again (user restored a couple, now doesn't need them) [C3D-PDFMERGE2] done
Re-runs the PDF2 cleanup: finds whatever PDF2_* layers exist now (the user brought some back after C3D-0366 and no longer needs them), moves their entities to a single PDF2 layer, erases them, and deletes the emptied layers. Idempotent - reports 'nothing to do' if none remain. Verifies no PDF2 layer or entity is left.
C# payload
// Merge all PDF2_* layers into one layer "PDF2", then erase that layer's entities and delete
// every emptied PDF2_* layer. Net: the exploded PDF geometry is removed from the drawing.
// The user confirmed erase (not freeze) - they do not want this geometry in the file at all.
const string TARGET = "PDF2";
Log("drawing: " + Db.Filename);
var lt = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForWrite);
// which layers are PDF2_* (the merge sources)
var srcNames = new List<string>();
foreach (ObjectId id in lt) {
var l = (LayerTableRecord)Tx.GetObject(id, OpenMode.ForRead);
if (l.Name.StartsWith("PDF2_", StringComparison.OrdinalIgnoreCase)) srcNames.Add(l.Name);
}
Log("PDF2_ source layers: " + srcNames.Count);
if (srcNames.Count == 0) { Log("nothing to do"); return; }
// ensure the target layer exists and is thawed/on so entities can be moved and then erased
ObjectId targetId;
if (lt.Has(TARGET)) targetId = lt[TARGET];
else {
var nl = new LayerTableRecord(); nl.Name = TARGET;
targetId = lt.Add(nl); Tx.AddNewlyCreatedDBObject(nl, true);
Log("created target layer " + TARGET);
}
{
var t = (LayerTableRecord)Tx.GetObject(targetId, OpenMode.ForWrite);
if (t.IsFrozen) t.IsFrozen = false;
if (t.IsOff) t.IsOff = false;
if (t.IsLocked) t.IsLocked = false;
}
// thaw/unlock the sources so their entities are editable
foreach (var n in srcNames) {
var l = (LayerTableRecord)Tx.GetObject(lt[n], OpenMode.ForWrite);
if (l.IsFrozen) l.IsFrozen = false;
if (l.IsOff) l.IsOff = false;
if (l.IsLocked) l.IsLocked = false;
}
// move every entity on a PDF2_* layer to TARGET, then erase it
var srcSet = new HashSet<string>(srcNames, StringComparer.OrdinalIgnoreCase);
var ms = (BlockTableRecord)Tx.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(Db), OpenMode.ForRead);
int moved = 0, erased = 0;
foreach (ObjectId id in ms) {
var probe = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
if (probe == null) continue;
if (!srcSet.Contains(probe.Layer)) continue;
var e = (Autodesk.AutoCAD.DatabaseServices.Entity)Tx.GetObject(id, OpenMode.ForWrite);
e.Layer = TARGET;
moved++;
e.Erase();
erased++;
}
Log(string.Format("moved {0} entities to {1}, erased {2}", moved, TARGET, erased));
// delete the now-empty layers (sources + the target). A layer must be current-off to delete.
var clayer = Db.Clayer;
// make sure current layer is not one we're deleting
{
var cl = (LayerTableRecord)Tx.GetObject(clayer, OpenMode.ForRead);
if (srcSet.Contains(cl.Name) || string.Equals(cl.Name, TARGET, StringComparison.OrdinalIgnoreCase)) {
Db.Clayer = lt["0"];
Log("current layer was a delete target - switched to 0");
}
}
var toDelete = new List<string>(srcNames); toDelete.Add(TARGET);
int deleted = 0;
foreach (var n in toDelete) {
if (!lt.Has(n)) continue;
try {
var l = (LayerTableRecord)Tx.GetObject(lt[n], OpenMode.ForWrite);
l.Erase();
deleted++;
} catch (System.Exception ex) { Log("could not delete " + n + ": " + ex.Message); }
}
Log("deleted " + deleted + " layers");
// verify: no PDF2 layer remains, no entity references one
var lt2 = (LayerTable)Tx.GetObject(Db.LayerTableId, OpenMode.ForRead);
int remain = 0;
foreach (ObjectId id in lt2) {
var l = (LayerTableRecord)Tx.GetObject(id, OpenMode.ForRead);
if (l.Name.StartsWith("PDF2", StringComparison.OrdinalIgnoreCase)) { remain++; Log(" STILL PRESENT: " + l.Name); }
}
int stray = 0;
foreach (ObjectId id in ms) {
var e = Tx.GetObject(id, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
if (e != null && !e.IsErased && e.Layer.StartsWith("PDF2", StringComparison.OrdinalIgnoreCase)) stray++;
}
Log(string.Format("VERIFY: PDF2 layers remaining={0}, entities still on a PDF2 layer={1} {2}",
remain, stray, (remain==0 && stray==0) ? "CLEAN" : "*** NOT CLEAN ***"));
Ed.Regen();
Result
Log
drawing: C:\Users\sanja\OneDrive\_SurveyDisco\260613 - 5030 E Lake Pkwy, Stockbridge, GA 30281, USA\ACAD-260612-Springdale Sanitary Sewer Extension As-Built 60 wPROFILE.dwg PDF2_ source layers: 7 created target layer PDF2 moved 5815 entities to PDF2, erased 5815 deleted 8 layers VERIFY: PDF2 layers remaining=0, entities still on a PDF2 layer=0 CLEAN
Notes
What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.
No notes yet.