Author Topic: FYI... SaveAs in 2010 gotcha  (Read 2571 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
FYI... SaveAs in 2010 gotcha
« on: January 19, 2011, 02:17:45 PM »
It appears that there is a problem in the Acad .Net assemblies that doesn't work all the time when trying to use the SaveAs method on a Database.   The problem is with calling the method like

Db.SaveAs( path, DwgVersion.Current );

To fix the issue I had, I had to call it like

Db.SaveAs( path, true, DwgVersion.Current, Db.SecurityParameters );

Problem was confirmed by this page, which also supplied the solution:
[ http://www.private.peterlink.ru/poleshchuk/cad/2009/Bug2apie.htm ]
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: FYI... SaveAs in 2010 gotcha
« Reply #1 on: December 21, 2017, 04:43:15 PM »
Thanks Tim, this came in handy today. I have a simple wrapper I enjoy using for side database interactions. It worked fine when I was the only one in the file system, but my old hack is just a breeding ground for race conditions to thwart the process if someone else happened to open the file before I saved. I used to just open the file for read so that the later db.SaveAs(db.FullName, db.OriginalFileVersion) wouldn't crash on filesharing violation
Code - C#: [Select]
  1.         public static void PerformActionOnDatabase(this Action<Database> databaseaction, string dwgpath, FileOpenMode fileOpenMode = FileOpenMode.OpenForReadAndAllShare)
  2.         {
  3.             using (var db = new Database(false, true))
  4.             {
  5.                 db.ReadDwgFile(dwgpath, fileOpenMode, true, null);
  6.                 databaseaction(db);
  7.             }
  8.         }

Code - C#: [Select]
  1.             var dbAction = new Action<Database>((db) =>
  2.             {
  3.                 using (var tr = db.TransactionManager.StartTransaction())
  4.                 {
  5.                    
  6.                     tr.Commit();
  7.                 }
  8.                 db.CloseInput(true);
  9.                 db.SaveAs(db.Filename, db.SecurityParameters);
  10.             });
  11.             dbAction.PerformActionOnDatabase(dwgpath, FileOpenMode.OpenForReadAndWriteNoShare);