I am making a dll that reads sheet set data to open drawings, run a command within that drawing, save, and close. One thing I have put off until the end is controlling which layout is opened in drawings with multiple paperspace layouts.
There are a bunch of solutions for changing the layout online, and none of them have worked for me.
Using "layoutManager.CurrentLayout = layoutObj.LayoutName;" always results in an eSetFailed error.
The most complicated looking solutions utilize a "docManager.MdiActiveDocument.Database.GetLayoutNames()" to check layout names against what they want to set the layout to, and I don't seem to have the .GetLayoutNames() method definition?
Here is the code I am using:
public async Task<int> ChLayout(string layout)
{
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager; // document manager
Document doc = acDocMgr.MdiActiveDocument;
//Database db = doc.Database;
Database db = HostApplicationServices.WorkingDatabase;
using (doc.LockDocument())
{
LayoutManager lm;
lm = LayoutManager.Current;
string message = "Layouts: \n";
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
DBDictionary lays = acTrans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
doc.Editor.WriteMessage("\nLayouts:");
// Step through and list each named layout and Model
foreach (DBDictionaryEntry item in lays)
{
message += item.Key + "\n";
if (item.Key == layout)
{
Layout newLayout = acTrans.GetObject(lm.GetLayoutId(lm.CurrentLayout), OpenMode.ForRead) as Layout;
lm.CurrentLayout = newLayout.LayoutName;
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog($"Current layout is {lm.CurrentLayout}.\n I should be on {layout}.");
acTrans.Commit();
return 1;
}
}
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(message);
// Abort the changes to the database
acTrans.Abort();
}
}
return 0;
}
any ideas?