AutoCAD .Net 设置UCS与直线对齐

以下代码展示:
将 用户坐标系(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();
}

参考文章
Align the UCS to an entity

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值