Message 1 of 4
Cannot Open and Close DWG in Same Command

Not applicable
10-08-2011
02:47 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I need to copy and rename my current drawing then open the copied drawing, and I need to close the original drawing.
However when I try to both open the renamed drawing and close the original drawing (in the code below), the original drawing will not close.
If I don't open the renamed drawing, the following code works and I can close the original drawing. Can anyone see what I am doing wrong?
[CommandMethod("TestClose", CommandFlags.Session)] public void TestCloseDocument() { string fullName = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name; string projectDirectory = System.IO.Path.GetDirectoryName(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name); string drawingNameWithOutExt = System.IO.Path.GetFileNameWithoutExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name); string ext = System.IO.Path.GetExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name); // create directory if it doesn't exists string newTempDir = string.Concat(projectDirectory, "\\temp"); if (!Directory.Exists(newTempDir)) { Directory.CreateDirectory(newTempDir); } string fullNewDrawingName = string.Concat(newTempDir, "\\", drawingNameWithOutExt, "_1", ext); if (!File.Exists(fullNewDrawingName)) File.Copy(fullName, fullNewDrawingName); try { foreach (Autodesk.AutoCAD.ApplicationServices.Document doc in Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager) { if (doc.Name == fullName) { doc.CloseAndDiscard(); break; } } // } catch (SystemException ex) { string message = ex.ToString(); } Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(fullNewDrawingName, false); }