AutoCAD .Net 设置UCS与直线对齐
程序员文章站
2024-03-18 12:38:34
...
以下代码展示:
将 用户坐标系(UCS)的X轴设置为与直线平齐,原点设置为直线的起点。
[CommandMethod("SetUCS")]
static public void SetUCS()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
// 选择直线
PromptEntityOptions options = new PromptEntityOptions("\n选择直线: ");
options.SetRejectMessage("\n选择直线");
options.AddAllowedClass(typeof(Line), false);
PromptEntityResult res = doc.Editor.GetEntity(options);
if (res.Status != PromptStatus.OK)
return;
// 设置UCS与直线平齐
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Line line = tr.GetObject(res.ObjectId, OpenMode.ForRead) as Line;
// 新的UCS坐标系
Matrix3d ecs = new Matrix3d();
Vector3d xVec = (line.EndPoint - line.StartPoint).GetNormal();
CoordinateSystem3d csys = new CoordinateSystem3d(
line.StartPoint, // 原点
xVec, // x轴
line.Normal.CrossProduct(xVec) // y轴
);
// 新UCS的坐标变换矩阵
ecs = Matrix3d.AlignCoordinateSystem(
Point3d.Origin,
Vector3d.XAxis,
Vector3d.YAxis,
Vector3d.ZAxis,
csys.Origin,
csys.Xaxis,
csys.Yaxis,
csys.Zaxis);
doc.Editor.CurrentUserCoordinateSystem = ecs;
tr.Commit();
}
// 刷新Viewport
doc.Editor.UpdateTiledViewportsInDatabase();
}