Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug that makes Grasshopper previews visible on Revit… #1035

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ public static bool IsInside(this BoundingBoxXYZ value, XYZ point)

public static class OutlineExtension
{
public static Outline Empty => new Outline(XYZExtension.MaxValue, XYZExtension.MinValue);

public static void Deconstruct(this Outline outline, out XYZ min, out XYZ max)
{
min = outline.MinimumPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace RhinoInside.Revit.External.DB.Extensions
readonly ICollection<ElementId> collection;
internal ReadOnlyElementIdSet(ICollection<ElementId> source) => collection = source;

public static readonly ReadOnlyElementIdSet Empty = new ReadOnlyElementIdSet(Array.Empty<ElementId>());

#region IEnumerable
public IEnumerator<ElementId> GetEnumerator() => collection.GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => collection.GetEnumerator();
Expand Down
9 changes: 9 additions & 0 deletions src/RhinoInside.Revit.External/DB/Extensions/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public static bool Close(this View view)
return false;
}

public static bool IsOpen(this View view)
{
if (view is null)
throw new ArgumentNullException(nameof(view));

using (var uiDoc = new UIDocument(view.Document))
return uiDoc.GetOpenUIViews().Any(x => x.ViewId == view.Id);
}

#if REVIT_2021
const ViewType ViewType_SystemsAnalysisReport = ViewType.SystemsAnalysisReport;
const ViewFamily ViewFamily_SystemsAnalysisReport = ViewFamily.SystemsAnalysisReport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
<Compile Include="UI\Selection.cs" />
<Compile Include="UI\TaskDialogIcons.cs" />
<Compile Include="UI\UIHostApplication.cs" />
<Compile Include="UI\UIHostApplicationConstrained.cs" />
<Compile Include="UI\UIHostApplicationUnconstrained.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Settings\KeyboardShortcuts.cs" />
Expand Down
176 changes: 6 additions & 170 deletions src/RhinoInside.Revit.External/UI/UIHostApplication.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using RhinoInside.Revit.External.DB.Extensions;
using RhinoInside.Revit.External.UI.Extensions;

namespace RhinoInside.Revit.External.UI
{
#region UIHostApplication
using External.DB.Extensions;

public abstract class UIHostApplication : IDisposable
{
protected internal UIHostApplication() { }
public abstract void Dispose();

public static implicit operator UIHostApplication(UIApplication value) => new UIHostApplicationU(value);
public static implicit operator UIHostApplication(UIControlledApplication value) => new UIHostApplicationC(value);
public static implicit operator UIHostApplication(UIApplication value) => new UIHostApplicationUnconstrained(value);
public static implicit operator UIHostApplication(UIControlledApplication value) => new UIHostApplicationConstrained(value);

public abstract object Value { get; }
public abstract bool IsValid { get; }
Expand All @@ -26,6 +24,8 @@ protected internal UIHostApplication() { }

#region UI
public abstract IntPtr MainWindowHandle { get; }

internal abstract bool IsViewOpen(View view);
#endregion

#region Ribbon
Expand Down Expand Up @@ -140,170 +140,6 @@ private void CompareSelection(object sender, IdlingEventArgs e)
#endif
#endregion
}

sealed class UIHostApplicationC : UIHostApplication
{
readonly UIControlledApplication _app;
public UIHostApplicationC(UIControlledApplication app)
{
_app = app;

#if REVIT_2023
_app.SelectionChanged += SelectionChangedHandler;
#endif
}
public override void Dispose()
{
#if REVIT_2023
_app.SelectionChanged -= SelectionChangedHandler;
#endif
}
public override object Value => _app;
public override bool IsValid => true;

public override ApplicationServices.HostServices Services => new ApplicationServices.HostServicesC(_app.ControlledApplication);
public override UIDocument ActiveUIDocument { get => default; set => throw new InvalidOperationException(); }

#region UI
public override IntPtr MainWindowHandle
{
#if REVIT_2019
get => _app.MainWindowHandle;
#else
get => System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
#endif
}
#endregion

#region Ribbon
public override void CreateRibbonTab(string tabName) =>
_app.CreateRibbonTab(tabName);

public override RibbonPanel CreateRibbonPanel(Tab tab, string panelName) =>
_app.CreateRibbonPanel(tab, panelName);

public override RibbonPanel CreateRibbonPanel(string tabName, string panelName) =>
_app.CreateRibbonPanel(tabName, panelName);

public override IReadOnlyList<RibbonPanel> GetRibbonPanels(Tab tab) =>
_app.GetRibbonPanels(tab);

public override IReadOnlyList<RibbonPanel> GetRibbonPanels(string tabName) =>
_app.GetRibbonPanels(tabName);
#endregion

#region Addins
public override AddInId ActiveAddInId => _app.ActiveAddInId;
public override void LoadAddIn(string fileName) => _app.LoadAddIn(fileName);
public override ExternalApplicationArray LoadedApplications => _app.LoadedApplications;
#endregion

#region Commands
public override bool CanPostCommand(RevitCommandId commandId) => false;
public override void PostCommand(RevitCommandId commandId) => throw new InvalidOperationException();
#endregion

#region Events
public override event EventHandler<IdlingEventArgs> Idling
{
add => _app.Idling += ActivationGate.AddEventHandler(value);
remove => _app.Idling -= ActivationGate.RemoveEventHandler(value);
}
public override event EventHandler<ViewActivatingEventArgs> ViewActivating { add => _app.ViewActivating += value; remove => _app.ViewActivating -= value; }
public override event EventHandler<ViewActivatedEventArgs> ViewActivated { add => _app.ViewActivated += value; remove => _app.ViewActivated -= value; }
#endregion
}

sealed class UIHostApplicationU : UIHostApplication
{
readonly UIApplication _app;
public UIHostApplicationU(UIApplication app)
{
_app = app;

#if REVIT_2023
_app.SelectionChanged += SelectionChangedHandler;
#endif
}

public override void Dispose()
{
#if REVIT_2023
_app.SelectionChanged -= SelectionChangedHandler;
#endif
_app.Dispose();
}

public override object Value => _app.IsValidObject ? _app : default;
public override bool IsValid => _app.IsValidObject;

public override ApplicationServices.HostServices Services => new ApplicationServices.HostServicesU(_app.Application);
public override UIDocument ActiveUIDocument
{
get => _app.ActiveUIDocument;
set
{
if (value is null) throw new ArgumentNullException();
if (value.Document.IsEquivalent(_app.ActiveUIDocument.Document)) return;

if (value.TryGetActiveGraphicalView(out var uiView))
{
HostedApplication.Active.InvokeInHostContext
(() => value.Document.SetActiveGraphicalView(value.Document.GetElement(uiView.ViewId) as View));
}
}
}

#region UI
public override IntPtr MainWindowHandle
{
#if REVIT_2019
get => _app.MainWindowHandle;
#else
get => System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
#endif
}
#endregion

#region Ribbon
public override void CreateRibbonTab(string tabName) =>
_app.CreateRibbonTab(tabName);

public override RibbonPanel CreateRibbonPanel(Tab tab, string panelName) =>
_app.CreateRibbonPanel(tab, panelName);

public override RibbonPanel CreateRibbonPanel(string tabName, string panelName) =>
_app.CreateRibbonPanel(tabName, panelName);

public override IReadOnlyList<RibbonPanel> GetRibbonPanels(Tab tab) =>
_app.GetRibbonPanels(tab);

public override IReadOnlyList<RibbonPanel> GetRibbonPanels(string tabName) =>
_app.GetRibbonPanels(tabName);
#endregion

#region AddIns
public override AddInId ActiveAddInId => _app.ActiveAddInId;
public override void LoadAddIn(string fileName) => _app.LoadAddIn(fileName);
public override ExternalApplicationArray LoadedApplications => _app.LoadedApplications;
#endregion

#region Commands
public override bool CanPostCommand(RevitCommandId commandId) => _app.CanPostCommand(commandId);
public override void PostCommand(RevitCommandId commandId) => _app.PostCommand(commandId);
#endregion

#region Events
public override event EventHandler<IdlingEventArgs> Idling
{
add => _app.Idling += ActivationGate.AddEventHandler(value);
remove => _app.Idling -= ActivationGate.RemoveEventHandler(value);
}
public override event EventHandler<ViewActivatingEventArgs> ViewActivating { add => _app.ViewActivating += value; remove => _app.ViewActivating -= value; }
public override event EventHandler<ViewActivatedEventArgs> ViewActivated { add => _app.ViewActivated += value; remove => _app.ViewActivated -= value; }
#endregion
}
#endregion
}

namespace System.Windows.Interop
Expand Down
83 changes: 83 additions & 0 deletions src/RhinoInside.Revit.External/UI/UIHostApplicationConstrained.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;

namespace RhinoInside.Revit.External.UI
{
sealed class UIHostApplicationConstrained : UIHostApplication
{
readonly UIControlledApplication _app;
public UIHostApplicationConstrained(UIControlledApplication app)
{
_app = app;

#if REVIT_2023
_app.SelectionChanged += SelectionChangedHandler;
#endif
}
public override void Dispose()
{
#if REVIT_2023
_app.SelectionChanged -= SelectionChangedHandler;
#endif
}
public override object Value => _app;
public override bool IsValid => true;

public override ApplicationServices.HostServices Services => new ApplicationServices.HostServicesC(_app.ControlledApplication);
public override UIDocument ActiveUIDocument { get => default; set => throw new InvalidOperationException(); }

#region UI
public override IntPtr MainWindowHandle
{
#if REVIT_2019
get => _app.MainWindowHandle;
#else
get => System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
#endif
}

internal override bool IsViewOpen(View view) => false;
#endregion

#region Ribbon
public override void CreateRibbonTab(string tabName) =>
_app.CreateRibbonTab(tabName);

public override RibbonPanel CreateRibbonPanel(Tab tab, string panelName) =>
_app.CreateRibbonPanel(tab, panelName);

public override RibbonPanel CreateRibbonPanel(string tabName, string panelName) =>
_app.CreateRibbonPanel(tabName, panelName);

public override IReadOnlyList<RibbonPanel> GetRibbonPanels(Tab tab) =>
_app.GetRibbonPanels(tab);

public override IReadOnlyList<RibbonPanel> GetRibbonPanels(string tabName) =>
_app.GetRibbonPanels(tabName);
#endregion

#region Addins
public override AddInId ActiveAddInId => _app.ActiveAddInId;
public override void LoadAddIn(string fileName) => _app.LoadAddIn(fileName);
public override ExternalApplicationArray LoadedApplications => _app.LoadedApplications;
#endregion

#region Commands
public override bool CanPostCommand(RevitCommandId commandId) => false;
public override void PostCommand(RevitCommandId commandId) => throw new InvalidOperationException();
#endregion

#region Events
public override event EventHandler<IdlingEventArgs> Idling
{
add => _app.Idling += ActivationGate.AddEventHandler(value);
remove => _app.Idling -= ActivationGate.RemoveEventHandler(value);
}
public override event EventHandler<ViewActivatingEventArgs> ViewActivating { add => _app.ViewActivating += value; remove => _app.ViewActivating -= value; }
public override event EventHandler<ViewActivatedEventArgs> ViewActivated { add => _app.ViewActivated += value; remove => _app.ViewActivated -= value; }
#endregion
}
}
Loading
Loading