new database, readdwgfile, insunits again

new database, readdwgfile, insunits again

fieldguy
Advisor Advisor
753 Views
4 Replies
Message 1 of 5

new database, readdwgfile, insunits again

fieldguy
Advisor
Advisor

My code is working but I get "Warning: An error occured during save. We recommend that you run Recover".

I'm attaching the code and an empty dwg file that is causing the issue.  I believe it was created by microstation - not sure if that makes any difference.

A second question: after db.SaveAs, why does autocad still tell me that "This DWG file was saved by an application that was not developed or licensed by Autodesk"?

string msg = "rename, change units";
string docpath = "\\some path\\";
string oldname = "unitstest.dwg";
string newname = "unitstest_new.dwg";
Database curdb = acdb.HostApplicationServices.WorkingDatabase;
try
{
  using (Database db = new Database(false, true))
  {
    db.ReadDwgFile(docpath + oldname, FileOpenMode.OpenForReadAndAllShare, false, "");
    db.CloseInput(true);
    HostApplicationServices.WorkingDatabase = db;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
      db.Insunits = UnitsValue.Meters;
      tr.Commit();
    }// using tr
    db.SaveAs(docpath + newname, DwgVersion.Current);
    HostApplicationServices.WorkingDatabase = curdb;
  }// using new database           
}// try
catch (System.Exception ex)
{
  msg += "\nError:\n" + ex.Message;
}
0 Likes
Accepted solutions (1)
754 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Why do you keep on using all these useless statements (Transaction, WorkingDatabase, CloseInput)?

Doesn't simply do it like this work the same (or better)?

 

string msg = "rename, change units";
string docpath = "\\some path\\";
string oldname = "unitstest.dwg";
string newname = "unitstest_new.dwg";
try
{
  using (Database db = new Database(false, true))
  {
    db.ReadDwgFile(docpath + oldname, FileOpenMode.OpenForReadAndAllShare, false, "");
    db.Insunits = UnitsValue.Meters;
    db.SaveAs(docpath + newname, DwgVersion.Current);
  }// using new database           
}// try
catch (System.Exception ex)
{
  msg += "\nError:\n" + ex.Message;
}

For your second question, even you save the drawing with , it does not change the fact the drawing was originaly created with another software.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

_gile
Consultant
Consultant

If you absolutely want to change the WorkingDatabase (despite this is not mandatory to just change the insunits), try to reset the previous WorkingDatabase before calling db.SaveAs() (see this topic).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 5

fieldguy
Advisor
Advisor

Ok - this works.  Thanks.

>>Why do you keep on using all these useless statements (Transaction, WorkingDatabase, CloseInput)?<<

First - I didn't realise this change could be made to the database without a transaction.

Second - the WorkingDatabase was added to make sure the change was applied to the side database (not the Drawing1 database that is open in the editor).

Third - CloseInput is used for what the documentation states.

0 Likes
Message 5 of 5

_gile
Consultant
Consultant

@fieldguy wrote:

First - I didn't realise this change could be made to the database without a transaction.


I thaught you did. See your last reply in your previous topic.

 

 


@fieldguy wrote:

Second - the WorkingDatabase was added to make sure the change was applied to the side database (not the Drawing1 database that is open in the editor).


While calling the Insunits property on the Database instance you just created (db.Insunits), it cannot apply to another one.  Setting the working database is only mandatory for features such as LayoutManager.Current or Entity.SetDatabaseDefaults() (if the entity is not yet added to a Database) which are implicitly related to the working database.

 

 


@fieldguy wrote:

Third - CloseInput is used for what the documentation states.


The docs aren't such clear about the CloseInput using. You should read the ARX docs which says: "it may have undesirable results" and the description for the OpenForReadAndAllShare option of the FileOpenMode enum.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes