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指在图形
空间
三、过滤组⽅式
- 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
FilterType0
2、过滤条件为图元为CIRCLE或LINE
图元是CIRCLE OR图元是LINE
FilterData CIRCLE LINE OR>
FilterType-400-4
3、过滤条件为图元在DIM图层(LAYER)中的CIRCLE或LINE
(图元是CIRCLE OR图元是LINE) AND图层位于DIM层
FilterData CIRCLE LINE OR>DIM AND>
FilterType-4-400-48-4
4、过滤的条件为图元为CIRCLE或LINE但图层(LAYER)不属于DIM层
(图元是CIRCLE OR图元是LINE) AND NOT(图层位于DIM层)
FilterData CIRCLE LINE OR>DIM NOT>AND>
FilterType-4-400-4-48-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
}
>filter过滤对象数组

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。