1
- using Autodesk . AutoCAD . DatabaseServices ;
1
+ using Autodesk . AutoCAD . ApplicationServices ;
2
+ using Autodesk . AutoCAD . DatabaseServices ;
2
3
using Autodesk . AutoCAD . EditorInput ;
3
4
using Autodesk . AutoCAD . Geometry ;
4
5
using System ;
@@ -14,14 +15,21 @@ internal abstract class ITypeUtil
14
15
public abstract void WriteMask ( DBObject entity , bool newMask ) ;
15
16
public abstract void WriteText ( DBObject entity , string newText , Transaction t ) ;
16
17
public abstract string GetInternalContentType ( DBObject entity ) ;
17
- public abstract void MoveViewPort ( Editor ed , ViewTableRecord view , Transaction t , DBObject obj ) ;
18
- protected void MoveViewPort ( Editor ed , ViewTableRecord view , DBObject entity , Point3d position , double height , double width )
18
+ public abstract void MoveViewPort ( Editor ed , Transaction t , Entity entity ) ;
19
+ protected void MoveViewPort ( Editor ed , Entity entity )
19
20
{
21
+ if ( LayoutManager . Current . CurrentLayout != "Model" )
22
+ {
23
+ var doc = Application . DocumentManager . MdiActiveDocument ;
24
+ using ( doc . LockDocument ( ) )
25
+ {
26
+ LayoutManager . Current . CurrentLayout = "Model" ;
27
+ }
28
+ }
29
+ Extents3d ext = entity . GeometricExtents ;
30
+ ext . TransformBy ( ed . CurrentUserCoordinateSystem . Inverse ( ) ) ;
20
31
ed . SetImpliedSelection ( new [ ] { entity . Id } ) ;
21
- view . CenterPoint = new Point2d ( position . X , position . Y ) ;
22
- view . Height = height * 3 ;
23
- view . Width = width * 3 ;
24
- ed . SetCurrentView ( view ) ;
32
+ ZoomWin ( ed , ext . MinPoint , ext . MaxPoint ) ;
25
33
ed . Regen ( ) ; // Update gizmos to be accurate after movement
26
34
}
27
35
@@ -34,5 +42,22 @@ protected T Cast<T>(DBObject obj) where T : DBObject
34
42
35
43
throw new InvalidOperationException ( ) ;
36
44
}
45
+
46
+ /// <summary>
47
+ /// Moves and scales the viewport to center on the CAD element
48
+ /// https://through-the-interface.typepad.com/through_the_interface/2008/06/zooming-to-a-wi.html
49
+ /// http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-35da.htm,topicNumber=d0e44236
50
+ /// </summary>
51
+ private static void ZoomWin ( Editor ed , Point3d min , Point3d max )
52
+ {
53
+ Point2d min2d = new Point2d ( min . X , min . Y ) ;
54
+ Point2d max2d = new Point2d ( max . X , max . Y ) ;
55
+
56
+ ViewTableRecord view = new ViewTableRecord ( ) ;
57
+ view . CenterPoint = min2d + ( ( max2d - min2d ) / 2.0 ) ;
58
+ view . Height = ( max2d . Y - min2d . Y ) * 1.5 ;
59
+ view . Width = ( max2d . X - min2d . X ) * 1.5 ;
60
+ ed . SetCurrentView ( view ) ;
61
+ }
37
62
}
38
63
}
0 commit comments