I want to handle multiple DWG files containing MagiCAD proxy elements.
An easy and very fast method is to access the files using the ReadDwgFile
Database db = new Database(false, true); using (db) {
db.ReadDwgFile(dwgfile.FullName,FileOpenMode.OpenForReadAndAllShare,true,null); // open the dwg file
And then save the files using:
db.RetainOriginalThumbnailBitmap = true; // retains thumbnail previw image db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null);
However, when using this method, the display of MagiCAD proxy entities are not saved. The DWG fil still contains the proxy entities, it's just the AutoCAD representation of the elements that have been altered.
Is there a method to handle 3rd party applications when using the .ReadDwgFile?
Is the MagiCAD application loaded when the save is done so that the custom entities are not proxies at the time of save? If not, then whatever proxy graphics those entities had in the dwg file when it was opened will be saved back out again since there isn't any application to create new proxy graphics.
@artc2 wrote:Is the MagiCAD application loaded when the save is done so that the custom entities are not proxies at the time of save? If not, then whatever proxy graphics those entities had in the dwg file when it was opened will be saved back out again since there isn't any application to create new proxy graphics.
Yes, the MagiCAD application is loaded before processing the files.
To be more specific: Proxy elements are shown in the saved file, they are just showed differently. I've attached two dwg files. Before and after the .saveas.
here is the code used:
[CommandMethod("RDKPURGEMATERIALS", CommandFlags.Session)] public void cmdRdkPurgeMaterials() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; int materialsCount; 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); materialsCount = 0; // reset counter for new file // Create a database Database db = new Database(false, true); using (db) { try { db.ReadDwgFile(dwgfile.FullName, FileOpenMode.OpenForReadAndReadShare, true, null); // open the dwg file ObjectIdCollection ids = new ObjectIdCollection(); Transaction tr = db.TransactionManager.StartOpenCloseTransaction(); // should be faster than 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); // filters the list foreach (ObjectId id in ids) { DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForWrite); obj.Erase(); // purges the elements ++materialsCount; } tr.Commit(); }// end tr ed.WriteMessage("\n" + materialsCount + " materials purged in: " + dwgfile.FullName); db.SaveAs(dwgfile.FullName, true, DwgVersion.AC1024, null); // save as dwg2010 } //try catch (System.Exception e) { ed.WriteMessage("\nUnable to process file:" + dwgfile.FullName); ed.WriteMessage("\nException is: " + e.Message); } } // end db and close file } // end foreach } // end if else { ed.WriteMessage("Invalid string input: " + pStrRes.Status.ToString()); } } //end method
Perhaps MagiCAD's proxy graphics generation uses certain header variables or sysvars that are different between when the first dwg was saved to disk and the second was saved to disk.
@artc2 wrote:Perhaps MagiCAD's proxy graphics generation uses certain header variables or sysvars that are different between when the first dwg was saved to disk and the second was saved to disk.
Yes, that could be the case.
I'll try to get some information from MagiCAD.