195 lines
6.0 KiB
C#
195 lines
6.0 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Windows;
|
|||
|
|
using System.Windows.Controls;
|
|||
|
|
using System.Windows.Data;
|
|||
|
|
using System.Windows.Documents;
|
|||
|
|
using System.Windows.Input;
|
|||
|
|
using System.Windows.Media;
|
|||
|
|
using System.Windows.Media.Imaging;
|
|||
|
|
using System.Windows.Navigation;
|
|||
|
|
using System.Windows.Shapes;
|
|||
|
|
|
|||
|
|
using Microsoft.Win32;
|
|||
|
|
|
|||
|
|
using Newtonsoft.Json;
|
|||
|
|
|
|||
|
|
using Xbim.Ifc;
|
|||
|
|
using Xbim.Ifc2x3.PropertyResource;
|
|||
|
|
using Xbim.Ifc4.Interfaces;
|
|||
|
|
using Xbim.Ifc4.Kernel;
|
|||
|
|
using Xbim.Ifc4.ProductExtension;
|
|||
|
|
using Xbim.Ifc4.PropertyResource;
|
|||
|
|
|
|||
|
|
namespace IFCReader
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// MainWindow.xaml 的交互逻辑
|
|||
|
|
/// </summary>
|
|||
|
|
public partial class MainWindow : Window
|
|||
|
|
{
|
|||
|
|
public StringBuilder Message { get; private set; } = new();
|
|||
|
|
public string JsonMessage { get; private set; }
|
|||
|
|
|
|||
|
|
public MainWindow()
|
|||
|
|
{
|
|||
|
|
InitializeComponent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void ReadIFC(string fileName)
|
|||
|
|
{
|
|||
|
|
var model = IfcStore.Open(fileName);
|
|||
|
|
List<IFCElement> elements = new();
|
|||
|
|
var collection = model.Instances.OfType<IIfcElement>();
|
|||
|
|
foreach (var obj in collection)
|
|||
|
|
{
|
|||
|
|
Message.AppendLine("构件分割线----------------------------构件分割线");
|
|||
|
|
Message.AppendLine(obj.Name);
|
|||
|
|
|
|||
|
|
IFCElement element = new() { Name = obj.Name };
|
|||
|
|
elements.Add(element);
|
|||
|
|
//类型属性
|
|||
|
|
var typeGroups = obj.IsTypedBy.SelectMany(r => r.RelatingType.HasPropertySets);
|
|||
|
|
foreach (var property in typeGroups)
|
|||
|
|
{
|
|||
|
|
if (property is IIfcPropertySet group)
|
|||
|
|
{
|
|||
|
|
Message.AppendLine($"<类型属性分组>: {group.Name}-------------------");
|
|||
|
|
var ps = group.PropertySetDefinitions.OfType<IIfcPropertySet>().SelectMany(pset => pset.HasProperties);
|
|||
|
|
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
|
|||
|
|
element.Groups.Add(new Group() { Name = group.Name, Properties = keyValuePairs });
|
|||
|
|
|
|||
|
|
foreach (var item in ps)
|
|||
|
|
{
|
|||
|
|
if (item is IIfcPropertySingleValue propSingle)
|
|||
|
|
{
|
|||
|
|
Message.AppendLine($"<类型属性>: {propSingle.Name}, <值>: {propSingle.NominalValue}");
|
|||
|
|
//element.Groups.Add(propSingle.Name, propSingle.NominalValue.ToString());
|
|||
|
|
keyValuePairs.Add(propSingle.Name, propSingle.NominalValue.ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//if (group is IIfcPropertySet propertySet)
|
|||
|
|
//{
|
|||
|
|
// Message.AppendLine($"Set:{propertySet.Name}, Value:{propertySet.PropertySetDefinitions}");
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
//实例属性
|
|||
|
|
|
|||
|
|
var insGroups = obj.IsDefinedBy.SelectMany(r => r.RelatingPropertyDefinition.PropertySetDefinitions);
|
|||
|
|
////在这种情况下, 只考虑属性集。
|
|||
|
|
//.OfType<IIfcPropertySet>();
|
|||
|
|
////从所有属性集中获取所有属性
|
|||
|
|
//.SelectMany(pset => pset.HasProperties);
|
|||
|
|
//分组
|
|||
|
|
|
|||
|
|
foreach (var group in insGroups)
|
|||
|
|
{
|
|||
|
|
Message.AppendLine($"<实例属性分组>: {group.Name}");
|
|||
|
|
var ps = group.PropertySetDefinitions.OfType<IIfcPropertySet>().SelectMany(pset => pset.HasProperties);
|
|||
|
|
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
|
|||
|
|
element.Groups.Add(new Group() { Name = group.Name, Properties = keyValuePairs });
|
|||
|
|
|
|||
|
|
foreach (var item in ps)
|
|||
|
|
{
|
|||
|
|
if (item is IIfcPropertySingleValue propSingle)
|
|||
|
|
{
|
|||
|
|
Message.AppendLine($"<实例属性>: {propSingle.Name}, <值>: {propSingle.NominalValue}");
|
|||
|
|
//element.Groups.Add(propSingle.Name, propSingle.NominalValue.ToString());
|
|||
|
|
keyValuePairs.Add(propSingle.Name, propSingle.NominalValue.ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//if (group is IIfcPropertySet propertySet)
|
|||
|
|
//{
|
|||
|
|
// Message.AppendLine($"Set:{propertySet.Name}, Value:{propertySet.PropertySetDefinitions}");
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//var setting = new JsonSerializerSettings();
|
|||
|
|
//setting.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
|
|||
|
|
//setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|||
|
|
//JsonMessage += JsonConvert.SerializeObject(insGroups, setting);
|
|||
|
|
}
|
|||
|
|
JsonMessage = JsonConvert.SerializeObject(elements, Formatting.Indented);
|
|||
|
|
//var setting = new JsonSerializerSettings();
|
|||
|
|
//setting.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
|
|||
|
|
//setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|||
|
|
//JsonMessage = JsonConvert.SerializeObject(collection, setting);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OpenFile(object sender, RoutedEventArgs e)
|
|||
|
|
{
|
|||
|
|
OpenFileDialog dialog = new OpenFileDialog() { Filter = "IFC文件|*.ifc", Multiselect = false };
|
|||
|
|
if (dialog.ShowDialog() == true)
|
|||
|
|
{
|
|||
|
|
IfcFilePath.Text = dialog.FileName;
|
|||
|
|
ReadIFC(dialog.FileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void WriteJosn(object sender, RoutedEventArgs e)
|
|||
|
|
{
|
|||
|
|
WriteJsonFile(JsonMessage);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void WriteJsonFile(string jsonMessage)
|
|||
|
|
{
|
|||
|
|
var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\属性信息.json";
|
|||
|
|
|
|||
|
|
using (FileStream fs = new FileStream(filePath, FileMode.Create))
|
|||
|
|
{
|
|||
|
|
//写入
|
|||
|
|
using (StreamWriter sw = new StreamWriter(fs))
|
|||
|
|
{
|
|||
|
|
sw.WriteLine(jsonMessage);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
System.Diagnostics.Process.Start(filePath);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void WriteTxt(object sender, RoutedEventArgs e)
|
|||
|
|
{
|
|||
|
|
WriteTextFile(Message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void WriteTextFile(StringBuilder sb, string fileName = "IFC属性集")
|
|||
|
|
{
|
|||
|
|
var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + $"\\{fileName}.txt";
|
|||
|
|
File.WriteAllText(filePath, sb.ToString());
|
|||
|
|
System.Diagnostics.Process.Start(filePath);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class IFCElement
|
|||
|
|
{
|
|||
|
|
[JsonProperty("元素")]
|
|||
|
|
public string Name { get; set; }
|
|||
|
|
|
|||
|
|
[JsonProperty("属性")]
|
|||
|
|
public List<Group> Groups { get; set; } = new();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class Group
|
|||
|
|
{
|
|||
|
|
[JsonProperty("分组名称")]
|
|||
|
|
public string Name { get; set; }
|
|||
|
|
|
|||
|
|
[JsonProperty("参数")]
|
|||
|
|
public Dictionary<string, string> Properties = new Dictionary<string, string>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class Property
|
|||
|
|
{
|
|||
|
|
[JsonProperty("参数")]
|
|||
|
|
public string Name { get; set; }
|
|||
|
|
|
|||
|
|
[JsonProperty("值")]
|
|||
|
|
public string Value { get; set; }
|
|||
|
|
}
|
|||
|
|
//public record Property(string Key, string Value);
|
|||
|
|
}
|