Skip to content

Commit

Permalink
fix(revit): workaround for #1469 (#1470)
Browse files Browse the repository at this point in the history
* fix(revit): workaround for #1469

* Modified dialog message as per Connor's comments

Co-authored-by: Alan Rynne <[email protected]>
  • Loading branch information
teocomi and AlanRynne authored Aug 4, 2022
1 parent e1d0fe7 commit ac32fad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
27 changes: 6 additions & 21 deletions ConnectorRevit/ConnectorRevit/Entry/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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<ViewActivatedEventArgs>(Application_ViewActivated);
Expand Down
65 changes: 59 additions & 6 deletions ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Autodesk.Revit.Attributes;
Expand Down Expand Up @@ -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}"));
Expand All @@ -52,12 +52,16 @@ public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure<DesktopUI2.A

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
uiapp = commandData.Application;

if (UseDockablePanel)
{
var panel = commandData.Application.GetDockablePane(PanelId);
panel.Show();
try
{
RegisterPane();
var panel = App.AppInstance.GetDockablePane(PanelId);
panel.Show();
}
catch { }
}
else
CreateOrFocusSpeckle();
Expand All @@ -67,6 +71,55 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
return Result.Succeeded;
}

internal static void RegisterPane()
{
if (!UseDockablePanel)
return;

var registered = DockablePane.PaneIsRegistered(PanelId);
var created = DockablePane.PaneExists(PanelId);

if (registered && created)
{
_panel.Init();
return;
}

if (!registered)
{
//Register dockable panel
var viewModel = new MainViewModel(Bindings);
_panel = new Panel
{
DataContext = viewModel
};
App.AppInstance.RegisterDockablePane(PanelId, "Speckle", _panel);
_panel.Init();
}
created = DockablePane.PaneExists(PanelId);

//if revit was launched double-clicking on a Revit file, we're screwed
//could maybe show the old window?
if (!created && App.AppInstance.Application.Documents.Size > 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 =
"<a href=\"https://github.com/specklesystems/speckle-sharp/issues/1469 \">"
+ "Click here for more info</a>";

mainDialog.Show();

}

}

public static void CreateOrFocusSpeckle(bool showWindow = true)
{

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit ac32fad

Please sign in to comment.