87 lines
3.8 KiB
C#
87 lines
3.8 KiB
C#
|
|
using Bentley.DgnPlatformNET;
|
|||
|
|
using Bentley.DgnPlatformNET.Elements;
|
|||
|
|
using Bentley.GeometryNET;
|
|||
|
|
using Bentley.MstnPlatformNET;
|
|||
|
|
|
|||
|
|
namespace Mstn.Toolkit.Extensions
|
|||
|
|
{
|
|||
|
|
internal static class ElementTransformExtensions
|
|||
|
|
{
|
|||
|
|
public static void ApplyTransformInfo(this Element element, DTransform3d transform)
|
|||
|
|
{
|
|||
|
|
TransformInfo transformInfo = new(transform);
|
|||
|
|
element.ApplyTransform(transformInfo);
|
|||
|
|
element.AddToModel();//内存中定义的元素应用到磁盘文件的模型中
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置为当前视角旋转变换
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static void SetCurrentViewRotation(this Element element)
|
|||
|
|
{
|
|||
|
|
Session.GetActiveViewport().GetRotation().TryInvert(out DMatrix3d invertMatrix);//获得当前视角的旋转转置矩阵
|
|||
|
|
DTransform3d dTransform3D = new DTransform3d(invertMatrix);//使用旋转矩阵创建几何变换
|
|||
|
|
TransformInfo transform = new TransformInfo(dTransform3D);//使用几何变换创建变换信息
|
|||
|
|
element.ApplyTransform(transform);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 移动
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="element"></param>
|
|||
|
|
/// <param name="translateVector"></param>
|
|||
|
|
public static void Move(this Element element, DVector3d translateVector)
|
|||
|
|
{
|
|||
|
|
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter;//主单位与分辨率单位的比值,最常用的比值
|
|||
|
|
|
|||
|
|
var vector = translateVector * uorPerMas;
|
|||
|
|
DTransform3d trans = DTransform3d.FromTranslation(vector);
|
|||
|
|
TransformInfo info = new(trans);
|
|||
|
|
element.ApplyTransform(info);
|
|||
|
|
element.AddToModel();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 移动
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="element"></param>
|
|||
|
|
/// <param name="targetPoint"></param>
|
|||
|
|
public static void MoveTo(this Element element, DPoint3d targetPoint)
|
|||
|
|
{
|
|||
|
|
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter;//主单位与分辨率单位的比值,最常用的比值
|
|||
|
|
|
|||
|
|
var target = targetPoint * uorPerMas;
|
|||
|
|
DTransform3d trans = DTransform3d.FromTranslation(target);
|
|||
|
|
TransformInfo info = new(trans);
|
|||
|
|
element.ApplyTransform(info);
|
|||
|
|
element.AddToModel();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 旋转
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="element"></param>
|
|||
|
|
/// <param name="origin">基点</param>
|
|||
|
|
/// <param name="axis">旋转轴</param>
|
|||
|
|
/// <param name="angle">角度(°)</param>
|
|||
|
|
public static void Rotate(this Element element, DPoint3d origin, DVector3d axis, Angle angle)
|
|||
|
|
{
|
|||
|
|
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter;//主单位与分辨率单位的比值,最常用的比值
|
|||
|
|
|
|||
|
|
var vector = origin * uorPerMas;
|
|||
|
|
DTransform3d trans = DTransform3d.FromRotationAroundLine(origin, axis, angle);
|
|||
|
|
TransformInfo info = new(trans);
|
|||
|
|
element.ApplyTransform(info);
|
|||
|
|
element.AddToModel();
|
|||
|
|
}
|
|||
|
|
public static void Rotate(this DisplayableElement element, DVector3d axis, Angle angle)
|
|||
|
|
{
|
|||
|
|
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMeter;//主单位与分辨率单位的比值,最常用的比值
|
|||
|
|
element.GetTransformOrigin(out DPoint3d origin);
|
|||
|
|
var vector = origin * uorPerMas;
|
|||
|
|
DTransform3d trans = DTransform3d.FromRotationAroundLine(origin, axis, angle);
|
|||
|
|
TransformInfo info = new(trans);
|
|||
|
|
element.ApplyTransform(info);
|
|||
|
|
element.AddToModel();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|