Closing UIView Crashes Revit 2019

Closing UIView Crashes Revit 2019

RickyBell
Enthusiast Enthusiast
1,841 Views
5 Replies
Message 1 of 6

Closing UIView Crashes Revit 2019

RickyBell
Enthusiast
Enthusiast

I close views in previous versions of revit by using UIDocument.GetOpenUIViews() then using the UIView.Close() command.  This seems to be crashing Revit 2019. 

 

Do you recommend another way of dealing with views since the introduction of tabbed windows?

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

RickyBell
Enthusiast
Enthusiast

I should clarify a little.  If the code proceeds to open a wpf form after closing a UIView, Revit crashes.  If I isolate just the code that closes the view, it works.  If I isolate the code that opens the wpf form, it works.  I can't do both though.

Any help would be appreciated.  Thanks.

0 Likes
Message 3 of 6

RickyBell
Enthusiast
Enthusiast

Here's the entire code.  The form is completely bank. 

 

The .ShowDialog() line at the end errors out with "Attempted to read or write protected memory..."

 

Thanks.

 

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            UIDocument uiDoc = uiApp.ActiveUIDocument;

            //Access the document
            Document doc = uiDoc.Document;
            Document DbDoc = uiDoc.Document;
            Selection sel = uiDoc.Selection;

            if (uiDoc.ActiveView.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Sheets)
            {
                Autodesk.Revit.DB.View sheet = uiDoc.ActiveView;
                try
                {
                    foreach (ElementId eleId in sel.GetElementIds())
                    {
                        List<ElementId> selElements = new List<ElementId>();

                        Autodesk.Revit.DB.Viewport vp = doc.GetElement(eleId) as Autodesk.Revit.DB.Viewport;
                        FilteredElementCollector collector
                          = new FilteredElementCollector(doc)
                            .OfClass(typeof(Autodesk.Revit.DB.View));

                        foreach (Autodesk.Revit.DB.View v in collector)
                        {
                            if (vp != null)
                            {
                                if (v.Id == vp.ViewId)
                                {

                                    uiDoc.ActiveView = v;
                                    FilteredElementCollector fic = new FilteredElementCollector(doc, uiDoc.ActiveView.Id);

                                    foreach (ElementId elId in fic.ToElementIds())
                                    {
                                        selElements.Add(elId);
                                    }

                                    uiDoc.ActiveView = sheet;

                                    foreach (Autodesk.Revit.UI.UIView ov in uiDoc.GetOpenUIViews())
                                    {
                                        if (ov.ViewId == v.Id)
                                        {
                                            ov.Close(); //Commenting this line out will let the form open
                                            break;
                                        }
                                    }

                                    break;
                                }
                            }
                        }

                        if (selElements.Count > 0)
                        {
                            sel.SetElementIds(selElements);
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            try
            {
                //Open the form
                frmMyForm varForm = new frmMyForm();
                varForm.ShowDialog();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

            return Result.Succeeded;

        }

 

Message 4 of 6

jeremytammik
Autodesk
Autodesk

Dear Ricky,

 

Thank you for your report.

 

Could you please also attach a file containing a complete minimal reproducible case, that I can pass on to the development team for analysis?

 

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

 

They are not all add-in savvy, so a macro embedded in an RVT file would be best.

 

Alternatively, the complete Visual Studio solution zipped up and ready to compile would be acceptable.

 

I tried to create one for you from your sample code, but cannot since the definition of `frmMyForm` is missing.

 

Thank you.

 

Best regards,

 

Jeremy

 



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

0 Likes
Message 5 of 6

RickyBell
Enthusiast
Enthusiast
Accepted solution

Revit 2019.1 Update fixes this issue.  Thanks!

0 Likes
Message 6 of 6

rriggsQXKN6
Explorer
Explorer

Your detail here, helped me solve this in v2007 as well.

 

Here is the snippet of how I worked around the issue - I hope it helps someone else who is running into this problem.

this.Hide();
 foreach (UIView uiv in openUIVs) {
  if (uiv.ViewId == cView.Id) {
   uiv.Close();
   break;
  }
 }
 this.Show();
 
"this" is my active Form
0 Likes