C3D-PSUNDO · Undo bad block import, find real title block done
Erases the bad SS_BLOCK_22X17 reference and purges the 770-entity definition (the source DWG is a full survey drawing, not a title block). Lists the source DWG layouts and which blocks they use.
C# payload
// Back out the bad import: SS_BLOCK_22X17.dwg is a full survey drawing, not a title block, so
// Db.Insert pulled 770 model-space entities (564 CogoPoints, a TinSurface, parcels...) in as one
// block. Erase the inserted reference and purge the definition.
// Also report what the source DWG's two 215-entity paper spaces contain, since those are the
// likely 22x17 sheets.
// Host owns Tx - no using/Commit here.
const string BLK = "SS_BLOCK_22X17";
const string LAYOUT = "22X17 C";
var layDict = (DBDictionary)Tx.GetObject(Db.LayoutDictionaryId, OpenMode.ForRead);
var layout = (Layout)Tx.GetObject(layDict.GetAt(LAYOUT), OpenMode.ForRead);
var ps = (BlockTableRecord)Tx.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);
int erased = 0;
foreach (ObjectId id in ps)
{
var br = Tx.GetObject(id, OpenMode.ForRead) as BlockReference;
if (br == null) continue;
var def = (BlockTableRecord)Tx.GetObject(br.BlockTableRecord, OpenMode.ForRead);
if (def.Name != BLK) continue;
var w = (BlockReference)Tx.GetObject(id, OpenMode.ForWrite);
w.Erase();
erased++;
}
Log($"Erased {erased} bad '{BLK}' reference(s) from '{LAYOUT}'.");
var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForWrite);
if (bt.Has(BLK))
{
var def = (BlockTableRecord)Tx.GetObject(bt[BLK], OpenMode.ForWrite);
if (def.GetBlockReferenceIds(true, true).Count == 0)
{ def.Erase(); Log($"Purged the '{BLK}' definition (770 stray entities)."); }
else Log($"'{BLK}' still referenced elsewhere - left in place.");
}
// what's in the source paper spaces?
const string DWG = @"C:\Users\sanja\OneDrive\_SurveyDisco\SS_BLOCK_22X17.dwg";
using (var side = new Database(false, true))
{
side.ReadDwgFile(DWG, System.IO.FileShare.Read, true, null);
side.CloseInput(true);
using (var stx = side.TransactionManager.StartTransaction())
{
var sld = (DBDictionary)stx.GetObject(side.LayoutDictionaryId, OpenMode.ForRead);
foreach (DBDictionaryEntry e in sld)
{
var lay = (Layout)stx.GetObject(e.Value, OpenMode.ForRead);
var btr = (BlockTableRecord)stx.GetObject(lay.BlockTableRecordId, OpenMode.ForRead);
int n = 0; var brefs = new List<string>();
foreach (ObjectId x in btr)
{
n++;
var b2 = stx.GetObject(x, OpenMode.ForRead) as BlockReference;
if (b2 != null)
{
var d2 = (BlockTableRecord)stx.GetObject(b2.BlockTableRecord, OpenMode.ForRead);
if (!brefs.Contains(d2.Name)) brefs.Add(d2.Name);
}
}
Log($" source layout '{lay.LayoutName}': {n} entities, paper {lay.PlotPaperSize}, blocks used: {string.Join(", ", brefs)}");
}
stx.Commit();
}
}
Result
Log
Erased 1 bad 'SS_BLOCK_22X17' reference(s) from '22X17 C'. Purged the 'SS_BLOCK_22X17' definition (770 stray entities). source layout '11X17': 215 entities, paper (279.3999938964844,431.79998779296875), blocks used: SS_LEGEND, SS_BLOCK_11X17 source layout '13709-779': 215 entities, paper (279.3999938964844,431.79998779296875), blocks used: SS_LEGEND, SS_BLOCK_11X17 source layout '22X17': 5 entities, paper (558.7999877929688,431.79998779296875), blocks used: SS_BLOCK_11X17 source layout 'Model': 770 entities, paper (215.89999389648438,279.3999938964844), blocks used:
Notes
What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.
No notes yet.