SubOccurrences triggers OnOpenDocument event on referenced assembly children

SubOccurrences triggers OnOpenDocument event on referenced assembly children

CattabianiI
Collaborator Collaborator
896 Views
6 Replies
Message 1 of 7

SubOccurrences triggers OnOpenDocument event on referenced assembly children

CattabianiI
Collaborator
Collaborator

I'm a long time Inventor api user and I just discovered this behaviour that looks strange to me.
1. Open Inventor

2. Open Event Watcher in C:\Users\Public\Documents\Autodesk\Inventor 20XX\SDK\DeveloperTools\Tools\EventWatcher\bin\Release and check ApplicationEvents.OnOpenDocument event listener.

3. Open a drawing referencing an assembly with 2 parts.

4. Now, Inventor tells me there are 4 documents opened with unique file names (idw, iam and the 2 parts). 

5. On the other hand looking at the event watcher I see it has been triggered just two OnOpenDocument events on idw and iam. Why?

Going deeper if I add the following iLogic rule to count all occurrences recursively I'll see the line suboccs = subocc.SubOccurrences triggering the 2 remaning OnOpenDocument events. 

 

Is this an optimization done in recent versions? 

 

The problem is that in drawing referencing large assembly this counting process takes a long time due to documents opening. 

 

Sub Main()
	Dim drwDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim asmDoc As AssemblyDocument = drwDoc.ReferencedDocuments.Item(1)
	Dim count = 0
	CountSubOccs(asmDoc.ComponentDefinition.Occurrences, count)
	MsgBox(count)
End Sub

Sub CountSubOccs(ByVal occs As ComponentOccurrences, ByRef counter As Long)
	For Each subocc As ComponentOccurrence In occs
		Try
			If subocc.IsSubstituteOccurrence Then Continue For
		Catch
		End Try
		counter += 1
		
		Dim suboccs = Nothing 
		Try
			suboccs = subocc.SubOccurrences
		Catch
			Continue For
		End Try
		CountSubOccs(suboccs, counter)
	Next
End Sub

 

 

0 Likes
897 Views
6 Replies
Replies (6)
Message 2 of 7

YuhanZhang
Autodesk
Autodesk

Yes this is as designed for multiple versions, we provide the option that the referenced documents can be delayed to load when you work with drawing or assembly documents, this is to improve the performance when you just open a big assembly/drawing, the referenced documents will be loaded when you trigger it(like you query the SubOccurrences etc.).  Of course you can also load all referenced documents when open an assembly/drawing, just like in UI you can see that when you try to open an assembly/drawing there is Express/Full option on the Open dialog or in the Options... dialog.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 7

CattabianiI
Collaborator
Collaborator

I'm aware of assembly opening options. 
I don't know instead about express drawing option:
thru UI I get those options:
- Complete

- Defer updates

- Skip Unresolved file

thru API I get those options:

- DeferUpdates

- FileVersionOption

- Password

- FastOpen (Specifies whether skip all referenced files when open a drawing document.)

- SkipAllUnresolvedFiles.

 

If I run the rule above on a document opened with fastOpen option I got an exception because I haven't referenced document as expected.

If I run the rule above on a document opened with no options I got the behaviour explained in the original post: SubOccurrences triggering OnOpenDocument event.
How can I open an idw in a really complete mode so OnOpenDocument is triggered on idw opening for every children instead on SubOccurrences call?


 

0 Likes
Message 4 of 7

YuhanZhang
Autodesk
Autodesk

Current behavior is the drawing document always has all its referenced documents delayed to open, but for assembly you can use options to set the ExpressModeBehavior to OpenFull to force open all its referenced documents when open the assembly. Can you tell what you want to do with the referenced documents? Why you want them to be opened along with the drawing?

 

The FastOpen for drawing should be replaced by the SkipAllUnresolvedFiles, I will remove it from the API help.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 5 of 7

CattabianiI
Collaborator
Collaborator

Why you want them to be opened along with the drawing?
Because I have a quite complex exporting to autocad dwg job I run on drawing and I would prefer to work with a fully opened drawing instead of triggering OnOpenDocument event once I query a referenced document.

 

I think it's cleaner and lead to less error trigger OnOpenDocument outside (before) my job.

0 Likes
Message 6 of 7

YuhanZhang
Autodesk
Autodesk

If your worry is about whether the drawing document is up-to-date when the referenced model has changed, you need not worry about it, unless you set the DeferUpdates when open a drawing document, otherwise if the referenced model document has changed and now you open the drawing document it will prompt you to update the document and all the referenced model document will be loaded(fully opened). The situation you see the referenced model documents are not fully loaded is because the drawing document is update-to-date so needs not to load the referenced documents.

 

But to make sure your drawing document is update-to-date for exporting to DWG, you can do below check in your code before exporting it:

 

1. Iterate all the referenced model document and update them if required.

2. Check if the DrawingDocument.DrawingSettings.DeferUpdated is checked, if yes you can turn it off so the drawing can be updated.

2. Then check the DrawingDocument.RequiresUpdate, if it is True you can call DrawingDocument.Update to update  the document. You can also call Sheet.Update to update each and every Sheet in the drawing.

 

Below is VBA sample code to do this:

 

Sub UpdateDrawingSample()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oRefDoc As Document
    For Each oRefDoc In oDoc.AllReferencedDocuments
        If oRefDoc.RequiresUpdate Then
            oRefDoc.Update
        End If
    Next
    
    If oDoc.DrawingSettings.DeferUpdates Then
        oDoc.DrawingSettings.DeferUpdates = False
    End If
    
    Dim oSheet As Sheet
    For Each oSheet In oDoc.Sheets
        oSheet.Update
    Next
    
    If oDoc.RequiresUpdate Then
        oDoc.Update
    End If
End Sub

Hope this clears.

 

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 7 of 7

CattabianiI
Collaborator
Collaborator

@YuhanZhang wrote:

The situation you see the referenced model documents are not fully loaded is because the drawing document is update-to-date so needs not to load the referenced documents.

Yes, this clears a lot.

 

What I want to point out is that for me up-to-date has a wider meaning: for example let's say in my OnOpenDocument event handler I query for the today price of the part material and update the price custom iProperty that is linked to a drawing table. And if OnOpendDocument isn't triggered for every children my drawing is not up-to-date. 

Anyway I got your point and I'm creating a FullOpen method to call before my ExportToDwg to make the drawing up-to-date in my meaning; and I'll post to Ideas the request to get an open option to force the drawing to be fully opened.

Thank you.


0 Likes