03-03 創建和編輯AutoCAD實體(三) 使用選擇集(2)

4、DefineRules for Selection Filters定義選擇集過濾器規則

You can limit which objects are selectedand added to a selection set by using a selection filter. A selection filterlist can be used to filter selected objects by properties or type. For example,you might want to select only blue objects or objects on a certain layer. Youcan also combine selection criteria. For example, you can create a selectionfilter that limits selection to blue circles on the layer named Pattern.Selection filters can be specified as a parameter for the different selectionmethods in SelectObjects in the Drawing Area.

我們可以通過使用選擇過濾器來限制哪些對象被選中並添加到選擇集。選擇過濾器列表用來通過屬性或類型過濾所選對象,例如,我們可能想只選擇藍色的對象或某一圖層上的對象。我們還可以使用選擇條件組合,例如,我們可以創建一個選擇過濾器將選擇對象限定於Pattern圖層上的藍色的圓。可以爲“在圖形區域選擇對象”一節中的各種不同選擇方法指定選擇過濾器作爲參數。

 

Note Filtering recognizes values explicitly assigned toobjects, not those inherited by the layer. For example, if an object’s linetypeproperty is set to ByLayer and the layer it is assigned is set to the Hiddenlinetype; filtering for objects assigned the Hidden linetype will not selectthese objects since their linetype property is set to ByLayer.

注意:使用過濾只能識別顯式賦給對象的值,而不能識別繼承自圖層的那些值。比如,如果對象的線型屬性設置爲隨圖層(ByLayer)而該圖層線型爲Hidden,那麼要過濾線型爲Hidden的對象將不會選擇那些線型屬性爲隨圖層(ByLayer)的對象。

 

Topics in this section本小節內容

·      Use Selection Filters to Define Selection Set Rules 使用選擇過濾器定義選擇集規則

·      Specify Multiple Criteria in a Selection Filter 多個過濾條件

·      Add Complexity to Your Filter List Conditions 複雜的過濾條件

·      Use Wild-Card Patterns in Selection Set Filter Criteria 在過濾條件裏使用通配符

·      Filter for Extended Data 過濾擴展數據

 

4.1、UseSelection Filters to Define Selection Set Rules使用選擇過濾器定義選擇集規則

Selection filters are composed of pairs ofarguments in the form of TypedValues. The first argument of a TypedValueidentifies the type of filter (for example, an object), and the second argumentspecifies the value you are filtering on (for example, circles). The filtertype is a DXF group code that specifies which filter to use. A few of the mostcommon filter types are listed here.

選擇過濾器由TypedValue形式的一對參數構成。TypedValue的第一個參數表明過濾器的類型(例如對象),第二個參數爲要過濾的值(例如圓)。過濾器類型是一個DXF組碼,用來指定使用哪種過濾器。一些常用過濾器類型列表如下。

 

 

For a complete list of DXF group codes,see Group Code Value Types in the DXF Reference.

DXF組碼的完整列表,見DXF參考手冊中組碼值類型一節

Specify a single selection criterion fora selection set 指定單個選擇條件

The following code prompts users to selectobjects to be included in a selection set, and filters out all objects exceptfor circles.

下面程序提示用戶選擇對象放到選擇集內,然後過濾掉圓以外的其他所有對象。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.EditorInput

 

<CommandMethod("FilterSelectionSet")>_

Public SubFilterSelectionSet()

  '' Get the current document editor

  Dim acDocEd As Editor =Application.DocumentManager.MdiActiveDocument.Editor

 

  '' Create a TypedValue array to define thefilter criteria

  Dim acTypValAr(0) As TypedValue

  acTypValAr.SetValue(NewTypedValue(DxfCode.Start, "CIRCLE"), 0)

 

  '' Assign the filter criteria to aSelectionFilter object

  Dim acSelFtr As SelectionFilter = NewSelectionFilter(acTypValAr)

 

  '' Request for objects to be selected in thedrawing area

  Dim acSSPrompt As PromptSelectionResult

  acSSPrompt = acDocEd.GetSelection(acSelFtr)

 

  '' If the prompt status is OK, objects wereselected

  If acSSPrompt.Status = PromptStatus.OK Then

      Dim acSSet As SelectionSet =acSSPrompt.Value

 

      Application.ShowAlertDialog("Number ofobjects selected: " & _

                                 acSSet.Count.ToString())

  Else

      Application.ShowAlertDialog("Numberof objects selected: 0")

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.EditorInput;

 

