找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1485|回复: 0

[分享] 8eeb8eRetaining transient graphics always parallel to the screen

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2015-3-17 07:47:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在当前视口区左上角 (20 20) 像素单位固定显示的一个矩形框,不随屏幕缩放而变化大小Retaining transient graphics always parallel to the screenBy Balaji Ramamoorthy
You may be interested in using transient graphics that is always retained parallel to the screen despite of any view changes.
The “Editor.PointToWorld” method can be used to convert a point whose coordinates are known in screen coordinates and obtain its WCS equivalent. This method can be used to compute the points in WCS while we specify the coordinates in DCS.
However, the difficulty in implementing it is to know when the view changed so that you can refresh the transient graphics to follow the view change.
This is because there is no direct way provided by the API to know when the view parameters got changed.
The object modified event for a viewport table record is only triggered when the view change is performed using the view cube. Pan / zoom using mouse wheel does not trigger it.
As a workaround for this, you may consider using a timer callback to monitor the view parameters and after the view parameters change, use transient graphics to draw parallel to the screen (DCS).
The other way is to use the Editor.PointMonitor event but you may not get this event notification when zoom/pan from the command prompt. After the mouse enters the editor, the transient graphics does get refreshed.
Here is a sample code using the second approach. It draws a rectangle using transient graphics and retains it always parallel to the screen:

using System.Drawing;
using Autodesk.AutoCAD.GraphicsInterface;

public class TestCommands : IExtensionApplication
{
    public static DBObjectCollection _transients = null;
    public double _viewSize = 1.0;
    public Point3d _viewCtr = Point3d.Origin;
    public Point3d _viewDir = Point3d.Origin;

    [CommandMethod("DrawTransientRect", CommandFlags.Session)]
    public void DrawTransientRectMethod()
    {
        Document activeDoc
            = Application.DocumentManager.MdiActiveDocument;
        Database db = activeDoc.Database;
        Editor ed = activeDoc.Editor;

        // Clear the previous transient graphics
        ClearTransientGraphics();

        Point2d screenSize
            = (Point2d)Application.GetSystemVariable("SCREENSIZE");

        // Width and height of the rectangle
        int width = 20;
        int height = 60;

        // Four corner points in screen coordinates
        System.Drawing.Point upperLeft
                            = new System.Drawing.Point(20, 20);

        System.Drawing.Point upperRight
            = new System.Drawing.Point(upperLeft.X + width, upperLeft.Y);

        System.Drawing.Point lowerLeft
            = new System.Drawing.Point(upperLeft.X, upperLeft.Y + height);

        System.Drawing.Point lowerRight
            = new System.Drawing.Point(upperLeft.X + width, upperLeft.Y + height);

        // Four corner points in WCS
        Point3d upperLeftWorld = ed.PointToWorld(upperLeft, 0);
        Point3d upperRightWorld = ed.PointToWorld(upperRight, 0);
        Point3d lowerLeftWorld = ed.PointToWorld(lowerLeft, 0);
        Point3d lowerRightWorld = ed.PointToWorld(lowerRight, 0);

        // Create the transient entities
        Line l1 = new Line(upperLeftWorld, upperRightWorld);
        l1.ColorIndex = 2;
        Line l2 = new Line(upperRightWorld, lowerRightWorld);
        l2.ColorIndex = 2;
        Line l3 = new Line(lowerRightWorld, lowerLeftWorld);
        l3.ColorIndex = 2;
        Line l4 = new Line(lowerLeftWorld, upperLeftWorld);
        l4.ColorIndex = 2;

        _transients.Add(l1);
        _transients.Add(l2);
        _transients.Add(l3);
        _transients.Add(l4);

        IntegerCollection intCol = new IntegerCollection();
        TransientManager tm = TransientManager.CurrentTransientManager;
        tm.AddTransient
            (
                l1,
                TransientDrawingMode.Main,
                128,
                intCol
            );

        tm.AddTransient
            (
                l2,
                TransientDrawingMode.Main,
                128,
                intCol
            );

        tm.AddTransient
            (
                l3,
                TransientDrawingMode.Main,
                128,
                intCol
            );

        tm.AddTransient
            (
                l4,
                TransientDrawingMode.Main,
                128,
                intCol
            );
    }

    // Erases any transient graphics
    void ClearTransientGraphics()
    {
        TransientManager tm
                = TransientManager.CurrentTransientManager;
        IntegerCollection intCol = new IntegerCollection();
        if (_transients != null)
        {
            foreach (DBObject transient in _transients)
            {
                tm.EraseTransient(
                                    transient,
                                    intCol
                                    );
                transient.Dispose();
            }
            _transients.Clear();
        }
        else
            _transients = new DBObjectCollection();
    }

    #region IExtensionApplication Members

    void IExtensionApplication.Initialize()
    {
        Document activeDoc
            = Application.DocumentManager.MdiActiveDocument;
        Editor ed = activeDoc.Editor;

        // Subscribe to PointMonitor event to refresh
        // transient graphics in case of any view changes.
        ed.PointMonitor +=
                new PointMonitorEventHandler(ed_PointMonitor);
    }

    void ed_PointMonitor(object sender, PointMonitorEventArgs e)
    {
        Document activeDoc = Application.DocumentManager.MdiActiveDocument;
        double viewSize
            = (double)Application.GetSystemVariable("VIEWSIZE");

        Point3d viewCtr
            = (Point3d)Application.GetSystemVariable("VIEWCTR");

        Point3d viewDir
            = (Point3d)Application.GetSystemVariable("VIEWDIR");

        // Simple check to verify if the view parameters changed
        // since we last drew the transient graphics
        if (    viewSize != _viewSize ||
                viewCtr.Equals(_viewCtr) == false ||
                viewDir.Equals(_viewDir) == false
            )
        {
            _viewSize = viewSize;

            _viewCtr
                = (Point3d)Application.GetSystemVariable("VIEWCTR");

            _viewDir = viewDir;

            // Draw the transient graphics again since
            // the view parameters seem to have changed
            DrawTransientRectMethod();
        }
    }

    void IExtensionApplication.Terminate()
    {
        Document activeDoc
            = Application.DocumentManager.MdiActiveDocument;
        if (activeDoc != null)
        {
            // Unsubscribe
            Editor ed = activeDoc.Editor;
            ed.PointMonitor -=
                new PointMonitorEventHandler(ed_PointMonitor);
        }

        // Cleanup before we leave
        ClearTransientGraphics();
    }

    #endregion
}



论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-14 20:54 , Processed in 0.205390 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表