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
Reply
Message 1 of 5
hzamani
3363 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
jeremytammik
in reply to: hzamani

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

Message 3 of 5
hzamani
in reply to: jeremytammik

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.

Message 4 of 5
FAIR59
in reply to: hzamani

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
hzamani
in reply to: FAIR59

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

 

Cheers.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community