I have a program that creates a drawing of various entities. I would like to be able to start a new drawing then call my BuildDrawing method for each part in my jobs list. The code below is what I use to open new drawings. The drawings open OK but all jobs are built in the original document leaving the newly opened drawings blank. Any ideas?
string strTemplatePath = "acad.dwt";
DocumentCollection acDocMgr = Application.DocumentManager; foreach (var partDrawing in jobs) { Document acDoc = acDocMgr.Add(strTemplatePath); acDocMgr.MdiActiveDocument = acDoc; var myBuilder = new MyDrawingBuilder(partDrawing); myBuilder.BuildDrawing(); }
Solved! Go to Solution.
Solved by BrentBurgess1980. Go to Solution.
@BrentBurgess1980 thanks for the info . That fixed that issue. However I have another problem. When I add code to programmatically save the drawing it looks OK until you reopen the drawing. At that point I get a "The drawing file require recovery" dialog. If I "Save As" manually from the AutoCad Gui on those drawings I don't get the error anymore. If you are not sure about this I will mark your answer as my solution and re-post this new issue. Thanks.
Added code
acDoc.Database.SaveAs(fileName, true, DwgVersion.Current, acDoc.Database.SecurityParameters)
How are you closing your drawings after you save?
Perhaps try this method
AcDoc.CloseAndSave(fileName);
I tried to CloseAndSave but get the following error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".
The program I am working on has a lot of stuff going on so I added a test method that gives me similar results.Thanks
[CommandMethod("TestAddDwgs", CommandFlags.Session)] public void TestAddDwgs() { try { BuildDrawingTest(2); } catch (Exception ex) { Application.ShowAlertDialog(ex.Message); } finally { } } private void BuildDrawingTest(int numberOfDrawings) { var testDir = @"C:\Users\my.name\Desktop\myDir"; for (int i = 0; i < numberOfDrawings; i++) { Document acDoc = acDocMgr.Add(defaultAcadTemplate); acDocMgr.MdiActiveDocument = acDoc; AddCircle(acDoc); var path = Path.Combine(testDir, i + ".dwg"); //Has Errors acDoc.Database.SaveAs(path, true, DwgVersion.Current, acDoc.Database.SecurityParameters); //Has Errors //acDoc.CloseAndSave(path); } } private static void AddCircle(Document document) { // Get the current document and database document.LockDocument(); Database acCurDb = document.Database; // Start a transaction using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Create a circle that is at 2,3 with a radius of 4.25 using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 4.25; // Add the new object to the block table record and the transaction acBlkTblRec.AppendEntity(acCirc); acTrans.AddNewlyCreatedDBObject(acCirc, true); } // Save the new object to the database acTrans.Commit(); } }
I tested the code a few times, but I wasn't able to replicate your issue.
Ran the code (with the Database.Saveas and the doc.CloseAndSave), and was able to open the files, no recovery required.
You get the same results with this code?
Yes. I added a screen cast below.
36c4bc7e-7b25-41a6-8614-6bddd1332e6e
Thats strange that I can't replicate that. Before you close the drawing, have you tried an audit to see what is being affected?
When I run the audit command it finds no errors. However it still has the error when I reopen the dwg saying "One error was found in the drawing file during open. Would you..."
Can't find what you're looking for? Ask the community or share your knowledge.