- private delegate bool InRange(double d);
- private static Entity cachedEntity = null;
- private static ObjectId cachedEntId = ObjectId.Null;
- private static Entity GetEnt(Transaction tr, ObjectId id)
- {
- if (cachedEntId != id || cachedEntity == null)
- cachedEntity = tr.GetObject(id, OpenMode.ForRead) as Entity;
- return cachedEntity;
- }
- [CommandMethod("TestLinqInAutoCAD1")]
- public static void TestLinqInAutoCAD1_Method()
- {
- Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
- try
- {
- ObjectId[] ids;
- InRange inRange2to6 = r => { return r >= 2.0 && r <= 6.0;};
- using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
- {
- BlockTable bt = tr.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
- var v = from ObjectId id in btr
- where GetEnt(tr, id) is Circle &&
- (new int[]{1,3}.Contains(GetEnt(tr, id).ColorIndex)) && //Color Red or Yellow
- GetEnt(tr, id).Layer == "TEST" && //Layer 'TEST'
- !(tr.GetObject(GetEnt(tr, id).LayerId, OpenMode.ForRead) as LayerTableRecord).IsFrozen && //Layer not frozen
- (new string[]{"DASHED","DOT"}.Contains(GetEnt(tr, id).Linetype)) && //Linetype Dashed or Dotted
- inRange2to6((GetEnt(tr, id) as Circle).Radius) //Circle radius in the range [2.0, 6.0]
- select id;
- ids = v.ToArray();
- tr.Commit();
- }
- ed.WriteMessage(string.Format("\nThere are {0} circles meeting the filtering criteria.", ids.Length));
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage(ex.ToString());
- }
- }
复制代码As can be seen, in the above succinct and cool code snippets, a few wonderful modern .NET technologies have been demonstrated such as LINQ, Lambda Expression and Delegate. Better, it is not just for new thing show-off. Instead, each cool tech applied here has its own very reason!
In case there are some circles meeting the filtering criteria in the current drawing, the command may print out something onto the AutoCAD command line window as follows:
Command: TestLinqInAutoCAD1
There are 58 circles meeting the filtering criteria.
Command: -LAYER
Current layer: "TEST"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: lo
Enter name list of layer(s) to lock or <select objects>: TEST
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: TESTLINQINAUTOCAD1
There are 58 circles meeting the filtering criteria.
Command: layer
Command: -LAYER
Current layer: "TEST"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: s
Enter layer name to make current or <select object>: 0
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: freeze
Enter name list of layer(s) to freeze or <select objects>: test
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: TESTLINQINAUTOCAD1
There are 0 circles meeting the filtering criteria.
Command: -LAYER
Current layer: "0"
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: thaw
Enter name list of layer(s) to thaw: test
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
Command: TESTLINQINAUTOCAD1
There are 58 circles meeting the filtering criteria.
By the way, besides better code conciseness, cleanness, and readability in the code as demonstrated here, in terms of performance, I bet the new way is way better as well, though I haven’t done any experiment about it. If interested in, please feel free to go ahead and give it a shot.
Enjoy it! More coding gadgets will be created and demonstrated in the future. Please stay tuned.