Cycle thru Occurrences with Model State Fails

Cycle thru Occurrences with Model State Fails

SteveK88
Advocate Advocate
496 Views
6 Replies
Message 1 of 7

Cycle thru Occurrences with Model State Fails

SteveK88
Advocate
Advocate

This code runs fine in Inventor 2021 & Inventor 2023 if no part model state is used.

Fails in Inventor 2023 on any part that has a Model State.

Looking for a fix.  

==============================================

'Run in any assembly (iam) with a part (ipt) that has an active Model State

'Fails in Inventor 2023 if a part (ipt) has an active Model State
'Fails on line "Dim refDoc As PartDocument = occ.Definition.Document"
'Error code: Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

'Runs fine in Inventor 2021, Runs fine in Inventor 2023 if all parts (ipt) do NOT have an active Model State

'Code is meant to cycle thru all parts and turn On/Off visibility based on File Name. Runs fine in Inventor 2021 and earlier.

Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
Dim occ As Inventor.ComponentOccurrence
For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
Dim refDoc As PartDocument = occ.Definition.Document

'MessageBox.Show("Full FileName " & refDoc.FullFileName, "TEST")

Next

0 Likes
Accepted solutions (1)
497 Views
6 Replies
Replies (6)
Message 2 of 7

dalton98
Collaborator
Collaborator

maybe try this?

Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
Dim occ As Inventor.ComponentOccurrence
For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
Dim refDoc As PartDocument = occ.Definition.Document
If refDoc.ComponentDefinition.IsModelStateMember
	refDoc = occ.Definition.FactoryDocument
MessageBox.Show("Full FileName " & refDoc.FullFileName, "TEST")
End If
Next
0 Likes
Message 3 of 7

SteveK88
Advocate
Advocate

Thanks for the reply,

Code Fails on, 

Dim refDoc As PartDocument = occ.Definition.Document

 

So, changing Model State to Factory after this line will not work.

 

Manually changing all Model States to factory then running the code still fails on this same line.

Dim refDoc As PartDocument = occ.Definition.Document

 

 

 

 

0 Likes
Message 4 of 7

dalton98
Collaborator
Collaborator

Try these

Dim refDoc As Document = occ.Definition.Document

 

Dim refDoc As PartDocument = occ.ReferencedDocumentDescriptor.ReferencedDocument

 

Try
Dim refDoc As PartDocument = occ.Definition.Document
Catch
Dim refDoc As PartDocument = occ.Definition.FactoryDocument
End Try
0 Likes
Message 5 of 7

SteveK88
Advocate
Advocate

Still  not working with the Try statements.

 

I think there is an issue with occurrences and model states.

I am trying AllReferencedDocuments instead of occurrences.

 

 

0 Likes
Message 6 of 7

SteveK88
Advocate
Advocate
I think there is an issue with occurrences and model states.

Tried using AllReferencedDocuments but I need the occurrence to change visibility.
0 Likes
Message 7 of 7

SteveK88
Advocate
Advocate
Accepted solution

This will work for what I want.

 

	Dim oDoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
    Dim oOcc As ComponentOccurrence
	i = 0
	v = 0
		
    For Each oOcc In oOccs
		
		SearchName1 = oOcc.Name.Contains("Bolt")
		
		If SearchName1 Then
			v=v+1
			'code for something
			Else
			i=i+1
			'Code for something
		End If
    Next
	
	MessageBox.Show("Counter " & i & ", " & v, "TEST")		
0 Likes