[CommandMethod("FilterSelectionSet")]

public static voidFilterSelectionSet()

{

  // Get the current document editor獲取當前文檔編輯器

  Editor acDocEd =Application.DocumentManager.MdiActiveDocument.Editor;

 

  // Create a TypedValue array to define thefilter criteria

  // 創建一個TypedValue數組來定義過濾器條件

  TypedValue[] acTypValAr = new TypedValue[1];

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Start, "CIRCLE"), 0);

 

  // Assign the filter criteria to aSelectionFilter object

  // 將過濾器條件賦值給SelectionFilter對象

  SelectionFilter acSelFtr = newSelectionFilter(acTypValAr);

 

  // Request for objects to be selected in thedrawing area

  // 請求用戶在圖形區域選擇對象

  PromptSelectionResult acSSPrompt;

  acSSPrompt = acDocEd.GetSelection(acSelFtr);

 

  // If the prompt status is OK, objects wereselected

  // 提示狀態OK,表示用戶已選完

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      SelectionSet acSSet = acSSPrompt.Value;

 

     Application.ShowAlertDialog("Number of objects selected: " +

                                 acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Numberof objects selected: 0");

  }

}

VBA/ActiveX CodeReference

Sub FilterSelectionSet()

    ' Create a new selection set

    Dim sset As AcadSelectionSet

    Set sset =ThisDrawing.SelectionSets.Add("SS1")

 

    ' Define the filter list, only Circleobjects

    ' will be selectable

    Dim FilterType(0) As Integer

    Dim FilterData(0) As Variant

    FilterType(0) = 0

    FilterData(0) = "Circle"

 

    ' Prompt the user to select objects

    ' and add them to the selection set

    sset.SelectOnScreen FilterType, FilterData

 

    MsgBox "Number of objects selected:" & sset.Count

 

    ' Remove the selection set at the end

    sset.Delete

End Sub

 

4.2、SpecifyMultiple Criteria in a Selection Filter多個過濾條件

A selection filter can contain filteringcriteria for more than just one property or object. You define the total numberof conditions to filter on by declaring an array containing enough elements torepresent each of the filter criterion.

選擇過濾器可以包含過濾多個屬性或對象的條件。可以通過聲明一個包含足夠數量元素的數組來定義總的過濾條件,數組的每個元素代表一個過濾條件。

Select objects that meet two criterion 選擇滿足兩個條件的對象

The following example specifies twocriterion to filter selected objects by: the object must be a circle and itmust reside on layer 0.

下面示例指定兩個條件過濾所選對象:對象爲圓且在0層上。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.EditorInput

 

<CommandMethod("FilterBlueCircleOnLayer0")>_

Public SubFilterBlueCircleOnLayer0()

  '' Get the current document editor

  Dim acDocEd As Editor =Application.DocumentManager.MdiActiveDocument.Editor

 

  '' Create a TypedValue array to define thefilter criteria

  Dim acTypValAr(2) As TypedValue

  acTypValAr.SetValue(NewTypedValue(DxfCode.Color, 5), 0)

  acTypValAr.SetValue(NewTypedValue(DxfCode.Start, "CIRCLE"), 1)

  acTypValAr.SetValue(NewTypedValue(DxfCode.LayerName, "0"), 2)

 

  '' Assign the filter criteria to a SelectionFilterobject

  Dim acSelFtr As SelectionFilter = NewSelectionFilter(acTypValAr)

 

  '' Request for objects to be selected in thedrawing area

  Dim acSSPrompt As PromptSelectionResult

  acSSPrompt = acDocEd.GetSelection(acSelFtr)

 

  '' If the prompt status is OK, objects wereselected

  If acSSPrompt.Status = PromptStatus.OK Then

      Dim acSSet As SelectionSet =acSSPrompt.Value

 

      Application.ShowAlertDialog("Numberof objects selected: " & _

                                 acSSet.Count.ToString())

  Else

      Application.ShowAlertDialog("Numberof objects selected: 0")

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.EditorInput;

 

