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

Hi @ABHUT_9090.  I'm glad that you got the document level quantity information set-up correctly now, and that it is now showing proper value for quantity in the BOM.  I still do not understand why my code would be creating text type custom iProperties in your parts with an empty value, if the part already contained a custom iProperty with the same exact name that was set to a numerical value of zero.  The code is supposed to be checking for the existence of that custom iProperty by its name first, and if found, set its value to the calculated value gathered within the code.  Then if it is not found, it is set to create it with that exact name, but with the numerical value we gathered within the code.  Both of the variables I am using within the code (oArea & oTotalArea) are both defined as Double, not String.  The value of the oArea variable was pulled in from the custom iProperty called "SQ_INCH", so if the value of that custom iProperty was a String instead, that may have cause a problem.  I only checked if its value was zero, and if so, told it to skip to the next BOM row.

 

Anyways, here is another attempt at the iLogic code for populating the value of that custom iProperty named "TOTAL_SQ_INCH" with the value of the Unit Quantity x Item Quantity.  The Unit Quantity always returns a String, so I just have to check to see if it equals "Each", then if not, we have to attempt to extract just the numerical part of that String, so that we can multiply by it. 

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)
	'these edits were done in the documents, so they should also affect the Parts Only view
	'Dim oPartsOnlyView As BOMView = oBOM.BOMViews.Item("Parts Only")
	'MultiplyBOMColumnValues(oPartsOnlyView.BOMRows)
	oADoc.Update
End Sub

Sub MultiplyBOMColumnValues(oBOMRows As BOMRowsEnumerator)
	For Each oRow As BOMRow In oBOMRows
		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(oBOMRowDoc) Then Continue For
		Dim oCProps As PropertySet = oBOMRowDoc.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
		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")
			oTotalSQInchProp.Value = oTotalSqInches
		Catch
			MsgBox("A custom iProperty named 'TOTAL_SQ_INCH' was not found in" & vbCrLf & _
			"the document associated with BOMRow Item Number " & oRow.ItemNumber & vbCrLf & _
			"So the rule will attempt to create it.", vbExclamation, "")
			oTotalSQInchProp = oCProps.Add(oTotalSqInches, "TOTAL_SQ_INCH")
		End Try
		If Not oRow.ChildRows Is Nothing Then
			MultiplyBOMColumnValues(oRow.ChildRows)
		End If
	Next
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)