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); }
Hi,
if you use the "CommandFlags.Session" the opened document will become current. As long as your command (that is started within the other drawing) is not finished, AutoCAD has the approach "make thedoc current in the editor" in the queue, and in my opinion that is the reason of why you can't close the doc.
I would also not load the doc into the editor, just create a New Database and load the DWG-file into that object, make your modifications and save it back. Opening a DWG in the editor always uses more time.
HTH, - alfred -