qin
2024-09-28 e358beb08f5be49703009b64f058ecfbcfeefbd9
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Glodon.Revit.Utility;
using HStation.Model;
using HStation.RevitDev.Model.AttributeClass;
using HStation.RevitDev.Model.Common;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
 
namespace HStation.RevitDev.RevitDataExport.Utility
{
    public static class ElementExtense
    {
        public static JObject Export(this Element elem, RevitType revitType)
        {
            var exportType = ElementTypeMapping.GetExportType(revitType);
            var elemType = GetElementType(exportType);
            var ret = SetElementPropertys(elemType, elem);
            return ret;
        }
 
        private static JObject SetElementPropertys(Type elemType, Element elem)
        {
            var ret = new JObject();
            var props = elemType.GetProperties();
            foreach (PropertyInfo propInfo in props)
            {
                var attr = propInfo.GetCustomAttribute<ParameterAttribute>();
                if (attr == null) { continue; }
 
                string paramName = attr.m_parameterName;
                ParameterUnit unit = attr.m_unit;
                var paramType = propInfo.PropertyType;
                if (!AddManualParameter(elem, ret, paramName))
                {
                    AddGeneralParameter(elem, ret, propInfo, paramName, paramType, unit);
                }
            }
            return ret;
        }
 
        private static bool AddManualParameter(Element elem, JObject ret, string paramName)
        {
            return ParameterOperator.ProcessingManualParameter(elem, ret, paramName);
        }
 
        private static void AddGeneralParameter(Element elem, JObject ret, PropertyInfo propInfo, string paramName, Type paramType, ParameterUnit unit)
        {
            var propType = propInfo.PropertyType;
            object revitParamValue = GetParameterValue(elem, paramName, propType);
            if (revitParamValue == null) { return; }
 
            object outParamValue;
 
            switch (paramType.Name)
            {
                case "Int32":
                    outParamValue = revitParamValue.ConvertTo<int>();
                    ret[paramName] = (int)outParamValue;
                    break;
                case "Double":
                    switch (unit)
                    {
                        case ParameterUnit.PU_Unknown:
                            outParamValue = revitParamValue.ConvertTo<double>();
                            ret[paramName] = (double)outParamValue;
                            break;
                        case ParameterUnit.PU_MM:
                            outParamValue = revitParamValue.ConvertTo<double>();
                            ret[paramName] = (double)outParamValue.InnerToMM();
                            break;
                        case ParameterUnit.PU_M:
                            break;
                        case ParameterUnit.PU_MMM:
                            break;
                        case ParameterUnit.PU_Pa:
                            break;
                        case ParameterUnit.PU_MPa:
                            break;
                        default:
                            break;
                    }
                    break;
                case "String":
                    outParamValue = revitParamValue.ConvertTo<string>();
                    ret[paramName] = (string)outParamValue;
                    break;
                case "Boolean":
                    outParamValue = revitParamValue.ConvertTo<bool>();
                    ret[paramName] = (bool)outParamValue;
                    break;
                default:
                    throw new Exception("未知类型!");
            }
        }
 
        private static T ConvertTo<T>(this object inObj)
        {
            var inTypeName = inObj.GetType().Name;
            var outTypeName = typeof(T).Name;
            switch (inTypeName)
            {
                case "String":
                    switch (outTypeName)
                    {
                        case "String":
                            return (T)inObj;
                        case "Int32":
                            if (int.TryParse((string)inObj, out int iValue))
                            {
                                return (T)(object)iValue;
                            }
                            else
                            {
                                throw new Exception($"参数类型{inTypeName} 转换{outTypeName}异常!");
                            }
                        case "Double":
                            if (double.TryParse((string)inObj, out double dValue))
                            {
                                return (T)(object)dValue;
                            }
                            else
                            {
                                throw new Exception($"参数类型{inTypeName} 转换{outTypeName}异常!");
                            }
                        case "Boolean":
                            if (bool.TryParse((string)inObj, out bool bValue))
                            {
                                return (T)(object)bValue;
                            }
                            else
                            {
                                throw new Exception($"参数类型{inTypeName} 转换{outTypeName}异常!");
                            }
                        default:
                            break;
                    }
                    break;
                case "Int32":
                    switch (outTypeName)
                    {
                        case "String":
                            return (T)(object)inObj.ToString();
                        case "Int32":
                            return (T)inObj;
                        case "Double":
                            return (T)inObj;
                        case "Boolean":
                            throw new Exception($"无法转换参数类型{inTypeName}到{outTypeName}!");
                        default:
                            break;
                    }
                    break;
                case "Double":
                    switch (outTypeName)
                    {
                        case "String":
                            return (T)(object)inObj.ToString();
                        case "Int32":
                        case "Double":
                            return (T)inObj;
                        case "Boolean":
                            throw new Exception($"无法转换参数类型{inTypeName}到{outTypeName}!");
                        default:
                            break;
                    }
                    break;
                case "Boolean":
                    switch (outTypeName)
                    {
                        case "String":
                            return (T)(object)inObj.ToString();
                        case "Int32":
                        case "Double":
                        case "Boolean":
                            return (T)inObj;
                        default:
                            break;
                    }
                    break;
                default:
                    return default;
            }
            return default;
        }
 
        private static object GetParameterValue(Element elem, string paramName, Type propType)
        {
            object paramValue = null;
            try
            {
                if (propType.Name == "String")
                {
                    var strValue = ParameterOperator.GetParameterValueAsString(elem, paramName);
                    paramValue = strValue == null ? string.Empty : strValue;
                }
                else if (propType.Name == "Int32")
                {
                    paramValue = ParameterOperator.GetParameterValueAsInt(elem, paramName);
                }
                else if (propType.Name == "Double")
                {
                    paramValue = ParameterOperator.GetParameterValueAsDouble(elem, paramName);
                }
                else if (propType.Name == "Boolean")
                {
                    int? value = ParameterOperator.GetParameterValueAsInt(elem, paramName);
                    if (value == 1)
                    {
                        paramValue = true;
                    }
                    else
                    {
                        paramValue = false;
                    }
 
                }
            }
            catch (Exception ex)
            {
                return paramValue;
            }
 
