添加项目文件。
This commit is contained in:
296
RebarMsAddins/ElementExtensions.cs
Normal file
296
RebarMsAddins/ElementExtensions.cs
Normal file
@@ -0,0 +1,296 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// 元素扩展类
|
||||
/// </summary>
|
||||
public static class ElementExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过属性名获取钢筋的属性值
|
||||
/// </summary>
|
||||
/// <param name="elem">元素</param>
|
||||
/// <param name="propName">属性名</param>
|
||||
/// <returns>属性值,可根据情况进行转换数据类型</returns>
|
||||
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;
|
||||
}
|
||||
/// <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="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>
|
||||
/// 获取ItemType,包含多个CustomProperty
|
||||
/// </summary>
|
||||
/// <param name="itemLibName"></param>
|
||||
/// <param name="itemTypeName"></param>
|
||||
/// <returns></returns>
|
||||
public static ItemType GetItemType(string itemLibName, string itemTypeName)
|
||||
{
|
||||
DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
|
||||
//根据声明名称获取对应的ItemTypeLibrary
|
||||
ItemTypeLibrary itemTypeLibrary = ItemTypeLibrary.FindByName(itemLibName, dgnFile);
|
||||
return itemTypeLibrary?.GetItemTypeByName(itemTypeName);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// 设置元素图层
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <param name="leveName"></param>
|
||||
/// <remarks>图层名不存在时,则创建</remarks>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取元素的长度
|
||||
/// </summary>
|
||||
/// <param name="elem">元素</param>
|
||||
/// <returns>长度值,单位m,保留两位小数</returns>
|
||||
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);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取元素的CurveVector
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取元素中的圆弧
|
||||
/// </summary>
|
||||
/// <param leveName="elem"></param>
|
||||
/// <returns>若没有,则返回空列表</returns>
|
||||
public static List<DEllipse3d> GetArcs(this Element elem)
|
||||
{
|
||||
var list = new List<DEllipse3d>();
|
||||
foreach (var child in elem.GetCurveVector())
|
||||
{
|
||||
if (child.GetCurvePrimitiveType() == CurvePrimitive.CurvePrimitiveType.Arc)
|
||||
{
|
||||
child.TryGetArc(out var arc);
|
||||
list.Add(arc);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取元素中线
|
||||
/// </summary>
|
||||
/// <param leveName="elem"></param>
|
||||
/// <returns></returns>
|
||||
public static List<DSegment3d> GetLines(this Element elem)
|
||||
{
|
||||
var list = new List<DSegment3d>();
|
||||
foreach (var child in elem.GetCurveVector())
|
||||
{
|
||||
if (child.GetCurvePrimitiveType() == CurvePrimitive.CurvePrimitiveType.Arc)
|
||||
{
|
||||
child.TryGetLine(out var line);
|
||||
list.Add(line);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user