using System; using System.Collections.Generic; using Bentley.DgnPlatformNET; using Bentley.DgnPlatformNET.DgnEC; using Bentley.DgnPlatformNET.Elements; using Bentley.ECObjects.Instance; using Bentley.GeometryNET; using Bentley.MstnPlatformNET; namespace RebarMsAddins { /// /// 元素扩展类 /// public static class ElementExtensions { /// /// 通过属性名获取钢筋的属性值 /// /// 元素 /// 属性名 /// 属性值,可根据情况进行转换数据类型 public static object GetRebarPropertyValue(this Element elem, string propName) { var instances = new CustomItemHost(elem, false).CustomItems; object value = default; foreach (var instance in instances) { if ((instance.ClassDefinition.DisplayLabel == "钢筋" || instance.ClassDefinition.DisplayLabel == "纵向钢筋") && instance.ContainsValues) { foreach (var propValue in instance) { if (propValue.Property.DisplayLabel == propName && propValue.TryGetNativeValue(out value)) { break; } } } } return value; } /// /// 元素之间传递ItemTypes及值 /// /// /// public static void DeliverItemTypesTo(this Element source, Element target) { CustomItemHost host = new CustomItemHost(source, true);//声明元素的CustomItemHost var instances = host.CustomItems; //List list = new List(); 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); } } } } } /// /// 通过属性名设置属性值,多个同名属性会同时设置 /// /// 元素 /// 属性名 /// 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) { } } /// /// 获取ItemType,包含多个CustomProperty /// /// /// /// public static ItemType GetItemType(string itemLibName, string itemTypeName) { DgnFile dgnFile = Session.Instance.GetActiveDgnFile(); //根据声明名称获取对应的ItemTypeLibrary ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName(itemLibName, dgnFile); return itemTypeLibrary?.GetItemTypeByName(itemTypeName); } /// /// 挂接ItemType到元素 /// /// /// /// 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); } /// /// 设置元素图层 /// /// /// /// 图层名不存在时,则创建 /// public static Element SetLevel(this Element element, string leveName) { Element oldElem = Element.GetFromElementRef(element.GetNativeElementRef()); if (string.IsNullOrEmpty(leveName)) { return element; } //获取指定名层 DgnFile dgnfile = Session.Instance.GetActiveDgnFile(); FileLevelCache lvlCache = dgnfile.GetLevelCache(); LevelHandle lvlHandle = lvlCache.GetLevelByName(leveName); if (!lvlHandle.IsValid)//当前文件中不存在时创建 { lvlCache.CreateLevel(leveName); lvlCache.Write(); lvlHandle = lvlCache.GetLevelByName(leveName); } //设置元素的层属性写入模型中 ElementPropertiesSetter eleSet = new ElementPropertiesSetter(); eleSet.SetLevel(lvlHandle.LevelId); eleSet.Apply(element); element.ReplaceInModel(oldElem); return element; } /// /// 获取元素的长度 /// /// 元素 /// 长度值,单位m,保留两位小数 public static double GetLength(this Element elem) { var uorMeter = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter; double length = default; if (elem is LineElement line) { length = line.GetCurveVector().SumOfLengths(); } if (elem is ComplexStringElement complexString) { length = complexString.GetCurveVector().SumOfLengths(); } if (elem is LineStringBaseElement lineStringBase) { length = lineStringBase.GetCurveVector().SumOfLengths(); } if (elem is EllipseElement ellipseElement) { length = ellipseElement.GetCurveVector().SumOfLengths(); } //if (elem is LineStringElement lineString) //{ // curve = lineString.GetCurveVector().SumOfLengths(); // //GetFilteredECPropValue(lineString, "ID-100-名称"); //} //if (elem is ShapeElement shape) //{ // curve = shape.GetCurveVector().SumOfLengths(); //} if (elem is ComplexShapeElement complexShape) { length = complexShape.GetCurveVector().SumOfLengths(); } if (elem is BSplineCurveElement bSplineCurve) { length = bSplineCurve.GetCurveVector().SumOfLengths(); } return Math.Round(length / uorMeter, 3, MidpointRounding.AwayFromZero); } /// /// 获取元素的CurveVector /// /// public static CurveVector GetCurveVector(this Element elem) { CurveVector curve = default; if (elem is LineElement line) { curve = line.GetCurveVector(); } if (elem is ComplexStringElement complexString) { curve = complexString.GetCurveVector(); } if (elem is LineStringBaseElement lineStringBase) { curve = lineStringBase.GetCurveVector(); } if (elem is EllipseElement ellipseElement) { curve = ellipseElement.GetCurveVector(); } //if (elem is LineStringElement lineString) //{ // curve = lineString.GetCurveVector(); // //GetFilteredECPropValue(lineString, "ID-100-名称"); //} //if (elem is ShapeElement shape) //{ // curve = shape.GetCurveVector(); //} if (elem is ComplexShapeElement complexShape) { curve = complexShape.GetCurveVector(); } if (elem is BSplineCurveElement bSplineCurve) { curve = bSplineCurve.GetCurveVector(); } return curve; } /// /// 获取元素中的圆弧 /// /// /// 若没有,则返回空列表 public static List GetArcs(this Element elem) { var list = new List(); foreach (var child in elem.GetCurveVector()) { if (child.GetCurvePrimitiveType() == CurvePrimitive.CurvePrimitiveType.Arc) { child.TryGetArc(out var arc); list.Add(arc); } } return list; } /// /// 获取元素中线 /// /// /// public static List GetLines(this Element elem) { var list = new List(); foreach (var child in elem.GetCurveVector()) { if (child.GetCurvePrimitiveType() == CurvePrimitive.CurvePrimitiveType.Arc) { child.TryGetLine(out var line); list.Add(line); } } return list; } } }