Solid by union of solids in list

hzamani
Advocate
Advocate

Solid by union of solids in list

hzamani
Advocate
Advocate

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.

0 Likes
Reply
Accepted solutions (2)
4,112 Views
4 Replies
Replies (4)

jeremytammik
Autodesk
Autodesk
Accepted solution

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

 

Here is an iterative solution:

 

http://thebuildingcoder.typepad.com/blog/2013/04/room-and-furniture-loops-using-symbols.html#6

 

    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
Autodesk Developer Network, ADN Open
The Building Coder

hzamani
Advocate
Advocate

Thanks Jeremy, 

 

Your solution solves my problem. 

 

Cheers.

 

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

0 Likes

FAIR59
Advisor
Advisor
Accepted solution

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);

  

0 Likes

hzamani
Advocate
Advocate

Thank you! I was so focused on the recursion itself, never suspected this part. 

 

Cheers.

0 Likes