AutoCAD .Net 绘制临时图形

作者: admin 分类: CAD 发布时间: 2019-12-11 16:07

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

https://blog.csdn.net/hisinwang/article/details/78764531

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

标签云