Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: ABHUT_9090

Apparently in the last code I posted, I was using 2 different variables to represent the BOMRow's document, by mistake (oBOMRowDoc & oRowDoc).  That was likely the main problem.

 

Since you now have the Item Quantity and the Unit Quantity set up properly, and seem to be getting the proper value in the regular Quantity column, it looks like you just need the value shown in that Quantity column to be copied over to the TOTAL_SQ_INCH column.  However, there does not appear to be a Property of the BOMRow object for that value shown in the "QTY" column, so we have to get it the hard way...by attempting to multiply the Unit Quantity (which is returned as a String) by the Item Quantity (which is an Integer representing number of components).  Since you can not multiply a String by an Integer, I am using the Val() method in an attempt to extract the Double value from the String, within the multiplication line.  Then I attempt to find the custom iProperty named "TOTAL_SQ_INCH".  If that is not found, instead of automatically attempting to create it, I changed the code to let the user know about it with a message, then skip to the next BOM row.  If that custom iProperty is found, the code now attempts to find a matching Parameter.  (I was not sure if you had one or not, or if it was set to exposed/exported.)  If it finds that parameter, it will attempt to update its value, which would also automatically update the associated custom iProperty (won't work in the other direction).  If the iProperty was found, but the parameter was not found, it then just attempts to update the value of the custom iProperty.

 

See if this version works for you now.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oBOM As BOM = oADef.BOM
	oBOM.PartsOnlyViewEnabled = True
	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oStructuredView As BOMView = oBOM.BOMViews.Item("Structured")
	MultiplyBOMColumnValues(oStructuredView.BOMRows)
	oADoc.Update
End Sub

Sub MultiplyBOMColumnValues(oBOMRows As BOMRowsEnumerator)
	For Each oRow As BOMRow In oBOMRows
		If Not oRow.ChildRows Is Nothing Then
			MultiplyBOMColumnValues(oRow.ChildRows)
		End If
		Dim oRowDoc As Document = Nothing
		Dim oCD As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
		Try
			oRowDoc = oCD.Document
		Catch oEx As Exception
			MsgBox("Failed to get Document from BOMRow Item Number " & oRow.ItemNumber, vbExclamation, "")
			Continue For 'skip to next BOMRow
		End Try
		If IsNothing(oRowDoc) Then Continue For
		Dim oCProps As PropertySet = oRowDoc.PropertySets.Item("Inventor User Defined Properties")
		'UnitQuantity is derived from BaseUnits & BaseQuantity
		Dim oUnitQty As String = oCD.BOMQuantity.UnitQuantity
		Dim oItemQty As Integer = oRow.ItemQuantity
		'can not access value in "QTY" column directly...no BOMRow property for it
		Dim oTotalSqInches As Double = 0.0
		'if oUnitQty is set to "Each", this will either fail, or result in wrong result
		'can't multiply a String by an Integer, so attempting to extract number from String
		If oUnitQty = "Each" Then Continue For 'skip to next BOMRow
		Try
			oTotalSqInches = (Val(oUnitQty) * oItemQty)
		Catch
			MsgBox("Multiplying Unit Quantity x Item Quantity Failed!", vbExclamation, "")
			Continue For 'skip to next BOMRow
		End Try
		Dim oTotalSQInchProp As Inventor.Property = Nothing
		Try
			oTotalSQInchProp = oCProps.Item("TOTAL_SQ_INCH")
		Catch
			MsgBox("A custom iProperty named 'TOTAL_SQ_INCH' was not found in" & vbCrLf & _
			"the document associated with BOMRow Item Number " & oRow.ItemNumber, vbExclamation, "")
			Continue For 'skip to next BOMRow
		End Try
		If Not IsNothing(oTotalSQInchProp) Then 'if the iProperty was found...
			'check if there is a matching Parameter, if so, edit its value instead of the iProperty
			Dim oParams As Inventor.Parameters = oRowDoc.ComponentDefinition.Parameters
			Dim oParam As Inventor.Parameter = Nothing
			Try
				oParam = oParams.Item("TOTAL_SQ_INCH")
			Catch 'it was not found, so do nothing
			End Try
			If IsNothing(oParam) Then 'param not found, so edit iProperty value
				oTotalSQInchProp.Value = oTotalSqInches
			Else 'the parameter was found, so update its value/expession
				oParam.Expression = oTotalSqInches.ToString
				'which should also update the matching iProperty's value, if it is set to exported/exposed
			End If
		End If
	Next
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)