Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Count all in an assembly

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
ngnam1988
578 Views, 8 Replies

Count all in an assembly

Dears,

I'm trying get this information to check before progress next step. Could you please help me.

ngnam1988_0-1693281953758.png

 

Thanks

8 REPLIES 8
Message 2 of 9
Andrii_Humeniuk
in reply to: ngnam1988

Hi @ngnam1988 . What you want to check is the number of occurrences in the active document. The code that gets this:

 

Sub main
	Dim oDoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
	Dim i As Integer
	GetCountOccs(oOccs, i)
	MessageBox.Show("Total Occurrences in Active Document = " & i)
End Sub

Private Function GetCountOccs(ByVal oOccs As ComponentOccurrences, ByRef i As Integer)
	i += oOccs.Count
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.SubOccurrences IsNot Nothing Then GetCountOccs(oOcc.SubOccurrences, i)
	Next
End Function

Second code example:

Sub main
	Dim oDoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
	Dim i As Integer
	For Each oRefDoc As Document In oDoc.AllReferencedDocuments
		i += oOccs.AllReferencedOccurrences(oRefDoc).Count
	Next
	MessageBox.Show("Total Occurrences in Active Document = " & i)
End Sub

 

Andrii Humeniuk - Leading design engineer

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 3 of 9
ngnam1988
in reply to: ngnam1988

Thanks @Andrii_Humeniuk 

Message 4 of 9
Moverano
in reply to: ngnam1988

How would it be for me to tell you a count of each piece? to know how many times one is repeated 

Message 5 of 9
WCrihfield
in reply to: Moverano

Hi @Moverano.  I am not sure how you expect the feedback with that information to be presented, so here is a simple example that just writes that information to the iLogic Log window.  It writes the FullDocumentName on one line, then the Count of how many components in the assembly reference that document in the next line.  We could also create something like a Dictionary(Of Inventor.Document, Integer) to hold onto each document and its count, then use that information later, for some other purpose.

Sub Main
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Logger.Info("Assembly Quantities:" & vbCrLf)
	For Each oRefDoc As Document In oRefDocs
		Dim i As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		Logger.Info("FullDocumentName = " & oRefDoc.FullDocumentName & vbCrLf & "Count = " & i & vbCrLf)
	Next
End Sub

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

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 9
Moverano
in reply to: WCrihfield

Sin título.jpg

My inventor is version 2015, maybe it´s the problem 

Message 7 of 9
WCrihfield
in reply to: Moverano

Sorry for the delay, but I have been away from work on vacation for about a week and a half.  Yes, the version of Inventor you are using is preventing that specific line of code from working, but you could just use a different line of code there to avoid that error.  That line of code starting with "Logger" is using an iLogic tool that became available in the 2019.1 version of Inventor, as a way to let us write stuff from within our iLogic rules into the new iLogic Log window that also became available at that time.  This was a big step forward that not only helps us to debug our rules in an easier way, but also provided easier feedback mechanism for general iLogic rule research & development.  You would have to replace the code on lines 5 & 8 with something else.  You could just use a simple String type variable in both locations, then present the contents of that String in a message box at the end, like the following:

Sub Main
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Dim sReport As String = "Assembly Quantities:" & vbCrLf
	For Each oRefDoc As Document In oRefDocs
		Dim i As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		sReport = sReport & "FullDocumentName = " & oRefDoc.FullDocumentName & vbCrLf & "Count = " & i & vbCrLf
	Next
	MessageBox.Show(sReport, "Assembly Quantities", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

The following is a link to an online help page which mentions this feature being added to iLogic in that release of Inventor.

What's New for Inventor 2019.1 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 9
emanuel.c
in reply to: ngnam1988

@ngnam1988  I don't know if this is helpful to you, but this is what I use to store the item qty in an iProperty, later used on a drawing too. The code writes an iProperty "QTY / Project" in each occurence of the assembly. This iProperty is filled in with the total instances of that occurence in the top level assembly from which you're running the code. Someone else helped me with some of the code and I'm sorry I forget where it is to give proper credit. See if it would at all be useful to you.

 

Sub Main ()	
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If oDoc.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "Error")
		Exit Sub
	End If
	Dim customPropertyName As String = "QTY / Project"
	Dim oAsmDoc As AssemblyDocument = oDoc
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oAsmDef.Occurrences
	Dim oDict As New Dictionary(Of String, Integer)	
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.IsModifiable Then Continue For
'		Dim sNamePart As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim sPartNumb As String = oRefDoc.PropertySets("Design Tracking Properties")("Part Number").Value
		Dim iCount As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		If oDict.ContainsKey(sPartNumb) Then
			oDict(sPartNumb) = oDict(sPartNumb) + iCount
		Else
			oDict.Add(sPartNumb, iCount)
		End If
	Next
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.IsModifiable Then Continue For
'		Dim sNamePart As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim sPartNumb As String = oRefDoc.PropertySets("Design Tracking Properties")("Part Number").Value
		Dim iCount As Integer = oDict(sPartNumb)
		If iCount = 0 Then Continue For
		Dim oCustom As PropertySet = oRefDoc.PropertySets("Inventor User Defined Properties")
		Try : oCustom(customPropertyName).Value = iCount
		Catch : oCustom.Add(iCount, customPropertyName) : End Try
	Next
End Sub

 

Message 9 of 9
Moverano
in reply to: WCrihfield

Don't worry @WCrihfield , work and family come first.

With your new code it already works for me, thank you very much
The ideal would be for it to appear in the bill of materials,  I imagine that a custom iproperty  should be created and there it would be recorded in each component the times it appears

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report