Preventing ObjectModified event from firing when saving/regening

miguelmachadoecosta
Advocate
Advocate

Preventing ObjectModified event from firing when saving/regening

miguelmachadoecosta
Advocate
Advocate

 

I implemented some event handlers to manipulate some objects when some specific MTexts are manually modified with the "MTEDIT" command. Everything works fine except that the "QSAVE" and "REGEN" commands take a bit longer to execute than with the events disabled (about 1-2 seconds more in a drawing with not that many objects). This must be happening because the ObjectModified event is firing countless times when saving. I even put an if statement for the "QSAVE" command inside the event, but it still slows down the saving.

 

Here is what I first tried:

 

Public mtextObjHandle As String, currentCommand As String

Sub AcadDocument_BeginCommand(ByVal CommandName As String)
    currentCommand = CommandName
End Sub

Sub AcadDocument_ObjectModified(ByVal Object As Object)
    If currentCommand <> "QSAVE" And currentCommand <> "VBAIDE" And currentCommand <> "REGEN" Then
        If Object.ObjectName = "AcDbMText" Then
            mtextObjHandle = Object.handle
        End If
    End If
End Sub

Sub AcadDocument_EndCommand(ByVal CommandName As String)
    If CommandName = "MTEDIT" Then
       Dim mtextObj As AcadMText
       Set mtextObj = ThisDrawing.HandleToObject(mtextObjHandle)
       'here goes the rest of the code
    End If
End Sub

 

I searched some solutions but didn't manage to prevent the event from firing. This next solution resembles something that might work for the "QSAVE" command (probably not for the "REGEN" command), but the event is still firing when saving.

 

Public mtextObjHandle As String, currentCommand As String
Public WithEvents myDOC As AcadDocument

Public Sub EventON()
    Set myDOC = ThisDrawing
End Sub
Public Sub EventOFF() Set myDOC = Nothing End Sub Sub AcadDocument_BeginSave(ByVal FileName As String) EventOFF End Sub Sub AcadDocument_EndSave(ByVal FileName As String) EventON End Sub Sub myDOC_BeginSave(ByVal FileName As String) EventOFF End Sub Sub myDOC_EndSave(ByVal FileName As String) EventON End Sub Sub myDOC_ObjectModified(ByVal Object As Object) (...) End Sub Sub myDOC_EndCommand(ByVal CommandName As String) (...) End Sub

I am probably doing something wrong, but I never worked with events before so I don't know what might be.

 

Thank you in advance.

 

 

0 Likes
Reply
1,066 Views
2 Replies
Replies (2)

miguelmachadoecosta
Advocate
Advocate

A solution I found so far is to have a secondary dvb file that unloads the primary one at the beginning of the commands and reloads at the end:

 

Sub AcadDocument_BeginCommand(ByVal CommandName As String)
If UCase(CommandName) = "SAVE" Or UCase(CommandName) = "QSAVE" Or UCase(CommandName) = "SAVEAS" Or UCase(CommandName) = "REGEN" Then
    UnloadDVB "file_path.dvb"
End If
End Sub

Sub AcadDocument_EndCommand(ByVal CommandName As String)
If UCase(CommandName) = "SAVE" Or UCase(CommandName) = "QSAVE" Or UCase(CommandName) = "SAVEAS" Or UCase(CommandName) = "REGEN" Then
    LoadDVB "file_path.dvb"
End If
End Sub

This speeds up the commands a lot. Any other solution which does not require a second dvb file? Thanks.

0 Likes

miguelmachadoecosta
Advocate
Advocate

I found a solution for what I wanted to do, even if it isn't a solution to was I asked in this topic.

 

I was using the ObjectModified event only because I needed to determine which one was the last modified object. However the same result can be achieved by using:

 

ssetObj.Select acSelectionSetPrevious

This way the ObjectModified is no longer necessary and I have no more problems when saving/regening.