添加项目文件。
This commit is contained in:
399
KGdev.BI3D.Revit.DataExporter.JsonTable/JsonTableDataExporter.cs
Normal file
399
KGdev.BI3D.Revit.DataExporter.JsonTable/JsonTableDataExporter.cs
Normal file
@@ -0,0 +1,399 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Autodesk.Revit.DB;
|
||||
using KGdev.BI3D.Revit.Common;
|
||||
using KGdev.BI3D.Revit.Common.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace KGdev.BI3D.Revit.DataExporter.JsonTable
|
||||
{
|
||||
public class JsonTableDataExporter : IDataExporter
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get { return ".json single elements array"; }
|
||||
}
|
||||
|
||||
public string Description
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Export data to a .json file representing a single table with all parameters of all exported elements as columns. The .json file will be placed next to the .3dbi file and will be named [FILENAME]_data.json.";
|
||||
}
|
||||
}
|
||||
|
||||
public string Extension
|
||||
{
|
||||
get { return "json"; }
|
||||
}
|
||||
|
||||
public List<IOptionDefinition> Options { get; } = new List<IOptionDefinition>();
|
||||
|
||||
public JsonTableDataExporter(
|
||||
ILinkedDocumentsUtilities linkedDocumentsUtilities,
|
||||
IElementIdProvider elementIdProvider
|
||||
)
|
||||
{
|
||||
this.linkedDocumentsUtilities = linkedDocumentsUtilities;
|
||||
this.elementIdProvider = elementIdProvider;
|
||||
}
|
||||
|
||||
public void Export(
|
||||
Document rootDocument,
|
||||
List<LinkedElementIdPath> exportedElementIdPaths,
|
||||
string path
|
||||
)
|
||||
{
|
||||
JsonTableDataExporterResult jsonTableDataExporterResult =
|
||||
new JsonTableDataExporterResult();
|
||||
path = Path.Combine(
|
||||
Path.GetDirectoryName(path),
|
||||
Path.GetFileNameWithoutExtension(path) + "_data." + this.Extension
|
||||
);
|
||||
Dictionary<string, Dictionary<string, object>> dictionary =
|
||||
new Dictionary<string, Dictionary<string, object>>();
|
||||
HashSet<string> hashSet = new HashSet<string> { "Id", "CategoryName" };
|
||||
Dictionary<string, ElementType> dictionary2 = new Dictionary<string, ElementType>();
|
||||
foreach (LinkedElementIdPath linkedElementIdPath in exportedElementIdPaths)
|
||||
{
|
||||
Element element = this.linkedDocumentsUtilities.GetElement(
|
||||
rootDocument,
|
||||
linkedElementIdPath
|
||||
);
|
||||
string elementId = this.elementIdProvider.GetElementId(linkedElementIdPath);
|
||||
bool flag = element.Category == null;
|
||||
if (!flag)
|
||||
{
|
||||
ElementId typeId = element.GetTypeId();
|
||||
LinkedElementIdPath linkedElementIdPath2 = new LinkedElementIdPath(
|
||||
linkedElementIdPath.ElementIds
|
||||
);
|
||||
int count = linkedElementIdPath2.ElementIds.Count;
|
||||
linkedElementIdPath2.ElementIds.RemoveAt(count - 1);
|
||||
linkedElementIdPath2.ElementIds.Add(typeId);
|
||||
ElementType elementType =
|
||||
this.linkedDocumentsUtilities.GetElement(rootDocument, linkedElementIdPath2)
|
||||
as ElementType;
|
||||
string elementId2 = this.elementIdProvider.GetElementId(linkedElementIdPath2);
|
||||
bool flag2 = !dictionary2.ContainsKey(elementId2) && elementType != null;
|
||||
if (flag2)
|
||||
{
|
||||
dictionary2.Add(elementId2, elementType);
|
||||
}
|
||||
this.StoreElementInCollection(element, elementId, dictionary, hashSet);
|
||||
}
|
||||
}
|
||||
Action<IEnumerable<string>, Dictionary<string, Dictionary<string, object>>> action =
|
||||
null;
|
||||
if (action == null)
|
||||
{
|
||||
action = delegate(
|
||||
IEnumerable<string> keys,
|
||||
Dictionary<string, Dictionary<string, object>> objects
|
||||
)
|
||||
{
|
||||
foreach (string text3 in keys)
|
||||
{
|
||||
foreach (string text4 in objects.Keys)
|
||||
{
|
||||
Dictionary<string, object> dictionary5 = objects[text4];
|
||||
bool flag7 = !dictionary5.ContainsKey(text3);
|
||||
if (flag7)
|
||||
{
|
||||
dictionary5.Add(text3, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
List<Dictionary<string, object>> list = dictionary
|
||||
.Values
|
||||
.ToList<Dictionary<string, object>>();
|
||||
bool flag3 = list.Count > 0;
|
||||
if (flag3)
|
||||
{
|
||||
Dictionary<string, object> dictionary3 = list[0];
|
||||
foreach (string text in hashSet)
|
||||
{
|
||||
bool flag4 = !dictionary3.ContainsKey(text);
|
||||
if (flag4)
|
||||
{
|
||||
dictionary3.Add(text, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
jsonTableDataExporterResult.Objects.AddRange(list);
|
||||
dictionary.Clear();
|
||||
hashSet.Clear();
|
||||
foreach (KeyValuePair<string, ElementType> keyValuePair in dictionary2)
|
||||
{
|
||||
this.StoreElementInCollection(
|
||||
keyValuePair.Value,
|
||||
keyValuePair.Key,
|
||||
dictionary,
|
||||
hashSet
|
||||
);
|
||||
}
|
||||
list = dictionary.Values.ToList<Dictionary<string, object>>();
|
||||
bool flag5 = list.Count > 0;
|
||||
if (flag5)
|
||||
{
|
||||
Dictionary<string, object> dictionary4 = list[0];
|
||||
foreach (string text2 in hashSet)
|
||||
{
|
||||
bool flag6 = !dictionary4.ContainsKey(text2);
|
||||
if (flag6)
|
||||
{
|
||||
dictionary4.Add(text2, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
jsonTableDataExporterResult.Types.AddRange(list);
|
||||
JsonTableDataExporterResult jsonTableDataExporterResult2 = jsonTableDataExporterResult;
|
||||
JsonTableDataExporterDocumentInfo jsonTableDataExporterDocumentInfo =
|
||||
new JsonTableDataExporterDocumentInfo();
|
||||
jsonTableDataExporterDocumentInfo.Path = rootDocument.PathName;
|
||||
ProjectInfo projectInformation = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.Address = (
|
||||
(projectInformation != null) ? projectInformation.Address : null
|
||||
);
|
||||
ProjectInfo projectInformation2 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.Author = (
|
||||
(projectInformation2 != null) ? projectInformation2.Author : null
|
||||
);
|
||||
ProjectInfo projectInformation3 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.BuildingName = (
|
||||
(projectInformation3 != null) ? projectInformation3.BuildingName : null
|
||||
);
|
||||
ProjectInfo projectInformation4 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.ClientName = (
|
||||
(projectInformation4 != null) ? projectInformation4.ClientName : null
|
||||
);
|
||||
ProjectInfo projectInformation5 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.IssueDate = (
|
||||
(projectInformation5 != null) ? projectInformation5.IssueDate : null
|
||||
);
|
||||
ProjectInfo projectInformation6 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.Name = (
|
||||
(projectInformation6 != null) ? projectInformation6.Name : null
|
||||
);
|
||||
ProjectInfo projectInformation7 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.Number = (
|
||||
(projectInformation7 != null) ? projectInformation7.Number : null
|
||||
);
|
||||
ProjectInfo projectInformation8 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.OrganizationDescription = (
|
||||
(projectInformation8 != null) ? projectInformation8.OrganizationDescription : null
|
||||
);
|
||||
ProjectInfo projectInformation9 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.OrganizationName = (
|
||||
(projectInformation9 != null) ? projectInformation9.OrganizationName : null
|
||||
);
|
||||
ProjectInfo projectInformation10 = rootDocument.ProjectInformation;
|
||||
jsonTableDataExporterDocumentInfo.Status = (
|
||||
(projectInformation10 != null) ? projectInformation10.Status : null
|
||||
);
|
||||
jsonTableDataExporterResult2.DocumentInfo = jsonTableDataExporterDocumentInfo;
|
||||
using (StreamWriter streamWriter = File.CreateText(path))
|
||||
{
|
||||
JsonSerializer jsonSerializer = new JsonSerializer();
|
||||
jsonSerializer.Serialize(streamWriter, jsonTableDataExporterResult);
|
||||
}
|
||||
}
|
||||
|
||||
private void StoreElementInCollection(
|
||||
Element element,
|
||||
string id,
|
||||
Dictionary<string, Dictionary<string, object>> objectDictionaryByObjectId,
|
||||
HashSet<string> encounteredPropertyNames
|
||||
)
|
||||
{
|
||||
Document document = element.Document;
|
||||
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(document.PathName);
|
||||
bool flag = !objectDictionaryByObjectId.ContainsKey(id);
|
||||
Dictionary<string, object> dictionary;
|
||||
if (flag)
|
||||
{
|
||||
dictionary = new Dictionary<string, object>();
|
||||
dictionary.Add("DocumentName", fileNameWithoutExtension);
|
||||
dictionary.Add("Id", id);
|
||||
dictionary.Add("ElementId", element.Id.IntegerValue);
|
||||
Dictionary<string, object> dictionary2 = dictionary;
|
||||
string text = "CategoryName";
|
||||
Category category = element.Category;
|
||||
dictionary2.Add(text, (category != null) ? category.Name : null);
|
||||
Dictionary<string, object> dictionary3 = dictionary;
|
||||
string text2 = "TypeId";
|
||||
ElementId typeId = element.GetTypeId();
|
||||
dictionary3.Add(text2, (typeId != null) ? typeId.IntegerValue.ToString() : null);
|
||||
objectDictionaryByObjectId.Add(id, dictionary);
|
||||
}
|
||||
else
|
||||
{
|
||||
dictionary = objectDictionaryByObjectId[id];
|
||||
}
|
||||
foreach (object obj in element.Parameters)
|
||||
{
|
||||
Parameter parameter = (Parameter)obj;
|
||||
object obj2 = null;
|
||||
object obj3 = null;
|
||||
string text3 = null;
|
||||
switch (parameter.StorageType)
|
||||
{
|
||||
case StorageType.Integer:
|
||||
obj2 = parameter.AsInteger();
|
||||
break;
|
||||
case StorageType.Double:
|
||||
{
|
||||
obj2 = parameter.AsDouble();
|
||||
UnitType unitType = parameter.Definition.UnitType;
|
||||
bool flag2 = UnitUtils.IsValidUnitType(unitType);
|
||||
if (flag2)
|
||||
{
|
||||
DisplayUnitType displayUnits = document
|
||||
.GetUnits()
|
||||
.GetFormatOptions(unitType)
|
||||
.DisplayUnits;
|
||||
text3 = displayUnits.ToString();
|
||||
obj3 = UnitUtils.ConvertFromInternalUnits(
|
||||
parameter.AsDouble(),
|
||||
displayUnits
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case StorageType.String:
|
||||
obj2 = parameter.AsString();
|
||||
break;
|
||||
case StorageType.ElementId:
|
||||
{
|
||||
ElementId elementId = parameter.AsElementId();
|
||||
Element element2 = document.GetElement(elementId);
|
||||
obj2 = ((element2 != null) ? element2.Name : "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
int num = 2;
|
||||
string text4 = "Parameter." + parameter.Definition.Name;
|
||||
while (dictionary.ContainsKey(text4))
|
||||
{
|
||||
text4 = string.Format(
|
||||
"{0}{1}.{2}",
|
||||
"Parameter.",
|
||||
parameter.Definition.Name,
|
||||
num
|
||||
);
|
||||
num++;
|
||||
}
|
||||
dictionary.Add(text4, obj2);
|
||||
bool flag3 = !encounteredPropertyNames.Contains(text4);
|
||||
if (flag3)
|
||||
{
|
||||
encounteredPropertyNames.Add(text4);
|
||||
}
|
||||
bool flag4 = obj3 != null;
|
||||
if (flag4)
|
||||
{
|
||||
text4 = text4 + "." + text3;
|
||||
bool flag5 = !encounteredPropertyNames.Contains(text4);
|
||||
if (flag5)
|
||||
{
|
||||
encounteredPropertyNames.Add(text4);
|
||||
}
|
||||
bool flag6 = !dictionary.ContainsKey(text4);
|
||||
if (flag6)
|
||||
{
|
||||
dictionary.Add(text4, obj3);
|
||||
}
|
||||
}
|
||||
}
|
||||
IEnumerable<PropertyInfo> enumerable =
|
||||
from p in element.GetType().GetProperties()
|
||||
where p.GetIndexParameters().Length == 0
|
||||
select p;
|
||||
foreach (PropertyInfo propertyInfo in enumerable)
|
||||
{
|
||||
string text5 = "Property." + propertyInfo.Name;
|
||||
bool flag7 = dictionary.ContainsKey(text5);
|
||||
if (!flag7)
|
||||
{
|
||||
object obj4 = null;
|
||||
try
|
||||
{
|
||||
obj4 = propertyInfo.GetValue(element);
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
bool flag8 = !encounteredPropertyNames.Contains(text5);
|
||||
if (flag8)
|
||||
{
|
||||
encounteredPropertyNames.Add(text5);
|
||||
}
|
||||
bool flag9 = obj4 == null;
|
||||
if (flag9)
|
||||
{
|
||||
dictionary.Add(text5, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
Type type = obj4.GetType();
|
||||
bool isValueType = type.IsValueType;
|
||||
if (isValueType)
|
||||
{
|
||||
dictionary.Add(text5, obj4);
|
||||
}
|
||||
else
|
||||
{
|
||||
ElementId elementId2 = obj4 as ElementId;
|
||||
bool flag10 = null != elementId2;
|
||||
if (flag10)
|
||||
{
|
||||
dictionary.Add(text5, elementId2.IntegerValue.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
Element element3 = obj4 as Element;
|
||||
bool flag11 = element3 != null;
|
||||
if (flag11)
|
||||
{
|
||||
dictionary.Add(text5, element3.Id.IntegerValue.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
dictionary.Add(text5, obj4.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const string ID_KEY = "Id";
|
||||
|
||||
private const string ELEMENT_ID_KEY = "ElementId";
|
||||
|
||||
private const string CATEGORY_NAME_KEY = "CategoryName";
|
||||
|
||||
private const string TYPE_ID_NAME_KEY = "TypeId";
|
||||
|
||||
private const string ELEMENT_DOCUMENT_NAME_KEY = "DocumentName";
|
||||
|
||||
private const string PARAMETER_PREFIX = "Parameter.";
|
||||
|
||||
private const string PARAMETER_BUILTIN_PREFIX = "BuiltIn.";
|
||||
|
||||
private const string PROPERTY_PREFIX = "Property.";
|
||||
|
||||
private const string FILE_SUFFIX = "_data";
|
||||
|
||||
private readonly ILinkedDocumentsUtilities linkedDocumentsUtilities;
|
||||
|
||||
private readonly IElementIdProvider elementIdProvider;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user