Accessing Item Number Values At Assembly Level

Accessing Item Number Values At Assembly Level

amarinXG8V6
Advocate Advocate
4,000 Views
15 Replies
Message 1 of 16

Accessing Item Number Values At Assembly Level

amarinXG8V6
Advocate
Advocate

Hi everyone,

 

I would like to be able to the item number value that Inventor assigns to components in an assembly and feed these values down to the part level. Either to a parameter or i-property that I can then access at the part level and use as a text value to etch that value onto a part. Is this doable?

amarinXG8V6_0-1647035196073.png

amarinXG8V6_1-1647035512897.png

 

0 Likes
Accepted solutions (1)
4,001 Views
15 Replies
Replies (15)
Message 2 of 16

dalton98
Collaborator
Collaborator
Accepted solution

Getting the Item number is easy idk about creating an etch mark

Dim oAssyDoc as AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

Dim
oBOM As BOM oBOM = oAssyDoc.ComponentDefinition.BOM
oBOM.PartsOnlyViewEnabled = True Dim oBOMView As BOMView oBOMView = oBOM.BOMViews.Item("Parts Only") Dim oRow As BOMRow For Each oRow In oBOMView.BOMRows Dim invCustomPropertySet As PropertySet Try invCustomPropertySet = oRow.ComponentDefinitions.Item(1).Document.PropertySets.Item("Inventor User Defined Properties") invProperty = invCustomPropertySet.Add(oRow.ItemNumber, "ITEM") Catch End Try Next
Message 3 of 16

amarinXG8V6
Advocate
Advocate
Would the code that you described above only exist at the assembly level or would it also need to exist at the part level? Also would this type of code be best written and stored in an external rule that could be triggered and used by multiple template assembly files as opposed to it residing in one specific assembly? Thank you for the help thus far!
0 Likes
Message 4 of 16

dalton98
Collaborator
Collaborator

You just need to run it at the assembly level. It creates/updates a custom i-property named "ITEM" for each part in "Parts Only" BOM. If you are using the same part in multiple assemblies you would have to run the rule again to override the newly created "ITEM" value. Hope this helps.

Message 5 of 16

amarinXG8V6
Advocate
Advocate

That helped! It worked. Now I'm trying to take use the FileName as well as the value stored under the ITEM custom iProperty and feed it to a parameter. I tried the following but I'm getting an error. I know it has to do with the syntax but how do I make the string combination read out as text in a parameter? I'm getting a conversion from string to type 'Double' error which I'm guessing has to do with the syntax I'm using.

 

 

Snip of Code:

 

FileName=ThisDoc.FileName(False) 'without extension

PartTag = FileName - iProperties.Value("Custom", "ITEM")

iLogicVb.UpdateWhenDone = True

 

amarinXG8V6_0-1647281173016.png

 

 

amarinXG8V6_1-1647281201100.png

 

0 Likes
Message 6 of 16

WCrihfield
Mentor
Mentor

This question may be a bit too obvious, but is that Parameter named "PartTag" a numerical parameter, or a text type user parameter?  (It must be a text type user parameter for this to work.)  When combining two String's together into one String in a line, you should use the '&' symbol between the first and next Strings.  Using the '-' symbol without it being within quotes, invokes a mathematical operation.  And since the iProperties call returns a generic 'Object' rather than a specific data Type, it is thinking that you are trying to do math between a String and an Object, which is not possible.

Try it like this:

FileName = ThisDoc.FileName(False)
'if you are expecting a String, then predefine the variable's Type as a String
Dim oItem As String = iProperties.Value("Custom", "ITEM")
'use '&' between Strings to join them
PartTag = FileName & oItem

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 16

dalton98
Collaborator
Collaborator

If it's a parameter your after then replace the original code that creates an i-property to this:

(Sorry about the Try/Catch its ugly but idk another way to do it)

Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

