|
| 1 | +using Autodesk.DesignScript.Runtime; |
| 2 | +using Autodesk.Revit.DB; |
| 3 | +using DSCore.IO; |
| 4 | +using DynamoServices; |
| 5 | +using RevitServices.Persistence; |
| 6 | +using RevitServices.Transactions; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Drawing; |
| 10 | +using System.Linq; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | + |
| 14 | +namespace Revit.Elements |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A Revit ElementType |
| 18 | + /// </summary> |
| 19 | + public class ElementType : Element |
| 20 | + { |
| 21 | + #region Internal properties |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// An internal reference to the ElementType. |
| 25 | + /// </summary> |
| 26 | + internal Autodesk.Revit.DB.ElementType InternalElementType |
| 27 | + { |
| 28 | + get; private set; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Reference to the Element. |
| 33 | + /// </summary> |
| 34 | + public override Autodesk.Revit.DB.Element InternalElement |
| 35 | + { |
| 36 | + get { return InternalElementType; } |
| 37 | + } |
| 38 | + |
| 39 | + #endregion |
| 40 | + |
| 41 | + #region Private constructors |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Private constructor for the Element. |
| 45 | + /// </summary> |
| 46 | + /// <param name="elementType"></param> |
| 47 | + private protected ElementType(Autodesk.Revit.DB.ElementType elementType) |
| 48 | + { |
| 49 | + SafeInit(() => InitElementType(elementType)); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Initialize a ElementType element |
| 54 | + /// and sets the ElementType property, element id, and unique id. |
| 55 | + /// </summary> |
| 56 | + /// <param name="elementType"></param> |
| 57 | + private void InitElementType(Autodesk.Revit.DB.ElementType elementType) |
| 58 | + { |
| 59 | + this.InternalElementType = elementType; |
| 60 | + this.InternalElementId = elementType.Id; |
| 61 | + this.InternalUniqueId = elementType.UniqueId; |
| 62 | + } |
| 63 | + |
| 64 | + #endregion |
| 65 | + |
| 66 | + #region Public properties |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// The name of the ElementType. |
| 70 | + /// </summary> |
| 71 | + public new string Name |
| 72 | + { |
| 73 | + get { return InternalElementType.Name; } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// The FamilyName of the ElementType. |
| 78 | + /// </summary> |
| 79 | + public string FamilyName |
| 80 | + { |
| 81 | + get { return InternalElementType.FamilyName; } |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Determine if this ElementType can be deleted. |
| 86 | + /// </summary> |
| 87 | + public bool CanBeDeleted |
| 88 | + { |
| 89 | + get { return InternalElementType.CanBeDeleted; } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Determine if this ElementType can be copied. |
| 94 | + /// </summary> |
| 95 | + public bool CanBeCopied |
| 96 | + { |
| 97 | + get { return InternalElementType.CanBeCopied; } |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Determine if this ElementType can be renamed. |
| 102 | + /// </summary> |
| 103 | + public bool CanBeRenamed |
| 104 | + { |
| 105 | + get { return InternalElementType.CanBeRenamed; } |
| 106 | + } |
| 107 | + |
| 108 | + #endregion |
| 109 | + |
| 110 | + #region Public constructors |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Duplicates an existing element type and assigns it a new name. |
| 114 | + /// </summary> |
| 115 | + /// <param name="name">The new name of the element type.</param> |
| 116 | + /// <returns>The duplicated element type.</returns> |
| 117 | + public ElementType Duplicate(string name) |
| 118 | + { |
| 119 | + if (String.IsNullOrWhiteSpace(name)) |
| 120 | + throw new ArgumentNullException(nameof(name)); |
| 121 | + |
| 122 | + TransactionManager.Instance.EnsureInTransaction(Document); |
| 123 | + ElementType newElementType = FromExisting(this.InternalElementType.Duplicate(name), true); |
| 124 | + TransactionManager.Instance.TransactionTaskDone(); |
| 125 | + return newElementType; |
| 126 | + } |
| 127 | + |
| 128 | + #endregion |
| 129 | + |
| 130 | + #region Public static constructors |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Returns the type Element with the given name. |
| 134 | + /// </summary> |
| 135 | + /// <param name="name">Name of the type</param> |
| 136 | + /// <returns>Type Element</returns> |
| 137 | + public static ElementType ByName(string name) |
| 138 | + { |
| 139 | + if (String.IsNullOrWhiteSpace(name)) |
| 140 | + throw new ArgumentNullException(nameof(name)); |
| 141 | + |
| 142 | + var elementType = DocumentManager.Instance |
| 143 | + .ElementsOfType<Autodesk.Revit.DB.ElementType>() |
| 144 | + .FirstOrDefault(x => x.Name == name); |
| 145 | + |
| 146 | + if (elementType == null) |
| 147 | + throw new KeyNotFoundException(Properties.Resources.ElementTypeNameNotFound); |
| 148 | + return FromExisting(elementType, true); |
| 149 | + } |
| 150 | + |
| 151 | + #endregion |
| 152 | + |
| 153 | + #region Internal static constructors |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Create a ElementType from a user selected Element. |
| 157 | + /// </summary> |
| 158 | + /// <param name="elementType"></param> |
| 159 | + /// <param name="isRevitOwned"></param> |
| 160 | + /// <returns></returns> |
| 161 | + internal static ElementType FromExisting(Autodesk.Revit.DB.ElementType elementType, bool isRevitOwned) |
| 162 | + { |
| 163 | + return new ElementType(elementType) |
| 164 | + { |
| 165 | + IsRevitOwned = isRevitOwned |
| 166 | + }; |
| 167 | + } |
| 168 | + |
| 169 | + #endregion |
| 170 | + |
| 171 | + #region Public methods |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Get the preview image of an element. This image is similar to what is seen in |
| 175 | + /// the Revit UI when selecting the type of an element. |
| 176 | + /// </summary> |
| 177 | + /// <param name="size">The width and height of the preview image in pixels.</param> |
| 178 | + /// <returns>The preview image. null if there is no preview image.</returns> |
| 179 | + public Bitmap GetPreviewImage(int size = 500) |
| 180 | + { |
| 181 | + System.Drawing.Size imageSize = new System.Drawing.Size(size, size); |
| 182 | + Bitmap bitmapImage = this.InternalElementType.GetPreviewImage(imageSize); |
| 183 | + return bitmapImage; |
| 184 | + } |
| 185 | + |
| 186 | + #endregion |
| 187 | + } |
| 188 | +} |
0 commit comments