.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoCAD2013 - SaveAs method doesn't work correctly

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
mobj
1796 Views, 10 Replies

AutoCAD2013 - SaveAs method doesn't work correctly

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
10 REPLIES 10
Message 2 of 11
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 11
mobj
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
Message 4 of 11
Alfred.NESWADBA
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) 😉

 

 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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 11
mobj
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) 😉

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
Message 6 of 11
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 11
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 8 of 11
mobj
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
Message 9 of 11
mobj
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. Smiley Happy

 

 

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
Message 10 of 11
Balaji_Ram
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

Message 11 of 11
mobj
in reply to: Balaji_Ram


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.


Thanks!. I was not sure about the difference between reading the DWG file into a new database using:

db.ReadDwgFile

or loadning the file into AutoCAD using:

Document doc = Application.DocumentManager.Open

 

Morten Bastue Jacobsen
Rambøll Danmark A/S
www.ramboll.dk

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost