Revit freezes using Element.Delete() on a Revit Link Type? (Code included)

Revit freezes using Element.Delete() on a Revit Link Type? (Code included)

Anonymous
Not applicable
1,329 Views
5 Replies
Message 1 of 6

Revit freezes using Element.Delete() on a Revit Link Type? (Code included)

Anonymous
Not applicable

Hi, I am trying to remove all revit links from a revit file and for some reason when i call delete on the revit link type revit freezes and asks me if i would like to create a recovery file. my experience with revit is not huge so apologies if this is a stupid question.

Here is my code(Ignore commented code)

Revit freeze code.PNG

 

Thank you in advance, you guys are always great.

Jack

Accepted solutions (2)
1,330 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Jack,

 

Thank you for reporting this.

 

It sounds as if you are hitting some internal element that should not be removed.

  

Maybe you can add some additional filter checks to avoid hitting the sensitive element that is causing this behaviour.

 

Anyway, since you are checking the CanBeDeleted predicate before attempting to delete, this sounds like an error to me.

 

Can you please provide a full reproducible case, i.e. the entire Visual Studio solution, add-in manifest, and minimal sample model to run this in?

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b

 

Then I'll raise an issue with the development team to explore in more depth for you.

 

Thank you!

 

Cheers,

 

Jeremy



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

Message 3 of 6

Anonymous
Not applicable

Thank you Jeremy, here is everything you requested, If there is any problems let me know. 
Thanks again,
Jack

0 Likes
Message 4 of 6

jeremytammik
Autodesk
Autodesk

Dear Jack,

 

Thank you for your sample material.

 

I created a cleaned up version of your sample add-in, attached it below in PurgeLinksFreeze.zip, and can reproduce the behaviour you describe.

 

I'll pass it on to the development team as soon as I can. I don't have access to the system right now due to travels.

 

I'll let you know as soon as I have any news.

 

Cheers,

 

Jeremy

 

 



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

0 Likes
Message 5 of 6

jeremytammik
Autodesk
Autodesk
Accepted solution

Sorry, Jack, it seems it was your own fault after all.

 

You must not modify elements that you are iterating over.

 

That corrupts the iteration process.

 

Look at the new attached solution that achieves the deletion with no freeze:

 

  UIApplication uiapp = commandData.Application;
  UIDocument uidoc = uiapp.ActiveUIDocument;
  Document doc = uidoc.Document;

  FilteredElementCollector links 
    = new FilteredElementCollector( doc )
      .OfClass( typeof( RevitLinkType ) );

  ICollection<ElementId> ids = links.ToElementIds();

  using( Transaction t = new Transaction( doc ) )
  {
    t.Start( "Delete Links" );
    doc.Delete( ids );
    t.Commit();
  }

  return Result.Succeeded;

 

I attached the complete solution, source code and add-in manifest below.

 

Cheers,

 

Jeremy

 



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

Message 6 of 6

Anonymous
Not applicable

Thanks a million jeremy, You have been very helpful, I am still learning.

 

Jack

0 Likes