Introduction
Last time, we have learnt selection, but what if we have to filter certain types of object from the selection result. Today, we will learn how to select certain type of the object from the selection result
Define Rules for selection Filters
| DXF CODES | Filter Type |
|---|---|
| 0 (or DxfCode.Start) | Object Type (String)Such as “Line,” “Circle,” “Arc,” and so forth. |
| 2 (or DxfCode.BlockName) | Block Name (String)The block name of an insert reference. |
| 8 or (DxfCode.LayerName) | Layer Name (String)Such as “Layer 0.” |
| 60 (DxfCode.Visibility) | Object Visibility (Integer)Use 0 = visible, 1 = invisible. |
| 62 (or DxfCode.Color) | Color Number (Integer)Numeric index values ranging from 0 to 256.Zero indicates BYBLOCK. 256 indicates BYLAYER. A negative value indicates that the layer is turned off. |
| 67 | Model/paper space indicator (Integer)Use 0 or omitted = model space, 1 = paper space. |
Relational operators for selection set filter lists
| Operator | Description |
|---|---|
| “”"*""" | Anything goes (always 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) |
Example
Demand: Get all the text in red color on “layer01” and all the text in blue on “lay02” and “lay03”.
For Polyline objects , the type in DxfCode.Start is “LWPOLYLINE” rather than “Ployline”.
I don’t know why Autodesk design like this, but it took me long time to figure it out. Hence, I would like to emphesis this point.
< or
< and
Layer == "layer01"
Color == "1" // The index of Color red is 1
Entity type == "TEXT"
and >
< and
< or
Layer == "layer02"
Layer == "layer03"
or >
Color == "5" // The index of Color Blue is 5
Entity type == "POLYLINE" or "Line", "Circle"
and >
or >
Code
// Define the filter condition
TypedValue[] typeValue = {
new TypedValue((int) DxfCode.Operator, "<or"),
// Selection filter 1
new TypedValue((int) DxfCode.Operator, "<and"),
new TypedValue((int) DxfCode.Start, "TEXT"),
new TypedValue((int) DxfCode.LayerName, "layer01"),
new TypedValue((int) DxfCode.Color, "1"),
new TypedValue((int) DxfCode.Operator, "and>"),
// Selection filter 2
new TypedValue((int) DxfCode.Operator, "<and"),
new TypedValue((int) DxfCode.Start, "LWPOLYLINE, LINE, CIRCLE"),
new TypedValue((int) DxfCode.Color, "5"),
new TypedValue((int) DxfCode.Operator, "<or"),
new TypedValue((int) DxfCode.LayerName, "layer02"),
new TypedValue((int) DxfCode.LayerName, "layer03"),
new TypedValue((int) DxfCode.Operator, "or>"),
new TypedValue((int) DxfCode.Operator, "and>"),
// end of Selection filter 2
new TypedValue((int) DxfCode.Operator, "or>")
};
Full Code
public static void MultipleSelectionFilter()
{
// Get the current editor
Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor;
// Create a set filter
// Define the filter condition
TypedValue[] typeValue = {
new TypedValue((int) DxfCode.Operator, "<or"),
// Selection filter 1
new TypedValue((int) DxfCode.Operator, "<and"),
new TypedValue((int) DxfCode.Start, "TEXT"),
new TypedValue((int) DxfCode.LayerName, "layer01"),
new TypedValue((int) DxfCode.Color, "1"),
new TypedValue((int) DxfCode.Operator, "and>"),
// Selection filter 2
new TypedValue((int) DxfCode.Operator, "<and"),
new TypedValue((int) DxfCode.Start, "LWPOLYLINE,LINE, CIRCLE"),
new TypedValue((int) DxfCode.Color, "5"),
new TypedValue((int) DxfCode.Operator, "<or"),
new TypedValue((int) DxfCode.LayerName, "layer02"),
new TypedValue((int) DxfCode.LayerName, "layer03"),
new TypedValue((int) DxfCode.Operator, "or>"),
new TypedValue((int) DxfCode.Operator, "and>"),
new TypedValue((int) DxfCode.Operator, "or>")
};
// put the condition into the filter
SelectionFilter selFiter = new SelectionFilter(typeValue);
// get the filtered result
PromptSelectionResult psr = ed.GetSelection(selFiter);
// Check the result
if (psr.Status == PromptStatus.OK)
{
SelectionSet sSet = psr.Value;
Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("Number of objects selected: " + sSet.Count.ToString());
}
else
{
Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("Number of objects selected: 0");
}
}
Reference
http://docs.autodesk.com/ACD/2011/CHS/filesDXF/WSfacf1429558a55de185c428100849a0ab7-5f35.htm