// // (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.Text; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.DB.Structure; using Autodesk.Revit.ApplicationServices; namespace Revit.SDK.Samples.DoorSwing.CS { /// /// Left/Right feature based on family's actual geometry and country's standard. /// public class DoorFamily { #region "Members" // door family Family m_family; // opening value of one of this family's door which neither flipped nor mirrored. string m_basalOpeningValue; // one door instance of this family. FamilyInstance m_oneInstance; // Revit application UIApplication m_app; // the geometry of one of this family's door which neither flipped nor mirrored. DoorGeometry m_geometry; #endregion #region "Properties" /// /// Retrieval the name of this family. /// public string FamilyName { get { return m_family.Name; } } /// /// Retrieve opening value of one of this family's door which neither flipped nor mirrored. /// public string BasalOpeningValue { get { if (string.IsNullOrEmpty(m_basalOpeningValue)) { string paramValue = DoorSwingResource.Undefined; // get current opening value. System.Collections.Generic.List fss = new System.Collections.Generic.List(); foreach (ElementId elementId in m_family.GetFamilySymbolIds()) { fss.Add((FamilySymbol)(m_app.ActiveUIDocument.Document.GetElement(elementId))); } FamilySymbol doorSymbol = fss[0]; paramValue = doorSymbol.ParametersMap.get_Item("BasalOpening").AsString(); // deal with invalid string. if (!DoorSwingData.OpeningTypes.Contains(paramValue)) { paramValue = DoorSwingResource.Undefined; } m_basalOpeningValue = paramValue; } return m_basalOpeningValue; } set { m_basalOpeningValue = value; } } /// /// Retrieve the geometry of one door which belongs to this family and /// neither flipped nor mirrored. /// public DoorGeometry Geometry { get { if (null == m_geometry) { // create one instance of DoorFamilyGeometry class. m_geometry = new DoorGeometry(m_oneInstance); } return m_geometry; } } #endregion #region "Methods" /// /// construct function. /// /// one door family /// Revit application public DoorFamily(Family doorFamily, UIApplication app) { m_app = app; m_family = doorFamily; // one door instance which belongs to this family and neither flipped nor mirrored. m_oneInstance = CreateOneInstanceWithThisFamily(); } /// /// Update Left/Right feature based on family's actual geometry and country's standard. /// public void UpdateOpeningFeature() { // get current Left/Right feature's value of this door family. List ffs = new List(); foreach (ElementId elementId in m_family.GetFamilySymbolIds()) { ffs.Add((FamilySymbol)(m_app.ActiveUIDocument.Document.GetElement(elementId))); } foreach (FamilySymbol doorSymbol in ffs) { // update the the related family shared parameter's value if user already added it. if (doorSymbol.ParametersMap.Contains("BasalOpening")) { Parameter basalOpeningParam = doorSymbol.ParametersMap.get_Item("BasalOpening"); bool setResult = basalOpeningParam.Set(m_basalOpeningValue); } } } /// /// Delete the temporarily created door instance and its host. /// public void DeleteTempDoorInstance() { Document doc = m_app.ActiveUIDocument.Document; Autodesk.Revit.DB.Element tempWall = m_oneInstance.Host; doc.Delete(m_oneInstance.Id); // delete temporarily created door instance with this family. doc.Delete(tempWall.Id); // delete the door's host. } /// /// Create one temporary door instance with this family. /// /// the created door. private FamilyInstance CreateOneInstanceWithThisFamily() { Autodesk.Revit.DB.Document doc = m_app.ActiveUIDocument.Document; Autodesk.Revit.Creation.Document creDoc = doc.Create; Autodesk.Revit.Creation.Application creApp = m_app.Application.Create; // get one level. A project has at least one level. Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).FirstElement() as Level; // create one wall as door's host Line wallCurve = Line.CreateBound(new Autodesk.Revit.DB.XYZ(0, 0, 0), new Autodesk.Revit.DB.XYZ(100, 0, 0)); Wall host = Wall.Create(doc, wallCurve, level.Id, false); doc.Regenerate(); // door symbol. List ffs = new List(); foreach (ElementId elementId in m_family.GetFamilySymbolIds()) { ffs.Add((FamilySymbol)(m_app.ActiveUIDocument.Document.GetElement(elementId))); } FamilySymbol doorSymbol = ffs[0]; // create the door FamilyInstance createdFamilyInstance = creDoc.NewFamilyInstance(new Autodesk.Revit.DB.XYZ(0, 0, 0), doorSymbol, host, level, StructuralType.NonStructural); doc.Regenerate(); return createdFamilyInstance; } #endregion } }