[CommandMethod("FilterBlueCircleOnLayer0")]

public static voidFilterBlueCircleOnLayer0()

{

  // Get the current document editor獲取當前文檔編輯器

  Editor acDocEd =Application.DocumentManager.MdiActiveDocument.Editor;

 

  // Create a TypedValue array to define thefilter criteria

  // 創建TypedValue數組定義過濾條件

  TypedValue[] acTypValAr = new TypedValue[3];

  acTypValAr.SetValue(new TypedValue((int)DxfCode.Color,5), 0);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Start, "CIRCLE"), 1);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.LayerName, "0"), 2);

 

  // Assign the filter criteria to aSelectionFilter object

  // 將過濾條件賦值給SelectionFilter對象

  SelectionFilter acSelFtr = newSelectionFilter(acTypValAr);

 

  // Request for objects to be selected in thedrawing area

  // 請求在圖形區域選擇對象

  PromptSelectionResult acSSPrompt;

  acSSPrompt = acDocEd.GetSelection(acSelFtr);

 

  // If the prompt status is OK, objects wereselected

  // 如果提示狀態OK,表示對象已選

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      SelectionSet acSSet = acSSPrompt.Value;

 

      Application.ShowAlertDialog("Numberof objects selected: " +

                                  acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Numberof objects selected: 0");

  }

}

VBA/ActiveX CodeReference

SubFilterBlueCircleOnLayer0()

    Dim sset As AcadSelectionSet

    Set sset =ThisDrawing.SelectionSets.Add("SS1")

 

    ' Define the filter list, only blue Circleobjects

    ' on layer 0

    Dim FilterType(2) As Integer

    Dim FilterData(2) As Variant

 

    FilterType(0) = 62: FilterData(0) = 3

    FilterType(1) = 0: FilterData(1) ="Circle"

    FilterType(2) = 8: FilterData(2) ="0"

 

    ' Prompt the user to select objects

    ' and add them to the selection set

    sset.SelectOnScreen FilterType, FilterData

 

    MsgBox "Number of objects selected:" & sset.Count

 

    ' Remove the selection set at the end

    sset.Delete

End Sub

 

 

4.3、AddComplexity to Your Filter List Conditions複雜的過濾條件

When you specify multiple selectioncriteria, AutoCAD assumes the selected object must meet each criterion. You canqualify your criteria in other ways. For numeric items, you can specifyrelational operations (for example, the radius of a circle must be greaterthan or equal to 5.0). And for all items, you can specify logicaloperations (for example, Text or MText).

當指定多個選擇條件時,AutoCAD假設所選對象必須滿足每個條件。我們還可以以別的方式搞定過濾條件。對於數值項,可以使用關係運算(比如,遠的半徑必須大於等於5.0)。對於所有項,可以使用邏輯運算(比如單行文字或多行文字)。

Use a -4 DXF code or the constantDxfCode.Operator to indicate a relational operator in a selection filter. Theoperator is expressed as a string. The allowable relational operators are shownin the following table.

使用DXF組碼-4或常量DxfCode.Operator表示選擇過濾器中的關係預算符類型。運算符本身用字符串表示。可用的關係運算符列表如下:

 

Relational operators for selection set filter lists關係運算符列表

Operator運算符

Description描述

"*"

Anything goes (always true)任何情況(總爲True)

"="

Equals等於

"!="

Not equal to不等於

"/="

Not equal to不等於

"<>"

Not equal to不等於

"<"

Less than小於

"<="

Less than or equal to小於等於

">"

Greater than大於

">="

Greater than or equal to大於等於

"&"

Bitwise AND (integer groups only)位與(僅限整數組)

"&="

Bitwise masked equals (integer groups only)位屏蔽等於(僅限整數組)

 

