1+ using System . Collections . Generic ;
2+ using System . Windows . Forms ;
3+ using Autodesk . AutoCAD . ApplicationServices ;
4+ using Autodesk . AutoCAD . DatabaseServices ;
5+ using Autodesk . AutoCAD . EditorInput ;
6+ using Autodesk . AutoCAD . Geometry ;
7+ using Autodesk . AutoCAD . Runtime ;
8+
9+ namespace Test ;
10+
11+ /// <summary>
12+ /// https://forums.autodesk.com/t5/net-forum/viewport-get-entities-from-viewport/td-p/5832707
13+ /// </summary>
14+ public class TestViewPort
15+ {
16+
17+ [ CommandMethod ( "TestInsideViewport" ) ]
18+ public void TestInsideViewport ( )
19+ {
20+ Document doc = null ;
21+ Database db = null ;
22+ Editor ed = null ;
23+
24+ try
25+ {
26+ doc = Autodesk . AutoCAD . ApplicationServices . Core . Application . DocumentManager . MdiActiveDocument ;
27+ db = doc . Database ;
28+ ed = doc . Editor ;
29+ LayoutManager layoutManager = LayoutManager . Current ;
30+
31+ string layoutName = layoutManager . CurrentLayout ;
32+ ed . WriteMessage ( $ "\n Layout Name: { layoutName } ") ;
33+ ObjectId layoutId = layoutManager . GetLayoutId ( layoutName ) ;
34+
35+ ObjectId vpId = ObjectId . Null ;
36+ using ( Transaction tr = db . TransactionManager . StartTransaction ( ) )
37+ {
38+ Layout layout = ( Layout ) tr . GetObject ( layoutId , OpenMode . ForRead ) ;
39+ var vpIds = layout . GetViewports ( ) ;
40+ if ( vpIds . Count > 1 )
41+ vpId = vpIds [ 1 ] ; // First Viewport is Paperspace itself
42+ tr . Commit ( ) ;
43+ }
44+
45+ if ( vpId . IsNull )
46+ throw new Exception ( ErrorStatus . Vetoed , "viewport have issue" ) ;
47+
48+ List < Point3d > ? vpOutlinePntsInMs = null ;
49+ using ( Transaction tr = db . TransactionManager . StartTransaction ( ) )
50+ {
51+ Viewport vp = ( Viewport ) tr . GetObject ( vpId , OpenMode . ForRead ) ;
52+ Polyline ? vpOutlineInMs = null ;
53+
54+ if ( vp . NonRectClipOn )
55+ {
56+ ObjectId vpClipId = vp . NonRectClipEntityId ;
57+ Entity vpBoundary = ( Entity ) tr . GetObject ( vpClipId , OpenMode . ForRead ) ;
58+ vpOutlineInMs = ( Polyline ) vpBoundary . Clone ( ) ;
59+ }
60+ else
61+ {
62+ Extents3d vpExt = vp . GeometricExtents ;
63+ vpOutlineInMs = new Polyline ( 4 ) ;
64+ vpOutlineInMs . AddVertexAt ( 0 , new Point2d ( vpExt . MinPoint . X , vpExt . MinPoint . Y ) , 0 , 0 , 0 ) ;
65+ vpOutlineInMs . AddVertexAt ( 1 , new Point2d ( vpExt . MaxPoint . X , vpExt . MinPoint . Y ) , 0 , 0 , 0 ) ;
66+ vpOutlineInMs . AddVertexAt ( 2 , new Point2d ( vpExt . MaxPoint . X , vpExt . MaxPoint . Y ) , 0 , 0 , 0 ) ;
67+ vpOutlineInMs . AddVertexAt ( 3 , new Point2d ( vpExt . MinPoint . X , vpExt . MaxPoint . Y ) , 0 , 0 , 0 ) ;
68+ vpOutlineInMs . Closed = true ;
69+ }
70+
71+ Point3d center = new Point3d ( vp . ViewCenter . X , vp . ViewCenter . Y , 0.0 ) ;
72+ Matrix3d msToPs =
73+ Matrix3d . Displacement ( new Vector3d ( vp . CenterPoint . X - center . X , vp . CenterPoint . Y - center . Y , 0.0 ) ) *
74+ Matrix3d . Scaling ( vp . CustomScale , center ) *
75+ Matrix3d . Rotation ( vp . TwistAngle , Vector3d . ZAxis , Point3d . Origin ) *
76+ Matrix3d . WorldToPlane ( new Plane ( vp . ViewTarget , vp . ViewDirection ) ) ;
77+
78+ vpOutlineInMs . TransformBy ( msToPs . Inverse ( ) ) ;
79+
80+ vpOutlinePntsInMs = new List < Point3d > ( ) ;
81+ for ( int i = 0 ; i < vpOutlineInMs . NumberOfVertices ; i ++ )
82+ vpOutlinePntsInMs . Add ( vpOutlineInMs . GetPoint3dAt ( i ) ) ;
83+
84+ // Draw polyline in model space
85+ Polyline transformedPoly = new Polyline ( ) ;
86+ for ( int i = 0 ; i < vpOutlinePntsInMs . Count ; i ++ )
87+ transformedPoly . AddVertexAt ( i , new Point2d ( vpOutlinePntsInMs [ i ] . X , vpOutlinePntsInMs [ i ] . Y ) , 0 , 0 ,
88+ 0 ) ;
89+ transformedPoly . Closed = true ;
90+ transformedPoly . ColorIndex = 1 ; // Red
91+
92+ BlockTable bt = ( BlockTable ) tr . GetObject ( db . BlockTableId , OpenMode . ForRead ) ;
93+ BlockTableRecord ms =
94+ ( BlockTableRecord ) tr . GetObject ( bt [ BlockTableRecord . ModelSpace ] , OpenMode . ForWrite ) ;
95+ ms . AppendEntity ( transformedPoly ) ;
96+ tr . AddNewlyCreatedDBObject ( transformedPoly , true ) ;
97+
98+ tr . Commit ( ) ;
99+ }
100+
101+ using ( Transaction tr = db . TransactionManager . StartTransaction ( ) )
102+ {
103+ BlockTableRecord blockTableRecord = ( BlockTableRecord ) tr . GetObject ( SymbolUtilityServices . GetBlockModelSpaceId ( db ) ,
104+ OpenMode . ForRead ) ;
105+ foreach ( ObjectId entId in blockTableRecord )
106+ {
107+ Entity ent = ( Entity ) tr . GetObject ( entId , OpenMode . ForRead ) ;
108+ if ( ent is Polyline )
109+ continue ;
110+
111+ var pt1 = ent . GeometricExtents . MinPoint ;
112+ var pt2 = new Point3d ( ent . GeometricExtents . MaxPoint . X , ent . GeometricExtents . MinPoint . Y , 0 ) ;
113+ var pt3 = ent . GeometricExtents . MaxPoint ;
114+ var pt4 = new Point3d ( ent . GeometricExtents . MinPoint . X , ent . GeometricExtents . MaxPoint . Y , 0 ) ;
115+
116+ if ( IsInside2D ( vpOutlinePntsInMs . ToArray ( ) , pt1 ) &&
117+ IsInside2D ( vpOutlinePntsInMs . ToArray ( ) , pt2 ) &&
118+ IsInside2D ( vpOutlinePntsInMs . ToArray ( ) , pt3 ) &&
119+ IsInside2D ( vpOutlinePntsInMs . ToArray ( ) , pt4 ) )
120+ {
121+ if ( ent is BlockReference block )
122+ {
123+ ed . WriteMessage ( $ "\n Entity Block in Viewport: { entId . Handle } ({ block . Name } )") ;
124+ }
125+
126+ }
127+ }
128+
129+ tr . Commit ( ) ;
130+ }
131+ }
132+ catch ( Exception ex )
133+ {
134+ if ( ed != null )
135+ ed . WriteMessage ( "\n Error: " + ex . ToString ( ) ) ;
136+ else
137+ MessageBox . Show ( "Error: " + ex . Message , "TestInsideViewport" , MessageBoxButtons . OK ,
138+ MessageBoxIcon . Error ) ;
139+ }
140+ }
141+ private static bool IsInside2D ( Point3d [ ] polyPoints , Point3d entPoint )
142+ {
143+ bool oddNodes = false ;
144+
145+
146+ int i , j = polyPoints . Length - 1 ;
147+
148+ for ( i = 0 ; i < polyPoints . Length ; i ++ )
149+ {
150+ Point3d pp_i = polyPoints [ i ] ;
151+ Point3d pp_j = polyPoints [ j ] ;
152+
153+ if ( ( pp_i . Y < entPoint . Y && pp_j . Y >= entPoint . Y || pp_j . Y < entPoint . Y && pp_i . Y >= entPoint . Y ) &&
154+ ( pp_i . X <= entPoint . X || pp_j . X <= entPoint . X ) )
155+ oddNodes ^= ( pp_i . X + ( entPoint . Y - pp_i . Y ) / ( pp_j . Y - pp_i . Y ) * ( pp_j . X - pp_i . X ) < entPoint . X ) ;
156+
157+ j = i ;
158+ }
159+
160+ return oddNodes ;
161+ }
162+
163+ }
0 commit comments