// // (C) Copyright 2003-2019 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.Windows.Forms; using System.Text; using System.Linq; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Revit.SDK.Samples.NewRoof.RoofForms.CS; namespace Revit.SDK.Samples.NewRoof.RoofsManager.CS { /// /// The kinds of roof than can be created. /// public enum CreateRoofKind { FootPrintRoof, ExtrusionRoof }; /// /// The RoofsManager is used to manage the operations between Revit and UI. /// public class RoofsManager { // To store a reference to the commandData. ExternalCommandData m_commandData; // To store the levels info in the Revit. List m_levels; // To store the roof types info in the Revit. List m_roofTypes; // To store a reference to the FootPrintRoofManager to create footprint roof. FootPrintRoofManager m_footPrintRoofManager; // To store a reference to the ExtrusionRoofManager to create extrusion roof. ExtrusionRoofManager m_extrusionRoofManager; // To store the selected elements in the Revit Selection m_selection; // roofs list ElementSet m_footPrintRoofs; ElementSet m_extrusionRoofs; // To store the footprint roof lines. Autodesk.Revit.DB.CurveArray m_footPrint; // To store the profile lines. Autodesk.Revit.DB.CurveArray m_profile; // Reference Plane for creating extrusion roof List m_referencePlanes; // Transaction for manual mode Transaction m_transaction; // Current creating roof kind. public CreateRoofKind RoofKind; /// /// The constructor of RoofsManager class. /// /// The ExternalCommandData public RoofsManager(ExternalCommandData commandData) { m_commandData = commandData; m_levels = new List(); m_roofTypes = new List(); m_referencePlanes = new List(); m_footPrint = new CurveArray(); m_profile = new CurveArray(); m_footPrintRoofManager = new FootPrintRoofManager(commandData); m_extrusionRoofManager = new ExtrusionRoofManager(commandData); m_selection = commandData.Application.ActiveUIDocument.Selection; m_transaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "Document"); RoofKind = CreateRoofKind.FootPrintRoof; Initialize(); } /// /// Initialize the data member /// private void Initialize() { Document doc = m_commandData.Application.ActiveUIDocument.Document; FilteredElementIterator iter = (new FilteredElementCollector(doc)).OfClass(typeof(Level)).GetElementIterator(); iter.Reset(); while (iter.MoveNext()) { m_levels.Add(iter.Current as Level); } FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc); filteredElementCollector.OfClass(typeof(RoofType)); m_roofTypes = filteredElementCollector.Cast().ToList(); // FootPrint Roofs m_footPrintRoofs = new ElementSet(); iter = (new FilteredElementCollector(doc)).OfClass(typeof(FootPrintRoof)).GetElementIterator(); iter.Reset(); while (iter.MoveNext()) { m_footPrintRoofs.Insert(iter.Current as FootPrintRoof); } // Extrusion Roofs m_extrusionRoofs = new ElementSet(); iter = (new FilteredElementCollector(doc)).OfClass(typeof(ExtrusionRoof)).GetElementIterator(); iter.Reset(); while (iter.MoveNext()) { m_extrusionRoofs.Insert(iter.Current as ExtrusionRoof); } // Reference Planes iter = (new FilteredElementCollector(doc)).OfClass(typeof(ReferencePlane)).GetElementIterator(); iter.Reset(); while (iter.MoveNext()) { ReferencePlane plane = iter.Current as ReferencePlane; // just use the vertical plane if (Math.Abs(plane.Normal.DotProduct(Autodesk.Revit.DB.XYZ.BasisZ)) < 1.0e-09) { if (plane.Name == "Reference Plane") { plane.Name = "Reference Plane" + "(" + plane.Id.IntegerValue.ToString() + ")"; } m_referencePlanes.Add(plane); } } } /// /// Get the Level elements. /// public ReadOnlyCollection Levels { get { return new ReadOnlyCollection(m_levels); } } /// /// Get the RoofTypes. /// public ReadOnlyCollection RoofTypes { get { return new ReadOnlyCollection(m_roofTypes); } } /// /// Get the RoofTypes. /// public ReadOnlyCollection ReferencePlanes { get { return new ReadOnlyCollection(m_referencePlanes); } } /// /// Get all the footprint roofs in Revit. /// public ElementSet FootPrintRoofs { get { return m_footPrintRoofs; } } /// /// Get all the extrusion roofs in Revit. /// public ElementSet ExtrusionRoofs { get { return m_extrusionRoofs; } } /// /// Get the footprint roof lines. /// public CurveArray FootPrint { get { return m_footPrint; } } /// /// Get the extrusion profile lines. /// public CurveArray Profile { get { return m_profile; } } /// /// Select elements in Revit to obtain the footprint roof lines or extrusion profile lines. /// /// A curve array to hold the footprint roof lines or extrusion profile lines. public CurveArray WindowSelect() { if (RoofKind == CreateRoofKind.FootPrintRoof) { return SelectFootPrint(); } else { return SelectProfile(); } } /// /// Select elements in Revit to obtain the footprint roof lines. /// /// A curve array to hold the footprint roof lines. public CurveArray SelectFootPrint() { m_footPrint.Clear(); while (true) { ElementSet es = new ElementSet(); foreach (ElementId elementId in m_selection.GetElementIds()) { es.Insert(m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId)); } es.Clear(); IList selectResult; try { selectResult = m_selection.PickElementsByRectangle(); } catch (Exception ex) { Console.WriteLine(ex.Message); break; } if (selectResult.Count != 0) { foreach (Autodesk.Revit.DB.Element element in selectResult) { Wall wall = element as Wall; if (wall != null) { LocationCurve wallCurve = wall.Location as LocationCurve; m_footPrint.Append(wallCurve.Curve); continue; } ModelCurve modelCurve = element as ModelCurve; if (modelCurve != null) { m_footPrint.Append(modelCurve.GeometryCurve); } } break; } else { TaskDialogResult result = TaskDialog.Show("Warning", "You should select a curve loop, or a wall loop, or loops combination \r\nof walls and curves to create a footprint roof.", TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel); if (result == TaskDialogResult.Cancel) { break; } } } return m_footPrint; } /// /// Create a footprint roof. /// /// The base level of the roof to be created. /// The type of the newly created roof. /// Return a new created footprint roof. public FootPrintRoof CreateFootPrintRoof(Level level, RoofType roofType) { FootPrintRoof roof = null; roof = m_footPrintRoofManager.CreateFootPrintRoof(m_footPrint, level, roofType); if (roof != null) { this.m_footPrintRoofs.Insert(roof); } return roof; } /// /// Select elements in Revit to obtain the extrusion profile lines. /// /// A curve array to hold the extrusion profile lines. public CurveArray SelectProfile() { m_profile.Clear(); while (true) { m_selection.GetElementIds().Clear(); IList selectResult; try { selectResult = m_selection.PickElementsByRectangle(); } catch (Exception ex) { Console.WriteLine(ex.Message); break; } if (selectResult.Count != 0) { foreach (Autodesk.Revit.DB.Element element in selectResult) { ModelCurve modelCurve = element as ModelCurve; if (modelCurve != null) { m_profile.Append(modelCurve.GeometryCurve); continue; } } break; } else { TaskDialogResult result = TaskDialog.Show("Warning", "You should select a connected lines or arcs, \r\nnot closed in a loop to create extrusion roof.", TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel); if (result == TaskDialogResult.Cancel) { break; } } } return m_profile; } /// /// Create a extrusion roof. /// /// The reference plane for the extrusion roof. /// The reference level of the roof to be created. /// The type of the newly created roof. /// The extrusion start point. /// The extrusion end point. /// Return a new created extrusion roof. public ExtrusionRoof CreateExtrusionRoof(ReferencePlane refPlane, Level level, RoofType roofType, double extrusionStart, double extrusionEnd) { ExtrusionRoof roof = null; roof = m_extrusionRoofManager.CreateExtrusionRoof(m_profile, refPlane, level, roofType, extrusionStart, extrusionEnd); if (roof != null) { m_extrusionRoofs.Insert(roof); } return roof; } /// /// Begin a transaction. /// /// public TransactionStatus BeginTransaction() { if (m_transaction.GetStatus() == TransactionStatus.Started) { TaskDialog.Show("Revit", "Transaction started already"); } return m_transaction.Start(); } /// /// Finish a transaction. /// /// public TransactionStatus EndTransaction() { return m_transaction.Commit(); } /// /// Abort a transaction. /// public TransactionStatus AbortTransaction() { return m_transaction.RollBack(); } } }