forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryEntTests.cs
More file actions
435 lines (356 loc) · 17.6 KB
/
QueryEntTests.cs
File metadata and controls
435 lines (356 loc) · 17.6 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//
// (C) Copyright 2004 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using MgdDbg.CompBuilder;
using MgdDbg.ObjTests.TestFramework;
using System;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
namespace MgdDbg.ObjTests
{
/// <summary>
/// Summary description for QueryEntTests.
/// </summary>
public class QueryEntTests : MgdDbgTestFuncs
{
private Database m_db = null;
public
QueryEntTests()
{
m_testFrameworkFuncs.Add(new MgdDbgTestFuncInfo("Get Stretch Points", "Add Point entities where stretch points are", typeof(Entity), new MgdDbgTestFuncInfo.TestFunc(StretchPoints), MgdDbgTestFuncInfo.TestType.Query));
//m_testFrameworkFuncs.Add(new MgdDbgTestFuncInfo("Get Grip Points", "Add Point entities where grip points are", typeof(Entity), new MgdDbgTestFuncInfo.TestFunc(GripPoints), MgdDbgTestFuncInfo.TestType.Query));
m_testFrameworkFuncs.Add(new MgdDbgTestFuncInfo("Get Geometry Extents", "Add a block of Faces equal to extents cube", typeof(Entity), new MgdDbgTestFuncInfo.TestFunc(Extents), MgdDbgTestFuncInfo.TestType.Query));
m_testFrameworkFuncs.Add(new MgdDbgTestFuncInfo("Intersect With", "Add Point entities where intersection points are", typeof(Entity), new MgdDbgTestFuncInfo.TestFunc(IntersectWith), MgdDbgTestFuncInfo.TestType.Query));
m_testFrameworkFuncs.Add(new MgdDbgTestFuncInfo("Bounding Box Intersect With", "Add Point entities where intersection points are", typeof(Entity), new MgdDbgTestFuncInfo.TestFunc(BoundingBoxIntersectWith), MgdDbgTestFuncInfo.TestType.Query));
}
#region Tests
/// <summary>
/// Add Point entities where stretch points are
/// </summary>
public void
StretchPoints()
{
m_db = Utils.Db.GetCurDwg();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Point3dCollection pts = new Point3dCollection();
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = m_db.TransactionManager;
while (true)
{
PromptEntityResult prEntRes = ed.GetEntity("\nSelect entity to show stretch points");
if (prEntRes.Status != PromptStatus.OK)
break;
using (Autodesk.AutoCAD.DatabaseServices.Transaction tr = tm.StartTransaction())
{
AcDb.Entity ent = (AcDb.Entity)tr.GetObject(prEntRes.ObjectId, OpenMode.ForRead);
pts.Clear(); // reset to no points each time through loop
ent.GetStretchPoints(pts);
if (pts.Count == 0)
Utils.AcadUi.PrintToCmdLine("\nNo stretch points specified!");
else
{
for (int i = 0; i < pts.Count; i++)
{
Utils.AcadUi.PrintToCmdLine(string.Format("\nSTRETCH[{0:d}]: {1}", i, Utils.AcadUi.PtToStr(pts[i])));
MakePointEnt(pts[i], 1, tr);
}
}
tr.Commit();
}
}
}
/// <summary>
/// Add Point entities where grip points are
/// </summary>
public void
GripPoints()
{
m_db = Utils.Db.GetCurDwg();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Point3dCollection pts = new Point3dCollection();
IntegerCollection osnapModes = new IntegerCollection();
IntegerCollection geomIds = new IntegerCollection();
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = m_db.TransactionManager;
while (true)
{
PromptEntityResult prEntRes = ed.GetEntity("\nSelect entity to show grip points");
if (prEntRes.Status != PromptStatus.OK)
break;
using (Autodesk.AutoCAD.DatabaseServices.Transaction tr = tm.StartTransaction())
{
AcDb.Entity ent = (AcDb.Entity)tr.GetObject(prEntRes.ObjectId, OpenMode.ForRead);
pts.Clear(); // reset to no points each time through loop
osnapModes.Clear();
geomIds.Clear();
//TBD: Throws a "Value cannot be null exception"
ent.GetGripPoints(pts, osnapModes, geomIds);
if (pts.Count == 0)
Utils.AcadUi.PrintToCmdLine("\nNo grip points specified!");
else
{
for (int i = 0; i < pts.Count; i++)
{
Utils.AcadUi.PrintToCmdLine(string.Format("\nGRIP[{0:d}]: {1}", i, Utils.AcadUi.PtToStr(pts[i])));
MakePointEnt(pts[i], 2, tr);
}
}
if (osnapModes.Count == 0)
Utils.AcadUi.PrintToCmdLine("\nNo osnap modes specified!");
else
{
for (int i = 0; i < osnapModes.Count; i++)
{
Utils.AcadUi.PrintToCmdLine(string.Format("\nOSNAP MODE[{0:d}]: {1}", i, osnapModes[i]));
}
}
if (geomIds.Count == 0)
Utils.AcadUi.PrintToCmdLine("\nNo geometry IDs specified!");
else
{
for (int i = 0; i < geomIds.Count; i++)
{
Utils.AcadUi.PrintToCmdLine(string.Format("\nGEOM ID[{0:d}]: {1}", i, geomIds[i].ToString()));
}
}
tr.Commit();
}
}
}
/// <summary>
/// Add a block of Faces equal to extents cube
/// </summary>
public void
Extents()
{
m_db = Utils.Db.GetCurDwg();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = m_db.TransactionManager;
while (true)
{
PromptEntityResult prEntRes = ed.GetEntity("\nSelect entity to show extents");
if (prEntRes.Status != PromptStatus.OK)
break;
using (Autodesk.AutoCAD.DatabaseServices.Transaction tr = tm.StartTransaction())
{
AcDb.Entity ent = (AcDb.Entity)tr.GetObject(prEntRes.ObjectId, OpenMode.ForRead);
Extents3d ext = ent.GeometricExtents;
Point3d centerPt = Utils.Ge.Midpoint(ext.MinPoint, ext.MaxPoint);
Utils.AcadUi.PrintToCmdLine(string.Format("\nEXTMIN: {0}", Utils.AcadUi.PtToStr(ext.MinPoint)));
Utils.AcadUi.PrintToCmdLine(string.Format("\nEXTMAX: {0}", Utils.AcadUi.PtToStr(ext.MaxPoint)));
Utils.AcadUi.PrintToCmdLine(string.Format("\nCENTER PT: {0}", Utils.AcadUi.PtToStr(centerPt)));
tr.Commit();
MakeExtentsBlock(ext);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="ext"></param>
public void
MakeExtentsBlock(Extents3d ext)
{
m_db = Utils.Db.GetCurDwg();
double deltaX = Math.Abs(ext.MaxPoint.X - ext.MinPoint.X);
double deltaY = Math.Abs(ext.MaxPoint.Y - ext.MinPoint.Y);
double deltaZ = Math.Abs(ext.MaxPoint.Z - ext.MinPoint.Z);
Point3d[] pts = new Point3d[8];
pts[0] = ext.MinPoint;
pts[6] = ext.MaxPoint;
// make bottom face
pts[1] = new Point3d(pts[0].X + deltaX, pts[0].Y, pts[0].Z);
pts[2] = new Point3d(pts[1].X, pts[1].Y + deltaY, pts[1].Z);
pts[3] = new Point3d(pts[0].X, pts[0].Y + deltaY, pts[0].Z);
// project up by Z
pts[4] = new Point3d(pts[0].X, pts[0].Y, pts[0].Z + deltaZ);
pts[5] = new Point3d(pts[1].X, pts[1].Y, pts[1].Z + deltaZ);
pts[7] = new Point3d(pts[3].X, pts[3].Y, pts[3].Z + deltaZ);
Vector3d offset = ext.MinPoint.GetAsVector();
// move points so that they are centered at WCS origin
// for block creation. Express everything in WCS since
// that is what Entity.Extents works in.
for (int i = 0; i < pts.Length; i++)
{
pts[i] -= offset;
}
DBObjectCollection faceEnts = new DBObjectCollection();
faceEnts.Add(new Face(pts[0], pts[1], pts[2], pts[3], true, true, true, true)); // bottom face
faceEnts.Add(new Face(pts[4], pts[5], pts[6], pts[7], true, true, true, true)); // top face
faceEnts.Add(new Face(pts[0], pts[1], pts[5], pts[4], true, true, true, true)); // front face
faceEnts.Add(new Face(pts[1], pts[2], pts[6], pts[5], true, true, true, true)); // right side face
faceEnts.Add(new Face(pts[2], pts[3], pts[7], pts[6], true, true, true, true)); // back side face
faceEnts.Add(new Face(pts[3], pts[0], pts[4], pts[7], true, true, true, true)); // left side face
CompBldrAnonBlkDef compBldr = new CompBldrAnonBlkDef(m_db);
compBldr.Start();
foreach (Entity ent in faceEnts)
{
compBldr.SetToDefaultProps(ent);
compBldr.AddToDb(ent);
}
compBldr.Commit();
BlockReference blkRef = new BlockReference(ext.MinPoint, compBldr.BlockDefId);
blkRef.ColorIndex = 4;
Utils.SymTbl.AddToCurrentSpaceAndClose(blkRef, compBldr.Database);
}
/// <summary>
/// Add Point entities where intersection points are
/// </summary>
public void
IntersectWith()
{
m_db = Utils.Db.GetCurDwg();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
// get intersection type from user
PromptKeywordOptions prIntType = new PromptKeywordOptions("\nExtend objects");
prIntType.Keywords.Add("None");
prIntType.Keywords.Add("First");
prIntType.Keywords.Add("Second");
prIntType.Keywords.Add("Both");
PromptResult prIntTypeRes = ed.GetKeywords(prIntType);
if (prIntTypeRes.Status != PromptStatus.OK)
return;
//Intersect intType = Intersect.ExtendBoth;
//if (prIntTypeRes.StringResult == "None")
// intType = Intersect.OnBothOperands;
//else if (prIntTypeRes.StringResult == "First")
// intType = Intersect.ExtendThis;
//else if (prIntTypeRes.StringResult == "Second")
// intType = Intersect.ExtendArgument;
//else
// intType = Intersect.ExtendBoth;
PromptEntityOptions prEnt1 = new PromptEntityOptions("\nSelect first entity");
PromptEntityOptions prEnt2 = new PromptEntityOptions("\nSelect intersecting entity");
Point3dCollection pts = new Point3dCollection();
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = m_db.TransactionManager;
while (true)
{
PromptEntityResult prEnt1Res = ed.GetEntity(prEnt1);
if (prEnt1Res.Status != PromptStatus.OK)
return;
PromptEntityResult prEnt2Res = ed.GetEntity(prEnt2);
if (prEnt2Res.Status != PromptStatus.OK)
return;
using (Autodesk.AutoCAD.DatabaseServices.Transaction tr = tm.StartTransaction())
{
AcDb.Entity ent1 = (AcDb.Entity)tr.GetObject(prEnt1Res.ObjectId, OpenMode.ForRead);
AcDb.Entity ent2 = (AcDb.Entity)tr.GetObject(prEnt2Res.ObjectId, OpenMode.ForRead);
pts.Clear();
//ent1.IntersectWith(ent2, intType, pts, 0, 0); // TBD: Fix JMA
if (pts.Count == 0)
Utils.AcadUi.PrintToCmdLine("\nObjects do not intersect!");
else
{
for (int i = 0; i < pts.Count; i++)
{
Utils.AcadUi.PrintToCmdLine(string.Format("\nINTERSECT PT: {0}", Utils.AcadUi.PtToStr(pts[i])));
MakePointEnt(pts[i], 3, tr);
}
}
tr.Commit();
}
}
}
/// <summary>
/// Add Point entities where intersection points are
/// </summary>
public void
BoundingBoxIntersectWith()
{
m_db = Utils.Db.GetCurDwg();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
// get intersection type from user
PromptKeywordOptions prIntType = new PromptKeywordOptions("\nExtend objects");
prIntType.Keywords.Add("None");
prIntType.Keywords.Add("First");
prIntType.Keywords.Add("Second");
prIntType.Keywords.Add("Both");
PromptResult prIntTypeRes = ed.GetKeywords(prIntType);
if (prIntTypeRes.Status != PromptStatus.OK)
return;
//Intersect intType = Intersect.ExtendBoth;
//if (prIntTypeRes.StringResult == "None")
// intType = Intersect.OnBothOperands;
//else if (prIntTypeRes.StringResult == "First")
// intType = Intersect.ExtendThis;
//else if (prIntTypeRes.StringResult == "Second")
// intType = Intersect.ExtendArgument;
//else
// intType = Intersect.ExtendBoth;
PromptEntityOptions prEnt1 = new PromptEntityOptions("\nSelect first entity");
PromptEntityOptions prEnt2 = new PromptEntityOptions("\nSelect intersecting entity");
Point3dCollection pts = new Point3dCollection();
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = m_db.TransactionManager;
while (true)
{
PromptEntityResult prEnt1Res = ed.GetEntity(prEnt1);
if (prEnt1Res.Status != PromptStatus.OK)
return;
PromptEntityResult prEnt2Res = ed.GetEntity(prEnt2);
if (prEnt2Res.Status != PromptStatus.OK)
return;
using (Autodesk.AutoCAD.DatabaseServices.Transaction tr = tm.StartTransaction())
{
AcDb.Entity ent1 = (AcDb.Entity)tr.GetObject(prEnt1Res.ObjectId, OpenMode.ForRead);
AcDb.Entity ent2 = (AcDb.Entity)tr.GetObject(prEnt2Res.ObjectId, OpenMode.ForRead);
MakeExtentsBlock(ent1.GeometricExtents);
pts.Clear();
//ent1.BoundingBoxIntersectWith(ent2, intType, pts, 0, 0); // TBD: Fix JMA
if (pts.Count == 0)
Utils.AcadUi.PrintToCmdLine("\nObjects do not intersect!");
else
{
for (int i = 0; i < pts.Count; i++)
{
Utils.AcadUi.PrintToCmdLine(string.Format("\nINTERSECT PT: {0}", Utils.AcadUi.PtToStr(pts[i])));
MakePointEnt(pts[i], 3, tr);
}
}
tr.Commit();
}
}
}
#endregion Tests
#region Utils
/// <summary>
///
/// </summary>
/// <param name="pt"></param>
/// <param name="colorIndex"></param>
/// <param name="tr"></param>
private void
MakePointEnt(Point3d pt, int colorIndex, Transaction tr)
{
m_db = Utils.Db.GetCurDwg();
short mode = (short)AcadApp.GetSystemVariable("pdmode");
if (mode == 0)
AcadApp.SetSystemVariable("pdmode", 99);
using (DBPoint dbPt = new DBPoint(Utils.Db.WcsToUcs(pt)))
{
dbPt.ColorIndex = colorIndex;
Utils.Db.TransformToWcs(dbPt, m_db);
Utils.SymTbl.AddToCurrentSpace(dbPt, m_db, tr);
}
}
#endregion Utils
}
}