Making addin do something once for an open document.

Making addin do something once for an open document.

-niels-
Mentor Mentor
311 Views
4 Replies
Message 1 of 5

Making addin do something once for an open document.

-niels-
Mentor
Mentor
Hello all,



I've recently made my own addin for adding a custom "date" property to our drawings.

I managed this through a lot of copy/pasting, the template for addins from Brian's blog and some general googling.



Now I've got a working addin that runs everytime i save a drawing, but i would like it to run only on the first save for as long as the document is open.

(It asks if i want to update the "date" property every time... only want to see that once.)



Is this possible with VB?

If so, how would i go about doing this?

The only experience i have with programming in VB is making this addin, so that's not much.



Regards,



Niels.

Niels van der Veer
Inventor professional user & 3DS Max enthusiast
Vault professional user/manager
The Netherlands

0 Likes
312 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable


DISCLAIMER: this is just some pseudo-code written very quickly and not in VB.Net syntax





In you Add-In, make a container (an associative container could be used, or just a simple list type container, basically you just want it to be searchable and the ability to remove elements from anywhere in the container).



Hook into the DocumentSave and DocumentClose events (may not be the right event names here, but look for something close to this).



In the DocumentSave event handler, check the list for the document, if it is not in there, then add it to the list. If you added the document to the list, then also run your save-once code.



In the DocumentClose event handler, check the list for the document. If it is in the list, then remove it.

0 Likes
Message 3 of 5

Anonymous
Not applicable


echo

0 Likes
Message 4 of 5

-niels-
Mentor
Mentor
Hi Josh,



I understand from your post that it should be possible to run something only once on save.

I think I understand the general idea you are trying to give me, I'll see how far i can get myself.



Thanks so far, i'll post back if i can't manage it myself.

Niels van der Veer
Inventor professional user & 3DS Max enthusiast
Vault professional user/manager
The Netherlands

0 Likes
Message 5 of 5

-niels-
Mentor
Mentor
Well, i think I've actually managed it.
I'll post my code, which is probably not that pretty, for suggestions and / or corrections.

Here it is:
{code}
Public SavedState As Boolean = False 'variabele om document 1x op te slaan met datum


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

If BeforeOrAfter = EventTimingEnum.kBefore And SavedState = True Then

'variabele om te bepalen wat voor document actief is
Dim DitDoc As Integer = m_inventorApplication.ActiveDocumentType

'bepalen of actieve document een tekening is
If DitDoc = DocumentTypeEnum.kDrawingDocumentObject Then
SavedState = False

End If

End If

End Sub

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

If BeforeOrAfter = EventTimingEnum.kBefore And SavedState = False Then

SavedState = True

'variabele om te bepalen wat voor document actief is
Dim DitDoc As Integer = m_inventorApplication.ActiveDocumentType

'bepalen of actieve document een tekening is
If DitDoc = DocumentTypeEnum.kDrawingDocumentObject Then
Call Datum()

End If

End If

End Sub

Private Sub Datum()

On Error Resume Next

'variabele voor custom iproperty locatie
Dim TekProp As PropertySet
TekProp = m_inventorApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")

'variabele voor datum laatst opgeslagen
Dim Saved As Inventor.Property
Saved = TekProp.Item("Saved")

'Controle of custom property "Saved" al bestaat en zonodig aanmaken of vervangen
If Err.Number <> 0 Then

Call TekProp.Add(Format(Now, "dd-M-yyyy"), "Saved")
MsgBox("Datum aangemaakt.", vbOKOnly + vbInformation, "Succes!")

Else

'Keuzemenu
Dim Keuze As Integer
Keuze = MsgBox("Wilt u de datum bijwerken?", vbYesNo + vbQuestion, "Bijwerken?")
If Keuze = vbYes Then
Saved.Value = Format(Now, "dd-M-yyyy")
Else
SavedState = False
End If

End If

End Sub
{code}
I hope it's complete, it's what i've added to the template for IV addins.

Niels van der Veer
Inventor professional user & 3DS Max enthusiast
Vault professional user/manager
The Netherlands

0 Likes