iLogic - How to sum a property of all subasemblies with filter

iLogic - How to sum a property of all subasemblies with filter

iiiocep
Advocate Advocate
748 Views
6 Replies
Message 1 of 7

iLogic - How to sum a property of all subasemblies with filter

iiiocep
Advocate
Advocate

Hi, I have assemly with subassemblies which have custom Property "MyNumber". I found how to sum all properties, but I need to filter some subassemblies in which I want to calculaate sum. Subassemblies which I want to filter have  iProperties.Value("Summary", "Title") = MyNumber2. How can I first filter only subassemblies which have only this parameter and then sum their custom properties ?

 

'- - - - - - - - - - - sum the custom iProperty - - - - - - - - - - 
'clear the custom property in the assembly 
iProperties.Value("Custom", "MyNumber") = 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
'check for and skip virtual components
'(in case a virtual component trips things up)
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
'custom property in the assembly 
xNumber = iProperties.Value("Custom", "MyNumber") 
'custom property in the parts
yNumber = iProperties.Value(oOccurrence.Name, "Custom", "MyNumber")
sumNumber = xNumber + yNumber
'set custom property values
iProperties.Value("Custom", "MyNumber") = sumNumber 
Else
End If
Next

 

 

 

0 Likes
Accepted solutions (2)
749 Views
6 Replies
Replies (6)
Message 2 of 7

tyler.warner
Advocate
Advocate

@iiiocep You can try something like this:

 

I just added the Try/Catch & If/Then loops within your existing ForEach loop.

 

 

'- - - - - - - - - - - sum the custom iProperty - - - - - - - - - - 
'clear the custom property in the assembly 
iProperties.Value("Custom", "MyNumber") = 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
For Each oOccurrence As ComponentOccurrence In oAsmCompDef.Occurrences
	'check for and skip virtual components
	'(in case a virtual component trips things up)
	If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
		Try
			If iProperties.Value(oOccurrence.Name, "Summary", "Title") = "MyNumber2" Then
				'custom property in the assembly 
				xNumber = iProperties.Value("Custom", "MyNumber") 
				'custom property in the parts
				yNumber = iProperties.Value(oOccurrence.Name, "Custom", "MyNumber")
				sumNumber = xNumber + yNumber
				'set custom property values
				iProperties.Value("Custom", "MyNumber") = sumNumber 
			End If
		Catch
		End Try
	End If
Next

 

If this solved your problem or answered your question, please click ACCEPT SOLUTION.
If this helped you, please click LIKE.
0 Likes
Message 3 of 7

iiiocep
Advocate
Advocate

I have modified a little your code but it stiil dooesn't work correct. I need to sum only mass of  subassemblies which  Contains "Strut" in their - "Summary", "Title" but if I use this code all parts and subassemblies summarize.

 

'- - - - - - - - - - - sum the custom iProperty - - - - - - - - - - 
'clear the custom property in the assembly 
iProperties.Value("Custom", "MyNumber") = 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
'check for and skip virtual components
'(in case a virtual component trips things up)
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition 
 If iProperties.Value(oOccurrence.Name, "Summary", "Title").ToString.Contains("Srtut") then
 'custom property in the assembly 
xNumber = iProperties.mass
'set custom property values
iProperties.Value("Custom", "MyNumber") = Round(xNumber,0)
		End If
			End If
Next

 

 

0 Likes
Message 4 of 7

tyler.warner
Advocate
Advocate
Accepted solution

@iiiocep The .ToString.Contains() was a good replacement of the Try/Catch loop!

In your code above, "Strut" was spelled incorrectly.

Added the mass from the specific occurrence as well.

 

 

'- - - - - - - - - - - sum the custom iProperty - - - - - - - - - - 
'clear the custom property in the assembly 
iProperties.Value("Custom", "MyNumber") = 0

'set a reference to the assembly component definintion.
'This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'custom property in the assembly 
Dim xNumber As Double

'Iterate through all of the occurrences
For Each oOccurrence As ComponentOccurrence In oAsmCompDef.Occurrences
	'check for and skip virtual components
	'(in case a virtual component trips things up)
	If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
		If iProperties.Value(oOccurrence.Name, "Summary", "Title").ToString.Contains("Strut") Then
			Dim mass As Double = iProperties.Mass(oOccurrence.Name)
			xNumber = xNumber + mass
		End If
	End If
Next

'set custom property value
iProperties.Value("Custom", "MyNumber") = Round(xNumber, 0)

 

 

If this solved your problem or answered your question, please click ACCEPT SOLUTION.
If this helped you, please click LIKE.
Message 5 of 7

iiiocep
Advocate
Advocate

@tyler.warner Thanks a lot, now it works perfect. But I have a new question now, how can I sum mass of parts only from the General assemly without parts from subassemblies ?

0 Likes
Message 6 of 7

tyler.warner
Advocate
Advocate
Accepted solution

@iiiocep Am I understanding correctly: If you have an assembly with Part1, Part2, Sub-Assembly1, Sub-Assembly2 then you only want the mass of Part1, Part2?

Adding the SubOccurrences.Count (see line 19/20) can figure out if the occurrence is a part or sub-assembly.

 

 

'- - - - - - - - - - - sum the custom iProperty - - - - - - - - - - 
'clear the custom property in the assembly 
iProperties.Value("Custom", "MyNumber") = 0

'set a reference to the assembly component definintion.
'This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'custom property in the assembly 
Dim xNumber As Double

'Iterate through all of the occurrences
For Each oOccurrence As ComponentOccurrence In oAsmCompDef.Occurrences
	'check for and skip virtual components
	'(in case a virtual component trips things up)
		
	If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
		If iProperties.Value(oOccurrence.Name, "Summary", "Title").ToString.Contains("MyNumber2") And _
		oOccurrence.SubOccurrences.Count = 0 Then
			Dim mass As Double = iProperties.Mass(oOccurrence.Name)			
			xNumber = xNumber + mass
		End If
	End If
Next

'set custom property value
iProperties.Value("Custom", "MyNumber") = Round(xNumber, 2)

 

 

 

 

If this solved your problem or answered your question, please click ACCEPT SOLUTION.
If this helped you, please click LIKE.
Message 7 of 7

iiiocep
Advocate
Advocate

@tyler.warner You are completely correct understand, it woks perfect! Thank's a lot!

0 Likes