欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AutoCAD .Net 绘制临时图形

程序员文章站 2024-03-18 12:48:22
...

AutoCAD .Net API 中,Editor 类提供了两个方法来临时绘图。
public void DrawVector(Point3d from, Point3d to, int color, bool drawHighlighted);
public void DrawVectors(ResultBuffer rb, Matrix3d transform);
这些绘制的图形会在 AutoCAD 窗口刷新时消失。

以下示例代码实现在鼠标点击处绘制临时的十字图形。

[CommandMethod("Test")]
public void Test()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;

    PromptPointResult ret = doc.Editor.GetPoint("\nPick point");
    if (ret.Status != PromptStatus.OK)
        return;

    Point3d pnt = ret.Value;
    Point3d pnt1 = new Point3d(pnt.X - 20, pnt.Y, pnt.Z);
    Point3d pnt2 = new Point3d(pnt.X + 20, pnt.Y, pnt.Z);
    Point3d pnt3 = new Point3d(pnt.X, pnt.Y - 20, pnt.Z);
    Point3d pnt4 = new Point3d(pnt.X, pnt.Y + 20, pnt.Z);

    int colorIndex = 4;
    doc.Editor.DrawVector(pnt1, pnt2, colorIndex, false);
    doc.Editor.DrawVector(pnt3, pnt4, colorIndex, false);
}

或者用如下代码:

[CommandMethod("Test")]
public void Test()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;

    PromptPointResult ret = doc.Editor.GetPoint("\nPick point");
    if (ret.Status != PromptStatus.OK)
        return;

    Point3d pnt = ret.Value;
    Point2d pnt1 = new Point2d(pnt.X - 20, pnt.Y);
    Point2d pnt2 = new Point2d(pnt.X + 20, pnt.Y);
    Point2d pnt3 = new Point2d(pnt.X, pnt.Y - 20);
    Point2d pnt4 = new Point2d(pnt.X, pnt.Y + 20);

    int colorIndex = 4;
    int RTPOINT = 5002;
    int RTSHORT = 5003;
    using (ResultBuffer resBuf = new ResultBuffer())
    {
        resBuf.Add(new TypedValue(RTSHORT, colorIndex));
        resBuf.Add(new TypedValue(RTPOINT, pnt1));
        resBuf.Add(new TypedValue(RTPOINT, pnt2));
        resBuf.Add(new TypedValue(RTPOINT, pnt3));
        resBuf.Add(new TypedValue(RTPOINT, pnt4));
        doc.Editor.DrawVectors(resBuf, Matrix3d.Identity);
    }
}

参考文章:
http://adndevblog.typepad.com/autocad/2012/04/temporary-graphics-in-autocad.html

相关标签: AutoCAD