找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1118|回复: 0

[分享] GetActiveViewportExtents

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-7 17:37:29 | 显示全部楼层 |阅读模式

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

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

×
// Copyright (C) 2011 by EDA Inc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Project references to both AcMgd.dll & AcDbMgd.dll of ObjectARX are required.
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

namespace EDAInc.AcadAPI
{
    internal static class AcadHelper
    {
        // Generates the transformation matrix to go from WCS to DCS
        // units in a view/viewport.
        //      WCS - World Coordinate System
        //      DCS - Display Coordinate System
        // Copied from:
        //      http://through-the-interface.typ ... _interface/2011/11/
        //                         navigating-an-autocad-model-using-kinect-part-1.html
        //
        // To get a DCS to WCS transformation matrix, use the inverse of
        // the return matrix.  E.g.,
        //      Matrix3d dcs2wcs = Wcs2Dcs(vtr).Inverse();
        //
        public static Matrix3d Wcs2Dcs(AbstractViewTableRecord vtr)
        {
            Matrix3d mat = Matrix3d.PlaneToWorld(vtr.ViewDirection);
            mat = Matrix3d.Displacement(vtr.Target - Point3d.Origin) * mat;
            mat = Matrix3d.Rotation(
                    -vtr.ViewTwist, vtr.ViewDirection, vtr.Target) * mat;
            return mat.Inverse();
        }

        public static bool GetActiveViewportExtents(Document aDoc,
                                                    out Point3d aMinExtWCS,
                                                    out Point3d aMaxExtWCS)
        {
            if (aDoc == null)
                aDoc = Application.DocumentManager.MdiActiveDocument;
            Database db = aDoc.Database;
            Editor ed = aDoc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction()) {
                try {
                    ed.UpdateTiledViewportsInDatabase();

                    // Open the active view port.
                    ViewportTableRecord vp_rec = (ViewportTableRecord)
                            tr.GetObject(ed.ActiveViewportId, OpenMode.ForRead);

                    // Find the min & max extents of the active view port in the DCS.
                    Point2d center_dcs = vp_rec.CenterPoint;
                    double half_width_dcs = vp_rec.Width / 2;
                    double half_height_dcs = vp_rec.Height / 2;
                    Point3d min_ext_dcs = new Point3d(
                            center_dcs.X - half_width_dcs,
                            center_dcs.Y - half_height_dcs,
                            0);
                    Point3d max_ext_dcs = new Point3d(
                            center_dcs.X + half_width_dcs,
                            center_dcs.Y + half_height_dcs,
                            0);

                    // Convert the min & max extents of the view port to WCS.
                    Matrix3d dcs2wcs = AcadHelper.Wcs2Dcs(vp_rec).Inverse();
                    aMinExtWCS = min_ext_dcs.TransformBy(dcs2wcs);
                    aMaxExtWCS = max_ext_dcs.TransformBy(dcs2wcs);

                    tr.Commit();
                    return true;
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex) {
                    tr.Abort();
                    ed.WriteMessage("\n" + ex.Message + "\n");
                    aMinExtWCS = Point3d.Origin;
                    aMaxExtWCS = Point3d.Origin;
                    return false;
                }
            }
        }
    }
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-22 19:52 , Processed in 0.348627 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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