iLogic. Write the QTY of components and the QTY of assemblies to user iProperties

iLogic. Write the QTY of components and the QTY of assemblies to user iProperties

edik_z
Observer Observer
250 Views
2 Replies
Message 1 of 3

iLogic. Write the QTY of components and the QTY of assemblies to user iProperties

edik_z
Observer
Observer

Hallo zusammen,

Kann mir jemand helfen, einen iLogic-Code zu erstellen,
der in einer Hauptbaugruppe alle enthaltenen Bauteile und Unterbaugruppen durchsucht
und deren benutzerdefiniertes iProperty „ANZ“ so beschreibt,
dass dort die Stückzahl (Anzahl der Vorkommen) eingetragen wird?

Vielen Dank im Voraus2228.png

0 Likes
251 Views
2 Replies
Replies (2)
Message 2 of 3

vpeuvion
Advocate
Advocate

Hi,

Here is some sample code that lists the occurrences of an assembly and their quantity. This might help you.

Sub main()
    ' Get the top-level assembly document
    Dim doc As AssemblyDocument = ThisAssembly.Document

    ' Get the component definition of the assembly (to access its parts and subassemblies)
    Dim asmCompDef As AssemblyComponentDefinition = doc.ComponentDefinition

    ' Create a dictionary to store occurrence names and their counts
    Dim oOccCountDict As New Dictionary(Of String, Integer)

    ' Loop through all occurrences (components) in the assembly
    For Each occ As ComponentOccurrence In asmCompDef.Occurrences
        ' Extract the base name of the component (remove the instance number after the ":")
        Dim key As String = occ.Name.Split(":")(0)

        ' Increment count if it already exists, otherwise initialize to 1
        If oOccCountDict.ContainsKey(key) Then
            oOccCountDict(key) += 1
        Else
            oOccCountDict.Add(key, 1)
        End If
    Next

    ' Display the component name and quantity using a message box
    For Each kvp As KeyValuePair(Of String, Integer) In oOccCountDict
        MessageBox.Show(kvp.Key & " : " & kvp.Value)
    Next
End Sub

Here is a link that shows how to create an iProperty if it does not exist : 

https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-create-custom-ipropertie/m-p/29536...

Hope this will help you.

Vincent. 

0 Likes
Message 3 of 3

emanuel.c
Collaborator
Collaborator

Hi @edik_z,

 

I think this is what would work for you, since I use something very similar, though it didn't originate with me.

It works pretty well - I hardly ever have issues with it (not writing the correct quantities for example).

It will write a new iProperty, in your case called "ANZ" which will be filled with the total quantity of instances of that component in a given assembly.

You can then use that wherever you need, in a drawing for example or elsewhere.

Hope it is of good use.

 

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 = "ANZ"
	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
		If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then Continue For
		If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then Continue For			
		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
			'MessageBox.Show("1: " & sPartNumb & " " & iCount & " qty")
		Else
			oDict.Add(sPartNumb, iCount)
			'MessageBox.Show("2: " & sPartNumb & " " & iCount & " qty")
		End If
	Next
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.IsModifiable Then Continue For
		If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then Continue For
		If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then Continue For
		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