<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: View close event - Identify view is closed in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/11598597#M27149</link>
    <description>&lt;P&gt;In case anyone finds themself here (like me), I added a Revit Idea to provide a specific event for this:&amp;nbsp;&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/revit-ideas/api-viewclosed-viewclosing-events/idi-p/11598594" target="_blank"&gt;https://forums.autodesk.com/t5/revit-ideas/api-viewclosed-viewclosing-events/idi-p/11598594&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Please vote!&lt;/P&gt;</description>
    <pubDate>Mon, 05 Dec 2022 15:29:20 GMT</pubDate>
    <dc:creator>michael-coffey</dc:creator>
    <dc:date>2022-12-05T15:29:20Z</dc:date>
    <item>
      <title>View close event - Identify view is closed</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10188571#M27143</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I would like to do some operation when a view is closed in the document.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried with combination of view activated event and uiDocument.GetOpenUIViews(), but unfortunately it returns the closed view also in the list.&lt;/P&gt;&lt;P&gt;Is there any other workaround to identify view is closed.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 08:05:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10188571#M27143</guid>
      <dc:creator>sankar_g</dc:creator>
      <dc:date>2021-03-26T08:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: View close event - Identify view is closed</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10208864#M27144</link>
      <description>&lt;P&gt;Well! try this. it is a bit long but tested&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In short:&lt;/P&gt;&lt;P&gt;you need to do a couple of event registers to distinguish between what is currently UIViews are opened and what are closed. So we need to store the currently opened UIViews ids and keep adding newly created/opened ones. as well as to remove those which are closed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, we need to use Revit IdlingEvent to trigger an event, since Revit is not busy, meaning all views are now closed.&lt;/P&gt;&lt;P&gt;Hence, when Revit is in Idle state we compare the currently opened UIViews against what we stored then we run our Math.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One drawback of the below, if you closed a view that is not the current view. the below algorithm won't work. but eventually will work when you switch to another view. but I trust you can cover this drawback part.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;see the below code and let us know if something is not clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;as a hint, you can start registering all these events on Revit Start at the IExternalApplication class. or via IExternalCommand depends on how your strategy is.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ViewClosed_Event Workaround" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/901789iDD8584E38BFEF6AF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CloseView_Event.gif" alt="ViewClosed_Event Workaround" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;ViewClosed_Event Workaround&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Summary:&lt;/P&gt;&lt;P&gt;1. Register DocumentOpened all Available UIViews Id&lt;/P&gt;&lt;P&gt;2. Register on&amp;nbsp;ViewActivating To store new created View to the ViewIds store list&lt;/P&gt;&lt;P&gt;3. Register on ViewActivated the comparison between current opened Views against the closed using the power of Idling event&lt;/P&gt;&lt;P&gt;4. unregister all events once you no more need that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Detail:&lt;/P&gt;&lt;P&gt;1. Register DocumentOpened all Available UIViews Id&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;m_uiapp.Application.DocumentOpened += delegate{
viewids = m_uidoc.GetOpenUIViews().Select(o =&amp;gt; o.ViewId).ToList();
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Register on&amp;nbsp;ViewActivating To store new created View to the ViewIds store list&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;m_uiapp.ViewActivating += M_uiapp_ViewActivating;

