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 ๐
Solved! Go to Solution.
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 ๐
Solved! Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
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.
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.
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 ๐
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 ๐
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.
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.
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..
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..
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.
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.
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))"
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))"
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.
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.
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 ๐
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Can't find what you're looking for? Ask the community or share your knowledge.