            return paramValue;
        }
 
        private static Type GetElementType(ExportType exportType)
        {
            var types = Assembly.GetAssembly(typeof(RevitModel)).GetTypes();
            foreach (var type in types)
            {
                var attr = type.GetCustomAttribute<ExportTypeAttribute>();
                if (attr == null) { continue; }
 
                if (attr.m_exportType == exportType)
                {
                    return type;
                }
            }
            return null;
        }
 
        public static Level GetLevel(this Element element)
        {
            var levelId1 = element.LevelId;
            if (levelId1 != null)
            {
                Level level1 = element.Document.GetElement(levelId1) as Level;
                return level1;
            }
            var levelParam = element.LookupParameter("标高");
            if (levelParam != null)
            {
                ElementId levelId2 = levelParam.AsElementId();
                if (levelId2 != null)
                {
                    Level level2 = element.Document.GetElement(levelId2) as Level;
                    return level2;
                }
            }
            return null;
        }
    }
 
    public static class ElementExtense2
    {
        public static JObject Export2(this Element elem)
        {
            var ret = new JObject();
            if (elem == null) { return ret; }
 
            var customDict = elem.ExportCostomDict();
            foreach (var item in customDict)
            {
                ret[item.Key] = JToken.FromObject(item.Value);
            }
 
            elem.ExportParameters(ref ret);
            return ret;
        }
 
        public static void ExportParameters(this Element elem, ref JObject ret)
        {
            if (elem == null) { return; }
 
            var parameters = elem.Parameters;
            foreach (Parameter param in parameters)
            {
                var name = param.Definition.Name;
                switch (param.StorageType)
                {
                    case StorageType.None:
                        break;
                    case StorageType.Integer:
                        {
                            int value = param.AsInteger();
                            ret[name] = value;
                            break;
                        }
                    case StorageType.Double:
                        {
                            string value = param.AsValueString();
                            if (string.IsNullOrEmpty(value)) { value = param.AsString(); }
                            ret[name] = value;
                            break;
                        }
                    case StorageType.String:
                        {
                            string value = param.AsString();
                            ret[name] = value;
                            break;
                        }
                    case StorageType.ElementId:
                        {
                            ElementId value = param.AsElementId();
                            Element subElem = elem.Document.GetElement(value);
                            if (subElem == null) 
                            {
                                ret[name] = null;
                            }
                            else
                            {
                                ret[name] = subElem.Export2();
                            }
                            break;
                        }
                    default:
                        break;
                }
 
            }
            return;
        }
 
        public static Dictionary<string, object> ExportCostomDict(this Element elem)
        {
            if (elem == null) { return null; }
            var ret = new Dictionary<string, object>();
            ret.Add("构件编码", elem.Id.IntegerValue.ToString());
            List<Entity.Connector> connectors = elem.GetConnectors();
            ret.Add("连接列表", connectors);
            return ret;
        }
 
        public static List<Element> GetConnectElements(this Element element)
        {
            var ret = new List<Element>();
            if (element == null) { return ret; }
 
            ConnectorSet connectors = null;
            if (element is FamilyInstance fi)
            {
                var mepModel = fi.MEPModel;
                if (mepModel == null) { return ret; }
                connectors = mepModel.ConnectorManager?.Connectors;
            }
            else if(element is Pipe pipe)
            {
                connectors = pipe.ConnectorManager.Connectors;
            }
            else
            {
                return ret;
            }
 
            if (connectors == null)
            {
                return ret;
            }
            foreach (Autodesk.Revit.DB.Connector connector in connectors)
            {
                var linkedElems = connector.GetConnectorElements();
                ret.AddRange(linkedElems);
            }
            return ret; 
        }
 
        public static string GetLinkedElementIds(string id)
        {
            var doc = GlobalResource.CurrentDocument;
            var element = doc.GetElement(new ElementId(int.Parse(id)));
            var linkedElems = element.GetConnectElements();
            var linkedIds = linkedElems.Select(x => x.Id.IntegerValue.ToString());
            var result = string.Join(",", linkedIds);
            return result;
        }
 
        public static List<Entity.Connector> GetConnectors(this Element elem)
        {
            List<Entity.Connector> ret = new List<Entity.Connector>();
            if (elem == null) { return ret; }
            ConnectorManager manager = null;
            if (elem is Pipe pipe)
            {
                manager = pipe.ConnectorManager;
            }
            else if (elem is FamilyInstance fi)
            {
                manager = fi.MEPModel?.ConnectorManager;
            }
            if (manager == null) { return ret; }
 
            var connectors = manager.Connectors;
            foreach (Autodesk.Revit.DB.Connector connector in connectors)
            {
                if (connector.Domain != Domain.DomainHvac && connector.Domain != Domain.DomainPiping)
                {
                    continue;
                }
                var dir = connector.Direction ==
                    FlowDirectionType.Bidirectional ?
                    ConnectorDirction.BOTH :
                    (connector.Direction == FlowDirectionType.In ? ConnectorDirction.IN : ConnectorDirction.OUT);
                List<Element> connectElems = connector.GetConnectorElements();
 
                ret.Add(new Entity.Connector
                {
                    ConnectId = connectElems.FirstOrDefault()?.Id.IntegerValue.ToString(),
                    Dirction = dir,
                    Point = connector.Origin.ToCustomPoint(),
                });
            }
            return ret; 
        }
    }
}