Item Number to iProperty

Item Number to iProperty

CCarreiras
Mentor Mentor
2,489 Views
14 Replies
Message 1 of 15

Item Number to iProperty

CCarreiras
Mentor
Mentor

HI!

I'm trying to find a way to catch the Item number (structured BOM) and create an iProperty in every assembly's parts.

 

I'm using the code below and it works really good for assemblies in a "master LOD", and without Content center and/or library parts.

These are my two problems:

1- First issue is, i need a custom LOD, because i run other rules to make active(or not) some parts.

If i use a custom LOD, the rule doesn't work at all.

A) Any idea to put this code running in a custom LOD?

 

2- Also, i have some parts from the library, and besides the rule works in the normal parts, it stops when finding a non-modifiable part and an error box appear eventually.

The questions are:

A) how can i avoid the error message?

(I tried the " on error resume", but it's incompatible with the "try" statement).

(Also tried the "if the part is modificable= true then", but i think this will not work for this case).

 

 

Sub Main
Dim oAssemblyDocument As AssemblyDocument
oAssemblyDocument = ThisDoc.Document

Dim oAssemblyComponentDefinition As AssemblyComponentDefinition
oAssemblyComponentDefinition = oAssemblyDocument.ComponentDefinition

Dim oBOM As BOM
oBOM = oAssemblyComponentDefinition.BOM
oBom.StructuredViewEnabled = True
Dim oBOMView As BOMView
oBOMView = oBom.BOMViews(2) 'Structured view
oBom.StructuredViewFirstLevelOnly = False
oBom.StructuredViewDelimiter = "."

Call RecursiveCheckAndSetProps(oBOMView.BOMRows)

End Sub

Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator)

        For Each oBOMRow As BOMRow In oRowsElements
            Dim oComponentDefinition As ComponentDefinition
            oComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)

            Dim oBOMItemNumber As String
            oBOMItemNumber = oBOMRow.ItemNumber() 'this is item number in the BOM
           ' MessageBox.Show(oBOMItemNumber, "BOM Number") 'just to show what's going on
            Dim oComponentDefinitionPropertySet As PropertySet
            oComponentDefinitionPropertySet = oComponentDefinition.Document.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
            'creates the custom property and inputs the value
            If Not oBOMRow.ChildRows Is Nothing Then
				Call RecursiveCheckAndSetProps(oBOMRow.ChildRows)
            End If
        Next
End Sub

 

CCarreiras

EESignature

0 Likes
Accepted solutions (1)
2,490 Views
14 Replies
Replies (14)
Message 2 of 15

bradeneuropeArthur
Mentor
Mentor

Hi with this you can trace the Content Center Files:

 

Public Sub main()

Dim a As Application
a = ThisApplication

Dim b As PartDocument
b = a.ActiveDocument

Dim ps As PropertySet
Dim p As Property
For Each ps In b.PropertySets
For Each p In ps
If p.DisplayName = "Categories" Then

If p.Expression = "" Then
Debug.Print p.DisplayName + "  " + p.Expression
'!RUN YOUR CODE HERE
End If

End If
Next
Next
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 15

CCarreiras
Mentor
Mentor

HI!

@bradeneuropeArthur , I dont need the list of library parts, but Thank you for your tip.

 

The rule is meant to set an ipropertie in every assembly part and sub, based on the bom item number.

It Runs ok unless: you have library parts and/or use a different LOD.

I just want to add the code that possibility: to have library parts and use another lod level.

 

 

 

CCarreiras

EESignature

0 Likes
Message 4 of 15

Sergio.D.Suárez
Mentor
Mentor

Hi, try this code.

Sub Main
Dim oAssemblyDocument As AssemblyDocument
oAssemblyDocument = ThisDoc.Document

Dim oAssemblyComponentDefinition As AssemblyComponentDefinition
oAssemblyComponentDefinition = oAssemblyDocument.ComponentDefinition

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

Call RecursiveCheckAndSetProps(oBOMView.BOMRows,0)

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() 'this is item number in the BOM
			
				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

 I added a subroutine, in it to separate it from the rest of the code uses the instruction "Try". In the previous subroutine you can now use "on error resume next"
I hope the code will be useful. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 5 of 15

bradeneuropeArthur
Mentor
Mentor

Hi,

 

My piece of code was meant to be able to skip all library parts!

 

Regards,

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 6 of 15

kwalker1
Enthusiast
Enthusiast

Hi All,

I'm very much having the same issue as ccarreiras in regards to the code not working if the model is set to a custom level of detail (LoD). It only works when the 3D model is set to the master LoD.

 

