← Inbox

C3D-0167 · Reload SS_BLOCK_11X17 from the block file + refill attributes [RELOAD-BLOCK-260714] done

Replaces this drawing stale SS_BLOCK_11X17 definition with the updated one from SS_BLOCK_11X17.dwg using a side database and WblockCloneObjects with Replace, so the existing insert keeps its position, then rebuilds each insert attribute collection from the new definition and fills every value for job 260714 including FIRM_PANEL 13113C0155E, FIRM_DATE September 26 2008 and FLOOD_ZONE X.

Script file
C3D-0167.cs
Target
260714 · paper space
Author
claude
Approved
yes · reviewer
Timeout
240s

C# payload

// Replace this drawing's stale SS_BLOCK_11X17 definition with the updated one from the block
// file (which now carries the flood note + FIRM_PANEL / FIRM_DATE / FLOOD_ZONE attributes), then
// refill every attribute for job 260714.
//
// Method: read the block DEFINITION out of the source drawing with a side database and
// WblockCloneObjects with DuplicateRecordCloning.Replace - that redefines the block in place, so
// the existing insert keeps its position and simply picks up the new definition. Safer than
// erase + purge + re-insert, which loses the placement.
//
// Attribute values come from GET /api/project-by-id?job_number=260714 plus the FEMA fields:
//   firmPanel 13113C0155E | firmPanelDate 09/26/2008 | floodZone X
//
// Only SS_BLOCK_11X17 is touched.
const string BLOCK = "SS_BLOCK_11X17";
const string SRC   = @"C:\Users\sanja\OneDrive\_SurveyDisco\SS_BLOCK_11X17.dwg";
Log("drawing: " + Db.Filename);
if (!System.IO.File.Exists(SRC)) { Log("ABORT: source block file not found: " + SRC); return; }

// --- 1. pull the updated definition in, replacing the local one ---
using (var side = new Database(false, true))
{
    side.ReadDwgFile(SRC, FileOpenMode.OpenForReadAndAllShare, false, null);
    side.CloseInput(true);

    var srcBt = (BlockTable)side.TransactionManager.StartTransaction().GetObject(side.BlockTableId, OpenMode.ForRead);
    // (the side transaction is disposed with the side database)
    var ids = new ObjectIdCollection();
    if (!srcBt.Has(BLOCK)) { Log("ABORT: " + BLOCK + " not in the source file"); return; }
    ids.Add(srcBt[BLOCK]);

    var map = new IdMapping();
    Db.WblockCloneObjects(ids, Db.BlockTableId, map, DuplicateRecordCloning.Replace, false);
    Log("definition replaced from " + SRC);
}

// --- 2. sync every insert to the new attribute set, preserving values where the tag survives ---
var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);
var defBtr = (BlockTableRecord)Tx.GetObject(bt[BLOCK], OpenMode.ForRead);

// the definition's attribute defs, in order
var defs = new List<AttributeDefinition>();
foreach (ObjectId id in defBtr) {
    var ad = Tx.GetObject(id, OpenMode.ForRead) as AttributeDefinition;
    if (ad != null && !ad.Constant) defs.Add(ad);
}
Log("definition now has " + defs.Count + " attribute definitions");

var want = new Dictionary<string,string>();
want["JOB_NO"]               = "JOB NO.: 260714";
want["SITE_ADDRESS"]         = "285 Huckaby Rd, Brooks, GA 30205, USA";
want["COUNTY"]               = "COUNTY: Fayette";
want["LAND_LOT"]             = "LAND LOT: 124";
want["DISTRICT"]             = "DISTRICT: 4th";
want["DATE"]                 = "DATE: 08/13/26";
want["SURVEY_TYPE"]          = "Boundary Survey";
want["PREP_FOR"]             = "JOY EWING";
want["REF_DB_PG"]            = "REF DB 5857 Page 692";
want["SUBDIVISION_NAME_LOT"] = "";
want["FIRM_PANEL"]           = "13113C0155E";
want["FIRM_DATE"]            = "SEPTEMBER 26, 2008";
want["FLOOD_ZONE"]           = "X";

int inserts = 0;
foreach (ObjectId btrId in bt) {
    var btr = (BlockTableRecord)Tx.GetObject(btrId, OpenMode.ForRead);
    foreach (ObjectId id in btr) {
        var br0 = Tx.GetObject(id, OpenMode.ForRead) as BlockReference;
        if (br0 == null || br0.Name != BLOCK) continue;
        inserts++;
        var br = (BlockReference)Tx.GetObject(id, OpenMode.ForWrite);
        Log("");
        Log("insert [" + br.Handle + "] in " + btr.Name + " at "
            + br.Position.X.ToString("F4") + "," + br.Position.Y.ToString("F4"));

        // keep whatever values the old attributes had, keyed by tag
        var had = new Dictionary<string,string>();
        foreach (ObjectId aid in br.AttributeCollection) {
            var at = Tx.GetObject(aid, OpenMode.ForRead) as AttributeReference;
            if (at != null) had[at.Tag] = at.TextString;
        }

        // rebuild the attribute collection from the NEW definition
        foreach (ObjectId aid in br.AttributeCollection) {
            var at = (AttributeReference)Tx.GetObject(aid, OpenMode.ForWrite);
            at.Erase();
        }
        foreach (var ad in defs) {
            var ar = new AttributeReference();
            ar.SetAttributeFromBlock(ad, br.BlockTransform);
            string v = want.ContainsKey(ad.Tag) ? want[ad.Tag]
                     : (had.ContainsKey(ad.Tag) ? had[ad.Tag] : ad.TextString);
            ar.TextString = v;
            br.AttributeCollection.AppendAttribute(ar);
            Tx.AddNewlyCreatedDBObject(ar, true);
            Log(String.Format("   {0,-22} = \"{1}\"", ad.Tag, v));
        }
    }
}
Log("");
Log("inserts updated: " + inserts + " (verified)");
Ed.Regen();

Result

Status
success
Drawing file
C:\Users\sanja\OneDrive\_SurveyDisco\260714 - 285 Huckaby Rd, Brooks, GA 30205, USA\260714 - 285 Huckaby Rd.dwg
Message
Executed C3D-0167 (0 entities touched).
Entities
Duration
866 ms
Civil 3D
25.0.0.0

Log

drawing: C:\Users\sanja\OneDrive\_SurveyDisco\260714 - 285 Huckaby Rd, Brooks, GA 30205, USA\260714 - 285 Huckaby Rd.dwg
definition replaced from C:\Users\sanja\OneDrive\_SurveyDisco\SS_BLOCK_11X17.dwg
definition now has 13 attribute definitions

insert [20355] in *Paper_Space at 10.8150,0.2874
   JOB_NO                 = "JOB NO.: 260714"
   SITE_ADDRESS           = "285 Huckaby Rd, Brooks, GA 30205, USA"
   REF_DB_PG              = "REF DB 5857 Page 692"
   LAND_LOT               = "LAND LOT: 124"
   DISTRICT               = "DISTRICT: 4th"
   COUNTY                 = "COUNTY: Fayette"
   DATE                   = "DATE: 08/13/26"
   SUBDIVISION_NAME_LOT   = ""
   PREP_FOR               = "JOY EWING"
   SURVEY_TYPE            = "Boundary Survey"
   FIRM_PANEL             = "13113C0155E"
   FIRM_DATE              = "SEPTEMBER 26, 2008"
   FLOOD_ZONE             = "X"

inserts updated: 1 (verified)

Notes

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

No notes yet.