Logical operators in a selection filterare also indicated by a -4 group code or the constant DxfCode.Operator, and theoperator is a string, but the operators must be paired. The opening operator ispreceded by a less-than symbol (<), and the closing operator is followed bya greater-than symbol (>). The following table lists the logical operatorsallowed in selection set filtering.

選擇過濾器中的邏輯操作符同樣用-4組碼或常量DxfCode.Operator表示,邏輯操作符爲字符串,且必須成對出現。操作符開始於小於號(<),結束於大於號(>)。下表;列出了用於選擇集過濾器的邏輯操作符。

 

Logical grouping operators for selection set filter lists

Starting operator起始操作符

Encloses包括:

Ending operator結束操作符

"<AND"

One or more operands一個以上操作數

"AND>"

"<OR"

One or more operands一個以上操作數

"OR>"

"<XOR"

Two operands兩個操作數

"XOR>"

"<NOT"

One operand一個操作數

"NOT>"

 

Select a circle whose radius is greaterthan or equal to 5.0 選擇半徑大於等於5.0的圓

The following example selects circleswhose radius is greater than or equal to 5.0.

下面例子選擇半徑大於等於5.0的圓。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.EditorInput

 

<CommandMethod("FilterRelational")>_

Public SubFilterRelational()

  '' Get the current document editor

  Dim acDocEd As Editor =Application.DocumentManager.MdiActiveDocument.Editor

 

  '' Create a TypedValue array to define thefilter criteria

  Dim acTypValAr(2) As TypedValue

  acTypValAr.SetValue(NewTypedValue(DxfCode.Start, "CIRCLE"), 0)

  acTypValAr.SetValue(NewTypedValue(DxfCode.Operator, ">="), 1)

  acTypValAr.SetValue(New TypedValue(40, 5), 2)

 

  '' Assign the filter criteria to a SelectionFilterobject

  Dim acSelFtr As SelectionFilter = NewSelectionFilter(acTypValAr)

 

  '' Request for objects to be selected in thedrawing area

  Dim acSSPrompt As PromptSelectionResult

  acSSPrompt = acDocEd.GetSelection(acSelFtr)

 

  '' If the prompt status is OK, objects wereselected

  If acSSPrompt.Status = PromptStatus.OK Then

      Dim acSSet As SelectionSet =acSSPrompt.Value

 

      Application.ShowAlertDialog("Numberof objects selected: " & _

                                 acSSet.Count.ToString())

  Else

      Application.ShowAlertDialog("Numberof objects selected: 0")

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.EditorInput;

 

[CommandMethod("FilterRelational")]

public static voidFilterRelational()

{

  // Get the current document editor獲取當前文檔編輯器

  Editor acDocEd =Application.DocumentManager.MdiActiveDocument.Editor;

 

  // Create a TypedValue array to define thefilter criteria

  // 創建TypedValue來定義過濾條件

  TypedValue[] acTypValAr = new TypedValue[3];

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Start, "CIRCLE"), 0);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Operator, ">="), 1);

  acTypValAr.SetValue(new TypedValue(40, 5),2);

 

  // Assign the filter criteria to aSelectionFilter object

  // 將過濾條件複製給SelectionFilter對象

  SelectionFilter acSelFtr = newSelectionFilter(acTypValAr);

 

  // Request for objects to be selected in thedrawing area

  // 請求在圖形區域選擇對象

  PromptSelectionResult acSSPrompt;

  acSSPrompt = acDocEd.GetSelection(acSelFtr);

 

  // If the prompt status is OK, objects wereselected

  // 提示欄OK,表示對象已選

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      SelectionSet acSSet = acSSPrompt.Value;

 

      Application.ShowAlertDialog("Numberof objects selected: " +

                                 acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Numberof objects selected: 0");

  }

}

VBA/ActiveX Code Reference

SubFilterRelational()

    Dim sset As AcadSelectionSet

    Dim FilterType(2) As Integer

    Dim FilterData(2) As Variant

    Set sset =ThisDrawing.SelectionSets.Add("SS1")

    FilterType(0) = 0: FilterData(0) ="Circle"

    FilterType(1) = -4: FilterData(1) =">="

    FilterType(2) = 40: FilterData(2) = 5#

 

    sset.SelectOnScreen FilterType, FilterData

 

    MsgBox "Number of objects selected:" & sset.Count

 

    ' Remove the selection set at the end

    sset.Delete

