← Inbox

C3D-PSBLOCK · Insert SS_BLOCK_22X17 into 22X17 C done

Imports SS_BLOCK_22X17.dwg as a block definition (not currently in the drawing) and inserts it into the 22X17 C paper space layout at origin, scale 1. Idempotent - skips if already inserted.

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

C# payload

// Import SS_BLOCK_22X17.dwg as a block definition and insert it into the '22X17 C' paper
// space layout at the origin, scale 1.
// The block is NOT yet defined in this drawing (only SS_BLOCK_11X17 variants are), so it is
// read from the external DWG via a side database and Db.Insert(...) first.
// NOTE: this ticket appends to the LAYOUT's BlockTableRecord directly, NOT the host's
// ModelSpace global - ModelSpace is the current space and we need paper space explicitly.
// Host owns Tx - no using/Commit on it here. The side Database IS ours to dispose.
const string DWG    = @"C:\Users\sanja\OneDrive\_SurveyDisco\SS_BLOCK_22X17.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 = ObjectId.Null;

if (bt.Has(BLK))
{
    blkId = bt[BLK];
    Log($"Block '{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)}.");
}

// find the layout's paper-space BlockTableRecord
var layDict = (DBDictionary)Tx.GetObject(Db.LayoutDictionaryId, OpenMode.ForRead);
if (!layDict.Contains(LAYOUT)) { Log($"Layout '{LAYOUT}' not found."); return; }
var layout = (Layout)Tx.GetObject(layDict.GetAt(LAYOUT), OpenMode.ForRead);
var ps = (BlockTableRecord)Tx.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);

// already there?
int existing = 0;
foreach (ObjectId id in ps)
{
    var br0 = Tx.GetObject(id, OpenMode.ForRead) as BlockReference;
    if (br0 == null) continue;
    var d = (BlockTableRecord)Tx.GetObject(br0.BlockTableRecord, OpenMode.ForRead);
    if (d.Name == BLK) existing++;
}
if (existing > 0) { Log($"'{BLK}' is already inserted {existing}x in '{LAYOUT}' - not adding again."); return; }

var br = new BlockReference(Point3d.Origin, blkId);
ps.AppendEntity(br);
Tx.AddNewlyCreatedDBObject(br, true);
Log($"Inserted '{BLK}' into layout '{LAYOUT}' at (0,0) scale 1. 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-PSBLOCK (0 entities touched).
Entities
Duration
1514 ms
Civil 3D
25.0.0.0

Log

Imported 'SS_BLOCK_22X17' from SS_BLOCK_22X17.dwg.
Inserted 'SS_BLOCK_22X17' into layout '22X17 C' at (0,0) scale 1. Handle 222A6.

Notes

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

No notes yet.