← Inbox

C3D-PS22 · Insert + center 22x17 title block done

Imports the WBLOCKd 22x17 title block from _SurveyDisco and centers it on the 22X17 C sheet (22.00 x 17.00 in, centre 11,8.5). Aborts if the definition is empty or has >400 entities (full-drawing guard).

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

C# payload

// Import the WBLOCK'd 22x17 title block from _SurveyDisco and center it on '22X17 C'.
// Sheet is 22.00 x 17.00 in (from layout.PlotPaperSize 558.8 x 431.8 mm / 25.4), so the sheet
// centre is (11, 8.5). Tries the R3 then R2 filename.
// Verifies the imported definition actually has geometry BEFORE inserting, so a full-drawing
// file (like the earlier 5.5MB one that dragged in 770 survey entities) can't leave junk.
// Host owns Tx - no using/Commit here. The side Database IS ours to dispose.
const string BLK    = "SS_BLOCK_22X17";
const string LAYOUT = "22X17 C";
var candidates = new[] {
    @"C:\Users\sanja\OneDrive\_SurveyDisco\SS_BLOCK_22X17_R3.dwg",
    @"C:\Users\sanja\OneDrive\_SurveyDisco\SS_BLOCK_22X17_R2.dwg",
};

string dwg = null;
foreach (var c in candidates) { if (System.IO.File.Exists(c)) { dwg = c; break; } }
if (dwg == null)
{
    Log("None of these exist from the plugin's view:");
    foreach (var c in candidates) Log($"    {c}");
    var dir = @"C:\Users\sanja\OneDrive\_SurveyDisco";
    if (System.IO.Directory.Exists(dir))
        foreach (var f in System.IO.Directory.GetFiles(dir, "*22X17*"))
            Log($"  but found: {f}");
    return;
}
Log($"Using {System.IO.Path.GetFileName(dwg)}");

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}'.");
}

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; }
if (n > 400) { Log($"{n} entities is too many for a title block - looks like a full drawing. 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}'."); return; }
}

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

double sw = layout.PlotPaperSize.X / 25.4, sh = layout.PlotPaperSize.Y / 25.4;
Extents3d ext;
try { ext = br.GeometricExtents; }
catch { Log($"Inserted at (0,0); extents unavailable so 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 (centre {sw/2:F2},{sh/2:F2}); 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: block centre now ({(e2.MinPoint.X+e2.MaxPoint.X)/2:F3},{(e2.MinPoint.Y+e2.MaxPoint.Y)/2:F3}), extents ({e2.MinPoint.X:F2},{e2.MinPoint.Y:F2})-({e2.MaxPoint.X:F2},{e2.MaxPoint.Y:F2}) (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-PS22 (0 entities touched).
Entities
Duration
559 ms
Civil 3D
25.0.0.0

Log

Using SS_BLOCK_22X17_R3.dwg
Imported 'SS_BLOCK_22X17'.
definition holds 44 entities:
       1  Viewport
      25  Line
       1  MText
       7  DBText
      10  AttributeDefinition
sheet 22.00 x 17.00 in (centre 11.00,8.50); block 25.18 x 16.48 in
Centered: block centre now (11.000,8.500), extents (-1.59,0.26)-(23.59,16.74) (verified). Handle 222EB.

Notes

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

No notes yet.