End Sub


Select either Text or MText 選擇單行文字或多行文字

The following example specifies thateither Text or MText objects can be selected.

下面例子演示可以選擇Text(單行文字)或者MText(多行文字)。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.EditorInput

 

<CommandMethod("FilterForText")>_

Public SubFilterForText()

  '' Get the current document editor

  Dim acDocEd As Editor =Application.DocumentManager.MdiActiveDocument.Editor

 

  '' Create a TypedValue array to define thefilter criteria

  Dim acTypValAr(3) As TypedValue

  acTypValAr.SetValue(NewTypedValue(DxfCode.Operator, "<or"), 0)

  acTypValAr.SetValue(New TypedValue(DxfCode.Start,"TEXT"), 1)

  acTypValAr.SetValue(NewTypedValue(DxfCode.Start, "MTEXT"), 2)

  acTypValAr.SetValue(NewTypedValue(DxfCode.Operator, "or>"), 3)

 

  '' Assign the filter criteria to aSelectionFilter object

  Dim acSelFtr As SelectionFilter = NewSelectionFilter(acTypValAr)

 

  '' Request for objects to be selected in thedrawing area

  Dim acSSPrompt As PromptSelectionResult

  acSSPrompt = acDocEd.GetSelection(acSelFtr)

 

  '' If the prompt status is OK, objects wereselected

  If acSSPrompt.Status = PromptStatus.OK Then

      Dim acSSet As SelectionSet =acSSPrompt.Value

 

      Application.ShowAlertDialog("Numberof objects selected: " & _

                                 acSSet.Count.ToString())

  Else

      Application.ShowAlertDialog("Numberof objects selected: 0")

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.EditorInput;

 

[CommandMethod("FilterForText")]

public static voidFilterForText()

{

  // Get the current document editor

  Editor acDocEd =Application.DocumentManager.MdiActiveDocument.Editor;

 

  // Create a TypedValue array to define thefilter criteria

  TypedValue[] acTypValAr = new TypedValue[4];

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Operator, "<or"), 0);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Start, "TEXT"), 1);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Start, "MTEXT"), 2);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Operator, "or>"), 3);

 

  // Assign the filter criteria to aSelectionFilter object

  SelectionFilter acSelFtr = newSelectionFilter(acTypValAr);

 

  // Request for objects to be selected in thedrawing area

  PromptSelectionResult acSSPrompt;

  acSSPrompt = acDocEd.GetSelection(acSelFtr);

 

  // If the prompt status is OK, objects wereselected

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      SelectionSet acSSet = acSSPrompt.Value;

 

      Application.ShowAlertDialog("Numberof objects selected: " +

                                  acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Numberof objects selected: 0");

  }

}

VBA/ActiveX CodeReference

Sub FilterForText()

    Dim sset As AcadSelectionSet

    Dim FilterType(3) As Integer

    Dim FilterData(3) As Variant

    Set sset =ThisDrawing.SelectionSets.Add("SS1")

 

    FilterType(0) = -4: FilterData(0) ="<or"

    FilterType(1) = 0: FilterData(1) ="TEXT"

    FilterType(2) = 0: FilterData(2) ="MTEXT"

    FilterType(3) = -4: FilterData(3) ="or>"

 

    sset.SelectOnScreen FilterType, FilterData

 

    MsgBox "Number of objects selected:" & sset.Count

 

    ' Remove the selection set at the end

    sset.Delete

End Sub

 

4.4、UseWild-Card Patterns in Selection Set Filter Criteria在過濾條件裏使用通配符

Symbol names and strings in selectionfilters can include wild-card patterns.

選擇過濾器中的符號名字和字符串可以包含通配符。

The following table identifies thewild-card characters recognized by AutoCAD, and what each means in the contextof a string:

下表爲AutoCAD能夠識別的通配字符,及其在字符串上下文所代表的意義:

 

 

Use a reverse quote (`) to indicate that acharacter is not a wildcard, but is to be taken literally. For example, tospecify that only an anonymous block named “*U2” be included in the selectionset, use the value“`*U2”.

使用轉義引號(’)表示一個字符不是通配符,應逐個字符使用。例如,要指定只將名爲“*U2”的匿名塊包含在選擇集中,應使用值“’*U2”。


Select MText where a specific wordappears in the text 選擇包含指定文字的MText

The following example defines a selectionfilter that selects MText objects that contain the text string of “The”.

下例定義一個選擇過濾器,選擇包含文字串“The”的MText對象。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.EditorInput

 

<CommandMethod("FilterMtextWildcard")>_

Public SubFilterMtextWildcard()

  '' Get the current document editor

  Dim acDocEd As Editor =Application.DocumentManager.MdiActiveDocument.Editor

 

  '' Create a TypedValue array to define thefilter criteria

  Dim acTypValAr(1) As TypedValue

  acTypValAr.SetValue(NewTypedValue(DxfCode.Start, "MTEXT"), 0)

  acTypValAr.SetValue(New TypedValue(DxfCode.Text,"*The*"), 1)

 

  '' Assign the filter criteria to aSelectionFilter object

  Dim acSelFtr As SelectionFilter = NewSelectionFilter(acTypValAr)

 

  '' Request for objects to be selected in thedrawing area

  Dim acSSPrompt As PromptSelectionResult

  acSSPrompt = acDocEd.GetSelection(acSelFtr)

 

  '' If the prompt status is OK, objects wereselected

  If acSSPrompt.Status = PromptStatus.OK Then

      Dim acSSet As SelectionSet =acSSPrompt.Value

 

      Application.ShowAlertDialog("Numberof objects selected: " & _

                                 acSSet.Count.ToString())

  Else

      Application.ShowAlertDialog("Numberof objects selected: 0")

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.EditorInput;

 

[CommandMethod("FilterMtextWildcard")]

public static voidFilterMtextWildcard()

{

  // Get the current document editor

  Editor acDocEd =Application.DocumentManager.MdiActiveDocument.Editor;

 

  // Create a TypedValue array to define thefilter criteria

  TypedValue[] acTypValAr = new TypedValue[2];

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Start, "MTEXT"), 0);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.Text, "*The*"), 1);

 

  // Assign the filter criteria to aSelectionFilter object

  SelectionFilter acSelFtr = newSelectionFilter(acTypValAr);

 

  // Request for objects to be selected in thedrawing area

  PromptSelectionResult acSSPrompt;

  acSSPrompt = acDocEd.GetSelection(acSelFtr);

 

  // If the prompt status is OK, objects wereselected

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      SelectionSet acSSet = acSSPrompt.Value;

 

      Application.ShowAlertDialog("Numberof objects selected: " +

                                  acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Numberof objects selected: 0");

  }

}

VBA/ActiveX Code Reference

SubFilterMtextWildcard()

    Dim sset As AcadSelectionSet

    Dim FilterType(1) As Integer

    Dim FilterData(1) As Variant

    Set sset =ThisDrawing.SelectionSets.Add("SS1")

 

    FilterType(0) = 0

    FilterData(0) = "MTEXT"

    FilterType(1) = 1

    FilterData(1) = "*The*"

 

    sset.SelectOnScreen FilterType, FilterData

 

    MsgBox "Number of objects selected:" & sset.Count

 

    ' Remove the selection set at the end

    sset.Delete

End Sub

 

4.5、Filterfor Extended Data過濾擴展數據

External applications can attach data suchas text strings, numeric values, 3D points, distances, and layer names to AutoCADobjects. This data is referred to as extended data, or xdata. You can filterentities containing extended data for a specified application.

外部應用程序可以向AutoCAD對象提供諸如文本串、數值、3D點、距離、圖層名等數據。這些數據我們稱之爲擴展數據,或叫xdata。我們可以過濾含有指定外部程序擴展數據的實體。

Select circles that contain xdata 選擇含有擴展數據的圓

The following example filters for circlescontaining xdata added by the “MY_APP” application:

下例多慮含有由程序“MY_APP”添加了擴展數據的圓。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.EditorInput

 

