Count of selected component in the assembly

Count of selected component in the assembly

pavol_krasnansky
Enthusiast Enthusiast
535 Views
5 Replies
Message 1 of 6

Count of selected component in the assembly

pavol_krasnansky
Enthusiast
Enthusiast

Hello. I need to get count of components which are the same then the selected component in the assembly. Selected component can be suppressed or unsuppressed. Code below works correctly, when there is not any suppressed component in the assembly. I use Inventor 2023. Thank you.

 

 

Sub Main()
	
	Dim oDoc As Document
	oDoc = ThisDoc.Document
	
	If oDoc.DocumentType = kAssemblyDocumentObject Then
		
		Dim oAsmDoc As AssemblyDocument
		oAsmDoc = ThisApplication.ActiveDocument
		
		Dim FullFileName_Occurrence As String = ""
		Dim Count_The_Same_Items As Integer = 0
		
		Try
			
			Dim oOccurrence As ComponentOccurrence
			oOccurrence = ThisDoc.Document.SelectSet.Item(1)
			
			Dim oDoc_Occurrence As Document
			oDoc_Occurrence = oOccurrence.Definition.Document
			
			FullFileName_Occurrence = oDoc_Occurrence.FullFileName
			
		Catch
			
			MsgBox("A component must be selected!")
			Exit Sub
			
		End Try
		
		For Each oObj In oAsmDoc.ComponentDefinition.Occurrences
			
			Dim doc As Document
			doc = oObj.Definition.Document
			
			If doc.FullFileName = FullFileName_Occurrence Then
				
				Count_The_Same_Items += 1
				
			End If
			
		Next
				
		MsgBox(Count_The_Same_Items)
		
	End If
	
End Sub

 

 

 

 

0 Likes
Accepted solutions (1)
536 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @pavol_krasnansky.  There is actually a built-in Inventor API tool that can help with that exact task.  It will return a collection of all other components that are referencing a specific document.  Here is an example you can test.

 

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOcc As ComponentOccurrence = Nothing
If oADoc.SelectSet.Count = 0 Then Return
If TypeOf oADoc.SelectSet.Item(1) Is ComponentOccurrence Then
	oOcc = oADoc.SelectSet.Item(1)
Else
	Return
End If
Dim oOccDoc As Document = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
'get all other components that are also referencing the same document
Dim oAllRefOccs As ComponentOccurrencesEnumerator =  oADef.Occurrences.AllReferencedOccurrences(oOccDoc)
MsgBox("There are " & oAllRefOccs.Count & " total components referencing the same document" & _
vbCrLf & "that the selected component is referencing.", vbInformation, "")

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

pavol_krasnansky
Enthusiast
Enthusiast

Hi @WCrihfield . Your solution works fast without cyklus (it is amazing), but only with unsuppressed component and with components the same state model. I need solutions for suppressed and unsuppressed components and for  components in the different state model.

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @pavol_krasnansky.  This is sort of a tough one to deal with, because you normally can not do much of anything with a suppressed component.  You can not access its Definition, and you can not access its referenced document either, until after you un-suppress it.  But if you un-suppress it, you have to remember that you did so, and then re-suppress it later, to leave it the way it was found.  Also, dealing with document references when ModelStates are involved can be fairly complicated.  Most of the time, if no ModelState is involved, you will always get the main file document with most methods.  However, when non-master ModelStates are involved, you usually get what is called a 'member' document, which is not the main document, but a modified virtual copy of it.  And there are some object Properties sometimes encountered along the way which are pretty misleading, in my opinion.

 

Below is an iLogic rule you can try that I believe should do a better job at the specific task you wanted:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOcc As ComponentOccurrence = Nothing
	If oADoc.SelectSet.Count = 0 Then
		MsgBox("Nothing was pre-selected.  Exiting rule.", vbCritical,"")
		Exit Sub
	End If
	If TypeOf oADoc.SelectSet.Item(1) Is ComponentOccurrence Then
		oOcc = oADoc.SelectSet.Item(1)
	Else
		MsgBox("A single component was NOT pre-selected.  Exiting rule.", vbCritical,"")
		Return
	End If
	If IsNothing(oOcc) Then Exit Sub
	Dim oOriginalOccFactoryDoc As Document = GetComponentFactoryDocument(oOcc)
	If IsNothing(oOriginalOccFactoryDoc) Then
		MsgBox("No 'factory' document was returned from the originally selected component.", vbCritical,"")
		Exit Sub
	End If
	Dim oOccsLikeSelected As New List(Of ComponentOccurrence)
	
	'get all other components that are also referencing the same document
	'but also count them if they are suppressed or if set to a different ModelState than the original
	
	'this loop is only looking at 'top level' components, not components within sub-assemblies
	For Each oComp As ComponentOccurrence In oADef.Occurrences
		'if a component is suppressed, you can not access its Definition, or its referenced Document
		'also, when the component is under the influence of a non-master ModelState,
		'the Document you get from it will be a 'member' document, not the true 'factory' or main document
		Dim oCompFactoryDoc As Document = GetComponentFactoryDocument(oComp)
		If IsNothing(oCompFactoryDoc) Then
			Logger.Info("No 'factory' document was returned from component named '" & oComp.Name & "'.")
			Continue For
		End If
		If oCompFactoryDoc.FullFileName = oOriginalOccFactoryDoc.FullFileName Then
			oOccsLikeSelected.Add(oComp)
		End If
	Next
	'this count will include the originally selected component
	Dim oCount As Integer = oOccsLikeSelected.Count
	MsgBox("There are " & oCount & " total components referencing the same document" & _
	vbCrLf & "as the originally selected component.", vbInformation, "")
End Sub

Function GetComponentFactoryDocument(oComponent As ComponentOccurrence) As Inventor.Document
	If IsNothing(oComponent) Then Return Nothing
	Dim oSuppressed As Boolean = False
	If oComponent.Suppressed Then
		oSuppressed = True
		Try
			oComponent.Unsuppress
		Catch
			Logger.Error("Could not Unsuppress component named '" & oComponent.Name & "'.")
			Return Nothing
		End Try
	End If
	If TypeOf oComponent.Definition Is VirtualComponentDefinition Then Return Nothing
	Dim oMSs As ModelStates = oComponent.Definition.ModelStates
	Dim oFactoryDoc As Document = oMSs.Item(1).Document
	If oSuppressed Then
		Try
			oComponent.Suppress
		Catch
			Logger.Error("UnSuppressed component named:  '" & oComponent.Name & "'," & _
			vbCrLf & "but was unable to Suppress it again.")
		End Try
	End If
	Return oFactoryDoc
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 6

pavol_krasnansky
Enthusiast
Enthusiast

It works! Thank you @WCrihfield  🙂

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Hi @pavol_krasnansky.  I am glad I was able to help you.  By the way, I just created a new 'Contribution' post (Link below) about ModelStates and their Document references that you may find helpful in understanding them better.  That post also includes a copy of that custom Function I used above, so that others can find it easier and use it in their own automation projects.

Inventor's ModelStates and their 'Member' vs 'Factory' Document References Explained 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)