298 lines
13 KiB
C#
298 lines
13 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Windows;
|
|||
|
|
//using Bentley.DgnPlatform;
|
|||
|
|
using Bentley.DgnPlatformNET;
|
|||
|
|
using Bentley.DgnPlatformNET.DgnEC;
|
|||
|
|
using Bentley.DgnPlatformNET.Elements;
|
|||
|
|
using Bentley.ECObjects.Instance;
|
|||
|
|
using Bentley.ECObjects.Schema;
|
|||
|
|
using Bentley.ECObjects.XML;
|
|||
|
|
using Bentley.MstnPlatformNET;
|
|||
|
|
|
|||
|
|
using Mstn.Toolkit.Extensions;
|
|||
|
|
|
|||
|
|
namespace Mstn.Toolkit.Extensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// EC扩展类
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>
|
|||
|
|
//ECRepository 就相当于某某项目施工图项目阶段/整个数据库系统
|
|||
|
|
//ECSchema 就相当于结构专业/数据库的定义 ItemTypesLibrary包含多个ItemType
|
|||
|
|
//ecClass 就相当于结构柱明细表/数据库中表的定义 ItemType包含多个ECProperty
|
|||
|
|
//ECProperty 就相当于结构柱属性定义(钢筋型号,混凝土强度,混凝土保护层厚度)/表结构中每一列的定义
|
|||
|
|
//ECInstance 就相当于指定某个结构柱/每个表中的每一行
|
|||
|
|
//ECPropertyValue 就相当于该结构柱的属性值(如钢筋混凝土强度为C40,钢筋型号HRB400,混凝土保护层厚度取35)/每一行中的每一个单元
|
|||
|
|
//ECClass加上所产生的ECInstances 就相当于该项目的柱明细表/一个数据库所有的表格
|
|||
|
|
/// </remarks>
|
|||
|
|
public static class EngineeringContentExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 挂接ItemType到元素
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="elem"></param>
|
|||
|
|
/// <param name="itemLibName"></param>
|
|||
|
|
/// <param name="itemTypeName"></param>
|
|||
|
|
public static void AttachItemType(this Element elem, string itemLibName, string itemTypeName)
|
|||
|
|
{
|
|||
|
|
DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//获得当前激活的文件
|
|||
|
|
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
|
|||
|
|
//根据声明名称获取对应的ItemType
|
|||
|
|
ItemType itemType = GetItemType(itemLibName, itemTypeName);
|
|||
|
|
|
|||
|
|
CustomItemHost host = new CustomItemHost(elem, true);//声明元素的CustomItemHost
|
|||
|
|
//实例化
|
|||
|
|
IDgnECInstance item = host.ApplyCustomItem(itemType, true);//对CustomItemHost添加ItemType得到EC实例
|
|||
|
|
|
|||
|
|
EditParameterDefinitions defs = EditParameterDefinitions.GetForModel(dgnModel);//获得模型中的参数定义
|
|||
|
|
DgnECInstanceEnabler enabler = DgnECManager.Manager.ObtainInstanceEnabler(dgnFile, itemType.ECClass);//获得文件中使用指定EC类的EC实例
|
|||
|
|
if (null != enabler && enabler.SupportsCreateInstanceOnElement)//判断是否是否支持对元素挂接EC实例
|
|||
|
|
defs.SetDomainParameters(enabler.SharedWipInstance);//对参数定义设置域参数
|
|||
|
|
|
|||
|
|
item.ScheduleChanges(elem);//对元素更新ItemType信息
|
|||
|
|
elem.ReplaceInModel(elem);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 定义Schema
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static IECSchema DefineECSchema()
|
|||
|
|
{
|
|||
|
|
IECSchema schema = new ECSchema("ComplexSchema2", 1, 1, "");//声明ECSchema
|
|||
|
|
//create the Project class
|
|||
|
|
ECClass prjClass = new ECClass("ProjectClass");//创建EC类
|
|||
|
|
prjClass.Add(new ECProperty("ProjectID", Bentley.ECObjects.ECObjects.IntegerType));//在EC类中添加EC属性
|
|||
|
|
prjClass.Add(new ECProperty("ProjectName", Bentley.ECObjects.ECObjects.StringType));//在EC类中添加EC属性
|
|||
|
|
prjClass.Add(new ECProperty("ProjectCost", Bentley.ECObjects.ECObjects.DoubleType));//在EC类中添加EC属性
|
|||
|
|
prjClass.IsCustomAttribute = false;//设置EC类是否为自定义属性
|
|||
|
|
prjClass.IsDomainClass = true;//设置EC类是否是域类
|
|||
|
|
schema.AddClass(prjClass);//在ECSchema中添加EC类
|
|||
|
|
|
|||
|
|
//create the Member class
|
|||
|
|
ECClass memberClass = new ECClass("MemberClass");//声明EC类
|
|||
|
|
memberClass.Add(new ECProperty("MemberName", Bentley.ECObjects.ECObjects.StringType));//在EC类中添加EC属性
|
|||
|
|
memberClass.Add(new ECProperty("MemberID", Bentley.ECObjects.ECObjects.IntegerType));//在EC类中添加EC属性
|
|||
|
|
memberClass.Add(new ECProperty("MemberRole", Bentley.ECObjects.ECObjects.StringType));//在EC类中添加EC属性
|
|||
|
|
memberClass.IsCustomAttribute = false;//设置EC类是否为自定义属性
|
|||
|
|
memberClass.IsDomainClass = true;//设置EC类是否是域类
|
|||
|
|
schema.AddClass(memberClass);//在ECSchema中添加EC类
|
|||
|
|
return schema;//返回ECSchema
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 元素之间传递ItemTypes及值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="source"></param>
|
|||
|
|
/// <param name="target"></param>
|
|||
|
|
public static void DeliverItemTypesTo(this Element source, Element target)
|
|||
|
|
{
|
|||
|
|
CustomItemHost host = new CustomItemHost(source, true);//声明元素的CustomItemHost
|
|||
|
|
var instances = host.CustomItems;
|
|||
|
|
//List<ItemType> list = new List<ItemType>();
|
|||
|
|
|
|||
|
|
foreach (var instance in instances)
|
|||
|
|
{
|
|||
|
|
var ecClass = instance.ClassDefinition;//ItemTypes
|
|||
|
|
var lib = ecClass.Schema;//ItemTypesLibrary
|
|||
|
|
|
|||
|
|
target.AttachItemType(lib.DisplayLabel, ecClass.DisplayLabel);
|
|||
|
|
|
|||
|
|
if (instance.ContainsValues)
|
|||
|
|
{
|
|||
|
|
foreach (var propValue in instance)
|
|||
|
|
{
|
|||
|
|
if (propValue.TryGetNativeValue(out var value))
|
|||
|
|
{
|
|||
|
|
var prop = propValue.Property;
|
|||
|
|
target.SetECPropertyValues(prop.DisplayLabel, value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取元素的所有属性
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="element"></param>
|
|||
|
|
public static StringBuilder ReadAllProperties(this Element element)
|
|||
|
|
{
|
|||
|
|
if (null == element)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("所提供ElemId无效");
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
DgnECManager ecManager = DgnECManager.Manager;
|
|||
|
|
int count = 0;
|
|||
|
|
DgnECInstanceCollection instCol = ecManager.GetElementProperties(element, ECQueryProcessFlags.SearchAllClasses);
|
|||
|
|
foreach (IDgnECInstance inst in instCol)
|
|||
|
|
{
|
|||
|
|
count++;
|
|||
|
|
sb.AppendLine($"----ECInstance{count}[{inst.ClassDefinition.Name}]");
|
|||
|
|
IEnumerator<IECProperty> propertyEnum = inst.ClassDefinition.GetEnumerator();
|
|||
|
|
while (propertyEnum.MoveNext())
|
|||
|
|
{
|
|||
|
|
IECPropertyValue propertyValue = inst.GetPropertyValue(propertyEnum.Current.Name);
|
|||
|
|
if (propertyValue.IsArray)
|
|||
|
|
{
|
|||
|
|
IECArrayValue arrayVal = propertyValue as IECArrayValue;
|
|||
|
|
if (arrayVal.Count >= 1)
|
|||
|
|
propertyValue = arrayVal[0];
|
|||
|
|
}
|
|||
|
|
var prop = propertyEnum.Current.Name;
|
|||
|
|
ECNameValidation.DecodeFromValidName(ref prop);
|
|||
|
|
//if (prop == "Level")
|
|||
|
|
//{
|
|||
|
|
|
|||
|
|
//}
|
|||
|
|
if (propertyValue.TryGetStringValue(out var strVal))
|
|||
|
|
{
|
|||
|
|
sb.AppendLine($"Property={prop}, Value={strVal}");
|
|||
|
|
}
|
|||
|
|
else if (propertyValue.TryGetDoubleValue(out var dblVal))
|
|||
|
|
sb.AppendLine($"Property={prop}, Value={dblVal}");
|
|||
|
|
else if (propertyValue.TryGetIntValue(out var intVal))
|
|||
|
|
sb.AppendLine($"Property={prop}, Value={intVal}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//MessageBox.Show(sb.ToString());
|
|||
|
|
return sb;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取Attach引用的属性,ItemType属性
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="elem">元素</param>
|
|||
|
|
/// <param name="propName">属性名</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<IECPropertyValue> GetAttachedProperties(this Element elem, string propName)
|
|||
|
|
{
|
|||
|
|
var host = new CustomItemHost(elem, false).CustomItems;
|
|||
|
|
|
|||
|
|
List<IECPropertyValue> values = new List<IECPropertyValue>();
|
|||
|
|
foreach (var instance in host)
|
|||
|
|
{
|
|||
|
|
if (instance.ContainsValues)
|
|||
|
|
{
|
|||
|
|
foreach (var prop in instance)
|
|||
|
|
{
|
|||
|
|
if (prop.Property is ECProperty p && p.DisplayLabel == propName)
|
|||
|
|
{
|
|||
|
|
values.Add(prop);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return values;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取ItemType,包含多个CustomProperty
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="itemLibName"></param>
|
|||
|
|
/// <param name="itemTypeName"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private static ItemType GetItemType(string itemLibName, string itemTypeName)
|
|||
|
|
{
|
|||
|
|
DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
|
|||
|
|
//根据声明名称获取对应的ItemTypeLibrary
|
|||
|
|
ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName(itemLibName, dgnFile);
|
|||
|
|
return itemTypeLibrary?.GetItemTypeByName(itemTypeName);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取所有属性
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="element"></param>
|
|||
|
|
public static List<IECPropertyValue> GetProperyValues(this Element element, string propName)
|
|||
|
|
{
|
|||
|
|
if (null == element)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(element), "所提供ElemId无效");
|
|||
|
|
}
|
|||
|
|
List<IECPropertyValue> eCProperties = new List<IECPropertyValue>();
|
|||
|
|
DgnECManager ecManager = DgnECManager.Manager;
|
|||
|
|
int count = 0;
|
|||
|
|
DgnECInstanceCollection instCol = ecManager.GetElementProperties(element, ECQueryProcessFlags.SearchAllClasses);
|
|||
|
|
foreach (IDgnECInstance inst in instCol)
|
|||
|
|
{
|
|||
|
|
count++;
|
|||
|
|
IEnumerator<IECProperty> propertyEnum = inst.ClassDefinition.GetEnumerator();
|
|||
|
|
while (propertyEnum.MoveNext())
|
|||
|
|
{
|
|||
|
|
IECPropertyValue propertyValue = inst.GetPropertyValue(propertyEnum.Current.Name);
|
|||
|
|
if (propertyValue == null)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
if (propertyValue.IsArray)
|
|||
|
|
{
|
|||
|
|
IECArrayValue arrayVal = propertyValue as IECArrayValue;
|
|||
|
|
if (arrayVal.Count >= 1)
|
|||
|
|
propertyValue = arrayVal[0];
|
|||
|
|
}
|
|||
|
|
var prop = propertyEnum.Current.Name;
|
|||
|
|
ECNameValidation.DecodeFromValidName(ref prop);
|
|||
|
|
var displayLabel = propertyEnum.Current.DisplayLabel;
|
|||
|
|
|
|||
|
|
if (propName == prop || displayLabel == propName)
|
|||
|
|
{
|
|||
|
|
eCProperties.Add(propertyValue);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return eCProperties;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取属性值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="propertyValue"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static object GetValue(this IECPropertyValue propertyValue)
|
|||
|
|
{
|
|||
|
|
//解码属性名称
|
|||
|
|
propertyValue.TryGetNativeValue(out var value);
|
|||
|
|
return value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 通过属性名设置属性值,多个同名属性会同时设置
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="elem">元素</param>
|
|||
|
|
/// <param name="propName">属性名</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static void SetECPropertyValues(this Element elem, string propName, object value)
|
|||
|
|
{
|
|||
|
|
var instances = new CustomItemHost(elem, false).CustomItems;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
foreach (var instance in instances)
|
|||
|
|
{
|
|||
|
|
instance.SetValue(propName, value);
|
|||
|
|
instance.WriteChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置元素属性的Items中的属性值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="itemType"></param>
|
|||
|
|
/// <param name="propName"></param>
|
|||
|
|
/// <param name="value"></param>
|
|||
|
|
public static void SetItemTypeProp(this IDgnECInstance ecIns, string propName, object value)
|
|||
|
|
{
|
|||
|
|
ecIns.SetValue(propName, value);
|
|||
|
|
ecIns.WriteChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|