Beginner's Guide to Selecting Element With The Revit API
This video will help you understand how to select elements in your current Revit document through the Revit API. Programmatically select a single model element or select multiple model elements, then looping through each one to create a list of elements "List<Element>".
Tips:
- Using Linq - A Microsoft query language. I use a lambda expression which is simpler than using an "if" statement and going through the list.
- Using text - A string builder which will allow us to append strings to the end of the list. I convert it back to a regular string using the “ToString()”
Sample Code
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
IList<Reference> pickedObjs = uidoc.Selection.PickObjects(ObjectType.Element, "Select elements");
List<ElementId> ids = (from Reference r in pickedObjs select r.ElementId).ToList();
using (Transaction tx = new Transaction(doc))
{
StringBuilder sb = new StringBuilder();
tx.Start("transaction");
if (pickedObjs != null && pickedObjs.Count > 0)
{
foreach (ElementId eid in ids)
{
Element e = doc.GetElement(eid);
sb.Append("\n" + e.Name);
}
TaskDialog.Show("title :) ", sb.ToString());
}
tx.Commit();
}
Sample Code
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
Reference reference = uidoc.Selection.PickObject(ObjectType.Element);
Element element = uidoc.Document.GetElement(reference);
using (Transaction tx = new Transaction(doc))
{
TaskDialog.Show("title :) ", element .Name);
}
tx.Commit();
}
Comments
Post a Comment