Hello Gile
I am not sure why this don't work. I followed what you suggested.
still I don't see what I expect to see. I click on 'X' and when get prompt to save, I say save. after opening the DWG file I don't see the line in there. Would you mind please.
Public Class AllCommandAndInitialize
Implements Autodesk.AutoCAD.Runtime.IExtensionApplication
Public Sub Initialize() Implements IExtensionApplication.Initialize
AddHandler Application.DocumentManager.DocumentCreated, AddressOf StartUpEvents.On_Document_Created
End Sub
Public Sub Terminate() Implements IExtensionApplication.Terminate
End Sub
Class StartUpEvents
Shared Sub On_Document_Created(sender As Object, e As DocumentCollectionEventArgs)
AddHandler Application.DocumentManager.CurrentDocument.BeginDocumentClose, AddressOf StartUpEvents.OnBeginDocumentClose
End Sub
Shared Sub OnBeginDocumentClose(sender As Object, e As DocumentBeginCloseEventArgs)
AddHandler Application.DocumentManager.MdiActiveDocument.Database.SaveComplete, AddressOf OnSaveComplete
End Sub
Private Shared Sub OnSaveComplete(sender As Object, e As DatabaseIOEventArgs)
Dim Mydwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim MyDb As Database = Mydwg.Database
Using MyDb
Using tr As Transaction = MyDb.TransactionManager.StartTransaction
Try
Dim mybtr As BlockTableRecord = CType(tr.GetObject(MyDb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
Dim myline As Line = New Line(New Point3d(0, 0, 0), New Point3d(10000, 10000, 0))
mybtr.AppendEntity(myline)
tr.AddNewlyCreatedDBObject(myline, True)
tr.Commit()
Autodesk.AutoCAD.ApplicationServices.DocumentExtension.CloseAndSave(Mydwg, Mydwg.Name)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.StackTrace)
End Try
End Using
End Using
End Sub
End Class
End Class