We have introduced how to create various ribbon types using RevitAddinWizard in some early posts. Now let’s recap something important besides the convenience brought about by the friendly User Interface of the wizard. A good naming convention is necessary and critical for successful ribbon creations; otherwise it may become very difficult or even impossible to access those ribbons or update them.
Since the wizard follows such a good practise when creating ribbons it becomes somewhat straightforward to access them even if from a different addin. Of course there will be still some work but with the assistance of some technologies such as LINQ things are pretty manageable.
Let’s take the comprehensive ribbon we introduced previously for example. With the following code called from another AddIn other than the one which created the comprehensive ribbon:
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));
}
}
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
}
}
};
using (StreamWriter sw = new StreamWriter(@"c:\ribbons.txt"))
{
foreach (var slideout in groups)
{
sw.WriteLine(string.Format("SlideOut: {0}", slideout.SlideOut));
foreach (var container in slideout.Containers)
{
sw.WriteLine(string.Format("\tContainer: {0}", container.Container));
foreach (var group in container.Groups)
{
foreach (var item in group.Items)
{
sw.WriteLine(string.Format("\t\tItem: {0} ({1})", item.Item.ItemType, item.Item.ItemText));
WriteItemMembers(sw, item.Item);
}
}
}
}
}
}
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
all necessary information about the ribbons can be retrieved back including whether in a SlideOut or not, whether Stacked or not, which groups they belong to, and what members they have. More importantly, the items and groups can appear in the same order as they are created.
The resultant text file has been appended below:
SlideOut: False
Container: Panel
Item: PushButton (PushButton1)
Item: PushButton (PushButton2)
Item: RadioButtonGroup ()
Member: ToggleButton (ToggleButton1)
Member: ToggleButton (ToggleButton2)
Item: SplitButton (98172aef-4ffa-4043-b95e-05845c662653)
Member: PushButton (PushButton3)
Member: PushButton (PushButton4)
Container: Stacked
Item: PushButton (PushButton5)
Item: PushButton (PushButton6)
Item: PulldownButton (PulldownButton1)
Member: PushButton (PushButton7)
Member: PushButton (PushButton8)
Item: TextBox ()
Item: TextBox ()
Item: ComboBox (ComboBox1)
Member: ComboBoxMember (ComboBoxMember1)
Member: ComboBoxMember (ComboBoxMember2)
Member: ComboBoxMember (ComboBoxMember3)
SlideOut: True
Container: Panel
Item: PushButton (PushButton9)
Item: PushButton (PushButton10)
Item: PushButton (PushButton11)
Item: PulldownButton (PulldownButton2)
Member: PushButton (PushButton12)
Member: PushButton (PushButton13)
Item: SplitButton (de315e6d-dafd-440a-bc29-fe8cfb1b65d5)
Member: PushButton (PushButton14)
Member: PushButton (PushButton15)
Member: PushButton (PushButton16)
Item: PushButton (PushButton17)
Item: TextBox ()
Item: ComboBox (ComboBox2)
Member: ComboBoxMember (ComboBoxMember4)
Member: ComboBoxMember (ComboboxMember5)
Readers may notice the Separator information is lost but that is not something RevitAddinWizard can do about.
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