-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathRibbonUtils.cs
More file actions
86 lines (79 loc) · 3.3 KB
/
Copy pathRibbonUtils.cs
File metadata and controls
86 lines (79 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Autodesk.Revit.UI;
using Autodesk.Windows;
using RibbonPanel = Autodesk.Revit.UI.RibbonPanel;
namespace RevitAddinManager.Model;
public static class RibbonUtils
{
private static readonly FieldInfo RibbonPanelField = typeof(RibbonPanel).GetField("m_RibbonPanel", BindingFlags.Instance | BindingFlags.NonPublic);
public static Autodesk.Windows.RibbonPanel GetRibbonPanel(RibbonPanel panel)
{
return RibbonPanelField.GetValue(panel) as Autodesk.Windows.RibbonPanel;
}
public static void RemovePanels(string assemblyPath)
{
try
{
var ribbon = ComponentManager.Ribbon;
string assemblyName = System.IO.Path.GetFileNameWithoutExtension(assemblyPath);
foreach (var tab in ribbon.Tabs)
{
var panelsToRemove = new List<Autodesk.Windows.RibbonPanel>();
foreach (var panel in tab.Panels)
{
bool shouldRemove = false;
foreach (var item in panel.Source.Items)
{
if (item is Autodesk.Windows.RibbonButton button)
{
// Heuristic check: check button ID, text, or tooltips for the assembly name
if (button.Id != null && (button.Id.Contains(assemblyName) || button.Id.Contains(assemblyPath)))
{
shouldRemove = true;
break;
}
}
}
if (shouldRemove)
{
panelsToRemove.Add(panel);
}
}
foreach (var panel in panelsToRemove)
{
try
{
string panelName = panel.Source.Title;
tab.Panels.Remove(panel);
// Revit API internal cleanup to avoid "Panel already exists" error on re-load
var uiApplicationType = typeof(UIApplication);
var ribbonItemsProperty = uiApplicationType.GetProperty("RibbonItemDictionary",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly);
if (ribbonItemsProperty != null)
{
var ribbonItems = (Dictionary<string, Dictionary<string, Autodesk.Revit.UI.RibbonPanel>>)
ribbonItemsProperty.GetValue(null);
if (ribbonItems != null)
{
foreach (var tabItem in ribbonItems.Values)
{
tabItem.Remove(panelName);
}
}
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error removing panels: {ex.Message}");
}
}
}