Hi,
I am trying to copy and rename my current drawing to a folder. I then close the current drawing and open the renamed copy.
However it seems I can't both open the renamed drawing and close the original. When I run my code the original drawing does not close.
However if I remove the portion of my code that opens the other drawing then the original closes as expected.
Here is my code. Any clues?
[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);
// if I comment this bit out I can close the drawing
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(fullNewDrawingName, false);
try
{
foreach (Autodesk.AutoCAD.ApplicationServices.Document doc in Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager)
{
if (doc.Name == fullName)
{
doc.CloseAndDiscard();
return;
}
}
}
catch (SystemException ex)
{
string message = ex.ToString();
}
}