<CommandMethod("FilterXdata")>_

Public SubFilterXdata()

  '' Get the current document editor

  Dim acDocEd As Editor =Application.DocumentManager.MdiActiveDocument.Editor

 

  '' Create a TypedValue array to define thefilter criteria

  Dim acTypValAr(1) As TypedValue

  acTypValAr.SetValue(NewTypedValue(DxfCode.Start, "Circle"), 0)

  acTypValAr.SetValue(NewTypedValue(DxfCode.ExtendedDataRegAppName, _

                                    "MY_APP"), 1)

 

  '' Assign the filter criteria to aSelectionFilter object

  Dim acSelFtr As SelectionFilter = NewSelectionFilter(acTypValAr)

 

  '' Request for objects to be selected in thedrawing area

  Dim acSSPrompt As PromptSelectionResult

  acSSPrompt = acDocEd.GetSelection(acSelFtr)

 

  '' If the prompt status is OK, objects wereselected

  If acSSPrompt.Status = PromptStatus.OK Then

      Dim acSSet As SelectionSet =acSSPrompt.Value

 

      Application.ShowAlertDialog("Numberof objects selected: " & _

                                 acSSet.Count.ToString())

  Else

      Application.ShowAlertDialog("Numberof objects selected: 0")

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.EditorInput;

 

[CommandMethod("FilterXdata")]

public static voidFilterXdata()

{

  // Get the current document editor

  Editor acDocEd =Application.DocumentManager.MdiActiveDocument.Editor;

 

  // Create a TypedValue array to define thefilter criteria

  TypedValue[] acTypValAr = new TypedValue[2];

  acTypValAr.SetValue(new TypedValue((int)DxfCode.Start,"Circle"), 0);

  acTypValAr.SetValue(newTypedValue((int)DxfCode.ExtendedDataRegAppName,

                                    "MY_APP"), 1);

 

  // Assign the filter criteria to aSelectionFilter object

  SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);

 

  // Request for objects to be selected in thedrawing area

  PromptSelectionResult acSSPrompt;

  acSSPrompt = acDocEd.GetSelection(acSelFtr);

 

  // If the prompt status is OK, objects wereselected

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      SelectionSet acSSet = acSSPrompt.Value;

 

      Application.ShowAlertDialog("Numberof objects selected: " +

                                 acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Numberof objects selected: 0");

  }

}

VBA/ActiveX Code Reference

Sub FilterXdata()

    Dim sset As AcadSelectionSet

    Dim FilterType(1) As Integer

    Dim FilterData(1) As Variant

    Set sset =ThisDrawing.SelectionSets.Add("SS1")

 

    FilterType(0) = 0: FilterData(0) ="Circle"

    FilterType(1) = 1001: FilterData(1) ="MY_APP"

 

    sset.SelectOnScreen FilterType, FilterData

 

    MsgBox "Number of objects selected:" & sset.Count

 

    ' Remove the selection set at the end

    sset.Delete

End Sub

 

 

 

 

5、Remove Objects From a Selection Set從選擇集刪除對象

After you create a selection set, you canwork with the object ids of the objects selected. Selection sets do not allowyou to add or remove object ids from it, but you can use an ObjectIdCollectionobject to merge multiple selection sets into a single object to work with. Youcan add and remove object ids from an ObjectIdCollection object. Use the Remove or RemoveAt methods to remove an object id from anObjectIdCollection object. For information on merging multiple selection setsand working with an ObjectIdCollection object, see AddTo or Merge Multiple Selection Sets.

創建選擇集後,接下來就可以使用所選對象的id。選擇集不允許從中添加或刪除對象id,不過我們可以用ObjectIdCollection對象將多個選擇集合併爲單個選擇集使用。我們可以從ObjectIdCollection對象中添加或刪除對象id。從ObjectIdCollection對象中刪除一個對象id,使用Remove方法或RemoveAt方法。

關於合併多個選擇集和使用ObjectIdCollection對象的更多內容,見“添加或合併多個選擇集”。

 


 

發佈了4 篇原創文章 · 獲贊 20 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章