Sum up all elements of collections (those having Count property) of parts in assembly with one LINQ query

Sum up all elements of collections (those having Count property) of parts in assembly with one LINQ query

Maxim-CADman77
Advisor Advisor
360 Views
3 Replies
Message 1 of 4

Sum up all elements of collections (those having Count property) of parts in assembly with one LINQ query

Maxim-CADman77
Advisor
Advisor

I'd like to know whether it possible to count SurfaceBodies of any Assembly (including those of MultiBody parts present) with just one LINQ query.

For now I can do this for single-level (no-subassemblies) assemblies only with iLogic-code like:

 

Dim assyDoc As AssemblyDocument = ThisDoc.Document

MsgBox((From occ As ComponentOccurrence In assyDoc.ComponentDefinition.Occurrences.AllLeafOccurrences Select occ.SurfaceBodies.Count).Sum, , "Number of bodies in the assembly:" )

 

 

BUT this code is not applicable for more complex (even two-level) assembly - returns error "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))").

The same error will occur with traditional cycle-based code like:

 

Dim ttlSolidQty As Integer For Each occ As ComponentOccurrence In compDef.Occurrences.AllLeafOccurrences ' !! ComponentOccurrenceProxy
   ' If TypeOf occ Is ComponentOccurrence Then logger.info(occ.Name & " - occ")
   ' If TypeOf occ Is ComponentOccurrenceProxy Then logger.info(occ.Name & " - occPROXY")
   ttlSolidQty += occ.SurfaceBodies.Count
Next
MsgBox(ttlSolidQty, , "Number of bodies in the assembly:" )

 

Some LeafOccurrences are just ComponentOccurrence while other (those at 2nd and deeper levels) are both ComponentOccurrence and ComponentOccurrenceProxy.

This same code would work OK without Type Declaration in FOR line like:

 

For Each occ In compDef.Occurrences.AllLeafOccurrences

 

But it seems to be impossible to just omit Type Declaration In the LINQ query mentioned - it would then return "Overload resolution failed because no accessible 'Sum' accepts this number of arguments."

 

What I'm missing?

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Accepted solutions (1)
361 Views
3 Replies
Replies (3)
Message 2 of 4

Maxim-CADman77
Advisor
Advisor

During further study the issue occurred to be reproducible in just one (non-migrated) assembly. The issue was resolved with migration.
But I still can reproduce the issue on attempt to Sum up WorkSurfaces with code:

 

ttlSurfaceQty = (From occ As ComponentOccurrence In compDef.Occurrences.AllLeafOccurrences Select occ.Definition.WorkSurfaces.Count).Sum

 

It fails with error "Overload resolution failed because no accessible 'Sum' accepts this number of arguments."

 

The cycle-based code below works fine:

 

For Each occ As ComponentOccurrence In compDef.Occurrences.AllLeafOccurrences ' As ComponentOccurrence|ComponentOccurrenceProxy
    If Not (TypeOf occ.Definition Is WeldsComponentDefinition OR TypeOf occ.Definition Is VirtualComponentDefinition) Then ttlSurfaceQty += occ.Definition.WorkSurfaces.Count
Next

 

 

Any ideas?

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 3 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

I'm not sure it is useful for real usage, but as a challenge... You can try this

 

Dim asm As AssemblyDocument = ThisDoc.Document
Dim count = asm.ComponentDefinition.Occurrences.AllLeafOccurrences.OfType(Of ComponentOccurrence).Sum(Function(x) x.Definition.SurfaceBodies.Count)
Logger.Debug(count.ToString())

 

Message 4 of 4

Maxim-CADman77
Advisor
Advisor

This works.

Please vote for Inventor-Idea Text Search within Option Names

0 Likes