Skip to content

Commit

Permalink
Sync Graph Improvements. Now supports the ability to hide the graph b…
Browse files Browse the repository at this point in the history
…uttons if the DYN and folder disappeared.
  • Loading branch information
johnpierson committed Apr 19, 2024
1 parent 5c25403 commit 28d0cce
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 90 deletions.
Binary file modified _Release/Revit2025/Relay/Relay.dll
Binary file not shown.
2 changes: 0 additions & 2 deletions src/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
RibbonUtils.SyncGraphs(uiapp);
RibbonUtils.HideUnused();



return Result.Succeeded;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Utilities/Globals.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Reflection;
using Autodesk.Revit.UI;

namespace Relay.Utilities
{
Expand All @@ -24,5 +25,8 @@ public partial class Globals

public static bool ResetRibbonOnSync { get; set; } = false;

public static Dictionary<string, RibbonItem> RelayButtons = new Dictionary<string, RibbonItem>();
public static Dictionary<string, List<RibbonItem>> RelayPanels = new Dictionary<string, List<RibbonItem>>();

}
}
145 changes: 57 additions & 88 deletions src/Utilities/RibbonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using Relay.Classes;
using System.Reflection;
using Autodesk.Revit.DB.Electrical;

namespace Relay.Utilities
{
Expand Down Expand Up @@ -90,6 +91,8 @@ public static void AddItems(Autodesk.Revit.UI.RibbonPanel panelToUse, string[] d
return;
}

List<RibbonItem> createdButtons = new List<RibbonItem>();

var splitButtons = SplitList(pushButtonDatas, 2);

foreach (var buttonGroup in splitButtons)
Expand All @@ -98,83 +101,70 @@ public static void AddItems(Autodesk.Revit.UI.RibbonPanel panelToUse, string[] d
{
case 2:
var stack = panelToUse.AddStackedItems(buttonGroup[0], buttonGroup[1]);
createdButtons.Add(stack[0]);
createdButtons.Add(stack[1]);
break;
case 1:
panelToUse.AddItem(buttonGroup[0]);
var singleButton = panelToUse.AddItem(buttonGroup[0]);
createdButtons.Add(singleButton);
break;
}
}

foreach (var ribbonItem in createdButtons)
{
Globals.RelayButtons.Add(ribbonItem.ToolTip.GetStringBetweenCharacters('[', ']'), ribbonItem);
}

//now add the buttons to our panel dictionary for later
if (Globals.RelayPanels.ContainsKey(panelToUse.Name))
{
Globals.RelayPanels[panelToUse.Name] = createdButtons;
}
else
{
Globals.RelayPanels.Add(panelToUse.Name, createdButtons);
}

}

public static void HideUnused()
{
AW.RibbonControl ribbon = AW.ComponentManager.Ribbon;

foreach (AW.RibbonTab tab in ribbon.Tabs)
foreach (var key in Globals.RelayButtons.Keys)
{
if (tab.Name == Globals.RibbonTabName)
Globals.RelayButtons.TryGetValue(key, out RibbonItem ribbonItem);

if (ribbonItem != null)
{
foreach (var ribbonPanel in tab.Panels)
{
if (ribbonPanel.Source.Title != "Setup")
{
//hide every button that no longer has a dyn backing it
foreach (var ribbonItem in ribbonPanel.Source.Items)
{
if(ribbonItem.Description is null) continue;
AW.RibbonItem item = ribbonItem;
if (ribbonItem is AW.RibbonButton ribbonButton)
{
item = ribbonButton;
}


var dynPath = ribbonItem.Description.GetStringBetweenCharacters('[', ']');

if (!File.Exists(dynPath))
{
item.IsVisible = false;
}

}

//now hide the panel if all the buttons are gone
if (ribbonPanel.Source.Items.All(i => !i.IsVisible))
{
ribbonPanel.IsVisible = false;
}

}
}
var path = ribbonItem.ToolTip.GetStringBetweenCharacters('[', ']');

//check if the file exists, if not, hide the button and remove the button from our dictionary
if (File.Exists(path)) continue;
ribbonItem.Visible = false;

Globals.RelayButtons.Remove(key);
}
}
}
public static void ClearRibbon()
{
AW.RibbonControl ribbon = AW.ComponentManager.Ribbon;

foreach (AW.RibbonTab tab in ribbon.Tabs)
//now hide empty panels if there are any
foreach (var panel in Globals.RelayPanels.Keys)
{
if (tab.Name == Globals.RibbonTabName)
var worked = Globals.RelayPanels.TryGetValue(panel, out List<RibbonItem> ribbonItems);
if (worked)
{
foreach (var ribbonPanel in tab.Panels)
var allHidden = ribbonItems.All(r => !r.Visible);

if (allHidden)
{
if (ribbonPanel.Source.Title != "Setup")
{
//hide every button
foreach (var ribbonItem in ribbonPanel.Source.Items)
{
ribbonItem.IsVisible = false;
}

//now hide the panel
ribbonPanel.IsVisible = false;
}
//k, all the buttons are hidden. get the panel to hide now
var ribbonPanel = GetPanel(Globals.RibbonTabName, panel);
ribbonPanel.IsVisible = false;
}

}
}
}

private static string GetDescription(FileInfo fInfo)
{
string description;
Expand Down Expand Up @@ -227,36 +217,9 @@ public static AW.RibbonItem GetButton(string tabName, string panelName, string i
{
if (panel.Source.Title == panelName)
{
AW.RibbonItem ribbonItem = null;
foreach (var item in panel.Source.Items)
{
if (item is AW.RibbonRowPanel ribbonPanel)
{
foreach (var rItem in ribbonPanel.Items)
{
if (rItem.Id.Contains(itemName))
{
ribbonItem = rItem;
break;
}
}
}

if (item.Id.Contains(itemName))
{
ribbonItem = item;
break;
}
}

if (ribbonItem == null) return null;

if (!ribbonItem.IsVisible)
{
return null;
}

return ribbonItem;
return panel.FindItem("CustomCtrl_%CustomCtrl_%"
+ tabName + "%" + panelName + "%" + itemName,
true) as AW.RibbonItem;
}
}
}
Expand Down Expand Up @@ -351,10 +314,13 @@ public static void SyncGraphs(UIApplication uiapp)
try
{
panelToUse = uiapp.CreateRibbonPanel(potentialTab, dInfo.Name);
panelToUse.Title = dInfo.Name;
}
catch (Exception)
{
panelToUse = uiapp.GetRibbonPanels(potentialTab).First(p => p.Name.Equals(dInfo.Name));

panelToUse.Visible = true;
}

//find the files that do not have a button yet
Expand All @@ -363,7 +329,11 @@ public static void SyncGraphs(UIApplication uiapp)

foreach (var file in Directory.GetFiles(directory, "*.dyn"))
{
if (RibbonUtils.GetButton(potentialTab, dInfo.Name, $"relay{new FileInfo(file).Name.Replace(" ", "")}") == null)
FileInfo fInfo = new FileInfo(file);

var dynPath = fInfo.FullName;

if (!Globals.RelayButtons.ContainsKey(dynPath))
{
toCreate.Add(file);
}
Expand All @@ -376,7 +346,6 @@ public static void SyncGraphs(UIApplication uiapp)
if (toCreate.Any())
{
RibbonUtils.AddItems(panelToUse, toCreate.ToArray(), forceLargeIcons);

}
}
}
Expand Down

0 comments on commit 28d0cce

Please sign in to comment.