Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
S.vanderBrug
982 Views, 17 Replies

ilogic code for assemblies to send iproperties to their sub-assemblies

Hi there! my first ever post here so apologies if I mess something up.

 

I am trying to automatically fill in an iproperty in all sub-assemblies, which refers to the assembly above it, displaying the higher assembly's drawing number (TekNum), aswell as the position of the sub-assembly in the parts list of the assembly above it.

 

I have been lurking on these forums for a little while now, so I have some general understanding of how this all works, but I am by no means competent in ilogic, or vb.net or anything. 

 

I can usually manage by stealing some code from here and there, an provisorically tying them together with the little coding I dó understand :slightly_smiling_face:

 

That said, here's what I have so far, it gives me a BOM item number, and the "sub traverseassembly" part, plops it together with TekNum of the assembly it's being run in, into the Iproperties of the underlying components.

 

Sub Main
Dim oAssemblyDocument As AssemblyDocument
oAssemblyDocument = ThisDoc.Document

Dim oAssemblyComponentDefinition As AssemblyComponentDefinition
oAssemblyComponentDefinition = oAssemblyDocument.ComponentDefinition

''Get Name of Active LOD
'NameActiveLOD = oAssemblyComponentDefinition.RepresentationsManager.ActiveLevelOfDetailRepresentation.Name
''Active Master LOD
'oAssemblyComponentDefinition.RepresentationsManager.LevelOfDetailRepresentations(1).Activate

Dim oBOM As BOM
oBOM = oAssemblyComponentDefinition.BOM

oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = False
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews(2) 'Structured view

Call RecursiveCheckAndSetProps(oBOMView.BOMRows, 0)
Call TraverseAssembly(oAssemblyDocument.ComponentDefinition.Occurrences)

'Reactive original LOD
'oAssemblyComponentDefinition.RepresentationsManager.LevelOfDetailRepresentations(NameActiveLOD).Activate
End Sub

Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator, indent As Integer)
	On Error Resume Next
        For Each oBOMRow As BOMRow In oRowsElements
            Dim oComponentDefinition As ComponentDefinition
            oComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)

            Dim oBOMItemNumber As String
            oBOMItemNumber = oBOMRow.ItemNumber(indent*2) ' by multiplying by two, the itemnumber dodges the periods in the expanded part number.
			
				Dim rDoc As Document = oComponentDefinition.Document
				Call Check_Prop(rDoc,oBOMItemNumber)

            If Not oBOMRow.ChildRows Is Nothing Then
				Call RecursiveCheckAndSetProps(oBOMRow.ChildRows,indent+1)
            End If
        Next
End Sub

Sub Check_Prop(rDoc As Document,oBOMItemNumber As String)

    Dim oComponentDefinitionPropertySet As PropertySet
    oComponentDefinitionPropertySet = rDoc.PropertySets.Item("Inventor User Defined Properties")
    'custom property tab
	Try
        'if already exists then set it 
        oComponentDefinitionPropertySet.Item("BOM Number").Value = oBOMItemNumber
    Catch ex As Exception
        'else add it
        oComponentDefinitionPropertySet.Add(oBOMItemNumber, "BOM Number")
    End Try
End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences)
    ' Iterate through all of the occurrence in this collection
	Dim oOcc As ComponentOccurrence
	
	For Each oOcc In oOccs 
		
		Try
   			If iProperties.Value(oOcc.Name, "Custom", "Samenstelling") = "" Then
        			iProperties.Value(oOcc.Name, "Custom", "Samenstelling") = iProperties.Value("Custom", "TekNum") & " POS" & iProperties.Value(oOcc.Name, "Custom", "BOM Number")
			End If
		Catch
			'Logger.Info("Error in {0}", oOcc.Name) 'enable to show which components throw an error
		End Try

		' Recursively call sub if needed
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences)
		End If
	Next
End Sub

 

Ideally, "sub traverseassembly" would take the "TekNum" of every assembly, and put it in all underlying occurences. That would only require that part be changed.

 

The goal at this moment is to only need to run the rule in the very top (main) assembly.

 

If there's a way easier way to do this (I honestly expect there to be), then that is ofcourse also very welcome :slightly_smiling_face: