Update cost of assembly with component occurences
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello, I have a code I found and have tweeked a little to get to work for my situation. Basically this code i have is to be run on an assembly. I have a different rule that runs on parts and can fill in the iproperties of estimated cost. The part rule checks what material i have the part set to, and does the math to put a base cost of how much the material would cost based on overall size of the part as we buy raw stock and make the parts.
The assembly rule i am having problems with is supposed to look at a main assembly, and take all of these values in each iproperty for est cost of parts and sub assemblies, add them all up and update the main assembly iproperties est cost to reflect the total cost of all the parts and sub assemblies.
The rule that is in question is this
Sub Main TraverseAssemblySample() ' Get the active assembly. Dim oAsmDoc As AssemblyDocument = ThisDoc.Document Dim oCost As Decimal oCost = 0 ' Call the function that does the recursion. Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, oCost) oCost.ToString("#0.00") iProperties.Value("Project", "Estimated Cost") = oCost iLogicVb.UpdateWhenDone = True End Sub Private Sub TraverseAssembly(ByVal Occurrences As ComponentOccurrences, _ ByVal Level As Integer, _ ByRef oCost As Double) ' Iterate through all of the occurrence in this collection. This ' represents the occurrences at the top level of an assembly. For Each oOcc As ComponentOccurrence In Occurrences If oOcc.Visible = False Then Continue For If oOcc.Suppressed = True Then Continue For ' Get the document file for this occurrence Dim oDoc As Document oDoc = oOcc.Definition.Document ' Get the iPropertySet that constains the estimated cost property Dim oPropSet As PropertySet oPropSet = oDoc.PropertySets.Item("Design Tracking Properties") ' Get the cost of this occurrence oCost += oPropSet.Item("Cost").Value ' Check to see if this occurrence represents a subassembly ' and recursively call this function to traverse through it. If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences, Level + 1, oCount) End If Next End Sub
The issue i am running into is-best way to explain it i think would be, if i have a main ASM with say 4 parts, all of them are made out of 1018 steel-part rule was ran in each part on save to get the est cost so each part has a specific $value to it on its own iproperty est cost. From the main assembly if i change one of the parts to a different material, so the price would change, the assembly rule will change the est cost of the occurence/part that was changed from the assembly, which is what i want it to do, but what it does not do is change the est cost of the main assembly. I have to run this rule twice to get it to work properly if there is a change in that est cost, wether it be supplier cost or material change. It will change the occurences properly first run, then the second run it will change the main assembly.
Not sure if it helps, but i think it is updating the main assembly first, i was playing around with the numbers and changing the est cost in different parts to test it, say there is 3 parts and one sub assembly that has the proper est cost value in the main ASM. If i change the est cost of the sub assembly manually in the iproperties, and then re run the rule, what happens is the rule goes through and changes the est cost of the main to the new value (3 parts est cost + new est cost of the sub), and then as it goes through it will change the sub to the correct value based on the math i have set for specific materials. So it seems like it is updating the main first, when what i want it to do is change the main ASM last, so if there is a material change or something within a sub assembly, the main will reflect the correct value, so cycle through the occurences and change those first and then add up all the correct values and update on the main ASM.
Any help would be greatly appreciated, thanks!