- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
// 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;
}
}
}
}
} |
|