This seems odd because I have always found the BOM Dialogue Box to function as though it was the master LoD irrespective of the 3D model's actively set custom LoD. The Structured and Parts Only tabs seemed to display all items from the master model except those set with a Phantom or Reference BOM Structure.

 

Can anyone assist us in getting the code to work for 3D models set to a custom LoD?

I have been trying to find a solution but have been unsuccessful.

 

Many thanks in advance.

0 Likes
Message 7 of 15

CCarreiras
Mentor
Mentor

@Sergio.D.Suárez , your code works well... without library parts and/or in master LOD.

 

With other Custom LOD and/or libray parts, the behavior is the same... error.

 

Thank you anyway.

CCarreiras

EESignature

0 Likes
Message 8 of 15

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Correct me if I'm wrong. From what I understood, the LODs do not serve to filter PartsList. To filter the PartsList we have the "DesignViewRepresentations". I clarify this because when we have a component suppressed in a LOD when we activate and save this LOD, we will not see changes in the Item parts of the structured BOM. We see this reflected in the partsList when creating a drawing.
So if you want to access the BOM and then copy the item number to each component, I think you should first capture the active LOD, then change to the master LOD and execute the steps of the rule, and finally activate the LOD captured. This way you would avoid errors because the error is generated when you want to activate the BOM from the active LOD.

 

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)

'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() 'this is item number in the BOM
			
				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

 I suppose there could be an alternative way, which would be to save the assembly before executing the rule from the LOD but I have not tried that.
I hope this helps with your problem. I send a cordial greeting.


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 9 of 15

CCarreiras
Mentor
Mentor

@Sergio.D.Suárez  you're totally right.

I only use the "iLogic" LOD because i need to suppress some parts for certain cases.

I also thought about change temporarily the LOD like You suggested: set the LOD to master before running the rule and reestablish the correct "iLogic" LOD after running this rule.

 

My main concern is the errors due to the library and content center parts (non-modifiable parts).

 

Thanks.

CCarreiras

EESignature

0 Likes
Message 10 of 15

Sergio.D.Suárez
Mentor
Mentor

For that effect that you describe I have placed the "on error resume next", when it searches through the occurrences, if it finds an error it skips the occurrence, does not do anything about it and returns to the next occurrence. Previously gave the code error because no command was specified to avoid error above, in access to the BOM.
I hope you try the code and show us examples of the errors found, to be able to continue investigating the best method to solve the problem. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 11 of 15

CCarreiras
Mentor
Mentor

@Sergio.D.Suárez ... wow... it works like a charm!!!

Already tested in all conditions i need, and so far... it´s running very well.

Thank you!!

 

 

 

CCarreiras

EESignature

0 Likes
Message 12 of 15

Sergio.D.Suárez
Mentor
Mentor

If an error appears we continue investigating, these examples are very good because we learn between everyone giving a different contribution and sometimes we get a good result, greetings!


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 13 of 15

kwalker1
Enthusiast
Enthusiast

Thanks very much Sergio Daniel Suárez for the solution and assistance. It works great. Much appreciated.

On my way home yesterday, I did think of a similar solution of changing the LoD before running the code then reverting it back to the original custom LoD but was concerned with it negatively effecting the load time of the models.

Your solution works perfectly by changing the LoD only in memory which has no ill effects on load times.

Thanks again.

 

In regards to LoD's, keep in mind that when iLogic is used to suppress a component, it effectively performs two functions. Firstly it overrides the BOM Structure for that occurrence to reference then it suppresses the component.

Hence when a component is then manually unsuppressed, the component still retains the BOM Structure override which is why you don't see it in the parts list. You would need to remove the BOM Structure override (by right clicking on the component in the browser tree and selecting the BOM Structure back to default) for it to reappear in the parts list.

Message 14 of 15

CCarreiras
Mentor
Mentor

 


@Sergio.D.Suárez wrote:

If an error appears we continue investigating, these examples are very good because we learn between everyone giving a different contribution and sometimes we get a good result, greetings!


It's running without errors. For my case,  it's perfect!!!

It's a very complete code to do the job.

Thank you.

CCarreiras

EESignature

Message 15 of 15

Support_Charlies3DT
Contributor
Contributor

@CCarreiras 

 

Hello, we are launching a Numbering app this week. I was wondering if you would be interested in testing it? The app is capable of renaming a lot of properties, numbering parts and assemblies and many more things. 

 

You can send me a PM if interested!

0 Likes