private void M_uiapp_ViewActivating(object sender, Autodesk.Revit.UI.Events.ViewActivatingEventArgs e)
        {
            if (!viewids.Contains(e.NewActiveView.Id))
                viewids.Add(e.NewActiveView.Id);
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Register on ViewActivated the comparison between current opened Views against the closed using the power of Idling event&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; UT_Rvt.m_uiapp.ViewActivated += M_uiapp_ViewActivated;

  private void M_uiapp_ViewActivated(object sender, Autodesk.Revit.UI.Events.ViewActivatedEventArgs e)
        {
            UT_Rvt.m_uiapp.Idling += M_uiapp_Idling;
        }

 private void M_uiapp_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
        {
            var ids = m_uidoc.GetOpenUIViews().Select(o =&amp;gt; o.ViewId);
            List&amp;lt;ElementId&amp;gt; closedids = new List&amp;lt;ElementId&amp;gt;();
            foreach (var id in viewids)
            {
                if (ids.Contains(id)) continue;
                ViewIsClosed(id);
                 closedids.Add(id);
            }
            foreach (var id in closedids)
            {
                viewids.Remove(id);
            }
            // Stop Idling from repeating itself, since we are done here.
            m_uiapp.Idling -= M_uiapp_Idling;
        }

  private void ViewIsClosed(ElementId id)
        {
            var view = m_doc.GetElement(id) as View;
            TaskDialog.Show("Closed", $"View is Closed {id} {view.ViewName}");
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and finally, don't forget to unregister all events once you no more need that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; private void UnregisterEvents()
        {
            m_uiapp.Application.DocumentOpened -= Application_DocumentOpened;
            m_uiapp.ViewActivating -= M_uiapp_ViewActivating;
            m_uiapp.ViewActivated -= M_uiapp_ViewActivated;
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Apr 2021 05:14:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10208864#M27144</guid>
      <dc:creator>Moustafa_K</dc:creator>
      <dc:date>2021-04-03T05:14:58Z</dc:date>
    </item>
    <item>
      <title>Re: View close event - Identify view is closed</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10210483#M27145</link>
      <description>&lt;P&gt;Hi mostafa90,&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;I have solved the issue with ViewActivated event and MainWindow object. Idling event will produce performance issues so I tried the solution without idling.&lt;/P&gt;&lt;P&gt;As mentioned in your reply, we can’t get the trigger for closed view that is not the current view and this code also have the same drawback.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private bool IsViewClosed(View view, UIDocument uiDocument)
        {
            var uiView = uiDocument.GetOpenUIViews().ToList().FirstOrDefault(v =&amp;gt; v.ViewId == view.Id);
            if (uiView == null)
            {
                return true;
            }
            else
            {
                var mainWindow = UIFramework.MainWindow.getMainWnd();
                if (mainWindow == null)
                    return false;

                var childFrameControls = mainWindow.getAllViews();
                foreach (var childFrameControl in childFrameControls)
                {
                    if (!childFrameControl.isClosing)
                        continue;

                    var title = UIFrameworkServices.ManageViewsService.getFrameTitle(childFrameControl.hostHWnd);
                    if (string.IsNullOrEmpty(title))
                        continue;

                    if (title.Contains(view.Name))
                        return true;
                }
            }

            return false;
        }

        void application_ViewActivated(object sender, ViewActivatedEventArgs e)
        {
            var uiApplication = sender as UIApplication;
            if (uiApplication == null || uiApplication.ActiveUIDocument == null)
                return;

            var uiDocument = uiApplication.ActiveUIDocument;
            if (IsViewClosed(view, uiDocument))
            {

            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Apr 2021 08:10:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10210483#M27145</guid>
      <dc:creator>sankar_g</dc:creator>
      <dc:date>2021-04-04T08:10:41Z</dc:date>
    </item>
    <item>
      <title>Re: View close event - Identify view is closed</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10211123#M27146</link>
      <description>&lt;P&gt;You were on the right track using the UIFrameWork. The trick is to subscribe to the IsClosing event for newly opened views, for the startview in the DocumentOpened event, for the others in the ViewActivated event.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;Dictionary&amp;lt;object,Tuple&amp;lt;Document, ElementId&amp;gt;&amp;gt; ContentNames = new Dictionary&amp;lt;object, Tuple&amp;lt;Document, ElementId&amp;gt;&amp;gt;();
private void M_ctrlApp_ViewActivated(object sender, Autodesk.Revit.UI.Events.ViewActivatedEventArgs e)
{
    Autodesk.Revit.DB.View active = e.CurrentActiveView;
    var mainWindow = UIFramework.MainWindow.getMainWnd();
    if (mainWindow != null)
    {
        foreach (MFCMDIChildFrameControl child in mainWindow.getAllViews())
        {
            if (!ContentNames.Keys.Contains(child))
            {
                ContentNames.Add(child,new Tuple&amp;lt;Document, ElementId&amp;gt;(e.Document, active.Id));
                child.IsClosing += Child_IsClosing;
                break;
            }
        }
    }
}
private void ControlledApplication_DocumentOpened(object sender, Autodesk.Revit.DB.Events.DocumentOpenedEventArgs e)
{
    Autodesk.Revit.DB.View active = e.Document.ActiveView;
    var mainWindow = UIFramework.MainWindow.getMainWnd();
    if (mainWindow != null)
    {
        foreach (MFCMDIChildFrameControl child in mainWindow.getAllViews())
        {
            if (!ContentNames.Keys.Contains(child)) ContentNames.Add(child,new Tuple&amp;lt;Document, ElementId&amp;gt;(e.Document, active.Id));
            child.IsClosing += Child_IsClosing;
        }
    }
}

private void Child_IsClosing(object sender, EventArgs e)
{
    MFCMDIChildFrameControl ctrl = sender as MFCMDIChildFrameControl;
    if (ContentNames.Keys.Contains(ctrl))
    {
        Tuple &amp;lt; Document, ElementId &amp;gt; info = ContentNames[ctrl];
        Autodesk.Revit.DB.View closingview = info.Item1.GetElement(info.Item2) as Autodesk.Revit.DB.View;
        TaskDialog.Show("handler", string.Format("closing view {0} {1}",closingview==null? "&amp;lt;none&amp;gt;": closingview.Name, info.Item2.ToString()));
        ContentNames.Remove(ctrl);
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Apr 2021 18:12:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/10211123#M27146</guid>
      <dc:creator>FAIR59</dc:creator>
      <dc:date>2021-04-04T18:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: View close event - Identify view is closed</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/11307805#M27147</link>
      <description>Hello. What references to using UIFramework?</description>
      <pubDate>Wed, 20 Jul 2022 04:23:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/11307805#M27147</guid>
      <dc:creator>nguyenhai2499</dc:creator>
      <dc:date>2022-07-20T04:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: View close event - Identify view is closed</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/11310510#M27148</link>
      <description>It is the UIFramework.dll under revit installation folder</description>
      <pubDate>Thu, 21 Jul 2022 04:43:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/11310510#M27148</guid>
      <dc:creator>Kennan.Chen</dc:creator>
      <dc:date>2022-07-21T04:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: View close event - Identify view is closed</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/11598597#M27149</link>
      <description>&lt;P&gt;In case anyone finds themself here (like me), I added a Revit Idea to provide a specific event for this:&amp;nbsp;&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/revit-ideas/api-viewclosed-viewclosing-events/idi-p/11598594" target="_blank"&gt;https://forums.autodesk.com/t5/revit-ideas/api-viewclosed-viewclosing-events/idi-p/11598594&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Please vote!&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 15:29:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/view-close-event-identify-view-is-closed/m-p/11598597#M27149</guid>
      <dc:creator>michael-coffey</dc:creator>
      <dc:date>2022-12-05T15:29:20Z</dc:date>
    </item>
  </channel>
</rss>

