// // (C) Copyright 2003-2023 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.DB.Mechanical; namespace Revit.SDK.Samples.AddSpaceAndZone.CS { /// /// The DataManager Class is used to obtain, create or edit the Space elements and Zone elements. /// public class DataManager { ExternalCommandData m_commandData; List m_levels; SpaceManager m_spaceManager; ZoneManager m_zoneManager; Level m_currentLevel; Phase m_defaultPhase; /// /// The constructor of DataManager class. /// /// The ExternalCommandData public DataManager(ExternalCommandData commandData) { m_commandData = commandData; m_levels = new List(); Initialize(); m_currentLevel = m_levels[0]; Parameter para = commandData.Application.ActiveUIDocument.Document.ActiveView.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.VIEW_PHASE); Autodesk.Revit.DB.ElementId phaseId = para.AsElementId(); m_defaultPhase = commandData.Application.ActiveUIDocument.Document.GetElement(phaseId) as Phase; } /// /// Initialize the data member, obtain the Space and Zone elements. /// private void Initialize() { Dictionary> spaceDictionary = new Dictionary>(); Dictionary> zoneDictionary = new Dictionary>(); Document activeDoc = m_commandData.Application.ActiveUIDocument.Document; FilteredElementIterator levelsIterator = (new FilteredElementCollector(activeDoc)).OfClass(typeof(Level)).GetElementIterator(); FilteredElementIterator spacesIterator =(new FilteredElementCollector(activeDoc)).WherePasses(new SpaceFilter()).GetElementIterator(); FilteredElementIterator zonesIterator = (new FilteredElementCollector(activeDoc)).OfClass(typeof(Zone)).GetElementIterator(); levelsIterator.Reset(); while (levelsIterator.MoveNext()) { Level level = levelsIterator.Current as Level; if (level != null) { m_levels.Add(level); spaceDictionary.Add(level.Id, new List()); zoneDictionary.Add(level.Id, new List()); } } spacesIterator.Reset(); while (spacesIterator.MoveNext()) { Space space = spacesIterator.Current as Space; if (space != null) { spaceDictionary[space.LevelId].Add(space); } } zonesIterator.Reset(); while (zonesIterator.MoveNext()) { Zone zone = zonesIterator.Current as Zone; if (zone != null && activeDoc.GetElement(zone.LevelId) != null) { zoneDictionary[zone.LevelId].Add(zone); } } m_spaceManager = new SpaceManager(m_commandData, spaceDictionary); m_zoneManager = new ZoneManager(m_commandData, zoneDictionary); } /// /// Get the Level elements. /// public ReadOnlyCollection Levels { get { return new ReadOnlyCollection(m_levels); } } /// /// Create a Zone element. /// public void CreateZone() { if (m_defaultPhase == null) { Autodesk.Revit.UI.TaskDialog.Show("Revit", "The phase of the active view is null, you can't create zone in a null phase"); return; } try { this.m_zoneManager.CreateZone(m_currentLevel, m_defaultPhase); } catch (Exception ex) { Autodesk.Revit.UI.TaskDialog.Show("Revit", ex.Message); } } /// /// Create some spaces. /// public void CreateSpaces() { if (m_defaultPhase == null) { Autodesk.Revit.UI.TaskDialog.Show("Revit", "The phase of the active view is null, you can't create spaces in a null phase"); return; } try { if (m_commandData.Application.ActiveUIDocument.Document.ActiveView.ViewType == Autodesk.Revit.DB.ViewType.FloorPlan) { m_spaceManager.CreateSpaces(m_currentLevel, m_defaultPhase); } else { Autodesk.Revit.UI.TaskDialog.Show("Revit", "You can not create spaces in this plan view"); } } catch (Exception ex) { Autodesk.Revit.UI.TaskDialog.Show("Revit", ex.Message); } } /// /// Get the Space elements. /// /// A space list in current level. public List GetSpaces() { return m_spaceManager.GetSpaces(m_currentLevel); } /// /// Get the Zone elements. /// /// A Zone list in current level. public List GetZones() { return m_zoneManager.GetZones(m_currentLevel); } /// /// Update the current level. /// /// public void Update(Level level) { this.m_currentLevel = level; } } }