SaveEventHandler not working

SaveEventHandler not working

WPerciful
Advocate Advocate
3,323 Views
47 Replies
Message 1 of 48

SaveEventHandler not working

WPerciful
Advocate
Advocate

This is my fist time written an event handler.  While my code has no errors, it doesn't work.  Does anyone have any idea what I'm doing wrong?

 

Imports System.Windows.Controls
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime

Namespace BeginSaveHandlingSample
    Public Class SaveEventHandler
        Implements IExtensionApplication
        Public Sub Initialize()
            Dim docs = Application.DocumentManager
            AddHandler docs.DocumentCreated, AddressOf OnDocumentCreated
            For Each doc As Document In docs
                AddHandler doc.Database.BeginSave, AddressOf OnBeginSave
            Next
        End Sub

        Private Sub OnDocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
            AddHandler e.Document.Database.BeginSave, AddressOf OnBeginSave
        End Sub

        Private Sub OnBeginSave(ByVal sender As Object, ByVal e As DatabaseIOEventArgs)
            Application.ShowAlertDialog("It worked!")
        End Sub

        Public Sub Terminate()
        End Sub

        Private Sub IExtensionApplication_Initialize() Implements IExtensionApplication.Initialize
            Throw New NotImplementedException()
        End Sub
        Private Sub IExtensionApplication_Terminate() Implements IExtensionApplication.Terminate
            Throw New NotImplementedException()
        End Sub
    End Class

End Namespace


Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Accepted solutions (1)
3,324 Views
47 Replies
Replies (47)
Message 2 of 48

norman.yuan
Mentor
Mentor

Since the class implement IExtensionApplication, so whenever the code is loaded, AutoCAD will call

 

Initialize()

 

However, if any un-handled exception is thrown in Initialize(), the DLL loading would failed silently. So, it is important to use Try...Catch in the Initialze() for the code that does serious work.

 

But in your case, it is straightforward: you should remove the "Throw New NotImplementedException", which is added by Visual Studio automatically, and leave the Initialize() empty. If you do not plan to do any initializing work, you could have made the class not implement IExtensionApplication. It looks like, you may want to study a bit of more on what/why to use/implement IExtensionApplication.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 48

_gile
Consultant
Consultant

Hi,

Try removing:


        Private Sub IExtensionApplication_Initialize() Implements IExtensionApplication.Initialize
            Throw New NotImplementedException()
        End Sub
        Private Sub IExtensionApplication_Terminate() Implements IExtensionApplication.Terminate
            Throw New NotImplementedException()
        End Sub


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 48

WPerciful
Advocate
Advocate

When I remove those lines of code I get the following error which prohibits me from building the solution.

 

WPerciful_0-1702393981222.png

 



Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 5 of 48

_gile
Consultant
Consultant

Your code should look like this:

 

Imports System.Windows.Controls
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime

Namespace BeginSaveHandlingSample
    Public Class SaveEventHandler
        Implements IExtensionApplication
        Public Sub Initialize() Implements IExtensionApplication.Initialize
            Dim docs = Application.DocumentManager
            AddHandler docs.DocumentCreated, AddressOf OnDocumentCreated
            For Each doc As Document In docs
                AddHandler doc.Database.BeginSave, AddressOf OnBeginSave
            Next
        End Sub

        Private Sub OnDocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
            AddHandler e.Document.Database.BeginSave, AddressOf OnBeginSave
        End Sub

        Private Sub OnBeginSave(ByVal sender As Object, ByVal e As DatabaseIOEventArgs)
            Application.ShowAlertDialog("It worked!")
        End Sub

        Public Sub Terminate() Implements IExtensionApplication.Terminate
        End Sub
    End Class

End Namespace

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 48

WPerciful
Advocate
Advocate

I tried the code that you sent but when you save the drawing it post the message.



Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 7 of 48

_gile
Consultant
Consultant

@WPerciful wrote:

I tried the code that you sent but when you save the drawing it post the message.


Which message?

The event is raised after the save dialog box is closed with OK.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 48

WPerciful
Advocate
Advocate

