1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.DB.ExtensibleStorage; using Autodesk.Revit.UI; using System; using System.Collections.Generic; using System.Linq;
namespace dataStorage { [Transaction(TransactionMode.Manual)] public class Class1 : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Document doc = commandData.Application.ActiveUIDocument.Document; Transaction transaction = new Transaction(doc); List<Element> eles = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).Where(x => x.Name == "文档数据库").ToList(); Element dataStorage; if (eles.Count != 0) { dataStorage = eles.First(); } else { transaction.Start("创建文档数据库"); dataStorage = DataStorage.Create(doc); dataStorage.Name = "文档数据库"; transaction.Commit(); }
bool isSimpleBuild = false; bool isArrayBuild = false; bool isMapBuild = false; transaction.Start("创建文档参数"); isSimpleBuild = BuildField(dataStorage, new Guid("34B6239D-42FF-7076-D211-BEC5E64CD172"), "文档单属性", AccessLevel.Public, AccessLevel.Public, false, typeof(double), UnitType.UT_Length); isArrayBuild = BuildField(dataStorage, new Guid("BBF63B09-907B-E7C6-A598-8EC9B4D28A88"), "文档列表属性", AccessLevel.Public, AccessLevel.Public, true, typeof(double), UnitType.UT_Length); isMapBuild = BuildField(dataStorage, new Guid("9F2398BC-B703-8362-6137-27435FE49AB4"), "文档字典属性", AccessLevel.Public, AccessLevel.Public, typeof(string), typeof(double), UnitType.UT_Length); transaction.Commit();
transaction.Start("填写文档参数"); bool isSimpleSet = false; bool isArraySet = false; bool isMapSet = false; isSimpleSet = SetField(dataStorage, "文档单属性", 1200.2, DisplayUnitType.DUT_MILLIMETERS); IList<double> listString = new List<double>() { 1200.2, 1500.5 }; isArraySet = SetField(dataStorage, "文档列表属性", listString, DisplayUnitType.DUT_MILLIMETERS); IDictionary<string, double> myDictionary = new Dictionary<string, double>(); myDictionary.Add("1", 1200.2); isMapSet = SetField(dataStorage, "文档字典属性", myDictionary, DisplayUnitType.DUT_MILLIMETERS); transaction.Commit();
double simpleValue = GetField<double>(dataStorage, "文档单属性", DisplayUnitType.DUT_METERS); IList<double> arrayValue = GetField<IList<double>>(dataStorage, "文档列表属性", DisplayUnitType.DUT_METERS); IDictionary<string, double> mapValue = GetField<IDictionary<string, double>>(dataStorage, "文档字典属性", DisplayUnitType.DUT_METERS);
TaskDialog.Show("Revit", $"单属性创建:{isSimpleBuild}\n列表属性创建:{isArrayBuild}\n字典属性创建:{isMapBuild}\n" + $"单属性填写:{isSimpleSet}\n列表属性填写:{isArraySet}\n字典属性填写:{isMapSet}\n" + $"单属性值:{simpleValue}\n列表属性值:{arrayValue.First()}\n字典属性值:{mapValue.First()}"); return Result.Succeeded; }
bool BuildField(Element dataStorage, Guid fieldGuid, string fieldName, AccessLevel readAccessLevel, AccessLevel writeAccessLevel, bool isArrayType, Type keyType, Type fieldType, UnitType unitType) { bool isBuildSucceed = false; Schema specifiedSchema = Schema.Lookup(fieldGuid); if (specifiedSchema == null) { bool hasSameName = false; foreach (Schema schema in Schema.ListSchemas()) { if (schema.SchemaName == fieldName) { hasSameName = true; break; } } if (!hasSameName) { SchemaBuilder schemaBuilder = new SchemaBuilder(fieldGuid); schemaBuilder.SetSchemaName(fieldName); schemaBuilder.SetReadAccessLevel(readAccessLevel); schemaBuilder.SetWriteAccessLevel(writeAccessLevel); FieldBuilder fieldBuilder; if (keyType == null) { if (isArrayType) { fieldBuilder = schemaBuilder.AddArrayField(fieldName, fieldType); } else { fieldBuilder = schemaBuilder.AddSimpleField(fieldName, fieldType); } } else { fieldBuilder = schemaBuilder.AddMapField(fieldName, keyType, fieldType); } if (unitType != UnitType.UT_Undefined) { fieldBuilder.SetUnitType(unitType); } schemaBuilder.Finish(); isBuildSucceed =true; } else { throw new ArgumentException($"“{fieldName}”参数名称已存在无法创建,请修改参数名称后继续", "fieldGuid"); } } else { if (specifiedSchema.SchemaName == fieldName) { Entity entity = dataStorage.GetEntity(specifiedSchema); if (entity.Schema == null) { entity = new Entity(specifiedSchema); dataStorage.SetEntity(entity); isBuildSucceed = true; } else { if (entity.Schema.SchemaName == specifiedSchema.SchemaName) { dataStorage.SetEntity(entity); isBuildSucceed = true; } else { throw new ArgumentException("参数创建失败,请试试更换参数名称或GUID后继续"); } } } else { throw new ArgumentException($"GUID“{fieldGuid.ToString()}”对应的参数名称为{specifiedSchema.SchemaName},请修改参数名称后继续", fieldName); } } return isBuildSucceed; } bool BuildField(Element dataStorage, Guid fieldGuid, string fieldName, AccessLevel readAccessLevel, AccessLevel writeAccessLevel, bool isArrayType, Type fieldType, UnitType unitType) => BuildField(dataStorage, fieldGuid, fieldName, readAccessLevel, writeAccessLevel, isArrayType, null, fieldType, unitType); bool BuildField(Element dataStorage, Guid fieldGuid, string fieldName, AccessLevel readAccessLevel, AccessLevel writeAccessLevel, Type keyType, Type fieldType, UnitType unitType) => BuildField(dataStorage, fieldGuid, fieldName, readAccessLevel, writeAccessLevel, false, keyType, fieldType, unitType); bool SetField<T>(Element dataStorage, string fieldName, T fieldValue, DisplayUnitType displayUnitType) { bool isSetSucceed = false; Schema specifiedSchema = null; foreach (Schema schema in Schema.ListSchemas()) { if (schema.SchemaName == fieldName) { specifiedSchema = schema; break; } } if (specifiedSchema != null) { Field field = specifiedSchema.GetField(fieldName); if (field != null) { Entity entity = dataStorage.GetEntity(specifiedSchema); if (entity.Schema == null || entity.Schema.GUID != specifiedSchema.GUID) { entity = new Entity(specifiedSchema); } if (displayUnitType == DisplayUnitType.DUT_UNDEFINED) { entity.Set(field, fieldValue); } else { entity.Set(field, fieldValue, displayUnitType); } dataStorage.SetEntity(entity); isSetSucceed = true; } } return isSetSucceed; } T GetField<T>(Element dataStorage, string fieldName, DisplayUnitType displayUnitType) { T value = default(T); Schema specifiedSchema = null; foreach (Schema schema in Schema.ListSchemas()) { if (schema.SchemaName == fieldName) { specifiedSchema = schema; break; } } if (specifiedSchema != null) { Field field = specifiedSchema.GetField(fieldName); if (field != null) { Entity entity = dataStorage.GetEntity(specifiedSchema); if (entity.Schema != null && entity.Schema.GUID == specifiedSchema.GUID) { if (displayUnitType == DisplayUnitType.DUT_UNDEFINED) { value = entity.Get<T>(field); } else { value = entity.Get<T>(field, displayUnitType); } } else { value = default(T); } } } return value; } } }
|