C3D-PSCENTER · Center block on 22X17 C sheet done
Computes the SS_BLOCK_22X17 reference extents and translates it so its centre matches the sheet centre (from PlotPaperSize). Reads back and verifies.
C# payload
// Center the SS_BLOCK_22X17 reference on the '22X17 C' sheet.
// Insert was at (0,0) which puts the block's own origin at the paper corner. Compute the
// reference's GeometricExtents and translate so its centre matches the sheet centre.
// Sheet size comes from the layout's PlotPaperSize (mm) converted to inches, since Limits
// can be stale; falls back to Limits if needed.
// 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);
// sheet size in inches
double sw = layout.PlotPaperSize.X / 25.4;
double sh = layout.PlotPaperSize.Y / 25.4;
Log($"Sheet '{LAYOUT}': {sw:F3} x {sh:F3} in (plotPaperSize {layout.PlotPaperSize})");
int moved = 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;
Extents3d ext;
try { ext = br.GeometricExtents; }
catch { Log(" could not get extents - block may be empty"); continue; }
double bw = ext.MaxPoint.X - ext.MinPoint.X;
double bh = ext.MaxPoint.Y - ext.MinPoint.Y;
double cx = (ext.MinPoint.X + ext.MaxPoint.X) / 2.0;
double cy = (ext.MinPoint.Y + ext.MaxPoint.Y) / 2.0;
Log($" block extents {bw:F3} x {bh:F3} in, centre ({cx:F3},{cy:F3}), position ({br.Position.X:F3},{br.Position.Y:F3})");
double dx = sw / 2.0 - cx;
double dy = sh / 2.0 - cy;
var w = (BlockReference)Tx.GetObject(id, OpenMode.ForWrite);
w.TransformBy(Matrix3d.Displacement(new Vector3d(dx, dy, 0)));
moved++;
var e2 = w.GeometricExtents;
Log($" moved by ({dx:F3},{dy:F3}) -> new centre ({(e2.MinPoint.X+e2.MaxPoint.X)/2:F3},{(e2.MinPoint.Y+e2.MaxPoint.Y)/2:F3}) vs sheet centre ({sw/2:F3},{sh/2:F3}) (verified)");
}
Log($"Centered {moved} '{BLK}' reference(s) on '{LAYOUT}'.");
Result
Log
Sheet '22X17 C': 22.000 x 17.000 in (plotPaperSize (558.7999877929688,431.79998779296875)) could not get extents - block may be empty Centered 0 'SS_BLOCK_22X17' reference(s) on '22X17 C'.
Notes
What worked, what didn't, job-specific gotchas — flagged notes feed the recipes.
No notes yet.