This Revit Macro will get the last element placed in the model. It works by finding the largest Element ID number in the model.
public void GetLastElement()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = this.ActiveUIDocument.Document;
Autodesk.Revit.DB.View pView = ActiveUIDocument.Document.ActiveView;
Selection selection = uidoc.Selection;
ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
//get all the elements in the model;
ElementClassFilter familyInstanceFilter = new ElementClassFilter( typeof( FamilyInstance ) );
FilteredElementCollector familyInstanceCollector = new FilteredElementCollector( doc );
IList<Element> elementsCollection = familyInstanceCollector.WherePasses( familyInstanceFilter ).ToElements();
IList<Element> modelElements = new List<Element>();
foreach( Element e in elementsCollection )
{
if( ( null != e.Category ) && ( null != e.LevelId ) && ( null != e.get_Geometry( new Options() ) ) )
{
modelElements.Add( e );
}
}
//FIND THE HIGHEST ELEMENT ID
//Element e1 = elementsCollection.FirstOrDefault();
Element e1 = modelElements.FirstOrDefault ();
ElementId last_eid = e1.Id;
foreach (Element e in modelElements)
{
if (e.Id > last_eid)
{
last_eid = e.Id;
}
}
//ElementId last_eid = modelElements.Max();
//TaskDialog Box for Troubleshooting -
//TaskDialog.Show ("Last Element ID = ", last_eid.IntegerValue.ToString());
//Get the last element placed:
Element last_e = doc.GetElement(last_eid );
using (Transaction trans = new Transaction(doc, "Select Last Element Placed"))
{
if( last_eid != null)
trans.Start();
{
ICollection<ElementId> LastelementId = new List<ElementId>();
LastelementId.Add(last_eid);
selection.SetElementIds(LastelementId );
}
trans.Commit();
}
}