To close a drawing without saving it, you can use ether API “CloseAndDiscard” as shown in below code or you can use the ActiveX interface in AutoCAD (refer Method 2).
The AcadDocument object has the Close method from where you can specify a flag if you want to save the changes.The following is a sample that demonstrates how to call the AcadDocument.Close method from .NET. The sample code uses late binding technique to avoid referencing the AutoCAD ActiveX interops.
//Method1
[CommandMethod("closeDoc_Method1", CommandFlags.Session)]
public static void closeDoc_Method1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
doc.CloseAndDiscard();
}
//Method2 - using ActiveX API
[CommandMethod("closeDoc_Method2", CommandFlags.Session)]
public static void closeDoc_Method2()
{
Object acadObject = Application.AcadApplication;
object ActiveDocument = acadObject.GetType().InvokeMember(
"ActiveDocument",
BindingFlags.GetProperty,
null, acadObject, null);
object[] dataArry = new object[2];
dataArry[0] = false; //no save
dataArry[1] = ""; //drawing file name.. if saving
ActiveDocument.GetType().InvokeMember("close",
BindingFlags.InvokeMethod,
null, ActiveDocument, dataArry);
}

Leave a Reply