.Revit.Proxy.Exceptions

.Revit.Proxy.Exceptions

aclarke
Advocate Advocate
7,904 Views
11 Replies
Message 1 of 12

.Revit.Proxy.Exceptions

aclarke
Advocate
Advocate

Hi all,

I am wanting to delete all the Elevation Views from a model.  I am running into this exception:

Autodesk.Revit.Proxy.Exception.InvalidObjectExceptionProxy

 

I assume I need to load a Reference for 'Proxy' but I am not sure, nor can I find where to load from, it i not with the other .dll files.

I am trying to figure out which view or what is throw the exception or why it is happening

 

Thanks for any assistance...  🙂

 

t.Start();

List<View> views = myFunctions.CollectedViews(doc);
List<ViewType> viewTypes = myFunctions.ReturnListOfViewTypes(doc);

foreach (View v in views.Distinct())
{
    if (!v.IsTemplate)
    {
        foreach (ViewType vt in viewTypes)
        {
            if(vt == v.ViewType)
            {
                try
                {
                    doc.Delete(v.Id);
                }
                catch (Autodesk.Revit.Proxy.Exceptions.InvalidObjectExceptionProxy)//<-- No Good
                {
                    TD(v.Name);// TaskDialog.Show shortcut
                }
            }
        }
    }
}

t.Commit();

 

 Capture.PNG

0 Likes
Accepted solutions (2)
7,905 Views
11 Replies
Replies (11)
Message 2 of 12

TripleM-Dev.net
Advisor
Advisor
Accepted solution

Hi,

 

Not sure about the Proxy message (strange), but a couple of things:

- Views.Distinct not needed if you want to delete all elevation views (would always be unique)

- Make sure one of the open views isn't a Elevation! This gives a error message in the Revit UI

- Is the error raised at the first deletion ?

- in place of doc.delete(v.id), collect the id's in a collection and delete them together AFTER the loop.

- What is the viewTypes collection contain?

- Make sure not to try and delete a dependent view if parent view is already deleted.

 

See code below, do check if a non-elevation view is open first!

    FilteredElementCollector ViewCollector = new FilteredElementCollector(doc).OfClass(typeof(View));
    List<ElementId> DeleteViewIds = ViewCollector.Cast<View>.Where(v => (!v.IsTemplate) && v.ViewType == ViewType.Elevation && (v.GetPrimaryViewId != ElementId.InvalidElementId)).Select(e => e.Id).ToList;
    using (var t = new Transaction(doc, "Delete Elevations"))
    {
        t.Start();
        doc.Delete(DeleteViewIds);
        t.Commit();
    }

 

- Michel

0 Likes
Message 3 of 12

aclarke
Advocate
Advocate
Accepted solution

I figured out why I was getting this error...

 

The collection grabs all the views, and I failed to filter for Primary from the Dependent views, so as the deletion iterates thru the list of ID's, once the primary is deleted and then it gets to the dependent Id, the element is no longer there because it was deleted with the primary.

 

Filtering into two lists, a primary and dependent, then delete all the dependent views first then the primaries views fixed my issue.

 

 

I am still curious for the Revit.Proxy Exception

0 Likes
Message 4 of 12

aclarke
Advocate
Advocate

Thanks for the example code.  I like seeing examples of LINQ.  I am trying to do more with LINQ but I still struggle with it.

the Distinct() was me trying to understand how an ID was being deleted then trying to delete again, so I thought the ID was being captured twice...  I was throwing rocks at the problem trying to figure it out.

 

viewType collection:

It was going to contain a list of all the view types I wanted to delete, but this doesn't seem feasible now, seems better to tackle each view type independently.  

 

Thanks for your reply

 

 

public static List<ViewType> ReturnListOfViewTypes(Document doc)
{
    return new List<ViewType>()
    {
        ViewType.Elevation
    };
}

 

 

 

 

0 Likes
Message 5 of 12

TripleM-Dev.net
Advisor
Advisor

Hi,

 

I didn't test the code, below a correction for functional code:

FilteredElementCollector ViewCollector = new FilteredElementCollector(doc).OfClass(typeof(View));

            List<ElementId> DeleteViewIds = ViewCollector.Cast<View>().Where(v => !v.IsTemplate && 
                        v.ViewType == ViewType.Elevation && 
                        v.GetPrimaryViewId() == ElementId.InvalidElementId).Select(e => e.Id).ToList();

            using (var t = new Transaction(doc, "Delete Elevations"))
            {
                t.Start();
                doc.Delete(DeleteViewIds);
                t.Commit();
            }

 

 

Below a sample for using grouping on a property, ViewType in this case.

            FilteredElementCollector ViewCollector = new FilteredElementCollector(doc).OfClass(typeof(View));

            var groupedData = ViewCollector.Cast<View>().Where(v => !v.IsTemplate && v.GetPrimaryViewId() == ElementId.InvalidElementId).GroupBy(v => v.ViewType);

            foreach (var group in groupedData)
             {
                var groupkey = group.Key;

               //if(groupkey == ViewType.Elevation)
               // { 
               //     // only do something now
               // }

                foreach (var groupeditem in group)
                {
                    // the Views = groupeditem for the Viewtype = groupkey

                }

            }

 

- Michel

0 Likes
Message 6 of 12

aclarke
Advocate
Advocate

Another great LINQ example...   I will use this...

 

Thank you for sharing with me..  🙂

0 Likes
Message 7 of 12

aclarke
Advocate
Advocate

Why the use of 'var' because I wouldn't know to use it for this case?

 

var groupedData = ViewCollector.Cast<View>()
.Where(v => !v.IsTemplate
&& v.GetPrimaryViewId() == ElementId.InvalidElementId).GroupBy(v => v.ViewType);

 

 

 

Thanks

 

Thanks

0 Likes
Message 8 of 12

TripleM-Dev.net
Advisor
Advisor

It's just easier:

var groupedData = ViewCollector.Cast<View>().Where(v => !v.IsTemplate && v.GetPrimaryViewId() == ElementId.InvalidElementId).GroupBy(v => v.ViewType);

 

is identical to, both can be used.

IEnumerable<IGrouping<ViewType, View>> groupedData = ViewCollector.Cast<View>().Where(v => !v.IsTemplate && v.GetPrimaryViewId() == ElementId.InvalidElementId).GroupBy(v => v.ViewType);

 

The type of groupedData is already inferred because of the GroupBy.

If that's what you mean?

 

- Michel

0 Likes
Message 9 of 12

aclarke
Advocate
Advocate

you have answered my question,

Thanks

0 Likes
Message 10 of 12

TripleM-Dev.net
Advisor
Advisor

No thanks.

 

Please mark thread then as solved, for other users.

 

- Michel

0 Likes
Message 11 of 12

aclarke
Advocate
Advocate

I am confused by your 'no thanks'

0 Likes
Message 12 of 12

TripleM-Dev.net
Advisor
Advisor

Sorry, poor choice of words on my side.

 

No thanks needed. 😂

0 Likes