Let's say i'm trying to create several buttons for different categories and it supposed to filter the values of the category:
var filterPullDown = PullDownButtonUtilities.AddPulldownButton(app, TabName, filterElementName, "Filter", "Filter by Category", "Filter.png");
List<string> Kategorien = new List<string>
{
"heating", "lighting", "sanitary"
};
foreach (string kategorie in Kategorien)
{
PullDownButtonUtilities.AddPushButton(filterPullDown, kategorie, "MyPlugIn.Revit.FilterByCategoryCmd");
}
Where this is my PullDownButton Utility:
public static class PullDownButtonUtilities
{
public static PulldownButton AddPulldownButton(UIControlledApplication app, string tabName, string panelName, string pulldownButtonName, string pulldownButtonText, string iconPath = null)
{
RibbonPanel panel = app.CreateRibbonPanel(tabName, panelName);
PulldownButtonData pulldownButtonData = new PulldownButtonData(pulldownButtonName, pulldownButtonText);
PulldownButton pulldownButton = panel.AddItem(pulldownButtonData) as PulldownButton;
if (!string.IsNullOrEmpty(iconPath))
{
pulldownButton.LargeImage = ImageUtility.LoadImage(Assembly.GetExecutingAssembly(), iconPath);
}
return pulldownButton;
}
public static void AddPushButton(PulldownButton pulldownButton, string buttonText, string className)
{
var assembly = Assembly.GetExecutingAssembly();
var path = assembly.Location;
var buttonData = new PushButtonData(buttonText, buttonText, path, className);
pulldownButton.AddPushButton(buttonData);
}
}
How would I pass the category values to my command class so that it can filter elements according to the values set by the buttons?
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Document doc = uiDoc.Document;
try
{
// Get the active view
View activeView = doc.ActiveView;
// Collect all elements in the active view with the specified parameter value
FilteredElementCollector collector = new FilteredElementCollector(doc, activeView.Id);
IList<Element> elementsInView = collector.WhereElementIsNotElementType()
.Where(e => e.LookupParameter("Produktkategorie") != null
&& e.LookupParameter("Produktkategorie").HasValue
&& e.LookupParameter("Produktkategorie").AsString() == kategorie)
.ToList();
if (elementsInView == null || !elementsInView.Any())
{
message = $"No elements found in the active view with the category: {kategorie}";
TaskDialog.Show("No Elements Found", message);
return Result.Failed;
}
ICollection<ElementId> elementIds = elementsInView.Select(e => e.Id).ToList();
// Select elements
uiDoc.Selection.SetElementIds(elementIds);
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
TaskDialog.Show("Error", message);
return Result.Failed;
}
}