Skip to content

Commit

Permalink
This is a workaround for Revit's order of operations when initializing (
Browse files Browse the repository at this point in the history
#511)

* This is a workaround for Revit's order of operations when initializing

* Fix event listening
  • Loading branch information
adamhathcock authored Jan 22, 2025
1 parent fd2dd9a commit c9ca1c0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Speckle.Connectors.Common;
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.DUI.Models;
using Speckle.Converters.RevitShared.Helpers;
using Speckle.Sdk;

Expand Down Expand Up @@ -99,13 +100,17 @@ private void CreateTabAndRibbonPanel(UIControlledApplication application)
dui3Button.SetContextualHelp(new ContextualHelp(ContextualHelpType.Url, "https://speckle.systems"));
}

private void OnApplicationInitialized(object? sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e)
private async void OnApplicationInitialized(
object? sender,
Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e
)
{
var uiApplication = new UIApplication(sender as Application);
_revitContext.UIApplication = uiApplication;

// POC: might be worth to interface this out, we shall see...
RevitTask.Initialize(uiApplication);
await _serviceProvider.GetRequiredService<DocumentModelStore>().OnDocumentStoreInitialized();

PostApplicationInit(); // for double-click file open
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Result OnStartup(UIControlledApplication application)
services.AddRevitConverters();
services.AddSingleton(application);
_container = services.BuildServiceProvider();
_container.UseDUI();
_container.UseDUI(true);

// resolve root object
_revitPlugin = _container.GetRequiredService<IRevitPlugin>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void SubscribeToRhinoEvents(IEventAggregator eventAggregator)

// NOTE: BE CAREFUL handling things in this event handler since it is triggered whenever we save something into file!
eventAggregator
.GetEvent<ActiveDocumentChanged>()
.GetEvent<DocumentPropertiesChanged>()
.Subscribe(async e =>
{
var newUnit = e.Document.ModelUnitSystem;
Expand Down
8 changes: 6 additions & 2 deletions DUI3/Speckle.Connectors.DUI/ContainerRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static IServiceCollection AddEventsAsTransient(this IServiceCollection se
return serviceCollection;
}

public static IServiceProvider UseDUI(this IServiceProvider serviceProvider)
public static IServiceProvider UseDUI(this IServiceProvider serviceProvider, bool isRevit = false)
{
//observe the unobserved!
TaskScheduler.UnobservedTaskException += async (_, args) =>
Expand All @@ -64,7 +64,11 @@ await serviceProvider
args.SetObserved();
};

serviceProvider.GetRequiredService<DocumentModelStore>().OnDocumentStoreInitialized().Wait();
if (!isRevit)
{
serviceProvider.GetRequiredService<DocumentModelStore>().OnDocumentStoreInitialized().Wait();
}

return serviceProvider;
}
}

0 comments on commit c9ca1c0

Please sign in to comment.