.NET

Reply
Contributor
mobj
Posts: 25
Registered: ‎08-29-2006
Message 1 of 11 (352 Views)
Accepted Solution

AutoCAD2013 - SaveAs method doesn't work correctly

352 Views, 10 Replies
11-29-2013 04:17 AM
When using the SaveAs method: db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null); I get some strange results: 1: No .BAK files are created 2: When the saved DWG files are used as reference files, the reference manager freezes if another user has the reference open. When saving the reference DWG file using standard AutoCAD SAVE command, it works correctly again. See code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; namespace ExtractObjects { public class Commands { [CommandMethod("RDKPURGEFILES")] static public void cmdRdkPurgeFiles() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter filepath: "); pStrOpts.AllowSpaces = true; PromptResult pStrRes = ed.GetString(pStrOpts); if (pStrRes.Status == PromptStatus.OK) // check valid string input { ed.WriteMessage("Filepath is: {0}", pStrRes.StringResult); string filepath = pStrRes.StringResult; DirectoryInfo d = new DirectoryInfo(filepath); foreach (var dwgfile in d.GetFiles("*.dwg", SearchOption.TopDirectoryOnly)) { ed.WriteMessage("\nStart file: " + dwgfile.Name); // Create a database and try to load the file Database db = new Database(false, true); using (db) { try { db.ReadDwgFile(dwgfile.FullName,FileOpenMode.OpenForReadAndAllShare,true,null); // open the dwg file ObjectIdCollection ids = new ObjectIdCollection(); Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { DBDictionary matDict = (DBDictionary)tr.GetObject(db.MaterialDictionaryId, OpenMode.ForRead, false); foreach (DBDictionaryEntry entry in matDict) { string key = entry.Key; if ((key != "ByBlock") && (key != "ByLayer") && (key != "Global")) ids.Add(entry.Value); } db.Purge(ids); foreach (ObjectId id in ids) { DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForWrite); obj.Erase(); } tr.Commit(); ed.WriteMessage("\nMaterials purged in: " + dwgfile.FullName); db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null); // save as dwg2010 and create a BAK file } } //try catch (System.Exception e) { ed.WriteMessage("\nUnable to access file:" + dwgfile.FullName); ed.WriteMessage("\nException is: " + e.Message); db.Dispose(); // close transaction } //db.Dispose(); // close transaction } // end db } // end foreach } // end if else { ed.WriteMessage("Invalid string input: " + pStrRes.Status.ToString()); } } //end method } }
Morten Bastue Jacobsen
Rambøll Danmark A/S
www.ramboll.dk

I think I've found the solution to one of the problems - Reference Manager freezes when the reference file is open at another user.

 

Before .SAVEAS, you need to generate/enable previewbitmap.

So, when inserting this line:

 

db.RetainOriginalThumbnailBitmap = true;

before:

db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null);

 

The dwg files seems to work, when used as a reference. :smileyhappy:

 

 

Now, the remaining problem:

I want the .SAVEAS to create a .BAK file when overwriting the existing DWG file.

Hi Morten,

 

The "bBakAndRename" parameter of the SaveAs method is only applicable while saving the document that is already open in AutoCAD.

 

If you try this code after you open a drawing in AutoCAD, you will find a .bak file getting created.

 

Document doc = Application.DocumentManager.MdiActiveDocument;
doc.Database.SaveAs(doc.Name, true, DwgVersion.AC1024, null);

If it is not open in AutoCAD, you can create a backup of the database just after the ReadDwgFile without relying on SaveAs.

 

Regards,

*Expert Elite*
Alfred.NESWADBA
Posts: 9,064
Registered: ‎06-29-2007
Message 2 of 11 (344 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

11-29-2013 04:36 AM in reply to: mobj

Hi,

 

>> When using the SaveAs method: db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null); I get some strange results:

>> 1: No .BAK files are created

SAVEAS never creates a BAK as it is a new filename and it let's the old/previous DWG unmodified, so no need for a BAK ... only the SAVE (without "...AS") creates a BAK.

 

>> 2: When the saved DWG files are used as reference files, the reference manager freezes if another user has the reference open.

Can you upload two drawings? One saved by your .NET routine and another one saved from your AutoCAD manually?

 

BTW: what AutoCAD version do you use, if you have a vertical product, which one, and last but not least what servicepack?

 

- alfred -

PS: your code is not readable here, if you want anyone to try it (without editing it a lot) then upload the sourcefile, that's easier for helpers!

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
mobj
Posts: 25
Registered: ‎08-29-2006
Message 3 of 11 (326 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

11-29-2013 05:52 AM in reply to: Alfred.NESWADBA

alfred.neswadba wrote:

Hi,

 

>> When using the SaveAs method: db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null); I get some strange results:

>> 1: No .BAK files are created

>SAVEAS never creates a BAK as it is a new filename and it let's the old/previous DWG unmodified, so no need for a BAK ... only the SAVE (without >"...AS") creates a BAK.

Then I don't understand the reference guide. :-)
"[MarshalAs(UnmanagedType.U1)] bool bBakAndRename
 Input bool indicating whether or not to create a .bak file and change document name in AutoCAD"
 
I'm saving the file using the same filename, thus overwriting the existing file. So I'm expecting a .BAK file. Similar to SAVEAS from AutoCAD.

I'm not sure of the SAVE() method, as I need to specifiy the file format.

 

>> 2: When the saved DWG files are used as reference files, the reference manager freezes if another user has the reference open.

>Can you upload two drawings? One saved by your .NET routine and another one saved from your AutoCAD manually?

I've uploaded two files. One file that has been processed using the code, and the other is a AutoCAD SAVEAS of the processed file.

 

>BTW: what AutoCAD version do you use, if you have a vertical product, which one, and last but not least what servicepack?

I'm using AutoCAD2013 iwth the latest ServicePack. Also ACA2013 is installed, but I'm starting acad.exe without the ACA loaded.

 

>- alfred -

>PS: your code is not readable here, if you want anyone to try it (without editing it a lot) then upload the sourcefile, that's easier for helpers!

Yes, I know... But I have some problems with formatting the text.. I'll try to upload the code.


 

Morten Bastue Jacobsen
Rambøll Danmark A/S
www.ramboll.dk
*Expert Elite*
Alfred.NESWADBA
Posts: 9,064
Registered: ‎06-29-2007
Message 4 of 11 (324 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

11-29-2013 06:09 AM in reply to: mobj

Hi,

 

>> I'm saving the file using the same filename

Sorry, I didn't think about that you use SAVEAS but let the filename be the same (.SAVE would be enough) :smileywink:

 

 To your code (haven't tried it yet) .. does it fall into the exception handling? If so you are trying to .Dispose the database twice. that the only thing I saw with a short look to it.

 

- alfred -

PS: great you got the text here working with FF (I'm running FF too).

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
mobj
Posts: 25
Registered: ‎08-29-2006
Message 5 of 11 (315 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

11-29-2013 07:10 AM in reply to: Alfred.NESWADBA

>> I'm saving the file using the same filename

> Sorry, I didn't think about that you use SAVEAS but let the filename be the same (.SAVE would be enough) :smileywink:

Well, when using .SAVE() I now get an "eFileInternalErr" exception...

 

-edit: the explanation for the error is here: http://forums.autodesk.com/t5/NET/Database-Save-and-eFileInternalErr/td-p/2140848

 

> To your code (haven't tried it yet) .. does it fall into the exception handling? If so you are trying to .Dispose the database twice. that the only thing I saw with a short look to it.

No, I don't get any exceptions when using the SAVEAS method. Also the .dispose method is outcommented outside the Catch section. So it's not used.

 

- Morten -


Morten Bastue Jacobsen
Rambøll Danmark A/S
www.ramboll.dk
*Expert Elite*
Alfred.NESWADBA
Posts: 9,064
Registered: ‎06-29-2007
Message 6 of 11 (307 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

11-29-2013 07:19 AM in reply to: mobj

Hi,

 

>> Also the .dispose method is outcommented outside the Catch section. So it's not used.

.Dispose is done at the end of "Using xxx", so the additional db.Dispose in the catch area would be the one that is to much (but at least I'm not sure if the end of "Using" might be checking that, I'm not working with "Using" statement).

Do you have a drawing before your code was running over it, for the moment I have not seen any difference between those 2 drawings except of the current view.

 

>> Well, when using .SAVE() I now get an "eFileInternalErr" exception...

Oops, do you get that error always or only with some specific DWG files?

What happens if you open a new empty drawing and do the same workflow?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
Alfred.NESWADBA
Posts: 9,064
Registered: ‎06-29-2007
Message 7 of 11 (306 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

11-29-2013 07:22 AM in reply to: mobj

Hi,

 

and one additional idea: you run the .SAVEAS before the transaction is disposed, it might be more save to do the save afterwards.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Contributor
mobj
Posts: 25
Registered: ‎08-29-2006
Message 8 of 11 (297 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

11-29-2013 07:46 AM in reply to: Alfred.NESWADBA

>> Also the .dispose method is outcommented outside the Catch section. So it's not used.

>.Dispose is done at the end of "Using xxx", so the additional db.Dispose in the catch area would be the one that is to much (but at least I'm not sure if >the end of "Using" might be checking that, I'm not working with "Using" statement).

Ok, I'll have a look at that..

 

>Do you have a drawing before your code was running over it, for the moment I have not seen any difference between those 2 drawings except of the >current view.

Yup. I've attached the original drawing. Also, I can't see any difference btw. the two drawings.

 

>> Well, when using .SAVE() I now get an "eFileInternalErr" exception...

> Oops, do you get that error always or only with some specific DWG files?

> What happens if you open a new empty drawing and do the same workflow?

I don't know. I've just tested this with the current drawing.

 

>and one additional idea: you run the .SAVEAS before the transaction is disposed, it might be more save to do the save afterwards.
Good idea! I'll try the .SAVEAS before disposing the transaction as well as after so see if there's any difference
Morten Bastue Jacobsen
Rambøll Danmark A/S
www.ramboll.dk
Contributor
mobj
Posts: 25
Registered: ‎08-29-2006
Message 9 of 11 (262 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

12-02-2013 01:50 AM in reply to: mobj

I think I've found the solution to one of the problems - Reference Manager freezes when the reference file is open at another user.

 

Before .SAVEAS, you need to generate/enable previewbitmap.

So, when inserting this line:

 

db.RetainOriginalThumbnailBitmap = true;

before:

db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null);

 

The dwg files seems to work, when used as a reference. :smileyhappy:

 

 

Now, the remaining problem:

I want the .SAVEAS to create a .BAK file when overwriting the existing DWG file.

Morten Bastue Jacobsen
Rambøll Danmark A/S
www.ramboll.dk
ADN Support Specialist
Balaji_Ram
Posts: 699
Registered: ‎03-21-2011
Message 10 of 11 (242 Views)

Re: AutoCAD2013 - SaveAs method doesn't work correctly

12-04-2013 12:47 AM in reply to: mobj

Hi Morten,

 

The "bBakAndRename" parameter of the SaveAs method is only applicable while saving the document that is already open in AutoCAD.

 

If you try this code after you open a drawing in AutoCAD, you will find a .bak file getting created.

 

Document doc = Application.DocumentManager.MdiActiveDocument;
doc.Database.SaveAs(doc.Name, true, DwgVersion.AC1024, null);

If it is not open in AutoCAD, you can create a backup of the database just after the ReadDwgFile without relying on SaveAs.

 

Regards,



Balaji
Developer Technical Services
Autodesk Developer Network

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.