添加项目文件。
This commit is contained in:
256
Mstn.Toolkit/Extensions/ElementExtensions.cs
Normal file
256
Mstn.Toolkit/Extensions/ElementExtensions.cs
Normal file
@@ -0,0 +1,256 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Bentley.DgnPlatformNET;
|
||||
using Bentley.DgnPlatformNET.Elements;
|
||||
using Bentley.GeometryNET;
|
||||
using Bentley.MstnPlatformNET;
|
||||
|
||||
using Mstn.Toolkit.Extensions;
|
||||
|
||||
namespace Mstn.Toolkit.Extensions
|
||||
{
|
||||
public static class ElementExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 元素应用变换
|
||||
/// </summary>
|
||||
/// <param name="elem"></param>
|
||||
/// <param name="transform3D"></param>
|
||||
public static void MakeTransform(this Element elem, DTransform3d transform3D)
|
||||
{
|
||||
Element newElement = Element.GetFromElementRef(elem.GetNativeElementRef());
|
||||
TransformInfo info = new TransformInfo(transform3D);
|
||||
newElement.ApplyTransform(info);
|
||||
newElement.ReplaceInModel(elem);
|
||||
}
|
||||
/// <summary>
|
||||
/// 提取曲面边界点
|
||||
/// </summary>
|
||||
/// <param name="eleMesh"></param>
|
||||
/// <returns></returns>
|
||||
public static HashSet<DPoint3d> ExtractBoundaryPoints(Element eleMesh)
|
||||
{
|
||||
var listPtNew = new HashSet<DPoint3d>();
|
||||
|
||||
var query = MeshQuery.GetAsMeshQuery(eleMesh);
|
||||
PolyfaceHeader faceHeader = query.GetMeshData();
|
||||
|
||||
if (faceHeader.IsClosedByEdgePairing)
|
||||
{
|
||||
foreach (var item in faceHeader.Point)
|
||||
{
|
||||
listPtNew.Add(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var face = PolyfaceHeader.RemoveDuplicateFaces(faceHeader, true);
|
||||
var curVector = face.ExtractBoundaryStrings(out var openPaths, out int closedPaths);
|
||||
foreach (var item in face.Point)
|
||||
{
|
||||
listPtNew.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
return listPtNew;
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置颜色
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <param name="colorIndex"></param>
|
||||
/// <returns></returns>
|
||||
public static Element SetSolidColor(this Element element, uint colorIndex)
|
||||
{
|
||||
if (element.ElementType == MSElementType.Solid)//判断元素类型是否为Solid
|
||||
{
|
||||
ElementPropertiesSetter setter = new ElementPropertiesSetter();//修改元素属性
|
||||
setter.SetColor(colorIndex);//设置索引值colorIndex的颜色为元素颜色
|
||||
setter.Apply(element);//将设置应用于指定元素
|
||||
return element;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取元素的图层,如果是一般单元,则按第一个子元素来获取
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <returns></returns>
|
||||
public static LevelHandle GetLevel(this Element element)
|
||||
{
|
||||
ElementPropertiesGetter getter = new ElementPropertiesGetter(element);
|
||||
|
||||
var lvlcache = Session.Instance.GetActiveDgnFile().GetLevelCache();
|
||||
//一般单元
|
||||
if (element.GetChildren().Count() > 0)
|
||||
{
|
||||
foreach (var elem in element.GetChildren())
|
||||
{
|
||||
if (elem is DisplayableElement && !elem.IsInvisible)
|
||||
{
|
||||
return elem.GetLevel();
|
||||
}
|
||||
}
|
||||
}
|
||||
return lvlcache.GetLevel(getter.Level, true);
|
||||
}
|
||||
/// <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>长度值,单位mm</returns>
|
||||
public static double GetLength(this Element elem)
|
||||
{
|
||||
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, 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