public static ICollection <Apartment> GetApartments(Document doc) { try { FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id) .OfClass(typeof(SpatialElement)); IDictionary <string, Apartment> apartments = new Dictionary <string, Apartment>(); foreach (SpatialElement room in collector.ToElements()) { if (room.LookupParameter("APARTMENT NUMBER") != null && room.LookupParameter("APARTMENT NUMBER").AsString() != null && room.LookupParameter("APARTMENT NUMBER").AsString().Length != 0) { string aptNum = room.LookupParameter("APARTMENT NUMBER").AsString(); if (apartments.ContainsKey(aptNum)) { Apartment apt = apartments[aptNum]; apt.AddRoom(room); } else { Apartment newApartment = new Apartment(aptNum); newApartment.AddRoom(room); apartments.Add(aptNum, newApartment); } } } return(apartments.Values); } catch (Exception ex) { throw ex; } }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // Add an EventLogTraceListener object System.Diagnostics.Trace.Listeners.Add( new System.Diagnostics.EventLogTraceListener("Application")); // Lay the hands on the active document UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; // Set up a timer Timing myTimer = new Timing(); myTimer.StartTime(); try { var flatsGroupedByFlatNo = new FilteredElementCollector(doc, doc.ActiveView.Id) .WherePasses(new Autodesk.Revit.DB.Architecture.RoomFilter()) .WhereElementIsNotElementType() .ToElements() .Cast <Room>() .GroupBy(rm => rm.LookupParameter(FLAT_NO_PARAM).AsString()); IList <Apartment> aptsOnFloor = new List <Apartment>(); StringBuilder strBld = new StringBuilder(); foreach (var grp in flatsGroupedByFlatNo) { string number = grp.Key; Apartment apt = new Apartment(doc, number); foreach (Room rm in grp) { apt.AddRoom(rm); } aptsOnFloor.Add(apt); strBld.AppendLine(apt.ToString()); } System.Diagnostics.Trace.Write(strBld.ToString()); using (Transaction t = new Transaction(doc, "Set Area Parameters")) { t.Start(); foreach (Apartment apt in aptsOnFloor) { foreach (int id in apt.GetIds()) { Room rm = doc.GetElement(new ElementId(id)) as Room; rm.LookupParameter(TOTAL_AREA_SAW) .Set(apt.AreaNoWetAreas); rm.LookupParameter(TOTAL_AREA_PARAM) .Set(apt.TotalArea); } } t.Commit(); } TaskDialog.Show("Success", "The task has been completed successfully."); return(Result.Succeeded); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Cancelled); } catch (Exception ex) { TaskDialog.Show("Exception", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); System.Diagnostics.Trace.Write(string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); return(Result.Failed); } finally { myTimer.StopTime(); System.Diagnostics.Trace .Write(string.Format("Time elapsed: {0}s", myTimer.Duration.TotalSeconds)); } }