Removing paint from a split face

Removing paint from a split face

hzamani
Advocate Advocate
1,061 Views
7 Replies
Message 1 of 8

Removing paint from a split face

hzamani
Advocate
Advocate

I'm trying to remove painted material from faces of elements. This works fine on non-modified elements, but when an element's face has been split and the split faces are painted separately the code below doesn't work. Any ideas?

 

 

using (Transaction t = new Transaction(doc, "Removing Paint From Selected Elements"))
{
    t.Start();
    foreach (Element elem in Elements)
    {
        GeometryElement geomElement = elem.get_Geometry(geoOptions);
        IEnumerator<GeometryObject> geomObjectEnumerator = geomElement.GetEnumerator();
        while (geomObjectEnumerator.MoveNext())
        {
            Solid solid = geomObjectEnumerator.Current as Solid;
            if (null != solid)
            {
                foreach (Face face in solid.Faces)
                {
                    doc.RemovePaint(elem.Id, face);
                }
            }
        }
    }
    t.Commit();
}

 

0 Likes
Accepted solutions (2)
1,062 Views
7 Replies
Replies (7)
Message 2 of 8

hzamani
Advocate
Advocate
Accepted solution

Never mind! Updated the code to this and now it works:

 

using (Transaction t = new Transaction(doc, "Removing Paint From Selected Elements"))
{
    t.Start();
    foreach (Element elem in Elements)
    {
        GeometryElement geomElement = elem.get_Geometry(geoOptions);
        IEnumerator<GeometryObject> geomObjectEnumerator = geomElement.GetEnumerator();
        while (geomObjectEnumerator.MoveNext())
        {
            Solid solid = geomObjectEnumerator.Current as Solid;
            if (null != solid)
            {
                foreach (Face face in solid.Faces)
                {
                    if (face.HasRegions)
                    {
                        var regions = face.GetRegions();
                        foreach(Face regFace in regions)
                        {
                            doc.RemovePaint(elem.Id, regFace);
                        }
                    }
                    else
                    {
                        doc.RemovePaint(elem.Id, face);

                    }
                }
            }
        }
    }
    t.Commit();
}
Message 3 of 8

Revitalizer
Advisor
Advisor

Hi,

 

each of the regions could be divided into another set of faces, so I would add a recursion.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 4 of 8

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Hoss,

 

Thank you for your query, and many thanks to Revitalizer for his important suggestion.

 

I think he meant to say: "each of the region's faces could be divided into another set of regions, ..."

 

Here is an untested attempt at adding recursion to your code:

 

  void RemovePaintFromFace( Element e, Face face )
  {
    Document doc = e.Document;
    
    if (face.HasRegions)
    {
      var regions = face.GetRegions();
      foreach(Face regFace in regions)
      {
        RemovePaintFromFace( e, regFace );
      }
    }
    else
    {
      doc.RemovePaint(e.Id, face);
    }
  }

 

With that in place, in your code above, you can replace the contents of the foreach loop over the solid faces by one single call to the recursive method:

 

  foreach (Face face in solid.Faces)
  {
    RemovePaintFromFace( elem, face );
  }

 

I hope this helps, matches Revitalizer's suggestion and works as desired.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 8

hzamani
Advocate
Advocate

Spot on Jeremy. Thanks.

0 Likes
Message 6 of 8

hzamani
Advocate
Advocate

Thanks Revitalizer for the comment. I just tried splitting an already split face and it looks like even if the a face get further split, all the instances will be returned using the GetRegions method on the original face (so no recursion would be required). Is what I did, what you meant by "regions could be divided into another set of faces"? 

0 Likes
Message 7 of 8

Revitalizer
Advisor
Advisor

Hi,

 

I just tested it; I must admit that you are correct.

Sorry for confusion - I was mislead by UI behaviour.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 8 of 8

hzamani
Advocate
Advocate

Thanks Revitalizer, your suggestion seemed completely logical to me too. But I've also learned that one can get surprised by Revit sometimes. For the sake of completeness, I found that there are instances where my second code doesn't work. I found that the best approach is to RemovePaint on any face whether it's split or not. So the more reliable code will be:

 

foreach (Face face in solid.Faces)
{
    doc.RemovePaint(elem.Id, face);
    if (face.HasRegions)
    {
        var regions = face.GetRegions();
        foreach(Face regFace in regions)
        {
            doc.RemovePaint(elem.Id, regFace);
        }
    }

} 

 

0 Likes