← Inbox

C3D-0251 · 260714 Huckaby: create the 17X11 viewport (set layout current first) done

Retry after eNotInPaperspace: Viewport.On requires the owning layout to be current with TILEMODE=0, so the script switches to 17X11 before creating the viewport. Same geometry - fills the sheet above the title panel at the portrait sheets view and scale, locked.

Script file
C3D-0251.cs
Target
260714 - 285 Huckaby Rd.dwg · model space
Author
claude
Approved
yes · auto-auth
Timeout
60s

C# payload

// The 11X17 sheet's real viewport is a PAPER SPACE entity (#2), not the one inside the block
// definition. 17X11 was created with none, so make one: it must fill the sheet above the
// title panel, and show the same model view at the same scale as the portrait sheet.
//
// Sheet占 the block insert spans (0.290,0.290)-(17.290,11.290).
// Title panel inside the block occupies the bottom ~2.71 + the flood note to ~3.10,
// anchored bottom-left -> in sheet coords roughly x 0.29..10.49, y 0.29..3.39.
// So the viewport gets the full width above the panel.
// Viewport.On requires the owning layout to be CURRENT and the editor to be in paper space -
// otherwise Autodesk throws eNotInPaperspace. Switch to 17X11 and force TILEMODE=0 first.
LayoutManager.Current.CurrentLayout = "17X11";
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("TILEMODE", 0);
Log("current layout -> " + LayoutManager.Current.CurrentLayout);

var bt = (BlockTable)Tx.GetObject(Db.BlockTableId, OpenMode.ForRead);

// source view from 11X17
Point2d ctr = new Point2d(); double scale = 0; bool got = false;
foreach (ObjectId btrId in bt) {
    var rec = (BlockTableRecord)Tx.GetObject(btrId, OpenMode.ForRead);
    if (!rec.IsLayout) continue;
    var lay = (Layout)Tx.GetObject(rec.LayoutId, OpenMode.ForRead);
    if (lay.LayoutName != "11X17") continue;
    foreach (ObjectId id in rec) {
        var vp = Tx.GetObject(id, OpenMode.ForRead) as Viewport;
        if (vp == null || vp.Number == 1) continue;
        ctr = vp.ViewCenter; try { scale = vp.CustomScale; } catch { }
        got = true; break;
    }
}
if (!got) { Log("could not read the 11X17 viewport"); return; }
Log(string.Format("source view center ({0:F2},{1:F2}) scale {2:F6} => 1\"={3:F2}'", ctr.X, ctr.Y, scale, 1.0/scale));

foreach (ObjectId btrId in bt) {
    var rec = (BlockTableRecord)Tx.GetObject(btrId, OpenMode.ForRead);
    if (!rec.IsLayout) continue;
    var lay = (Layout)Tx.GetObject(rec.LayoutId, OpenMode.ForRead);
    if (lay.LayoutName != "17X11") continue;

    var sheet = (BlockTableRecord)Tx.GetObject(rec.ObjectId, OpenMode.ForWrite);

    // area above the title panel, inside the border
    double x0 = 0.333, x1 = 17.247;      // block inner edges in sheet coords
    double y0 = 3.100, y1 = 11.247;      // above the flood note, up to the inner top border
    var vp = new Viewport();
    vp.Width = x1 - x0;
    vp.Height = y1 - y0;
    vp.CenterPoint = new Point3d((x0 + x1) / 2.0, (y0 + y1) / 2.0, 0);
    vp.Layer = "BLOCK_TB2";
    sheet.AppendEntity(vp);
    Tx.AddNewlyCreatedDBObject(vp, true);

    vp.On = true;                         // must be appended to the db before turning on
    vp.ViewCenter = ctr;
    if (scale > 0) vp.CustomScale = scale;
    vp.Locked = true;

    var back = (Viewport)Tx.GetObject(vp.ObjectId, OpenMode.ForRead);
    Log(string.Format("VIEWPORT#{0} created: paper center ({1:F3},{2:F3}) size {3:F3} x {4:F3}",
        back.Number, back.CenterPoint.X, back.CenterPoint.Y, back.Width, back.Height));
    Log(string.Format("   view center ({0:F2},{1:F2}) height {2:F2} scale {3:F6} => 1\"={4:F2}'  on={5} locked={6} (verified)",
        back.ViewCenter.X, back.ViewCenter.Y, back.ViewHeight, back.CustomScale,
        back.CustomScale > 0 ? 1.0/back.CustomScale : 0, back.On, back.Locked));
}
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-0251 (0 entities touched).
Entities
Duration
825 ms
Civil 3D
25.0.0.0

Log

current layout -> 17X11
source view center (13.83,10.02) scale 1.000000 => 1"=1.00'
VIEWPORT#2 created: paper center (8.790,7.173) size 16.914 x 8.147
   view center (13.83,10.02) height 8.15 scale 1.000000 => 1"=1.00'  on=True locked=True (verified)

Notes

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

No notes yet.