ApplicationEvents OnSaveDocument OnOpenDocument

ApplicationEvents OnSaveDocument OnOpenDocument

JoAntt
Enthusiast Enthusiast
3,267 Views
9 Replies
Message 1 of 10

ApplicationEvents OnSaveDocument OnOpenDocument

JoAntt
Enthusiast
Enthusiast

Hi

 

I got a problem in my Inventor 2014 addin with applicationsEvents.

The code below fires not before but 2 times after event, what is wrong?

 

 

'In Class StandardAddInServer
Private WithEvents m_ApplicationEvents As Inventor.ApplicationEvents

 'in Public Sub Activate
 
            m_ApplicationEvents = m_inventorApplication.ApplicationEvents
            AddHandler m_ApplicationEvents.OnSaveDocument, AddressOf m_ApplicationEvents_OnSaveDocument

            m_ApplicationEvents = m_inventorApplication.ApplicationEvents
            AddHandler m_ApplicationEvents.OnOpenDocument, AddressOf Me.m_ApplicationEvents_OnOpenDocument
			
			
			' And then
			

        Private Sub m_ApplicationEvents_OnOpenDocument(ByVal DocumentObject As Inventor._Document,
                                                       ByVal FullDocumentName As String,
                                                       ByVal BeforeOrAfter As Inventor.EventTimingEnum,
                                                       ByVal Context As Inventor.NameValueMap,
                                                       ByRef HandlingCode As Inventor.HandlingCodeEnum)

            Dim kBefore As EventTimingEnum = Nothing

            If BeforeOrAfter = kBefore Then
                MessageBox.Show("open - Before")
            Else
                MessageBox.Show("Open - After")
            End If

        End Sub


        Private Sub m_ApplicationEvents_OnSaveDocument(ByVal DocumentObject As Inventor._Document,
        ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap,
        ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_ApplicationEvents.OnSaveDocument


            Dim kBefore As EventTimingEnum = Nothing

            If BeforeOrAfter = kBefore Then
                MessageBox.Show("Save as - Before")
            Else
                MessageBox.Show("Save as - After")
            End If


        End Sub
0 Likes
Accepted solutions (1)
3,268 Views
9 Replies
Replies (9)
Message 2 of 10

Owner2229
Advisor
Advisor

You used it wrong:

Dim kBefore As EventTimingEnum = Nothing

If BeforeOrAfter = kBefore Then
     MessageBox.Show("Save as - Before")
Else
     MessageBox.Show("Save as - After")
End If

With the code above you're basically checking if the BeforeOrAfter is Nothing, which is wrong.

Here's the correct use:

 

If BeforeOrAfter = EventTimingEnum.kBefore Then
     MessageBox.Show("Save as - Before")
Else
     MessageBox.Show("Save as - After")
End If

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 10

JoAntt
Enthusiast
Enthusiast

Ah, ok, My bad..
What got me confused was that i didnt understand that the onSaveDocument dosnt includes the actual opensave dialog.
I am trying to prevent the save as dialog to pop up and capture that.

So i tried OnFileSaveAsDialog and a bunch of others with the EventWatcher and i cant get any of them to tell me before and after save as dialog happens.
the OnFileSaveAsDialog dosnt fire att all.
Any ideas on that?

0 Likes
Message 4 of 10

Owner2229
Advisor
Advisor

Hey, remove the part " Handles m_ApplicationEvents.OnSaveDocument" that's behind the sub.

You already have the handler set, so there's no need for that and it might cause the issue.

 

To prevent the save dialog from even showing up you can use this:

If BeforeOrAfter = EventTimingEnum.kBefore Then
     HandlingCode = HandlingCodeEnum.kEventHandled
     'Here you can use the DocumentObject to do whatever you want with the file instead
End If

 

You're also setting the "m_ApplicationEvents = m_inventorApplication.ApplicationEvents" twice. Scrap the second one.

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 5 of 10

JoAntt
Enthusiast
Enthusiast

Thank you !

 

But i still got the same issue.

As you can see in the screencast the event dosnt fire before the save as dialog shows..

And in this case BeforeOrAfter contains kAbort {4099}, so it dosnt come empty, just to late.

I dont get it 😞

 

 

Namespace TheNameSpace

Private WithEvents m_ApplicationEvents As Inventor.ApplicationEvents

	Public Class StandardAddInServer

		Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal FirstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
			 m_ApplicationEvents = m_inventorApplication.ApplicationEvents
			AddHandler m_ApplicationEvents.OnSaveDocument, AddressOf m_ApplicationEvents_OnSaveDocument
		End Sub

		Private Sub m_ApplicationEvents_OnSaveDocument(ByVal DocumentObject As Inventor._Document,
				ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap,
				ByRef HandlingCode As Inventor.HandlingCodeEnum)

					If BeforeOrAfter = EventTimingEnum.kBefore Then
						HandlingCode = HandlingCodeEnum.kEventHandled
						MsgBox("Hey")
					End If	
		End Sub

	End Class
	
End Namespace

 

 

 

 

0 Likes
Message 6 of 10

Owner2229
Advisor
Advisor

If you would proceed further (and save the document under new name), you would find out that the event actually will fire (both before and after save).

What is happening is that Inventor prompts you for a new name and AFTER you provide it it will start the save.

 

However your problem here is that you want to prevent the Inventor from even showing this dialog.

You can try something like this:

 

AddHandler m_inventorApplication.FileUIEvents.OnFileSaveAsDialog, AddressOf Me.m_ApplicationEvents_OnSaveAsDialog


Private Sub m_ApplicationEvents_OnSaveAsDialog(ByRef FileTypes() As String, SaveCopyAs As Boolean, _
ParentHWND As Integer, ByRef FileName As String, Context As Inventor.NameValueMap, _
ByRef HandlingCode As Inventor.HandlingCodeEnum) HandlingCode = HandlingCodeEnum.kEventHandled 'my own saving code End Sub
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 10

JoAntt
Enthusiast
Enthusiast
Dosnt work 😞
 
FileUIEvents.OnFileSaveAsDialog dosnt capture anything.
Im starting to belive that this have been forgotten when upgrading to current save as dialog.
I have tried all dialog, fileui, and application events but no one can capture before save as dialog.
 
Ive even tried to capture this in inventor 2016 but its the same.
 
Any one have an idea about this?
0 Likes
Message 8 of 10

JoAntt
Enthusiast
Enthusiast
Accepted solution

Finally i got it to work!!!!

This is how to prevent the Save as dialog from showing

 

Start with reading the link below and set the interop types to false, the save as dialog handler dosnt work with this on true.

 

modthemachine

 

Then

 

'Namespace TheNameSpace
	'Public Class StandardAddInServer
		Private WithEvents m_FileUIEvents As Inventor.FileUIEvents
		'Public Sub Activate
			m_FileUIEvents = m_inventorApplication.FileUIEvents
			AddHandler m_inventorApplication.FileUIEvents.OnFileSaveAsDialog, AddressOf m_FileUIEvents_OnFileSaveAsDialog
		'End sub
			Private Sub m_FileUIEvents_OnFileSaveAsDialog(ByRef FileTypes() As String, SaveCopyAs As Boolean,
			ParentHWND As Integer, ByRef FileName As String, Context As Inventor.NameValueMap,
				ByRef HandlingCode As Inventor.HandlingCodeEnum)
				HandlingCode = HandlingCodeEnum.kEventHandled
				MsgBox("I am the new Save As Dialog!")
			End Sub
	'End Class
'End Namespace
Message 9 of 10

Owner2229
Advisor
Advisor

Now I'm interested where did the problem come from and even more, why wasn't it fixed in 5 years.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 10 of 10

antoniobaron
Contributor
Contributor

Hi Im using inventor 2019 and just starting programming two weeks ago.

 

I finally after various attempts approached the code to fire the messagebox, and coded the dialogbox, however, I'm not able to fire the saveas() option of the dialog. The open() dialog works perfect if i just change it.

 

Another problem im having when testing with the open dialog box it's not showing the Initialdirectory or filename. Do you know how fix this issue?

 

this is part of the code I have so far.

 

Private Sub m_FileUIEvents_OnFileSaveAsDialog(ByRef FileTypes() As String, SaveCopyAs As Boolean,
ParentHWND As Integer, ByRef FileName As String, Context As Inventor.NameValueMap,
ByRef HandlingCode As Inventor.HandlingCodeEnum)

HandlingCode = HandlingCodeEnum.kEventHandled

'MsgBox("new File Dialog will be opened")

filedialog()

 

End Sub

Sub SaveAsDialog() 'define the active document
Dim oDoc = m_inventorApplication.ActiveDocument


Dim CurrentFilename = oDoc.FullDocumentName


'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing

Call m_inventorApplication.CreateFileDialog(oFileDlg)

 

'Call m_inventorApplication.CreateFileDialog(oFileDlg)

 

Dim Filetype As String

'check file type and set dialog filter
If m_inventorApplication.ActiveDocumentType = DocumentTypeEnum.kPartDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
Filetype = ".ipt"
ElseIf m_inventorApplication.ActiveDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
Filetype = ".iam"
ElseIf m_inventorApplication.ActiveDocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
Filetype = ".idw"
End If

'Initial conditions of the dialog box
'Grabs the filename from the subrutine and place it in the text
Dim GenFileNumber As String

'FileNameGenerator(GenFileNumber)

 

'set the directory to open the dialog at
oFileDlg.InitialDirectory = m_inventorApplication.FileLocations.Workspace
'set the file name string to use in the input box
'********* on this place I will catch the value from the sub rutine
oFileDlg.FileName = "**** That" 'without extension

'work with an error created by the user backing out of the save
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog (rather than a open dialog)
MsgBox("Im at this section just before opening the basterd")
oFileDlg.ShowSave()

 

'catch an empty string in the imput
If Err.Number <> 0 Then
MsgBox("No File Saved.", "iLogic: Dialog Canceled")
'Goes to the subfunction to rever number generated
RevertGenNumber()
ElseIf oFileDlg.FileName <> "" Then
Dim MyFile = oFileDlg.FileName
'save the file
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As

 

0 Likes