cd3944cbb8
* Revit redone * ArcGIS and Rhino * Remove settings * Fixed warnings
131 lines
4.3 KiB
C#
131 lines
4.3 KiB
C#
#if REVIT2025
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using Autodesk.Revit.ApplicationServices;
|
|
using Autodesk.Revit.UI;
|
|
using Revit.Async;
|
|
using Speckle.Connectors.DUI.WebView;
|
|
using Speckle.Connectors.Utils;
|
|
using Speckle.Converters.RevitShared.Helpers;
|
|
using Speckle.Sdk;
|
|
|
|
namespace Speckle.Connectors.Revit.Plugin;
|
|
|
|
internal sealed class RevitWebViewPlugin : IRevitPlugin
|
|
{
|
|
private readonly UIControlledApplication _uIControlledApplication;
|
|
private readonly RevitContext _revitContext;
|
|
private readonly DUI3ControlWebViewDockable _webViewPanel;
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage(
|
|
"Style",
|
|
"IDE0290:Use primary constructor",
|
|
Justification = "<Pending>"
|
|
)]
|
|
public RevitWebViewPlugin(
|
|
UIControlledApplication uIControlledApplication,
|
|
RevitContext revitContext,
|
|
DUI3ControlWebViewDockable webViewPanel
|
|
)
|
|
{
|
|
_uIControlledApplication = uIControlledApplication;
|
|
_revitContext = revitContext;
|
|
_webViewPanel = webViewPanel;
|
|
}
|
|
|
|
public void Initialise()
|
|
{
|
|
// Create and register panels before app initialized. this is needed for double-click file open
|
|
CreateTabAndRibbonPanel(_uIControlledApplication);
|
|
RegisterDockablePane();
|
|
_uIControlledApplication.ControlledApplication.ApplicationInitialized += OnApplicationInitialized;
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
// POC: should we be cleaning up the RibbonPanel etc...
|
|
// Should we be indicating to any active in-flight functions that we are being closed?
|
|
}
|
|
|
|
// POC: Could be injected but maybe not worthwhile
|
|
private void CreateTabAndRibbonPanel(UIControlledApplication application)
|
|
{
|
|
// POC: some top-level handling and feedback here
|
|
try
|
|
{
|
|
application.CreateRibbonTab(Connector.TabName);
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
// exception occurs when the speckle tab has already been created.
|
|
// this happens when both the dui2 and the dui3 connectors are installed. Can be safely ignored.
|
|
}
|
|
|
|
RibbonPanel specklePanel = application.CreateRibbonPanel(Connector.TabName, Connector.TabTitle);
|
|
var dui3Button = (PushButton)
|
|
specklePanel.AddItem(
|
|
new PushButtonData(
|
|
Connector.Name,
|
|
Connector.TabTitle,
|
|
typeof(RevitExternalApplication).Assembly.Location,
|
|
typeof(SpeckleRevitCommand).FullName
|
|
)
|
|
);
|
|
|
|
string path = typeof(RevitWebViewPlugin).Assembly.Location;
|
|
dui3Button.Image = LoadPngImgSource($"Speckle.Connectors.Revit{Connector.VersionString}.Assets.logo16.png", path);
|
|
dui3Button.LargeImage = LoadPngImgSource(
|
|
$"Speckle.Connectors.Revit{Connector.VersionString}.Assets.logo32.png",
|
|
path
|
|
);
|
|
dui3Button.ToolTipImage = LoadPngImgSource(
|
|
$"Speckle.Connectors.Revit{Connector.VersionString}.Assets.logo32.png",
|
|
path
|
|
);
|
|
dui3Button.ToolTip = "Speckle Connector for Revit New UI";
|
|
//dui3Button.AvailabilityClassName = typeof(CmdAvailabilityViews).FullName;
|
|
dui3Button.SetContextualHelp(new ContextualHelp(ContextualHelpType.Url, "https://speckle.systems"));
|
|
}
|
|
|
|
private 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);
|
|
}
|
|
|
|
private void RegisterDockablePane()
|
|
{
|
|
// Registering dockable pane should happen before UiApplication is initialized with RevitTask.
|
|
// Otherwise pane cannot be registered for double-click file open.
|
|
_uIControlledApplication.RegisterDockablePane(
|
|
RevitExternalApplication.DockablePanelId,
|
|
Connector.TabTitle,
|
|
_webViewPanel
|
|
);
|
|
}
|
|
|
|
private ImageSource? LoadPngImgSource(string sourceName, string path)
|
|
{
|
|
try
|
|
{
|
|
var assembly = Assembly.LoadFrom(Path.Combine(path));
|
|
var icon = assembly.GetManifestResourceStream(sourceName);
|
|
PngBitmapDecoder decoder = new(icon, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
|
|
ImageSource source = decoder.Frames[0];
|
|
return source;
|
|
}
|
|
catch (Exception ex) when (!ex.IsFatal())
|
|
{
|
|
// POC: logging
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
#endif
|