Hello,
I know it's an old topic but I've been having the same problem while creating viewports on unnactivated layouts and I came up with a solution.
Its a tricky workaround and I'd like to share it 😄
First I create the layout and its blocktablerecord:
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary layoutsEx = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
BlockTable blkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
using (BlockTableRecord blkBlkRec = new BlockTableRecord())
{
int layoutCount = layoutsEx.Count - 1;
blkBlkRec.Name = "*Paper_Space" + layoutCount.ToString();
blkTbl.Add(blkBlkRec);
tr.AddNewlyCreatedDBObject(blkBlkRec, true);
DBDictionary layouts = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite) as DBDictionary;
using (Layout lay = new Layout())
{
lay.LayoutName = LayoutName;
lay.AddToLayoutDictionary(db, blkBlkRec.ObjectId);
tr.AddNewlyCreatedDBObject(lay, true);
}
The thing is that AutoCad always recognize the first Viewport on a layout's Blocktablerecord as the layout's own ViewportTableRecord alike (because Layouts do no have ViewportTable like the Modelspace), so when you create and configure the first Viewport of a Layouts BlockTableRecord manually AutoCad makes a mess and assumes it to be Layout View.
As a solution, before committing the transaction I'd add a Viewport to the BlockTableRecord with the same custom properties that Autocad would use in this situation:
using (Autodesk.AutoCAD.DatabaseServices.Viewport VP = new Autodesk.AutoCAD.DatabaseServices.Viewport()
{
Color = Color.FromRgb(51, 51, 51),
BackClipDistance = 0,
BackClipOn = false,
Background = ObjectId.Null,
Brightness = 0,
CenterPoint = new Point3d(0, 0, 0),
CircleSides = 1000,
Contrast = 0,
CustomScale = 0.0143,
DefaultLightingOn = true,
DefaultLightingType = Autodesk.AutoCAD.DatabaseServices.DefaultLightingType.TwoDistantLights,
Elevation = 0,
FastZoomOn = true,
FrontClipAtEyeOn = true,
FrontClipDistance = 0,
FrontClipOn = false,
GridAdaptive = true,
GridBoundToLimits = false,
GridFollow = false,
GridIncrement = new Vector2d(10, 10),
GridMajor = 5,
GridSubdivisionRestricted = true,
Height = 9,
HiddenLinesRemoved = false,
LensLength = 50,
Locked = false,
NonRectClipEntityId = ObjectId.Null,
NonRectClipOn = false,
PerspectiveOn = false,
ShadePlot = ShadePlotType.AsDisplayed,
SnapAngle = 0,
SnapBasePoint = new Point2d(0, 0),
SnapIncrement = new Vector2d(10, 10),
SnapIsometric = false,
SnapIsoPair = 0,
SnapOn = false,
TwistAngle = 0,
UcsFollowModeOn = false,
UcsIconAtOrigin = false,
UcsIconVisible = false,
VisualStyleId = (db.VisualStyleDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary).GetAt("2dWireframe")
})
{
blkBlkRec.AppendEntity(VP);
tr.AddNewlyCreatedDBObject(VP, true);
//These properties after adding to Database because they are Database Dependent:
VP.GridOn = false;
VP.UcsFollowModeOn = false;
VP.UcsIconAtOrigin = false;
VP.UcsIconVisible = true;
VP.UcsPerViewport = true;
}
}
tr.Commit();
}Now you can add to this layout BlockTableRecord as many viewports as you'd like and there should be no more errors. 😄