Dim oBOM As BOM
oBOM = oAssyDoc.ComponentDefinition.BOM
oBOM.PartsOnlyViewEnabled = True

Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Parts Only")

Dim oRow As BOMRow
For Each oRow In oBOMView.BOMRows
	Dim oPart As PartDocument
	oPart = oRow.ComponentDefinitions.Item(1).Document
	Dim userParams As UserParameters
	userParams = oPart.ComponentDefinition.Parameters.UserParameters
	Try
	itemParam = userParams.Item("PartTag")
	itemParam.Delete
	Catch
	End Try
	Try
	itemParam = userParams.AddByValue("PartTag", Left(oPart.DisplayName, oPart.DisplayName.Length - 4) & "-" & oRow.ItemNumber, UnitsTypeEnum.kTextUnits)
	Catch
	End Try
Next

 

Message 8 of 16

amarinXG8V6
Advocate
Advocate
That worked great! Yes, the PartTag parameter is a text type. Thank you for the description on what is causing the issue with the syntax.
0 Likes
Message 9 of 16

amarinXG8V6
Advocate
Advocate
This could be an interesting way to do it as well. I see the benefit with this as I don't have to have the code create a custom i-property when I already have the PartTag parameter created as the "bucket" to catch the ITEM value from the assembly. Would there be a way to take the ITEM value and the FileName and feed them both into the PartTag parameter as a text string without the need for the custom ITEM i-property? Essentially merging the 2 portions of code that you shared along with what @WCrihfield shared?
0 Likes
Message 10 of 16

WCrihfield
Mentor
Mentor

Hi @amarinXG8V6.  Actually, it looks like the last code that @dalton98 posted was attempting to incorporate the file name into the parameter, without using the iProperty, so it kind of sounds like that might have worked the way you wanted.  This code below is basically just another very similar way of doing what his code was attempting to do, but I'm getting the file name a little differently, and avoiding use of the two Try...Catch blocks, in favor of a loop scenario.  We have just assumed you wanted to go through 'Parts Only' view of the BOM because that is the tab being shown in your original image.  If that needs to change too, that's not that difficult to change.

Dim oADoc As AssemblyDocument = ThisDoc.Document
oBOM = oADoc.ComponentDefinition.BOM
oBOM.PartsOnlyViewEnabled = True
oBOMView = oBOM.BOMViews.Item("Parts Only")
For Each oRow As BOMRow In oBOMView.BOMRows
	Dim oPartDoc As PartDocument = oRow.ComponentDefinitions.Item(1).Document
	'get file name, without path or file extension
	oFileName = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
	oItemNum = oRow.ItemNumber
	oParamValue = oFileName & "-" & oItemNum
	'now try to find the parameter within this part
	oUParams = oPartDoc.ComponentDefinition.Parameters.UserParameters
	Dim oPartTagParam As UserParameter = Nothing
	For Each oParam As Inventor.Parameter In oParams
		If oParam.Name = "PartTag" Then
			oPartTagParam = oParam
			oPartTagParam.Value = oParamValue
			Exit For
		End If
	Next
	If oPartTagParam Is Nothing Then
		oPartTagParam = oUParams.AddByValue("PartTag", oParamValue, UnitsTypeEnum.kTextUnits)
	End If
Next

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 16

amarinXG8V6
Advocate
Advocate

@WCrihfield 

 

That worked! One last question on this string. What if instead of gathering the filename I wanted to access the Project: Project i-property and combine that with the Part Only value from the assembly in a string. I assume I would chance the syntax for the portion of the code listed below. What would that syntax look like? Originally the intent was that the filename would be unique on a per project basis but based on my time studies, it would be better if we had a portion of the structural frame already modeled which would lend itself to more generic file names as opposed to project specific file names. That is why instead I think it would be best to use the Project: Project iProperty as a way to define which project the tube parts are associated with.

 

amarinXG8V6_0-1647355707490.png

 

