partDocument.Save trigger OnSaveDocument event

partDocument.Save trigger OnSaveDocument event

Frederick_Law
Mentor Mentor
297 Views
3 Replies
Message 1 of 4

partDocument.Save trigger OnSaveDocument event

Frederick_Law
Mentor
Mentor

I have addin to change part file BOM Structure to Reference when a new file is saved with "Master" in the name.

It worked since IV 2012 to IV2020.

After upgrade to 2023.2 it cause IV to crash.

I traced to problem to PartDocument.Save().

It trigger OnSave event and cause an endless loop.

If I can get the new filename Before save, I can avoid the Save again.

Or anyway to avoid event trigger?

Private Sub M_ApplicationEvents_OnSaveDocument(
        oDoc As _Document, BeforeOrAfter As EventTimingEnum, Context As NameValueMap,
        ByRef HandlingCode As HandlingCodeEnum) Handles M_ApplicationEvents.OnSaveDocument

Dim oPartDoc As PartDocument
     
'Cannot get new filename Before
If BeforeOrAfter = EventTimingEnum.kAfter Then
    'Check if new file, kAfter FileSaveCounter = 1
    If oDoc.FileSaveCounter = 1 Then
        If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
            If (InStr(oDoc.DisplayName, "Master", CompareMethod.Text)) > 0 Then
                oPartDoc = oDoc
                oPartDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
                Saved = True
                'IV2023 trigger OnSave event
				oPartDoc.Save()
            End If
        End If
    End If
End Sub

 

0 Likes
298 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @Frederick_Law.  Interesting paradox scenario.  I assume you are already aware of how to use the Context (NameValueMap) portion of that method works, as explained in the online help documentation for it.  Maybe you could try looking at the ThisApplication.CommandManager.PeekPrivateEvent(kFileNameEvent, oInputObjectForCapture) method.  Just something related that comes to mind, but have not tried it in that scenario before.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

Frederick_Law
Mentor
Mentor

I'll debug and watch the Context, not promising:

"When a document is saved for the first time, this value will be "" for the kBefore timing and the filename of the document in the kAfter timing."

 

I'll watch other names in the PartDocument and see if I can find what I need.

 

Problem is Save() didn't fire OnSave in IV2020 and before.

Only have problem with IV2023.

0 Likes
Message 4 of 4

Frederick_Law
Mentor
Mentor

One of the Context give full path of the file being saved during BeforeSave.

I can cut the file name out and use it BeforeSave.

This way I can remove Save().

 

I tested Save2, still fire OnSave.

Context Item Name doesn't change during the loop.  So I can't check if I'm in a loop.

0 Likes