Introduction to Searching using the Revit API
This video will get you started understanding search using a loop and LINQ (language integrated query).
Looping is very common and easy to understand as you move over your list and compare it to your search using == as equals. If your search item is found or true the rest of your code will execute.
LINQ is very powerful and is very commonly used, but can be challenging to understand at the beginning. The benefit is less code to write and very efficient search.
The first part of this video is to shows how to make a method for reusable code. Using the loop over the ICollection elements such as levels, floors, and walls adding them to an empty list and return the list.
Sample code:
namespace MyTest
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Revit application documents.
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
List<Level> levels = getLevels(doc);
List<Floor> floors = getFloors(doc);
List<Wall> walls = getWalls(doc);
foreach (Level l in levels)
{
if (l.Name == "Level 2")
{
TaskDialog.Show("id", l.Id.ToString());
}
}
Floor f = floors.Find(x => x.Name == "S18");
Wall w = walls.Find(x => x.Name == "SW12");
Element solidFill = new FilteredElementCollector(doc)
.OfClass(typeof(FillPatternElement))
.Where(q => q.Name.Equals("Solid fill")).First();
OverrideGraphicSettings ogs = new OverrideGraphicSettings();
ogs.SetProjectionFillColor(new Color(255, 0, 0));
ogs.SetProjectionFillPatternId(solidFill.Id);
using (Transaction t = new Transaction(doc, "search"))
{
try
{
t.Start();
doc.ActiveView.SetElementOverrides(f.Id, ogs);
doc.ActiveView.SetElementOverrides(w.Id, ogs);
t.Commit();
}
catch { }
}
return Result.Succeeded;
}
//*****************************(getLevels)*****************************
public List<Level> getLevels(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> levels = collector.OfClass(typeof(Level)).ToElements();
List<Level> List_levels = new List<Level>();
foreach(Level level in levels)
{
List_levels.Add(level);
}
return List_levels;
}
//*****************************(getFloors)*****************************
public List<Floor> getFloors(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> floors = collector.OfClass(typeof(Floor)).ToElements();
List<Floor> List_Floors = new List<Floor>();
foreach (Floor floor in floors)
{
List_Floors.Add(floor);
}
return List_Floors;
}
//*****************************(getWalls)*****************************
public List<Wall> getWalls(Document doc)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> walls = collector.OfClass(typeof(Wall)).ToElements();
List<Wall> List_Walls = new List<Wall>();
foreach (Wall w in walls)
{
List_Walls.Add(w);
}
return List_Walls;
}
}
}
Tips:
- I would suggest to take a closer look at Linq.
Comments
Post a Comment