- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Parts variables and assembly variable multiplication in BOM
Hello,
In the BOM , I want multiplication of column1 (lengths of parts) and column2 (Item qty of that pats) in another column3( total length required ). Can anybody help me with the ilogic code for this? Main problem is how can we multiply this two column and get output in third column.
Thank you
Ankit Bhut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @ABHUT_9090. This seems like a fairly common type of request. I'm pretty sure it can be accomplished. What is the source for the information in those first two columns? Do they both contain custom iProperties? If one or both are one of the regular BOM quantity specifiers, which type? There are several types of quantities involved in BOM's: Unit Quantity, Base Quantity, Base Unit, Item Quantity (net/filtered), Total Quantity (gross/not filtered). Do you plan on using a custom iProperty for column 3, or is it representing something else? If that custom iProperty does not exist in every component document, then do you want it created within every unique document represented by those components?
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello @WCrihfield , yes it's simple one but I am not able to figure it out because one column is item qty which is system generated i guess it shows how many time (number) you use that part in assembly. and another column is custom iproperty of parts which gives value in ''inch''. so i want multiplication of both in third column which will be custom iproperty of assembly named ''total inch''. And i want this for all the rows in the BOM . I think now you have better idea.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
What is the name of the custom iProperty being referenced by column 1 (part lengths)?
Which view of the BOM are you wanting to be working with (Model Data, Structured, or Parts Only)?
If Structured, then 'first level only' or 'all levels' (in View Options or View Properties)?
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello, @WCrihfield , custom iproperty of column 1 name is ''LENGTH''. and i want to work on parts only and structured with all level BOM view.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @ABHUT_9090. I created something that you may be able to use.
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
If Not oBOM.StructuredViewEnabled Then oBOM.StructuredViewEnabled = True
If oBOM.StructuredViewFirstLevelOnly Then oBOM.StructuredViewFirstLevelOnly = False
Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
For Each oRow As BOMRow In oBOMView.BOMRows
Dim oRowDoc As Document = Nothing
Try
oRowDoc = oRow.ComponentDefinitions.Item(1).Document
Catch
Continue For 'skip to next BOMRow
End Try
If IsNothing(oRowDoc) Then Continue For
Dim oPartLength As Double = 0.0
Dim oCProps As PropertySet = oRowDoc.PropertySets.Item("Inventor User Defined Properties")
Try
oPartLength = oCProps.Item("LENGTH").Value
Catch
Continue For 'skip to next BOMRow
End Try
If oPartLength = 0.0 Then Continue For 'skip to next BOMRow
Dim oQty As Integer = oRow.ItemQuantity
Dim oTotalLength As Double = (oPartLength * oQty)
'value may need to have units converted if not correct
Dim oTotalInchCProp As Inventor.Property = Nothing
Try
oTotalInchCProp = oCProps.Item("total inch")
oTotalInchCProp.Value = oTotalLength
Catch
oTotalInchCProp = oCProps.Add(oTotalLength, "total inch")
End Try
Next
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS)
.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @WCrihfield , thank you for the code but unfortunately i did't work but i am trying to understand the code so can you please explain me this ''oPartLength''. Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello @WCrihfield , you can see in the BOM image that I actually want multiplication of this two column and get the result in the third column . is it possible with ilogic? for structured -all level and parts only BOM views, Thanks
- 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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello @WCrihfield, I tried this code but in the output( column3) it shows nothing may be the units problem, can you tell me , What is the unit of itemqty ? because i change other units to unitless (ul) but it still doesn't work.
And also i see in code that if something don't work then it will show us message nut it doesn't show any message after running the code ,so I think it's good news.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @ABHUT_9090. This type of thing can be challenging to write code for remotely, due to lack of being able to test the code before posting it. If it is not putting any value at all in the 'TOTAL_SQ_INCH' column, then that would not be because of a units problem. There must still be something else going on preventing this from working properly. Is the value within the "SQ_INCH" column a String (quoted), instead of a Double? When you have one of those model documents open, and have the iProperties dialog open, and on the Custom tab, does the Type column for that custom iProperty listed as Text or Number? If it is listed as Text, then that will be what is causing the problem. If that custom iProperty was created automatically by checking the check box within the Parameters dialog box in the Export column for that parameter, then you will need to right-click on that parameter's row, then choose 'Custom Property Format...'. That will open a small dialog where you can change the property type from text to Number, and set the other related options like Units and Precision.
I'm pretty sure that the Item Quantity is always just a unit-less Integer, representing the 'net' (as apposed to 'gross') number of components found within the assembly. Those other similar sounding quantity related properties are set within each model document by opening the model files, then going to the Tools tab, then clicking on Document Settings, which will open a dialog. Then click on the 'Bill of Materials' tab within that dialog. That is where you set Default BOM Structrue, Unit Quantity, Base Quantity, & Base Unit. Those are used by the BOM to help determine actual quantities the way you want them to be represented when there may be multiple of this model document being represented by components within an assembly.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Actually that reminds me of something else that might work. If that one column in your BOM labeled "TOTAL_SQ_INCH" is representing a custom iProperty that was automatically created because of setting a parameter to be exported, then we may need to access the related parameter in that model file directly, instead of the custom iProperty. When we set or change the value of that parameter, then that will automatically change the value of the related custom iProperty to match it, which will update the BOM.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello @WCrihfield , thank you for this information, according to this information i am able to get what i want in part list of drawing file without any code just manipulations of the names, but not in the BOM (assembly file) because the code doesn't work yet...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @WCrihfield , i understand now your code is generating custom iproperty for the parts named '' TOTAL_SQ_INCH'' as type ''text'' and it's empty. but all the parts already have custom iproperty named ''TOTAL_SQ_INCH'' as type '' number'' and value = zero (0). Can you use this information to update your code, please?
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
hi @WCrihfield , in this image you see how inventor automatically multiply unit qty and item qty and give output as QTY , Can we write this kind of code for our problem ?
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
hello @WCrihfield , I tried to do some variations in the program but it still doesn't work, Can you please give it a another try? and I also found that Item qty is read only property so that may be causing the problem !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@ABHUT_9090 - just curious if you read my reply to your initial request over here: Re: BOM (Assembly) columns multiplication - Autodesk Community - Inventor