CAD中的選擇集過濾----有條件選擇AutoCAD實體

轉載自:http://www.cnblogs.com/jdmei520/articles/1326120.html

一、選擇集過濾時的使用方式如下:

Object 指使用SelectionSet這個方法適用的對象

1)      object.Select Mode[, Point1][, Point2][, FilterType][, FilterData] 

2)      object.SelectOnScreen [FilterType][, FilterData] 

3)      object.SelectAtPoint Point, FilterType, FilterData 

FilterType:Variant[變體](整數數組); 僅用於輸入;(可選項) 指定使用的過濾器類型的 DXF 組碼。

FilterData:Variant[變體](變體數組); 僅用於輸入;(可選項) 過濾器的值。

二、DXF羣組碼共同羣組碼代碼一覽表

羣碼

說明

預設值

-4

過濾羣組方式,例如 <AND、AND>、<OR、OR>、<XOR、XOR>、<NOT、NOT>

單一條件時可省略

-1

圖元名稱(會隨每一個圖檔開啓而有所不同)

不可省略

0

圖元類型,例如 "ARC"、 "LINE"、"CIRCLE"...

不可省略

5

處理碼

不可省略

6

線型名稱(如果線型不爲"BYLAYER",此羣碼值會出現)

BYLAYER

8

圖層名稱

不可省略

48

線性比例(選擇性)

1.0

60

物件可見性, 0=可見, 1=不可見

0

62

顏色編號 (如果線型不爲"BYLAYER",此羣羣碼會出現)當值爲0時,即指BYLAYER,如果是負值即指該圖層是關閉的(選擇性)

BYLAYER

67

值爲空或0時即指圖元在模型空間,如果爲1指在圖形空間

0

三、過濾羣組方式

- FilterType (DXF 羣組碼) = -4

過濾羣組方式

內含項目

描述

運算法則

"<AND" ... "AND>"

1 或 多個

所有項目的交集

1+1=1, 1+0=0, 0+1=0, 0+0=0

"<OR" ... "OR>"

1 或多個

所有項目的並集

1+1=1, 1+0=1, 0+1=1, 0+0=0

"<XOR" ... "XOR>"

2個

兩個項目的異或運算

1+1=0, 1+0=1, 0+1=1, 0+0=0

"<NOT" ... "NOT>"

1個

不包含此項目的值 

NOT(1)=0,NOT(0)=1

四、範例:

1、過濾條件爲圖元爲MTEXT

圖元是MTEXT

FilterData

MTEXT

FilterType

0

2、過濾條件爲圖元爲CIRCLE或LINE

圖元是CIRCLE OR 圖元是LINE

FilterData

<OR

CIRCLE

LINE

OR>

FilterType

-4

0

0

-4

3、過濾條件爲圖元在DIM 圖層(LAYER)中的CIRCLE或LINE

  (圖元是CIRCLE OR 圖元是LINE) AND 圖層位於DIM層

FilterData

<AND

<OR

CIRCLE

LINE

OR>

DIM

AND>

FilterType

-4

-4

0

0

-4

8

-4

4、過濾的條件爲圖元爲CIRCLE或LINE但圖層(LAYER)不屬於DIM層

(圖元是CIRCLE OR 圖元是LINE) AND NOT(圖層位於DIM層)

FilterData

<AND

<OR

CIRCLE

LINE

OR>

<NOT

DIM

NOT>

AND>

FilterType

-4

-4

0

0

-4

-4

8

-4

-4

 

 


 

讓我們看一個實例:我們想要選擇層0上的所有直線和所有直徑大於10的圓,該如何組合條件呢?

Let's take a concrete example: let's say we want to select all lines on on layer 0 and all the circles with radii greater than 10.'s how we would compose the conditions, in pseudo-code:

  • <or
    • <and
      • Layer == "0"
      • Entity type == "LINE"
    • and>
    • <and
      • Entity type == "CIRCLE"
      • Radius >= 10.0
    • and>
  • or>

轉換爲c#如下代碼:爲清楚起見,此處我把指定的屬性/值以硬編碼的形式實現,另如果需要應該直接由用戶從數據庫中進行選擇。

This translates into the following C# code - for clarity I've left the specific properties/values hard-coded, but clearly it would be straightforward to ask the user or pick them out of a database, as needed.


using Autodesk.AutoCAD.ApplicationServices;
 using Autodesk.AutoCAD.Runtime;
 using Autodesk.AutoCAD.DatabaseServices;
 using Autodesk.AutoCAD.EditorInput;
 
 namespace EntitySelection
  {
     public class Commands
      {
         [CommandMethod("SEWP")]
         public static void SelectEntitiesWithProperties()
          {
             Document doc = Application.DocumentManager.MdiActiveDocument;
             Editor ed = doc.Editor;
 
             // Build a conditional filter list so that only
             // entities with the specified properties are
             // selected
             TypedValue[] tvs = new TypedValue[] 
              {
                 new TypedValue((int)DxfCode.Operator, "<or"),
                 new TypedValue((int)DxfCode.Operator, "<and"),
                 new TypedValue((int)DxfCode.LayerName, "0"),
                 new TypedValue((int)DxfCode.Start, "LINE"),
                 new TypedValue((int)DxfCode.Operator, "and>"),
                 new TypedValue((int)DxfCode.Operator, "<and"),
                 new TypedValue((int)DxfCode.Start, "CIRCLE"),
                 new TypedValue((int)DxfCode.Operator, ">="),
                 new TypedValue((int)DxfCode.Real, 10.0),// Circle Radius
                 new TypedValue((int)DxfCode.Operator, "and>"),
                 new TypedValue((int)DxfCode.Operator, "or>")
             };
 
             SelectionFilter sf = new SelectionFilter(tvs);
             PromptSelectionResult psr = ed.SelectAll(sf);
             if (psr.Status == PromptStatus.OK)
              {
                 SelectionSet SS = psr.Value;
                 ObjectId[] idArray = SS.GetObjectIds();
                 for (int i = 0; i < idArray.Length; i++)
                  {
                     Entity ent = (Entity)Tools.GetDBObject(idArray[i]);
                     ent.Highlight();
                     Tools.WriteMessage(i + ":" + ent.ObjectId.ToString() + "," + ent.GetType().Name);
                 }
             }
 
         }
 
     }//end class
 }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章