-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorExtension.cs
128 lines (121 loc) · 6.21 KB
/
EditorExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
namespace Gile.AutoCAD.R19.Geometry
{
/// <summary>
/// Provides extension methods for the Editor type.
/// </summary>
public static class EditorExtension
{
/// <summary>
/// Gets the transformation matrix from the current User Coordinate System (UCS) to the World Coordinate System (WCS).
/// </summary>
/// <param name="ed">The instance to which this method applies.</param>
/// <returns>The UCS to WCS transformation matrix.</returns>
/// <exception cref="System.ArgumentNullException">ArgumentNullException is thrown if <paramref name="ed"/> is null.</exception>
public static Matrix3d UCS2WCS(this Editor ed)
{
Assert.IsNotNull(ed, nameof(ed));
return ed.CurrentUserCoordinateSystem;
}
/// <summary>
/// Gets the transformation matrix from the World Coordinate System (WCS) to the current User Coordinate System (UCS).
/// </summary>
/// <param name="ed">The instance to which this method applies.</param>
/// <returns>The WCS to UCS transformation matrix.</returns>
/// <exception cref="System.ArgumentNullException">ArgumentNullException is thrown if <paramref name="ed"/> is null.</exception>
public static Matrix3d WCS2UCS(this Editor ed)
{
Assert.IsNotNull(ed, nameof(ed));
return ed.CurrentUserCoordinateSystem.Inverse();
}
/// <summary>
/// Gets the transformation matrix from the current viewport Display Coordinate System (DCS) to the World Coordinate System (WCS).
/// </summary>
/// <param name="ed">The instance to which this method applies.</param>
/// <returns>The DCS to WCS transformation matrix.</returns>
/// <exception cref="System.ArgumentNullException">ArgumentNullException is thrown if <paramref name="ed"/> is null.</exception>
public static Matrix3d DCS2WCS(this Editor ed)
{
Assert.IsNotNull(ed, nameof(ed));
Matrix3d retVal = new Matrix3d();
bool tilemode = ed.Document.Database.TileMode;
if (!tilemode)
ed.SwitchToModelSpace();
using (ViewTableRecord vtr = ed.GetCurrentView())
{
retVal =
Matrix3d.Rotation(-vtr.ViewTwist, vtr.ViewDirection, vtr.Target) *
Matrix3d.Displacement(vtr.Target - Point3d.Origin) *
Matrix3d.PlaneToWorld(vtr.ViewDirection);
}
if (!tilemode)
ed.SwitchToPaperSpace();
return retVal;
}
/// <summary>
/// Gets the transformation matrix from the World Coordinate System (WCS) to the current viewport Display Coordinate System (DCS).
/// </summary>
/// <param name="ed">The instance to which this method applies.</param>
/// <returns>The WCS to DCS transformation matrix.</returns>
/// <exception cref="System.ArgumentNullException">ArgumentNullException is thrown if <paramref name="ed"/> is null.</exception>
public static Matrix3d WCS2DCS(this Editor ed)
{
Assert.IsNotNull(ed, nameof(ed));
return ed.DCS2WCS().Inverse();
}
/// <summary>
/// Gets the transformation matrix from the paper space active viewport Display Coordinate System (DCS) to the Paper space Display Coordinate System (PSDCS).
/// </summary>
/// <param name="ed">The instance to which this method applies.</param>
/// <returns>The DCS to PSDCS transformation matrix.</returns>
/// <exception cref="System.ArgumentNullException">ArgumentNullException is thrown if <paramref name="ed"/> is null.</exception>
/// <exception cref=" Autodesk.AutoCAD.Runtime.Exception">
/// eNotInPaperSpace is thrown if this method is called form Model Space.</exception>
/// <exception cref=" Autodesk.AutoCAD.Runtime.Exception">
/// eCannotChangeActiveViewport is thrown if there is none floating viewport in the current layout.</exception>
public static Matrix3d DCS2PSDCS(this Editor ed)
{
Assert.IsNotNull(ed, nameof(ed));
Database db = ed.Document.Database;
if (db.TileMode)
throw new AcRx.Exception(AcRx.ErrorStatus.NotInPaperspace);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Viewport vp =
(Viewport)tr.GetObject(ed.CurrentViewportObjectId, OpenMode.ForRead);
if (vp.Number == 1)
{
try
{
ed.SwitchToModelSpace();
vp = (Viewport)tr.GetObject(ed.CurrentViewportObjectId, OpenMode.ForRead);
ed.SwitchToPaperSpace();
}
catch
{
throw new AcRx.Exception(AcRx.ErrorStatus.CannotChangeActiveViewport);
}
}
return vp.DCS2PSDCS();
}
}
/// <summary>
/// Gets the transformation matrix from the Paper space Display Coordinate System (PSDCS) to the paper space active viewport Display Coordinate System (DCS).
/// </summary>
/// <param name="ed">The instance to which this method applies.</param>
/// <returns>The PSDCS to DCS transformation matrix.</returns>
/// <exception cref="System.ArgumentNullException">ArgumentNullException is thrown if <paramref name="ed"/> is null.</exception>
/// <exception cref=" Autodesk.AutoCAD.Runtime.Exception">
/// eNotInPaperSpace is thrown if this method is called form Model Space.</exception>
/// <exception cref=" Autodesk.AutoCAD.Runtime.Exception">
/// eCannotChangeActiveViewport is thrown if there is none floating viewport in the current layout.</exception>
public static Matrix3d PSDCS2DCS(this Editor ed)
{
Assert.IsNotNull(ed, nameof(ed));
return ed.DCS2PSDCS().Inverse();
}
}
}