← Inbox

C3D-PSBLOCK3 · Insert + center SS_BLOCK_22X17 (R3) done

Imports the WBLOCKd SS_BLOCK_22X17_R3.dwg, verifies the definition has geometry, inserts into 22X17 C paper space and centers it on the sheet. Aborts if the definition is empty.

Script file
C3D-PSBLOCK3.cs
Target
active document · paper space
Author
claude
Approved
yes · auto-auth
Timeout
60s

C# payload

// Import the WBLOCK'd title block and center it on the '22X17 C' sheet.
// Source is 170KB (a real WBLOCK) vs the earlier 5.5MB full-drawing file that pulled in 770
// survey entities. Verifies the definition has geometry BEFORE inserting, so a bad import
// can't leave junk behind.
// Host owns Tx - no using/Commit here. The side Database IS ours to dispose.
const string DWG    = @"C:\Users\sanja\OneDrive\Documents\SS_BLOCK_22X17_R3.dwg";
const string BLK    = "SS_BLOCK_22X17";
const string LAYOUT = "22X17 C";

if (!System.IO.File.Exists(DWG)) { Log($"NOT FOUND: {DWG}"); return; }

var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);
ObjectId blkId;
if (bt.Has(BLK)) { blkId = bt[BLK]; Log($"'{BLK}' already defined - reusing."); }
else
{
    using (var side = new Database(false, true))
    {
        side.ReadDwgFile(DWG, System.IO.FileShare.Read, true, null);
        side.CloseInput(true);
        blkId = Db.Insert(BLK, side, true);
    }
    Log($"Imported '{BLK}' from {System.IO.Path.GetFileName(DWG)}.");
}

var def = (BlockTableRecord)Tx.GetObject(blkId, OpenMode.ForRead);
int n = 0; var kinds = new Dictionary<string,int>();
foreach (ObjectId x in def) { n++; var k = Tx.GetObject(x, OpenMode.ForRead).GetType().Name;
    kinds[k] = kinds.ContainsKey(k) ? kinds[k]+1 : 1; }
Log($"definition holds {n} entities:");
foreach (var kv in kinds) Log($"    {kv.Value,4}  {kv.Key}");
if (n == 0) { Log("EMPTY definition - aborting."); return; }

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);

foreach (ObjectId id in ps)
{
    var b0 = Tx.GetObject(id, OpenMode.ForRead) as BlockReference;
    if (b0 == null) continue;
    var d0 = (BlockTableRecord)Tx.GetObject(b0.BlockTableRecord, OpenMode.ForRead);
    if (d0.Name == BLK) { Log($"'{BLK}' already placed in '{LAYOUT}' - not adding again."); return; }
}

var br = new BlockReference(Point3d.Origin, blkId);
ps.AppendEntity(br);
Tx.AddNewlyCreatedDBObject(br, true);

double sw = layout.PlotPaperSize.X / 25.4;
double sh = layout.PlotPaperSize.Y / 25.4;
Extents3d ext;
try { ext = br.GeometricExtents; }
catch { Log($"Inserted at (0,0) but extents unavailable - not centered. Handle {br.Handle}."); return; }

double bw = ext.MaxPoint.X - ext.MinPoint.X, bh = ext.MaxPoint.Y - ext.MinPoint.Y;
double cx = (ext.MinPoint.X + ext.MaxPoint.X)/2.0, cy = (ext.MinPoint.Y + ext.MaxPoint.Y)/2.0;
Log($"sheet {sw:F2} x {sh:F2} in; block {bw:F2} x {bh:F2} in");

br.TransformBy(Matrix3d.Displacement(new Vector3d(sw/2.0 - cx, sh/2.0 - cy, 0)));
var e2 = br.GeometricExtents;
Log($"Centered on '{LAYOUT}': centre ({(e2.MinPoint.X+e2.MaxPoint.X)/2:F3},{(e2.MinPoint.Y+e2.MaxPoint.Y)/2:F3}) vs sheet ({sw/2:F3},{sh/2:F3}) (verified). Handle {br.Handle}.");

Result

Status
success
Drawing file
C:\Users\sanja\OneDrive\_SurveyDisco\260619 - 3625 Stonewall Tell Rd, Atlanta, GA 30349, USA\260619 - 3625 Stonewall Tell Rd ROTATE.dwg
Message
Executed C3D-PSBLOCK3 (0 entities touched).
Entities
Duration
423 ms
Civil 3D
25.0.0.0

Log

NOT FOUND: C:\Users\sanja\OneDrive\Documents\SS_BLOCK_22X17_R3.dwg

Notes

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

No notes yet.