From ac32fad3655b8d1d91918dba0fa2df3b62aebd4b Mon Sep 17 00:00:00 2001 From: Matteo Cominetti Date: Thu, 4 Aug 2022 09:31:05 +0200 Subject: [PATCH] fix(revit): workaround for #1469 (#1470) * fix(revit): workaround for #1469 * Modified dialog message as per Connor's comments Co-authored-by: Alan Rynne --- ConnectorRevit/ConnectorRevit/Entry/App.cs | 27 ++------ .../Entry/SpeckleRevitCommand2.cs | 65 +++++++++++++++++-- .../ConnectorBindingsRevit2.Events.cs | 16 ++--- 3 files changed, 73 insertions(+), 35 deletions(-) diff --git a/ConnectorRevit/ConnectorRevit/Entry/App.cs b/ConnectorRevit/ConnectorRevit/Entry/App.cs index 70894bca37..13cd7722fc 100644 --- a/ConnectorRevit/ConnectorRevit/Entry/App.cs +++ b/ConnectorRevit/ConnectorRevit/Entry/App.cs @@ -3,6 +3,7 @@ using System.Reflection; using System.Windows.Media; using System.Windows.Media.Imaging; +using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.UI; using DesktopUI2.ViewModels; using Revit.Async; @@ -17,17 +18,13 @@ public class App : IExternalApplication public static UIControlledApplication UICtrlApp { get; set; } - public static IDockablePaneProvider Panel; - public Result OnStartup(UIControlledApplication application) { //Always initialize RevitTask ahead of time within Revit API context RevitTask.Initialize(application); UICtrlApp = application; - // Fires an init event, where we can get the UIApp - UICtrlApp.Idling += Initialise; - + UICtrlApp.ControlledApplication.ApplicationInitialized += ControlledApplication_ApplicationInitialized; string tabName = "Speckle"; try @@ -136,13 +133,11 @@ public Result OnStartup(UIControlledApplication application) return Result.Succeeded; } - private void Initialise(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e) + private void ControlledApplication_ApplicationInitialized(object sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e) { - UICtrlApp.Idling -= Initialise; - AppInstance = sender as UIApplication; - AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve); - + AppInstance = new UIApplication(sender as Application); + AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve); #if REVIT2019 @@ -152,7 +147,6 @@ private void Initialise(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs SpeckleRevitCommand.Bindings.SetExecutorAndInit(eventHandler); #else //DUI2 - pre build app, so that it's faster to open up - SpeckleRevitCommand2.uiapp = AppInstance; SpeckleRevitCommand2.InitAvalonia(); var bindings = new ConnectorBindingsRevit2(AppInstance); bindings.RegisterAppEvents(); @@ -161,18 +155,9 @@ private void Initialise(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs OneClickSendCommand.Bindings = bindings; QuickShareCommand.Bindings = bindings; - if (SpeckleRevitCommand2.UseDockablePanel) - { - //Register dockable panel - var viewModel = new MainViewModel(bindings); - Panel = new Panel - { - DataContext = viewModel - }; - AppInstance.RegisterDockablePane(SpeckleRevitCommand2.PanelId, "Speckle", Panel); - } + SpeckleRevitCommand2.RegisterPane(); #endif //AppInstance.ViewActivated += new EventHandler(Application_ViewActivated); diff --git a/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs b/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs index d2de28f50d..a2e2417dbe 100644 --- a/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs +++ b/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Runtime.InteropServices; using System.Threading; using Autodesk.Revit.Attributes; @@ -29,8 +30,7 @@ public class SpeckleRevitCommand2 : IExternalCommand public static ConnectorBindingsRevit2 Bindings { get; set; } - internal static UIApplication uiapp; - + private static Panel _panel { get; set; } internal static DockablePaneId PanelId = new DockablePaneId(new Guid("{0A866FB8-8FD5-4DE8-B24B-56F4FA5B0836}")); @@ -52,12 +52,16 @@ public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure 0) + { + TaskDialog mainDialog = new TaskDialog("Dockable Panel Issue"); + mainDialog.MainInstruction = "Dockable Panel Issue"; + mainDialog.MainContent = + "Revit cannot properly register Dockable Panels when launched by double-clicking a Revit file. " + + "Please close and re-open Revit without launching a file OR open/create a new project to trigger the Speckle panel registration."; + + + // Set footer text. Footer text is usually used to link to the help document. + mainDialog.FooterText = + "" + + "Click here for more info"; + + mainDialog.Show(); + + } + + } + public static void CreateOrFocusSpeckle(bool showWindow = true) { @@ -104,7 +157,7 @@ public static void CreateOrFocusSpeckle(bool showWindow = true) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - var parentHwnd = uiapp.MainWindowHandle; + var parentHwnd = App.AppInstance.MainWindowHandle; var hwnd = MainWindow.PlatformImpl.Handle.Handle; SetWindowLongPtr(hwnd, GWL_HWNDPARENT, parentHwnd); } diff --git a/ConnectorRevit/ConnectorRevit/UI/ConnectorBindingsRevit2/ConnectorBindingsRevit2.Events.cs b/ConnectorRevit/ConnectorRevit/UI/ConnectorBindingsRevit2/ConnectorBindingsRevit2.Events.cs index ae80dd1d26..32d23c04dc 100644 --- a/ConnectorRevit/ConnectorRevit/UI/ConnectorBindingsRevit2/ConnectorBindingsRevit2.Events.cs +++ b/ConnectorRevit/ConnectorRevit/UI/ConnectorBindingsRevit2/ConnectorBindingsRevit2.Events.cs @@ -52,6 +52,7 @@ public void RegisterAppEvents() RevitApp.Application.DocumentCreated += Application_DocumentCreated; RevitApp.Application.DocumentCreating += Application_DocumentCreating; RevitApp.Application.DocumentOpened += Application_DocumentOpened; + RevitApp.Application.DocumentOpening += Application_DocumentOpening; ; RevitApp.Application.DocumentClosed += Application_DocumentClosed; RevitApp.Application.DocumentSaved += Application_DocumentSaved; RevitApp.Application.DocumentSynchronizingWithCentral += Application_DocumentSynchronizingWithCentral; @@ -63,8 +64,10 @@ public void RegisterAppEvents() // thus triggering the focus on a new project } + private void Application_DocumentOpening(object sender, Autodesk.Revit.DB.Events.DocumentOpeningEventArgs e) + { - + } private void Application_DocumentCreating(object sender, Autodesk.Revit.DB.Events.DocumentCreatingEventArgs e) { @@ -136,8 +139,7 @@ private void RevitApp_ViewActivated(object sender, Autodesk.Revit.UI.Events.View if (e.Document == null || e.PreviousActiveView == null || e.Document.GetHashCode() == e.PreviousActiveView.Document.GetHashCode()) return; - if (SpeckleRevitCommand2.UseDockablePanel) - (App.Panel as Panel).Init(); + SpeckleRevitCommand2.RegisterPane(); var streams = GetStreamsInFile(); UpdateSavedStreams(streams); @@ -181,8 +183,7 @@ private void Application_DocumentChanged(object sender, Autodesk.Revit.DB.Events { } private void Application_DocumentCreated(object sender, Autodesk.Revit.DB.Events.DocumentCreatedEventArgs e) { - if (SpeckleRevitCommand2.UseDockablePanel) - (App.Panel as Panel).Init(); + SpeckleRevitCommand2.RegisterPane(); //clear saved streams if opening a new doc if (UpdateSavedStreams != null) @@ -191,15 +192,14 @@ private void Application_DocumentCreated(object sender, Autodesk.Revit.DB.Events private void Application_DocumentOpened(object sender, Autodesk.Revit.DB.Events.DocumentOpenedEventArgs e) { - if (SpeckleRevitCommand2.UseDockablePanel) - (App.Panel as Panel).Init(); var streams = GetStreamsInFile(); if (streams != null && streams.Count != 0) { if (SpeckleRevitCommand2.UseDockablePanel) { - var panel = RevitApp.GetDockablePane(SpeckleRevitCommand2.PanelId); + SpeckleRevitCommand2.RegisterPane(); + var panel = App.AppInstance.GetDockablePane(SpeckleRevitCommand2.PanelId); panel.Show(); } else