I am creating multiple layouts using side database. This is the overview of the code
dynamic preferences = Autodesk.AutoCAD.ApplicationServices.Application.Preferences;
string templatePath = preferences.Files.TemplateDWGPath;
for (int i = 0; i < count; i++)
{
Database database = AutoCADUtilities.Instance.OpenSideDatabase(templatePath + "\\acad.dwt", false);
HostApplicationServices.WorkingDatabase = database;
//Attach XREF based on UserInput
//Update Layers
//CreateViewport
//Save
}
using (Viewport viewPort = new Viewport())
{
viewPort.SetDatabaseDefaults();
viewPort.CenterPoint = new Point3d(x, y, 0);
viewPort.Width = width/ scale;
viewPort.Height = height/ scale;
// Add the new object to the block table record and the transaction
blockTableRecord.AppendEntity(viewPort);
transaction.AddNewlyCreatedDBObject(viewPort, true);
// Enable the viewport
viewPort.On = true;
viewPort.StandardScale = vpScale;
// Change the view direction
viewPort.ViewDirection = new Vector3d(0, 0, 1);
//Assign the view location
viewPort.ViewTarget = pt3GridCenter;
//Rotate the view within viewport to match the rotation of Grid Block (out of phase by 180 deg)
viewPort.TwistAngle = dblRotation;
//Set viewport layer to defpoints
viewPort.Layer = AssignLayer("Defpoints", database, transaction);
//Set shade plot to legacy hidden for the conveyor and catwalk sheets
//lock viewport
viewPort.Locked = true;
}
This works perfectly fine.
Issues comes up when I try to change current layout. When I open file, modelspace is accessible through viewport(please see image), viewport scale change 1:1 and viewport dimensions are completely different.
I tried to call this method in two different ways with same outcome
1) SwitchToPaperSpace after creating viewport.
2) Save and close drawing, then reopen drawing in side database and call SwitchToPaper space
internal void SwitchToPaperSpace(Database database, Transaction transaction)
{
HostApplicationServices.WorkingDatabase = database;
BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Paper space for write
BlockTableRecord blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
//Set current Layout
Layout layout = (Layout)transaction.GetObject(blockTableRecord.LayoutId, OpenMode.ForRead);
LayoutManager.Current.CurrentLayout = layout.LayoutName;
}
Thank you.![image.png image.png](https://forums.autodesk.com/t5/image/serverpage/image-id/1401415i687326A5603E947E/image-size/large?v=v2&px=999)