I'm trying to get this message when you perform the save.  It's only for testing right now but once it works I'm going to have it call a sub to do some clean up.

 

        Private Sub OnBeginSave(ByVal sender As Object, ByVal e As DatabaseIOEventArgs)
            Application.ShowAlertDialog("It worked!")
        End Sub

 



Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 9 of 48

_gile
Consultant
Consultant



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 48

WPerciful
Advocate
Advocate

When you save nothing happens other than the save itself.



Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 11 of 48

_gile
Consultant
Consultant

The saving process can only be started after the user has clicked OK (if he clicks Cancel, the saving process will not start).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 12 of 48

_gile
Consultant
Consultant

If you replace:

Application.ShowAlertDialog("It worked!")

by any process which modify the database, it will occur after the save file dialog closed and before the save process itself begins.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 48

WPerciful
Advocate
Advocate

When I test the same code it doesn't do that. What could I be doing differently?



Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 14 of 48

_gile
Consultant
Consultant

Try something like this (not sure about my VB).

Public Class Initialization
    Implements IExtensionApplication

    Public Sub Initialize() Implements IExtensionApplication.Initialize
	Dim docs = Application.DocumentManager
	AddHandler docs.DocumentCreated, AddressOf OnDocumentCreated
	For Each doc As Document In docs
	    AddHandler doc.Database.BeginSave, AddressOf OnBeginSave
	Next
    End Sub

    Public Sub Terminate() Implements IExtensionApplication.Terminate
    End Sub

    Private Sub OnDocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
        AddHandler e.Document.Database.BeginSave, AddressOf OnBeginSave
    End Sub

    Private Sub OnBeginSave(ByVal sender As Object, ByVal e As DatabaseIOEventArgs)
        Dim db As Database = CType(sender, Database)
        DoItBeforeSave(db)
    End Sub

    Private Shared Sub DoItBeforeSave(ByVal db As Database)
        Using tr = New OpenCloseTransaction()
            Dim circle As Circle = New Circle(Point3d.Origin, Vector3d.XAxis, 10.0)
            circle.ColorIndex = 30
            Dim modelSpace As BlockTableRecord = CType(tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite), BlockTableRecord)
            modelSpace.AppendEntity(circle)
            tr.AddNewlyCreatedDBObject(circle, True)
            tr.Commit()
        End Using
    End Sub
End Class


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 15 of 48

WPerciful
Advocate
Advocate

When I test what you shared nothing happens.  Could it be a setting or something?

 

Imports System.Windows.Controls
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime

Namespace BeginSaveHandlingSample
    Public Class Initialization
        Implements IExtensionApplication

        Public Sub Initialize() Implements IExtensionApplication.Initialize
            Dim docs = Application.DocumentManager
            AddHandler docs.DocumentCreated, AddressOf OnDocumentCreated
            For Each doc As Document In docs
                AddHandler doc.Database.BeginSave, AddressOf OnBeginSave
            Next
        End Sub

        Public Sub Terminate() Implements IExtensionApplication.Terminate
        End Sub

        Private Sub OnDocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
            AddHandler e.Document.Database.BeginSave, AddressOf OnBeginSave
        End Sub

        Private Sub OnBeginSave(ByVal sender As Object, ByVal e As DatabaseIOEventArgs)
            Dim db As Database = CType(sender, Database)
            DoItBeforeSave(db)
        End Sub

        Private Shared Sub DoItBeforeSave(ByVal db As Database)
            Using tr = New OpenCloseTransaction()
                Dim circle As Circle = New Circle(Point3d.Origin, Vector3d.XAxis, 10.0)
                circle.ColorIndex = 30
                Dim modelSpace As BlockTableRecord = CType(tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite), BlockTableRecord)
                modelSpace.AppendEntity(circle)
                tr.AddNewlyCreatedDBObject(circle, True)
                tr.Commit()
            End Using
        End Sub
    End Class

End Namespace


Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 16 of 48

_gile
Consultant
Consultant

Did you try to close the file after saving it and re-open it?

