Author Topic: eSetFailed error changing layout  (Read 200 times)

0 Members and 1 Guest are viewing this topic.

TheCaconym

  • Mosquito
  • Posts: 2
eSetFailed error changing layout
« on: February 02, 2021, 11:29:55 AM »
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:
Code - C#: [Select]
  1. public async Task<int> ChLayout(string layout)
  2.         {
  3.             DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;       // document manager
  4.             Document doc = acDocMgr.MdiActiveDocument;
  5.             //Database db = doc.Database;
  6.             Database db = HostApplicationServices.WorkingDatabase;
  7.  
  8.             using (doc.LockDocument())
  9.             {
  10.                 LayoutManager lm;
  11.                 lm = LayoutManager.Current;
  12.                 string message = "Layouts: \n";
  13.                 using (Transaction acTrans = db.TransactionManager.StartTransaction())
  14.                 {
  15.                     DBDictionary lays = acTrans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
  16.  
  17.                     doc.Editor.WriteMessage("\nLayouts:");
  18.  
  19.                     // Step through and list each named layout and Model
  20.                     foreach (DBDictionaryEntry item in lays)
  21.                     {
  22.                         message += item.Key + "\n";
  23.  
  24.                         if (item.Key == layout)
  25.                         {
  26.                             Layout newLayout = acTrans.GetObject(lm.GetLayoutId(lm.CurrentLayout), OpenMode.ForRead) as Layout;
  27.                             lm.CurrentLayout = newLayout.LayoutName;
  28.                             Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog($"Current layout is {lm.CurrentLayout}.\n I should be on {layout}.");
  29.                             acTrans.Commit();
  30.                             return 1;
  31.                         }
  32.  
  33.                     }
  34.                     Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(message);
  35.                     // Abort the changes to the database
  36.                     acTrans.Abort();
  37.                 }
  38.             }
  39.             return 0;
  40.         }

any ideas?