exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException'

exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException'

62BJW
Advocate Advocate
6,592 Views
5 Replies
Message 1 of 6

exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException'

62BJW
Advocate
Advocate

I'm attempting to change all "Swing Angle" parameter for all the doors in the project. I'm getting the following error in the foreach loop. No elements are being deleted as the error describes.

 

An exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException' occurred in RevitAPI.dll but was not handled in user code

Additional information: The iterator cannot proceed due to changes made to the Element table in Revit's database (typically, This can be the result of an Element deletion).

 

Here is the code.

 

Thanks.

 

            FilteredElementCollector coll = new FilteredElementCollector(doc)
            .OfCategory(BuiltInCategory.OST_Doors)
            .OfClass(typeof(FamilyInstance));

            // Filtered element collector is iterable

            foreach (Element e in coll)
            {
                // Get the parameter name
                Parameter parameter = e.LookupParameter("Swing Angle");
                using (Transaction t = new Transaction(doc, "parameters"))

                // Modify document within a transaction
                using (Transaction tx = new Transaction(doc))
                {
                    tx.Start("Transaction Name");
                    try
                    {
                        parameter.Set(0.785398163);
                    }
                    catch { }
                    tx.Commit();
                }
            }

 

0 Likes
Accepted solutions (1)
6,593 Views
5 Replies
Replies (5)
Message 2 of 6

so-chong
Advocate
Advocate
Accepted solution

Hi,

 To avoid this kind of error, maybe you could try something like this

 

    FilteredElementCollector collector = new FilteredElementCollector(doc);
    List<Element> coll = collector.OfClass(typeof(FamilyInstance))
        .OfCategory(BuiltInCategory.OST_Doors)
        .ToList();
    
    // Filtered element collector is iterable
    
    foreach (Element e in coll)
    {
    
        // Get the parameter name
        Parameter parameter = e.LookupParameter("Swing Angle");
        using (Transaction t = new Transaction(doc, "parameters"))
    
        // Modify document within a transaction
        using (Transaction tx = new Transaction(doc))
        {
            tx.Start("Transaction Name");
            try
            {
                parameter.Set(0.785398163);
            }
            catch { }
            tx.Commit();
        }
    }

Hope this helps.

Message 3 of 6

62BJW
Advocate
Advocate

Perfect. Thanks!

0 Likes
Message 4 of 6

jweber-
Contributor
Contributor

Any chance you could explain why this error is occurring, and why your solution works? I tried it for my issue and it solved it, but I'm unclear as to why. I'm just wandering why you need to make a list?? Thanks in advance...

0 Likes
Message 5 of 6

aclarke
Advocate
Advocate

I believe it is because you are trying to make the changes inside a FilteredElementCollect vs a List.

Turn your collection into a list and work with the List not the Filter Collection.

 

deleting-a-collection-of-elementsfamilies-the-correct-approach. 

 

 

0 Likes
Message 6 of 6

jweber-
Contributor
Contributor

Thanks! That link is useful too, much appreciated.

0 Likes