Thank you,

Alex

 

0 Likes
Message 12 of 16

WCrihfield
Mentor
Mentor

Hi @amarinXG8V6.  If you want to replace file name with project iProperty value, we can do that fairly easily too.  I would leave the line in place that gets the document object, because we can still use that to get the iProperty within that document.

Just replace this line:

oFileName = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)

with this line:

Dim oProject As String = oPartDoc.PropertySets.Item("Design Tracking Properties").Item("Project").Value

Then change this line:

oParamValue = oFileName & "-" & oItemNum

to something like this:

oParamValue = oProject & "-" & oItemNum

Beware though...you may want to check to see if the Project iProperty has been filled in or not, just in case it is empty.  I don't know if that would cause any problems if it were empty.  To do so, you could just check if oProject = "".  Then if it is empty, you could show yourself a message or use the iLogic Logger to write something to the log.  Also beware of any 'virtual' components you may have in your BOM (if any), because they may not have a Document, which might cause an error.

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 16

amarinXG8V6
Advocate
Advocate

@WCrihfield 

 

I have updated the code to the following listed below, however when I run the rule I get the following error. Have I written something incorrectly?

 

amarinXG8V6_1-1647363425716.png

amarinXG8V6_2-1647363436422.png

 

 

 

amarinXG8V6_0-1647363357193.png

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
oBOM = oADoc.ComponentDefinition.BOM
oBOM.PartsOnlyViewEnabled = True
oBOMView = oBOM.BOMViews.Item("Parts Only")
For Each oRow As BOMRow In oBOMView.BOMRows
	Dim oPartDoc As PartDocument = oRow.ComponentDefinitions.Item(1).Document
	'get file name, without path or file extension
	Dim oProject As String = oPartDoc.PropertySets.Item("Design Tracking Properties").Item("Project").Value
	oItemNum = oRow.ItemNumber
	oParamValue = oProject & "-" & oItemNum
	'now try to find the parameter within this part
	oUParams = oPartDoc.ComponentDefinition.Parameters.UserParameters
	Dim oPartTagParam As UserParameter = Nothing
	For Each oParam As Inventor.Parameter In oParams
		If oParam.Name = "PartTag" Then
			oPartTagParam = oParam
			oPartTagParam.Value = oParamValue
			Exit For
		End If
	Next
	If oPartTagParam Is Nothing Then
		oPartTagParam = oUParams.AddByValue("PartTag", oParamValue, UnitsTypeEnum.kTextUnits)
	End If
Next

 

0 Likes
Message 14 of 16

WCrihfield
Mentor
Mentor

I'm not sure what might be causing that error.  Are there any virtual components in your assembly?  Are you using any non-Master ModelStates settings?  It seems like somewhere along the way, some variable is not getting a value set to it, then some other code is trying to use the variable without it having a value (or something like that).  You will have to do some 'debug' type actions to help determine specifically where (what line of code) within the rule the error is being thrown.  There are several ways to do that. (1)using the iLogic Logger to record a message between each action or line of code (2)putting something line MsgBox(1), MsgBox(2), MsgBox(3)... between each line of code, then watch the latest number message to show...the error is happening right after the last numbered message, (4) use Try...Catch blocks where something might possibly fail, with a message in the Catch part.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 15 of 16

Support_Charlies3DT
Contributor
Contributor

@amarinXG8V6 

 

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
Message 16 of 16

dschulteHR4D5
Advocate
Advocate

I am playing around with this code for similar function like the OP posted for. I am finding that this code you posted does work, but when ran on an assembly that already has a value in the ITEM property, it will not update to the assembly.

 

So for a part that is shared, in ASM1 say Prt11 is ITEM=3, run rule, sets ITEM to 3, within ASM2 the same Prt11 is item 6 due to sorting in the BOM, run rule and it will leave the original value at 3, will not update to the current assembly value of 6. Any ideas?

0 Likes