I have a vb.net application that works fine in acad2012. I converted it to 2013 (new references, including accoremgd.dll) and I'm aware of the document extension aspect of the CloseAndDiscard method, although I don't fully understand the "big split" and its details. I'm using Visual Studio 2010 Express.
Everything works until the end of the application where I need to close the drawing. I'll paste in the last bit of code (this thing wraps the longer code lines):
------------------------------
acDoc.Database.SaveAs(OPORPath,True, DwgVersion.AC1024, acDoc.Database.SecurityParameters)
Application.SetSystemVariable("ISAVEBAK", 1)
Application.SetSystemVariable("FILEDIA", 1)
MsgBox("POR layout has been saved to the Official folder")
'End the last transaction
EndUsing
'remove the documentlock
EndUsing
DocumentExtension.CloseAndDiscard(Application.DocumentManager.MdiActiveDocument)
EndSub
------------------------------
So, just before EndSub, I'm trying to close the drawing (not exit ACAD, just close the drawing). Does anyone know what I'm missing here? I've been looking online to find how other users do this and this code is what I've found. But it's not working for me.
Thanks so much,
Erik
Solved! Go to Solution.
Solved by khoa.ho. Go to Solution.
Hi,
You should set CommandFlags.Session in your custom AutoCAD command. Otherwise, it will fail to close the current AutoCAD drawing session (the opened document, not AutoCAD application).
DocumentExtension.CloseAndSave(Application.DocumentManager.MdiActiveDocument) will save and the close the working document.
DocumentExtension.CloseAndDiscard will close document without saving. Here is the code for testing:
<CommandMethod("CloseAndSave", CommandFlags.Session)> _ Public Shared Sub CloseAndSave() Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Using doc.LockDocument() Using trans As Transaction = db.TransactionManager.StartTransaction() Dim blockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) Dim modelSpace = DirectCast(trans.GetObject(blockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord) Dim line = New Line(Point3d.Origin, New Point3d(5, 5, 0)) modelSpace.AppendEntity(line) trans.AddNewlyCreatedDBObject(line, True) trans.Commit() End Using doc.TransactionManager.FlushGraphics() End Using doc.CloseAndSave(doc.Name) 'doc.CloseAndDiscard(); End Sub
-Khoa