.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

savehandler problems

5 REPLIES 5
Reply
Message 1 of 6
Burnit87
526 Views, 5 Replies

savehandler problems

good day to you all

 

for my work im writing some apps, currently im working on something that needs to be activated when save is completed

right now i'm working on the trigger part

the're a few problems

- with several drawings open the the dll loaded, the save all command kills the handlers

- when you have several drawings open, when  loading the dll, it applies the save handler tot the first drawing

- when a drawing was open with the handlers when autocad crashes, it keeps crashing autocad 

 

 

 

anyone has any suggestions?

thanks in advance, and merry christmas

Björn 

 

here's the code 

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports System.IO

<Assembly: ExtensionApplication(GetType(pdfsaver.MyPlugin))> 

Namespace pdfsaver
    
    Public Class MyPlugin
        Implements IExtensionApplication
        Private BS_DocMan As DocumentCollection
        Private Shared BS_Controllers As Dictionary(Of Document, PerDocController)

        Public Sub Initialize() Implements IExtensionApplication.Initialize
            '#### document controller ####
            BS_DocMan = Application.DocumentManager
            BS_Controllers = New Dictionary(Of Document, PerDocController)

            If Not (BS_Controllers.ContainsKey(e.Document)) Then
                For Each BS_document As Document In BS_DocMan
                    BS_Controllers.Add(BS_document, New PerDocController(BS_document))
                Next
                AddHandler BS_DocMan.DocumentActivated, AddressOf DocumentActivated
                AddHandler BS_DocMan.DocumentCreated, AddressOf DocumentCreated
                AddHandler BS_DocMan.DocumentToBeDestroyed, AddressOf DocumentToBeDestroyed
            end if


                MsgBox("handlers added")

        End Sub
       
        Public Sub Terminate() Implements IExtensionApplication.Terminate
            MsgBox("handlers will be removed")

            RemoveHandler BS_DocMan.DocumentActivated, AddressOf DocumentActivated
            RemoveHandler BS_DocMan.DocumentCreated, AddressOf DocumentCreated
            RemoveHandler BS_DocMan.DocumentToBeDestroyed, AddressOf DocumentToBeDestroyed

            MsgBox("handlers removed")
        End Sub

        Private Sub DocumentActivated(sender As Object, e As DocumentCollectionEventArgs)
            If Not (BS_Controllers.ContainsKey(e.Document)) Then
                BS_Controllers.Add(e.Document, New PerDocController(e.Document))
                e.Document.Editor.WriteMessage(vbCrLf + " - Document Activated! ")
         
            End If
            e.Document.Editor.WriteMessage(vbCrLf + " - Document Activated! 2")
        End Sub

    
        Private Sub DocumentToBeDestroyed(sender As Object, e As DocumentCollectionEventArgs)
            If (BS_Controllers.ContainsKey(e.Document)) Then
                BS_Controllers(e.Document).Destroy()
                BS_Controllers.Remove(e.Document)
            End If

            e.Document.Editor.WriteMessage(vbCrLf + " - DocumentToBeDestroyed  ")
        End Sub
        Private Sub DocumentCreated(sender As Object, e As DocumentCollectionEventArgs)
            If Not (BS_Controllers.ContainsKey(e.Document)) Then
                BS_Controllers.Add(e.Document, New PerDocController(e.Document))
                e.Document.Editor.WriteMessage(vbCrLf + " - Document Created! ")
            
            End If
            e.Document.Editor.WriteMessage(vbCrLf + " - Document Created! 2")
        End Sub
    End Class

    Public Class PerDocController
        Private BS_document As Document
        Public Sub New(ByVal document As Document)
            BS_document = document

            ' AddHandler BS_document.Database.BeginSave, AddressOf BeginSave
            AddHandler BS_document.Database.SaveComplete, AddressOf SaveComplete
            BS_document.Editor.WriteMessage(vbCrLf + " - Savehandler Added! ")

        End Sub

        Public Sub Destroy()
            ' RemoveHandler BS_document.Database.BeginSave, AddressOf BeginSave
            RemoveHandler BS_document.Database.SaveComplete, AddressOf SaveComplete
            MsgBox("destroy activated")
        End Sub

        Private Sub SaveComplete(sender As Object, e As DatabaseIOEventArgs)
            BS_document.Editor.WriteMessage(vbCrLf + " - Save Complete! ")

        End Sub
    End Class


End Namespace

 

 

 

 

BP
5 REPLIES 5
Message 2 of 6
hgasty1001
in reply to: Burnit87

Hi,

 

Are you running the code in application context? 

 

Gaston Nunez

Message 3 of 6
FRFR1426
in reply to: Burnit87

DocumentActivated is called each time you switch from a document to another, so it's not here that you need to setup your SaveComplete event handler. Just use DocumentCreated and DocumentToBeDestroyed. For the blank document AutoCAD opens at startup, you should use DocumentBecameCurrent.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 4 of 6
Burnit87
in reply to: hgasty1001

what do you mean?

BP
Message 5 of 6
Burnit87
in reply to: FRFR1426

thats a change i'd might do, but it doesn't solve any of the stated problems

BP
Message 6 of 6
Burnit87
in reply to: FRFR1426

documentbecame currunt keeps crashing non stop so i changed it back

BP

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report