- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
OK. Give this one a try. I modified it to suit what I'm seeing in that last image you posted. It looks like each document in that assembly should have two custom iProperties. One named "SQ_INCH" that contains a Double value in inches, and one named "TOTAL_SQ_INCH" which you want to contain a Double value in inches that is the product of multiplying the document's 'Item Quantity' by the value of the "SQ_INCH" custom iProperty. This code is targeting the structured view of the BOM, while it is set to the 'all levels' setting. And it is set-up to iterate down through any 'child rows' it may find under any BOM rows. Since these edits are being done at the document levels, these changes should also affect the contents of the 'Parts Only' view of the BOM. So there should not be any need to also attempt to run that code on the Parts Only view. On the other hand, if you only want these edits to effect parts, and not attempt these edits on any sub-assembly documents, you may want to only run this on the Parts Only view, in which case there should not be any 'child rows'.
I also added a lot more information to a message in the Catch side of those Try...Catch blocks, to help you understand when things are not working as planned, and where it may be having those problems.
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
Try
oRowDoc = oRow.ComponentDefinitions.Item(1).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 oAREA As Double = 0.0
Dim oCProps As PropertySet = oBOMRowDoc.PropertySets.Item("Inventor User Defined Properties")
Try
oAREA = oCProps.Item("SQ_INCH").Value
Catch
MsgBox("Problem getting value of custom iProperty named 'SQ_INCH', from" & vbCrLf & _
"BOMRow Item Number " & oRow.ItemNumber, vbExclamation, "")
Continue For 'skip to next BOMRow
End Try
If oAREA = 0.0 Then Continue For 'skip to next BOMRow
Dim oItemQty As Integer = oRow.ItemQuantity
Dim oTotalArea As Double = (oAREA * oItemQty)
'value may need to have units converted if not correct
Dim oTotalSQInchProp As Inventor.Property = Nothing
Try
oTotalSQInchProp = oCProps.Item("TOTAL_SQ_INCH")
oTotalSQInchProp.Value = oTotalArea
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(oTotalArea, "TOTAL_SQ_INCH")
End Try
If Not oRow.ChildRows Is Nothing Then
MultiplyBOMColumnValues(oRow.ChildRows)
End If
Next
End Sub
Wesley Crihfield
(Not an Autodesk Employee)