Or, force the newly created circle drawing.

        Private Shared Sub DoItBeforeSave(ByVal db As Database)
            Using tr = New OpenCloseTransaction()
                Dim circle As New Circle(Point3d.Origin, Vector3d.ZAxis, 10.0)
                circle.ColorIndex = 30
                Dim modelSpace As BlockTableRecord = CType(tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite), BlockTableRecord)
                modelSpace.AppendEntity(circle)
                tr.AddNewlyCreatedDBObject(circle, True)
                circle.Draw()
                tr.Commit()
            End Using
        End Sub


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 17 of 48

_gile
Consultant
Consultant

I tried with a VB project in Visual Studio (you can brag about getting me to write VB).

It works as expected.

Here's the code:

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime

Public Class Initialization
    Implements IExtensionApplication

    Public Sub Initialize() Implements IExtensionApplication.Initialize
        Dim docs = Application.DocumentManager
        AddHandler docs.DocumentCreated, AddressOf OnDocumentCreated
        For Each doc As Document In docs
            AddHandler doc.Database.BeginSave, AddressOf OnBeginSave
        Next
    End Sub

    Public Sub Terminate() Implements IExtensionApplication.Terminate
    End Sub

    Private Sub OnDocumentCreated(sender As Object, e As DocumentCollectionEventArgs)
        AddHandler e.Document.Database.BeginSave, AddressOf OnBeginSave
    End Sub

    Private Sub OnBeginSave(sender As Object, e As DatabaseIOEventArgs)
        Dim db As Database = CType(sender, Database)
        DoItBeforeSave(db)
    End Sub

    Private Shared Sub DoItBeforeSave(db As Database)
        Using tr = New OpenCloseTransaction()
            Dim circle As New Circle() With {
                .Center = Point3d.Origin,
                .Radius = 10.0,
                .ColorIndex = 30
            }
            Dim modelSpace As BlockTableRecord = CType(tr.GetObject(
                SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite), BlockTableRecord)
            modelSpace.AppendEntity(circle)
            tr.AddNewlyCreatedDBObject(circle, True)
            circle.Draw()
            tr.Commit()
        End Using
    End Sub
End Class


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 18 of 48

WPerciful
Advocate
Advocate

I've implemented the new code, opened a new file and saved it.  Closed the file and ran save again.  Nothing happens.  How do  I force it?

 

Imports System.Windows.Controls
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime

Namespace BeginSaveHandlingSample
    Public Class Initialization
        Implements IExtensionApplication

        Public Sub Initialize() Implements IExtensionApplication.Initialize
            Dim docs = Application.DocumentManager
            AddHandler docs.DocumentCreated, AddressOf OnDocumentCreated
            For Each doc As Document In docs
                AddHandler doc.Database.BeginSave, AddressOf OnBeginSave
            Next
        End Sub

        Public Sub Terminate() Implements IExtensionApplication.Terminate
        End Sub

        Private Sub OnDocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
            AddHandler e.Document.Database.BeginSave, AddressOf OnBeginSave
        End Sub

        Private Sub OnBeginSave(ByVal sender As Object, ByVal e As DatabaseIOEventArgs)
            Dim db As Database = CType(sender, Database)
            DoItBeforeSave(db)
        End Sub

        Private Shared Sub DoItBeforeSave(ByVal db As Database)
            Using tr = New OpenCloseTransaction()
                Dim circle As New Circle(Point3d.Origin, Vector3d.ZAxis, 10.0)
                circle.ColorIndex = 30
                Dim modelSpace As BlockTableRecord = CType(tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite), BlockTableRecord)
                modelSpace.AppendEntity(circle)
                tr.AddNewlyCreatedDBObject(circle, True)
                circle.Draw()
                tr.Commit()
            End Using
        End Sub
    End Class

End Namespace


Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 19 of 48

WPerciful
Advocate
Advocate

@_gile wrote:

" (you can brag about getting me to write VB)."

 


 You're a gentlemen and a scholar and I can't thank you enough for the help!  I am indeed already bragging about it 🙂



Wayne Perciful
Founder | Perciful Consulting LLC
Perciful Consulting

0 Likes
Message 20 of 48

_gile
Consultant
Consultant

After you save the file, you should see an orange circle of radius equal to 10 and center at origin (0, 0).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes