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:ย 

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

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
S.vanderBrug
921 Views, 17 Replies

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

S.vanderBrug
Contributor
Contributor

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 ๐Ÿ™‚

 

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 ๐Ÿ™‚

0 Likes

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 ๐Ÿ™‚

 

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 ๐Ÿ™‚

17 REPLIES 17
Message 2 of 18

Andrii_Humeniuk
Advisor
Advisor

Hi @S.vanderBrug . Not sure I understood you, but I did optimize your current code. Please show on the screenshots what exactly you want to do, I will be happy to help you.

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String = oCustom("BOM Number").Value
	
	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
	    Dim oCustom As PropertySet = oCompDef.Document.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		If oSamenstelling.Value = "" Then
			oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		End If
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oTekNum, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

0 Likes

Hi @S.vanderBrug . Not sure I understood you, but I did optimize your current code. Please show on the screenshots what exactly you want to do, I will be happy to help you.

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String = oCustom("BOM Number").Value
	
	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
	    Dim oCustom As PropertySet = oCompDef.Document.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		If oSamenstelling.Value = "" Then
			oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		End If
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oTekNum, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

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 18

S.vanderBrug
Contributor
Contributor

Hi @Andrii_Humeniuk , thanks for your reply ๐Ÿ™‚

 

I had considered doing something like you did for optimizing, but didn't trust that I knew for sure what I was doing.

With your code, it throws an "exception at Hresult" error on line 5. I probably should have mentioned that "TekNum" is automatically populated, which I'd imagine is what causes this. 

 

Currently, teknum is: "=<ProjectYear><OrderNO><PART NUMBER>"

 

I am not quite sure what you would want a screenshot of?

 

The main issue that remains with this(my) code, is that if it is run, all occurences get the "TekNum" property of the assembly from which the code is getting run.

Instead, I want the "TekNum" property of every assembly be sent to it's underlying occurences.

 

So: Main assembly sends the "TekNum" value to "samenstelling" in the subassemblies it contains. Those assemblies send their "TekNum" value to "samenstelling" in their underlying subassemblies. It doesn't matter if it also does, or tries to, populate these values in other occurences apart from assemblies, which is why I have just been using occurences.

 

I imagine this would complicate the code needed for the "TekNum" part quite a bit, but I don't really have insight into this.

 

Thanks for taking the time to look at my scribblings ๐Ÿ˜„

0 Likes

Hi @Andrii_Humeniuk , thanks for your reply ๐Ÿ™‚

 

I had considered doing something like you did for optimizing, but didn't trust that I knew for sure what I was doing.

With your code, it throws an "exception at Hresult" error on line 5. I probably should have mentioned that "TekNum" is automatically populated, which I'd imagine is what causes this. 

 

Currently, teknum is: "=<ProjectYear><OrderNO><PART NUMBER>"

 

I am not quite sure what you would want a screenshot of?

 

The main issue that remains with this(my) code, is that if it is run, all occurences get the "TekNum" property of the assembly from which the code is getting run.

Instead, I want the "TekNum" property of every assembly be sent to it's underlying occurences.

 

So: Main assembly sends the "TekNum" value to "samenstelling" in the subassemblies it contains. Those assemblies send their "TekNum" value to "samenstelling" in their underlying subassemblies. It doesn't matter if it also does, or tries to, populate these values in other occurences apart from assemblies, which is why I have just been using occurences.

 

I imagine this would complicate the code needed for the "TekNum" part quite a bit, but I don't really have insight into this.

 

Thanks for taking the time to look at my scribblings ๐Ÿ˜„

Message 4 of 18

Andrii_Humeniuk
Advisor
Advisor

I think I got you, try this code:

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try
	
	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
		Dim oDoc As Document = oCompDef.Document
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		If oSamenstelling.Value = "" Then
			oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		End If
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oCustom("TekNum").Value, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

0 Likes

I think I got you, try this code:

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try
	
	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
		Dim oDoc As Document = oCompDef.Document
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		If oSamenstelling.Value = "" Then
			oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		End If
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oCustom("TekNum").Value, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

Message 5 of 18

S.vanderBrug
Contributor
Contributor

Thanks for the quick reply!

 

I am sorry, I am completely out of my depth now, so I am not sure what's going on:

 

On line 36 it throws "Object reference not set to an instance of an object"

 

I must say that I am getting quite excited by such a short piece of code being able to do all this.

Some day I'll get there too..

0 Likes

Thanks for the quick reply!

 

I am sorry, I am completely out of my depth now, so I am not sure what's going on:

 

On line 36 it throws "Object reference not set to an instance of an object"

 

I must say that I am getting quite excited by such a short piece of code being able to do all this.

Some day I'll get there too..

Message 6 of 18

Andrii_Humeniuk
Advisor
Advisor

I write the code intuitively without conducting tests, so there are children's mistakes in it, I hope everything will work well now.

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try
	
	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
		Dim oDoc As Document = oCompDef.Document
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oBOMNumb = oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		If oSamenstelling.Value = "" Then
			oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		End If
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oCustom("TekNum").Value, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

0 Likes

I write the code intuitively without conducting tests, so there are children's mistakes in it, I hope everything will work well now.

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try
	
	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
		Dim oDoc As Document = oCompDef.Document
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oBOMNumb = oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		If oSamenstelling.Value = "" Then
			oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		End If
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oCustom("TekNum").Value, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

Message 7 of 18

S.vanderBrug
Contributor
Contributor

I am starting to feel like a spoiled child now, but it throws me an error on line 31 now. It also did in the previous one, but I managed to circumvent it then by throwing out the "BOM Number" values.

line 31, it gives "(Exception from HRESULT: 0x80004005 (E_FAIL))"

0 Likes

I am starting to feel like a spoiled child now, but it throws me an error on line 31 now. It also did in the previous one, but I managed to circumvent it then by throwing out the "BOM Number" values.

line 31, it gives "(Exception from HRESULT: 0x80004005 (E_FAIL))"

Message 8 of 18

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

I think this is due to the fact that your assembly contains components that cannot be edited, for example: content center components. I added a check (line 23), if it doesn't help then put the code section (lines 31 - 36) in a Try-Catch.

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try

	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		Dim oDoc As Document = oCompDef.Document
		If Not oDoc.IsModifiable Then Continue For
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oBOMNumb = oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oCustom("TekNum").Value, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

I think this is due to the fact that your assembly contains components that cannot be edited, for example: content center components. I added a check (line 23), if it doesn't help then put the code section (lines 31 - 36) in a Try-Catch.

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document	
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try

	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2) 'Structured view
	
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum, 0)
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,
										ByVal oTekNum As String,
										ByRef indent As Integer)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		Dim oDoc As Document = oCompDef.Document
		If Not oDoc.IsModifiable Then Continue For
		
		'by multiplying by two, the itemnumber dodges the periods in the expanded part number.
		Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2) 
		
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oBOMNumb = oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, oCustom("TekNum").Value, indent+1)
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

Message 9 of 18

S.vanderBrug
Contributor
Contributor

That does it! Did not need to add the try-catch, but I'll keep it in mind in case it errors with a slightly different situation or something. Thanks a bunch, this saved me loads of time and headaches ๐Ÿ˜„

That does it! Did not need to add the try-catch, but I'll keep it in mind in case it errors with a slightly different situation or something. Thanks a bunch, this saved me loads of time and headaches ๐Ÿ˜„

Message 10 of 18

S.vanderBrug
Contributor
Contributor

While I still have you here: I have shot myself in the foot a bit with how I index which BOMItemNumber it needs to pick.

 

When the item number reaches double digits, it no longer chooses the correct one(or well, two in that case), do you have any ideas on how to fix this?

Attached a picture that hopefully makes clear what's going on.

As you can see, there's also double digits in the deeper ItemNumbers again.

 

ItemNumber mistake.png

 

0 Likes

While I still have you here: I have shot myself in the foot a bit with how I index which BOMItemNumber it needs to pick.

 

When the item number reaches double digits, it no longer chooses the correct one(or well, two in that case), do you have any ideas on how to fix this?

Attached a picture that hopefully makes clear what's going on.

As you can see, there's also double digits in the deeper ItemNumbers again.

 

ItemNumber mistake.png

 

Message 11 of 18

Andrii_Humeniuk
Advisor
Advisor

To be honest, I can't figure out exactly what kind of description you want in oBOMItemNumber. This line of code is responsible for it:

Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2)

 Please describe the logic and the final result. For example, position "10.9" - description "999100_120 POS10.9".

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

0 Likes

To be honest, I can't figure out exactly what kind of description you want in oBOMItemNumber. This line of code is responsible for it:

Dim oBOMItemNumber As String = oBOMRow.ItemNumber(indent*2)

 Please describe the logic and the final result. For example, position "10.9" - description "999100_120 POS10.9".

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

Message 12 of 18

S.vanderBrug
Contributor
Contributor
ahh, apologies. I want it to display what the item number is of that component, in the assembly it is in. So if you look at the pic, the top one is correct, showing POS9 as it is item 9 in the structure.
The one below it shows POS1, as it only selected the first digit of ItemNumber "10". And because I simply multiplied the indent number by two to pick the right number, all other ones are off after this one too.
0 Likes

ahh, apologies. I want it to display what the item number is of that component, in the assembly it is in. So if you look at the pic, the top one is correct, showing POS9 as it is item 9 in the structure.
The one below it shows POS1, as it only selected the first digit of ItemNumber "10". And because I simply multiplied the indent number by two to pick the right number, all other ones are off after this one too.
Message 13 of 18

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

OK. Try this:

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try

	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2)
	
	Dim oTM As Transaction = oInvApp.TransactionManager.StartTransaction(oAsmDoc, "CheckAndSetProps")
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum)
	oTM.End()
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,	ByVal oTekNum As String)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		Dim oDoc As Document = oCompDef.Document
		If Not oDoc.IsModifiable Then Continue For
		
		Dim oListNumb() As String = oBOMRow.ItemNumber.Split(".")
		Dim oBOMItemNumber As String = oListNumb(oListNumb.Length-1)
		
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oBOMNumb = oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Dim sTek As String = ""
			Try : sTek = oCustom("TekNum").Value : Catch : End Try
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, sTek)
		End If
	Next
End Sub

 

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

0 Likes

OK. Try this:

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oCustom As PropertySet = oAsmDoc.PropertySets.Item("Inventor User Defined Properties")
	Dim oTekNum As String
	Try : oTekNum = oCustom("TekNum").Value
	Catch : oTekNum = "" : End Try

	Dim oBOM As BOM = oAsmDef.BOM	
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews(2)
	
	Dim oTM As Transaction = oInvApp.TransactionManager.StartTransaction(oAsmDoc, "CheckAndSetProps")
	Call RecursiveCheckAndSetProps(oBOMView.BOMRows, oTekNum)
	oTM.End()
End Sub

Private Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator,	ByVal oTekNum As String)
	For Each oBOMRow As BOMRow In oRowsElements
		Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
		Dim oDoc As Document = oCompDef.Document
		If Not oDoc.IsModifiable Then Continue For
		
		Dim oListNumb() As String = oBOMRow.ItemNumber.Split(".")
		Dim oBOMItemNumber As String = oListNumb(oListNumb.Length-1)
		
	    Dim oCustom As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oBOMNumb, oSamenstelling As Inventor.Property
		
		Try : oBOMNumb = oCustom("BOM Number") : oBOMNumb.Value = oBOMItemNumber
	    Catch : oBOMNumb = oCustom.Add(oBOMItemNumber, "BOM Number") : End Try
		
		Try : oSamenstelling = oCustom("Samenstelling")
		Catch : oSamenstelling = oCustom.Add("", "Samenstelling") : End Try
		oSamenstelling.Value = oTekNum & " POS" & oBOMNumb.Value
		
		If Not oBOMRow.ChildRows Is Nothing Then
			Dim sTek As String = ""
			Try : sTek = oCustom("TekNum").Value : Catch : End Try
			Call RecursiveCheckAndSetProps(oBOMRow.ChildRows, sTek)
		End If
	Next
End Sub

 

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

Message 14 of 18

S.vanderBrug
Contributor
Contributor

Sorry, line 15 throws an error : "Object reference not set to an instance of an object".
I did try to put some "try, catch"commands in, here and there, but couldnt get the errors out that way.
Again thanks a bunch man, I really do appreciate it, and hope I'm not annoying ya yet..

 

edit: changed line 14 to 15, my bad.

0 Likes

Sorry, line 15 throws an error : "Object reference not set to an instance of an object".
I did try to put some "try, catch"commands in, here and there, but couldnt get the errors out that way.
Again thanks a bunch man, I really do appreciate it, and hope I'm not annoying ya yet..

 

edit: changed line 14 to 15, my bad.

Message 15 of 18

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Again, I made a childish mistake. In line 2, it should be as follows:

Dim oInvApp As Inventor.Application = ThisApplication

Line 15 is Transaction, all actions and changes made by the code are packed into one change, so you can return everything by pressing Ctrl+Z.

Trans.png

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

0 Likes

Again, I made a childish mistake. In line 2, it should be as follows:

Dim oInvApp As Inventor.Application = ThisApplication

Line 15 is Transaction, all actions and changes made by the code are packed into one change, so you can return everything by pressing Ctrl+Z.

Trans.png

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

Message 16 of 18

S.vanderBrug
Contributor
Contributor
That seems to work like a charm now! Thanks a million!
At this point it's straight sorcery to me though XD
Do you have any suggestions on how or where to learn this?
If not thats fine too, you have already helped me greatly ๐Ÿ™‚

That seems to work like a charm now! Thanks a million!
At this point it's straight sorcery to me though XD
Do you have any suggestions on how or where to learn this?
If not thats fine too, you have already helped me greatly ๐Ÿ™‚
Message 17 of 18

Andrii_Humeniuk
Advisor
Advisor

If you want to program in iLogic, I would first of all recommend that you learn the VB.NET language and the Inventor API well (link, link). Also, I am sure that reading articles on the forum will help you better understand the work of the code in Inventor. May the force be with you!

 

Please mark some of my answers as solutions, this will help other users and encourage me to help others. Thank you.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

If you want to program in iLogic, I would first of all recommend that you learn the VB.NET language and the Inventor API well (link, link). Also, I am sure that reading articles on the forum will help you better understand the work of the code in Inventor. May the force be with you!

 

Please mark some of my answers as solutions, this will help other users and encourage me to help others. Thank you.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

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

EESignature

Message 18 of 18

S.vanderBrug
Contributor
Contributor
Thanks bunches! I never quite knew what to search for, so I'd just end up at the ilogic help stuff. So far, forum has pulled me through quite a bunch, but this certainly will help me better write my own stuff, aswell as more accurately ask questions ๐Ÿ™‚

Thanks bunches! I never quite knew what to search for, so I'd just end up at the ilogic help stuff. So far, forum has pulled me through quite a bunch, but this certainly will help me better write my own stuff, aswell as more accurately ask questions ๐Ÿ™‚

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

Post to forums  

Autodesk Design & Make Report