We have talked about how to access ribbons, created by RevitAddinWizard, from a different addin last time. Let’s see how to update the ribbons this time based on the same ribbon naming convention.
Let’s also take the comprehensive ribbon we introduced earlier for example.
Supposing we’d like to update the ToolTip of the first TextBox of the first item group in the second Stacked container of the Non-SlideOut, what shall we do?
The following code does so after all these ribbons have been analyzed into well categorized groups beforehand:
groups.First(g => g.SlideOut == false) //Non-SlideOut
.Containers.First(c=>c.Container=="Stacked").Groups //All Stacked containers
.ElementAt(2) //The 1st group of the 2nd Stacked container
.Items.First(i => i.Item.ItemType == RibbonItemType.TextBox) //The 1st TextBox
.Item.ToolTip = "Updated ToolTip of the TextBox"; //Its ToolTip
Another task, supposing we’d like to disable the second PushButton in the Non-SlideOut, what shall we do?
The following code does so:
// Disable the second PushButton in the Non-SlideOut.
groups.First(g => g.SlideOut == false) //Non-SlideOut
.Containers.First(c => c.Container == "Panel").Groups //All Panel containers
.ElementAt(0) //The 1st group of in the Panel container
.Items.ElementAt(1) //The 2nd Item (PushButton)
.Item.Enabled = false; //Disable it!
One more, supposing we’d like to replace the image of the second ToggleButton of the first RadioButtonGroup in the Non-SlideOut, what shall we do?
The following code does so:
// Replace the LargeImage of the 2nd ToggleButton of the 1st RadioButtonGroup in the Non-SlideOut.
RadioButtonGroup rbGroup =
groups.First(g => g.SlideOut == false) //Non-SlideOut
.Containers.First(c => c.Container == "Panel").Groups //All Panel containers
.First(g=>g.Items.ElementAt(0).
Item.ItemType == RibbonItemType.RadioButtonGroup)//The 1st RadioButtonGroup
.Items.First(i=>i.Item.ItemType == RibbonItemType.RadioButtonGroup).Item as RadioButtonGroup;
ToggleButton tb = rbGroup.GetItems()[1]; //The 2nd member/ToggleButton
tb.LargeImage = BmpImageSource("RevitAddinCS1.Resources.ExtCmd32x32.bmp"); //Replace the LargeImage!
private System.Windows.Media.ImageSource BmpImageSource(string embeddedPath)
{
Stream stream = this.GetType().Assembly.GetManifestResourceStream(embeddedPath);
var decoder = new System.Windows.Media.Imaging.BmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
return decoder.Frames[0];
}
Of course, if the item name is known we can also access it through the name directly. For example, the following code disables the ComboBox in the SlideOut:
//Hide the ComboBox group in the SlideOut using its name.
panel.GetItems().First(i => i.Name == "SlideOut_cnt10Panel_grp1").Visible = false;
All the above code along with some relevant tool classes and methods has been appended below:
class ProItem
{
public bool SlideOut { get; set; }
public string Container { get; set; }
public string GroupID { get; set; }
public int Index { get; set; }
public RibbonItem Item { get; set; }
}
string GetGroupID(string name)
{
string id = name.Substring(name.IndexOf("cnt"), name.IndexOf("grp") - name.IndexOf("cnt") + 4);
if (id[4] < '0' || id[4] > '9')
id = id.Insert(3, "0");
if (id[id.Length - 2] < '0' || id[id.Length - 2] > '9')
id = id.Insert(id.Length - 1, "0");
return id;
}
int GetIndex(string name)
{
if (name.Contains("item"))
return int.Parse(name.Substring(name.IndexOf("item") + 4));
return 0;
}
void WriteItemMembers(StreamWriter sw, RibbonItem item)
{
List<RibbonItem> members = null;
switch (item.ItemType)
{
case RibbonItemType.PulldownButton:
members = (item as PulldownButton).GetItems().Select(e => e as RibbonItem).ToList();
break;
case RibbonItemType.RadioButtonGroup:
members = (item as RadioButtonGroup).GetItems().Select(e => e as RibbonItem).ToList();
break;
case RibbonItemType.ComboBox:
members = (item as Autodesk.Revit.UI.ComboBox).GetItems().Select(e => e as RibbonItem).ToList();
break;
case RibbonItemType.SplitButton:
members = (item as SplitButton).GetItems().Select(e => e as RibbonItem).ToList();
break;
default:
return;
}
foreach (RibbonItem mem in members)
{
sw.WriteLine(string.Format("\t\t\tMember: {0} ({1})", mem.ItemType, mem.ItemText));
}
}
private System.Windows.Media.ImageSource BmpImageSource(string embeddedPath)
{
Stream stream = this.GetType().Assembly.GetManifestResourceStream(embeddedPath);
var decoder = new System.Windows.Media.Imaging.BmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
return decoder.Frames[0];
}
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
List<RibbonPanel> allAddinPanels = CachedUiApp.GetRibbonPanels();
RibbonPanel panel = allAddinPanels.FirstOrDefault(p => p.Name.Equals("ABC_APP3_PNL2"));
if (panel != null)
{
var names = from item in panel.GetItems() select item.Name;
var items = from item in panel.GetItems()
select new ProItem
{
Item = item,
SlideOut = item.Name.StartsWith("SlideOut_"),
Container = item.Name.Contains("Stacked_") ? "Stacked" : "Panel",
GroupID = GetGroupID(item.Name),
Index = GetIndex(item.Name)
};
var groups = from itm in items
orderby itm.SlideOut
group itm by itm.SlideOut into g1
select new
{
SlideOut = g1.Key,
Containers = from o in g1
orderby o.Container
group o by o.Container into g2
select new
{
Container = g2.Key,
Groups = from c in g2
orderby c.GroupID
group c by c.GroupID into g3
select new
{
Group = g3.Key,
Items = g3
}
}
};
//Update the ToolTip of the 1st TextBox of the first item group of
//the 2nd Stacked container of the Non-SlideOut.
groups.First(g => g.SlideOut == false) //Non-SlideOut
.Containers.First(c=>c.Container=="Stacked").Groups //All Stacked containers
.ElementAt(2) //The 1st group of the 2nd Stacked container
.Items.First(i => i.Item.ItemType == RibbonItemType.TextBox) //The 1st TextBox
.Item.ToolTip = "Updated ToolTip of the TextBox"; //Its ToolTip
// Disable the second PushButton in the Non-SlideOut.
groups.First(g => g.SlideOut == false) //Non-SlideOut
.Containers.First(c => c.Container == "Panel").Groups //All Panel containers
.ElementAt(0) //The 1st group of in the Panel container
.Items.ElementAt(1) //The 2nd Item (PushButton)
.Item.Enabled = false; //Disable it!
// Replace the LargeImage of the 2nd ToggleButton of the 1st RadioButtonGroup in the Non-SlideOut.
RadioButtonGroup rbGroup =
groups.First(g => g.SlideOut == false) //Non-SlideOut
.Containers.First(c => c.Container == "Panel").Groups //All Panel containers
.First(g=>g.Items.ElementAt(0).
Item.ItemType == RibbonItemType.RadioButtonGroup)//The 1st RadioButtonGroup
.Items.First(i=>i.Item.ItemType == RibbonItemType.RadioButtonGroup).Item as RadioButtonGroup;
ToggleButton tb = rbGroup.GetItems()[1]; //The 2nd member/ToggleButton
tb.LargeImage = BmpImageSource("RevitAddinCS1.Resources.ExtCmd32x32.bmp"); //Replace the LargeImage!
//Hide the ComboBox group in the SlideOut using its name.
panel.GetItems().First(i => i.Name == "SlideOut_cnt10Panel_grp1").Visible = false;
}
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
Here are the ribbon screenshots before and after the update for comparison purpose:
So with the naming convention introduced by RevitAddinWizard things are all under control.
Links to some related articles:
Ribbon of Revit API - PushButton And TextBox
Use Ribbon Creator of RevitAddCoder to Create PushButton And Separator
Use Ribbon Creator of RevitAddCoder to Create TextBox
Ribbon of Revit API - ComboBox And ComboBoxMember
Use Ribbon Creator of RevitAddCoder to Create ComboBox And ComboBoxMember
Ribbon of Revit API - PulldownButton And SplitButton
Use Ribbon Creator of RevitAddCoder to Create PulldownButton And PushButton
Use Ribbon Creator of RevitAddCoder to Create SplitButton And PushButton
Ribbon of Revit API - RadioButtonGroup And ToggleButton
Use Ribbon Creator of RevitAddCoder to Create RadioButtonGroup And ToggleButton
Ribbon of Revit API - Stacked Group And AddStackedItems
Ribbon of Revit API - Stacked Group And PulldownButton
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And PushButton Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And TextBox Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And PulldownButton Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And ComboBox Items
Use Ribbon Creator of RevitAddCoder to Create Stacked Group And Various Items
Ribbon of Revit API - Slideout
Use Ribbon Creator of RevitAddCoder to Create Slideout And Various Buttons
Ribbon of Revit API - Manipulate Panels Created by Other Addins
Access Ribbons Created by Ribbon Creator From Another Addin
Update Ribbons Created by Ribbon Creator From Another Addin
Use Ribbon Creator of RevitAddCoder to Create a Comprehensive Ribbon Panel
Ribbon of Revit API - Title And Name of Panels
Ribbon of Revit API - Text And Name of RibbonItem
Ribbon of Revit API - LongDescription And TooltipImage of RibbonItem
Recent Comments