Community
Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for
Show  only  | Search instead for
Did you mean:

## Solid by union of solids in list

4 REPLIES 4
SOLVED
Message 1 of 5
3772 Views, 4 Replies

## Solid by union of solids in list

Hi,

I'm making a recursive function to create a solid which is the union of all solids that are given. Here's my code:

```        private static Solid SolidByUnion(List<Solid> solids)
{
Solid result;
if (solids.Count > 2)
{

List<Solid> restOfSolids = solids.Skip(0).Take(solids.Count - 1).ToList();
Solid solid1 = solids[0];
Solid solid2 = SolidByUnion(restOfSolids);
result = BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Union);
return result;

}
else
{
Solid solid1 = solids[0];
Solid solid2 = solids[1];
result = BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Union);
return result;
}
}```

However what I'm getting as the result is only the union of first two ( or maybe last two ) solids in the list. It's probably a basic recursion mistake but I can't figure out what I'm doing wrong here. Any hints?

Thanks.

4 REPLIES 4
Message 2 of 5

Normally, iteration is easier to implement and understand than recursion.

Here is an iterative solution:

```    Solid union = null;

foreach( GeometryObject obj in geo )
{
Solid solid = obj as Solid;

if( null != solid
&& 0 < solid.Faces.Size )
{
if( null == union )
{
union = solid;
}
else
{
union = BooleanOperationsUtils
.ExecuteBooleanOperation( union, solid,
BooleanOperationsType.Union );
}
}
}```

Cheers,

Jeremy

Jeremy Tammik
Developer Technical Services
The Building Coder

Message 3 of 5

Thanks Jeremy,

Cheers.

PS: I'm still looking however to the recursive solution ( just for learning purposes ) if anyone has any ideas.

Message 4 of 5

you misunderstand the use of List<>.Skip(). The value passed to the function is the number of elements to skip, and  not the index of the listelement to needs to be skipped.

So in solids.Skip(0).Take(solids.Count - 1).ToList();  the skip part doesn't do anything. The Take part then selects the first Count-1 entries. You never process the last entry.

Correct code syntax to eliminate the first entry:

```			solids.GetRange(1,solids.Count-1);
solids.Skip(1);```

Message 5 of 5