Update cost of assembly with component occurences

Update cost of assembly with component occurences

dschulteHR4D5
Advocate Advocate
508 Views
5 Replies
Message 1 of 6

Update cost of assembly with component occurences

dschulteHR4D5
Advocate
Advocate

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!

 

 

0 Likes
509 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @dschulteHR4D5.  It sounds to me like you are manually overwriting the estimated cost of sub assemblies differently than what the sum of its included parts estimated cost would be in the process you described.  If that is the case, then you would need a way to tell your code to ignore the parts within that sub assembly, and only use the sub assembly's own estimated cost value, instead of manually getting the sum of its parts.  Since an iProperty does not have an 'Overwritten' property, this can be difficult to handle, unless you create another 'custom' iProperty to check for that would include this status.

If this is not the case, and you just want to process all parts first, then process assembly files last, then you may need to run your TraverseAssembly type routine twice, each with a different DocumentType filter active.  First time...If oDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For'.  Then second time only process sub assemblies.  That might require a slightly modified version of the routine, with an additional 'input' variable for DocumentTypeEnum variation to filter.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

dschulteHR4D5
Advocate
Advocate

I was manually writing in different values to test the rule to see how it interacted in the assembly with sub assemblies. As the code is now, it does seem to work the way i want it to. It changed the est cost in each part and sub assembly the way I want it to, it just doesnt update the main assembly correctly. As the code is written it does work properly, it just updates the est cost iprop of th emain assembly based on what occurences are at. 

 

If i have 2 parts-part1 and part2 in an assembly that have correct est cost values, say 10 each, run the ASM rule, it works and displays the proper $20 in the est cost field of the main assembly. Now if i manually alter part1 est cost field in iproperties, when i run the ASM rule, the part rule changes part1 back to 10 like it should be-but the main assembly reflects the incorrect value of $15, when it should be $20. BUT the rule i have that runs in the background, will change part1 back to the proper $10, realistically this would only happen if say material changed, i have a different custom material that when run will not alter this est cost.

 

So when it cycled through, it looked at everything and added up the current-after changing to 5-added everything to get 15. I guess what i am trying to do with this specific situation is to get the rule to update the main asm last, as when running this rule, it updates the main asm est cost and then updates the occurences est cost. 

 

All in all the rule seems to do what i want exactly-except for it changes the est cost of the main, and then updates its occurences.

 

Also, the rule that runs in the background of each part that checks material and gets the est cost of each part, i have that rule set to make custom i properties as well, it uses a bounding box and enters custom values of the material size, stock volume, and est cost.

0 Likes
Message 4 of 6

dschulteHR4D5
Advocate
Advocate
Okay so the part rule that sets the est cost in the part, i set to run after a material change, so when the ASM rule starts the number is already changed as i want it-before i would change material, save assembly, ASM rule would fire on save, check the numbers and do the math, and then because of material change on the part, then the part would save which would trigger the part rule that sets the est cost in the individual part. Now the only thing i am having an issue with is, if i change a material of a part in a sub assembly from the main assembly, after material change the part rule runs and gets the new est cost, updates the part, but it does not update the sub assembly file est cost. I need to manually open each sub assembly and save to trigger the rule and get the correct value in that ASM est cost. After doing that, running the rule from the main ASM gets the correct values. Trying to figure out how to get the sub assemblies to save. I was thinking this rule would go through and update the sub assemblies but it does not. They need to be opened manually, or is there a way to reference the sub assemblies that it is checking the occurences and then update the sub assemblies est cost to reflect the main ASM?
0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor

That's definitely a tough one, because there is generally not anything in a part that tells it that it is being used in an assembly, so the part does not know it needs to update an assembly, or which assembly it would need to update.  I looked into the AssemblyEvents and did not find anything I believed to be useful there you could use for this situation.  However, there is a property of all documents called ReferencingDocuments that is only useful when those other documents are currently initiated or open (held in Inventor's session memory), that you may be able to make use of in this situation.  You can have your code check the Count of that property, and if more than one, it can attempt to access those 'parent' documents (use DocumentType check to avoid any drawings that may be open) and update them as needed.  Just a thought.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 6

dschulteHR4D5
Advocate
Advocate
Yeah, trying to wrap my head around exactly what the rule is doing. I am testing a bunch of different situations within an assembly, trying different things. Is there a way to reference an assembly document? when it gets to this point,
If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
Call TraverseAssembly(oOcc.SubOccurrences, Level + 1, oCount)
to be able to reference the sub assembly and then update the iproperties of that?
I could be wrong but i feel like it is referencing the main assembly and that is it, which is fine, but im not sure if i would be able to reference the main to do the occurence and then reference a sub when it finds one and then update that as well?
0 Likes