Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Deleting Lines that are not assigned to the <Room Separation> subcategory

62BJW
Advocate

Deleting Lines that are not assigned to the <Room Separation> subcategory

62BJW
Advocate
Advocate

I have code that deletes all lines in the document. I only want to delete the lines that are not on the <Room Separation> subcategory. I probably need to add something to my FilteredElementCollector but I can't figure out what. Thanks in advance.

 

                    //Lines
                    var linIds = new FilteredElementCollector(doc, vw.Id)
                        .OfClass(typeof(CurveElement))
                        .ToElementIds();
                    foreach (var lin_id in linIds)
                    {
                        doc.Delete(lin_id);
                        linCount++;
                    }
0 Likes
Reply
Accepted solutions (1)
599 Views
4 Replies
Replies (4)

recepagah12
Advocate
Advocate

You can use LINQ to query the elements. With LINQ, you can check element's category.

Here is the code snippet.

// This filtered element collector collects all elements in the document except room seperation lines
var lines = new FilteredElementCollector(doc).OfClass(typeof(CurveElement)).Cast<CurveElement>()
        .Where(q => q.Category.Id != new ElementId(BuiltInCategory.OST_RoomSeparationLines)).ToList();

foreach (var line in lines)
{
     doc.Delete(line.Id);
}

I hope this helps,

Recep.

0 Likes

jeremytammik
Autodesk
Autodesk

Dear Bernie,

 

Thank you for your query, and many thanks to Recep for his valuable help.

 

His suggestion can be improved upon.

 

Calling ToList at the end is a waste of time and space.

 

It requests a (totally unnecessary) copy of all the results from the filtered element collector.

 

You can iterate over the collector itself directly.

 

Furthermore, a built-in Revit filter will always be faster than LINQ post-processing.

 

In this case, you can use a negated ElementCategoryFilter:

 

https://apidocs.co/apps/revit/2019/6b8f4e3a-1975-7388-3848-462cf305d523.htm

 

Yet further, you might gain some additional performance by deleting all the elements in one single call rather than by stepping through them one by one.

 

For instance, like this:

  

  /// <summary>
  /// Delete all non-room-separating curve elements
  /// </summary>
  void DeleteNonRoomSeparators( Document doc )
  {
    ElementCategoryFilter non_room_separator 
      = new ElementCategoryFilter( 
        BuiltInCategory.OST_RoomSeparationLines, 
        true );

    FilteredElementCollector a
      = new FilteredElementCollector( doc )
        .OfClass( typeof( CurveElement ) )
        .WherePasses( non_room_separator );

    doc.Delete( a.ToElementIds() );
  }

 

I added this method to The Building Coder samples for you:

 

https://github.com/jeremytammik/the_building_coder_samples/compare/2020.0.145.2...2020.0.145.3

 

I hope this helps.

 

Best regards,

 

Jeremy

 



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

recepagah12
Advocate
Advocate

Hi @jeremytammik 

 

Thank you very much for your valuable suggestion.

Also many thanks to pointing out the wrong side of my code. 

Your solution is much elegant and faster. 

 

Thank you again,

Recep.

0 Likes

jeremytammik
Autodesk
Autodesk
Accepted solution