CAD二次开发之选择集过滤

一、选择集过滤时的使用方式如下:

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>OR>XOR>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

过滤群组方式

內含项目

描述

运算法则

""

1 或 多个

所有项目的交集

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

""

1 或多个

所有项目的并集

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

""

2

两个项目的异或运算

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

""

1

不包含此项目的值

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

四、范例:

1、过滤条件为图元为MTEXT

图元是MTEXT

FilterData

MTEXT

FilterType

0

2、过滤条件为图元为CIRCLELINE

图元是CIRCLE OR 图元是LINE

FilterData

 

CIRCLE

LINE

OR>

FilterType

-4

0

0

-4

3、过滤条件为图元在DIM 图层(LAYER)中的CIRCLELINE

(图元是CIRCLE OR 图元是LINE) AND 图层位于DIM

FilterData

 

 

CIRCLE

LINE

OR>

DIM

AND>

FilterType

-4

-4

0

0

-4

8

-4

4、过滤的条件为图元为CIRCLELINE但图层(LAYER)不属于DIM

(图元是CIRCLE OR 图元是LINE) AND NOT(图层位于DIM)

FilterData

 

 

CIRCLE

LINE

OR>

 

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:

·        

·        

·         Layer == "0"

·         Entity type == "LINE"

·         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, "),
new TypedValue((int)DxfCode.Operator, "),
new TypedValue((int)DxfCode.LayerName, "0"),
new TypedValue((int)DxfCode.Start, "LINE"),
new TypedValue((int)DxfCode.Operator, "and>"),
new TypedValue((int)DxfCode.Operator, "),
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
}

猜你喜欢

转载自blog.csdn.net/weixin_42339460/article/details/80662972