Skip to content

Commit e776b2c

Browse files
Enhanced Ribbon management with internal Revit cleanup
- Added internal Revit cleanup using reflection to clear RibbonItemDictionary. - This effectively prevents "Panel already exists" errors when re-loading. - Improved panel identification and cleanup resilience. Co-authored-by: chuongmep <31106432+chuongmep@users.noreply.github.com>
1 parent f045b12 commit e776b2c

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

AddInManager/Model/RibbonUtils.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,25 @@ public static void RemovePanels(string assemblyPath)
5151
{
5252
try
5353
{
54+
string panelName = panel.Source.Title;
5455
tab.Panels.Remove(panel);
56+
57+
// Revit API internal cleanup to avoid "Panel already exists" error on re-load
58+
var uiApplicationType = typeof(UIApplication);
59+
var ribbonItemsProperty = uiApplicationType.GetProperty("RibbonItemDictionary",
60+
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly);
61+
if (ribbonItemsProperty != null)
62+
{
63+
var ribbonItems = (Dictionary<string, Dictionary<string, Autodesk.Revit.UI.RibbonPanel>>)
64+
ribbonItemsProperty.GetValue(null);
65+
if (ribbonItems != null)
66+
{
67+
foreach (var tabItem in ribbonItems.Values)
68+
{
69+
tabItem.Remove(panelName);
70+
}
71+
}
72+
}
5573
}
5674
catch (Exception e)
5775
{

0 commit comments

Comments
 (0)