Hi everyone,
I'm not the best at illogic (still learning it) and i seem to have stumped myself. I have written code to be able to select multiple parts and show an iproperties in a single message box but that not quite what I am looking for, What I am looking for is to be able to sum an iproperties and display the total in a message box. I have spent a lot of time trying to modify what I have got but with no luck. what would be the best way of handling this?
My code:
Dim oOccurrence As ComponentOccurrence
Dim Prop As String
Dim messageText As String
Try
oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
MessageBox.Show("Please select a component(s) before running this rule.", "iLogic")
Return
End Try
For Each oOccurrence In ThisDoc.Document.SelectSet
If TypeOf oOccurrence Is ComponentOccurrenceProxy Then
Cost = iProperties.Value(oOccurrence.NativeObject.Name, "Project", "Estimated Cost")
Else
Cost = iProperties.Value(oOccurrence.Name, "Project", "Estimated Cost")
End If
messageText = messageText &
"Cost = " & Cost
Next
MessageBox.Show(messageText, "Summary")
Solved! Go to Solution.
Solved by Lewis.Young. Go to Solution.
SyntaxEditor Code Snippet
Sub Main TraverseAssemblySample() ' Get the active assembly. Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument Dim oCost As Decimal oCost = 0 Try oOcc = ThisDoc.Document.SelectSet.Item(1) Catch MessageBox.Show("Please select a component(s) before running this rule.", "iLogic") Return End Try ' Call the function that does the recursion. Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, oCost) MessageBox.Show("£" & oCost, "Total Cost") 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. Dim oOcc As ComponentOccurrence Dim Prop As String Dim messageText As String 'Try ' oOcc = ThisDoc.Document.SelectSet.Item(1) 'Catch ' MessageBox.Show("Please select a component(s) before running this rule.", "iLogic") 'Return 'End Try For Each oOcc In ThisDoc.Document.SelectSet 'For Each oOcc As ComponentOccurrence In Occurrences ' Skip Phantom and reference parts. If oOcc.BOMStructure = BOMStructureEnum.kPhantomBOMStructure _ Or oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then ' Do Nothing Else ' The occurrence is valid count the cost. ' 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 End If ' 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, oCost) End If Next End Sub
Lewis Young
Windows 7 x64 - 32GB Ram
Intel Xeon E5-1620 v2 @ 3.70GHz
nVidia Quadro M2000 - 4GB
Inventor Professional 2017.3
Vault Basic 2017
3ds Max 2018
Thanks Lewis, thanks for the reply, I think I'm doing something wrong. Every time I us the code you sent, my inventor crashes and doesn't complete the rule. Did it work for you? (this could totally be fault)
Try this code instead:
SyntaxEditor Code Snippet
Sub Main '- - - - - - - - - - - sum the custom iProperty - - - - - - - - - - 'clear the custom property in the assembly iProperties.Value("Project", "estimated cost") = 0 'set a reference to the assembly component definintion. 'This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'Iterate through all of the occurrences Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Call ProcessAllChildren(oOccurrence) Next MessageBox.Show("Setting Estimated cost for top level assembly to: " & "£" & sumNumber, "Estimated cost") iProperties.Value("Project", "estimated cost") = Round (sumNumber,2) End Sub 'Global Private sumNumber As Double = 0 Public Sub ProcessAllChildren(ByRef oOccurrence As ComponentOccurrence) If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then 'property in the parts yNumber = iProperties.Value(oOccurrence.Name, "Project", "estimated cost") 'MessageBox.Show("yNumber = " & yNumber, oOccurrence.Name) sumNumber = sumNumber + yNumber 'MessageBox.Show("sumNumber = " & sumNumber,"Running Total") Else 'MessageBox.Show("This is a VirtualComponentDefinition", oOccurrence.Name) End If Dim oCompOcc As ComponentOccurrence Dim oComponentSubOccurrences As ComponentOccurrences ' For Each oCompOcc In oOccurrence.SubOccurrences If oCompOcc.DefinitionDocumentType = kAssemblyDocumentObject Then oComponentSubOccurrences = oCompOcc.SubOccurrences If Not oComponentSubOccurrences Is Nothing Then If oComponentSubOccurrences.count > 0 Then ProcessAllChildren(oCompOcc) End If End If Else ProcessAllChildren(oCompOcc) End If Next End Sub
Lewis Young
Windows 7 x64 - 32GB Ram
Intel Xeon E5-1620 v2 @ 3.70GHz
nVidia Quadro M2000 - 4GB
Inventor Professional 2017.3
Vault Basic 2017
3ds Max 2018
Can't find what you're looking for? Ask the community or share your knowledge.