有条件选择AutoCAD实体
2016-07-14 14:37
253 查看
引自 http://through-the-interface.typepad.com/through_the_interface/2008/07/conditional-sel.html
这篇文章有感自之前的 一篇文章 ,在那篇文章中我们可以找到选择指定层上的所有实体的代码。这儿我们关注的是如何更好的选择多个层中的实体:AutoCAD的选择过滤机理使得这个选择起来实现起来很容易,同时还能实现跟实体的各个属性相关的复合性选择。
This post was inspired by a comment on this previous
post, where we looked at some code to select entities on a specific layer. The question was regarding how best to select entities from multiple layers: the selection filtering mechanism inside AutoCAD makes this very easy, and can cope with composition
of conditions related to various entity properties.
基本的思路就是添加你所想要过滤的实体特性的标签,标签标明了过滤条件:条件"or",添加"<or"与">or";条件"and",添加"<and"与">and"。
The basic concept is to enclose sets of entity properties for which you wish to filter with tags indicating the composition of the conditions: for "or" you enclose the conditions with "<or" and "or>" and for "and" you use "<and" and "and>". Wow: I can safely
say that that's probably the only sentence I've ever written that has 6 of the last 10 words being "and". :-)
让我们看一个实例:我们想要选择层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.
另外,你也可以使用"<xor"与"xor>"进行一个高级"or"选择的测试。
By the way - you can also choose to perform an "exclusive or" test by using "<xor" and "xor>".
To try out this code, draw a number of lines in a blank drawing, and run the SEWP command. This simply tells you how many entities met the selection criteria - it doesn't leave them selected for use by further commands. You can then see how drawing circles
of varying radii changes the number of entities selected by the command.
On a final note... SelectionFilters can be used either non-interactively (as in this example, via the SelectAll() method) or interactively (via GetSelection(), SelectWindow(), SelectCrossingPolygon(), SelectFence(), etc.). I've shown simple uses of SelectionFilters
in previous posts, but it's also possible to use quite complicated groupings of conditions - as we've scratched the surface of in this post.
这篇文章有感自之前的 一篇文章 ,在那篇文章中我们可以找到选择指定层上的所有实体的代码。这儿我们关注的是如何更好的选择多个层中的实体:AutoCAD的选择过滤机理使得这个选择起来实现起来很容易,同时还能实现跟实体的各个属性相关的复合性选择。
This post was inspired by a comment on this previous
post, where we looked at some code to select entities on a specific layer. The question was regarding how best to select entities from multiple layers: the selection filtering mechanism inside AutoCAD makes this very easy, and can cope with composition
of conditions related to various entity properties.
基本的思路就是添加你所想要过滤的实体特性的标签,标签标明了过滤条件:条件"or",添加"<or"与">or";条件"and",添加"<and"与">and"。
The basic concept is to enclose sets of entity properties for which you wish to filter with tags indicating the composition of the conditions: for "or" you enclose the conditions with "<or" and "or>" and for "and" you use "<and" and "and>". Wow: I can safely
say that that's probably the only sentence I've ever written that has 6 of the last 10 words being "and". :-)
让我们看一个实例:我们想要选择层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 }
另外,你也可以使用"<xor"与"xor>"进行一个高级"or"选择的测试。
By the way - you can also choose to perform an "exclusive or" test by using "<xor" and "xor>".
To try out this code, draw a number of lines in a blank drawing, and run the SEWP command. This simply tells you how many entities met the selection criteria - it doesn't leave them selected for use by further commands. You can then see how drawing circles
of varying radii changes the number of entities selected by the command.
On a final note... SelectionFilters can be used either non-interactively (as in this example, via the SelectAll() method) or interactively (via GetSelection(), SelectWindow(), SelectCrossingPolygon(), SelectFence(), etc.). I've shown simple uses of SelectionFilters
in previous posts, but it's also possible to use quite complicated groupings of conditions - as we've scratched the surface of in this post.
相关文章推荐
- 测试linux下磁盘的读写速率
- GPS 定位精度查看
- Python+lxml解析html
- NodeJS学习系列课程笔记(NodeJs zlib库文件解压缩的相关使用方法)
- Linux C 学习笔记
- Activiti基础教程--04管理流程定义(流程定义部署ZIP/classpath)、查看(获取流程定义图图片)、删除,不能修改
- Effetive Objective - C 1.Objective 概述
- [图像] 二值图像的位置、朝向与投影
- 328. Odd Even Linked List
- 从内核驱动到android app
- 【POI】使用POI处理xlsx的cell中的超链接 和 插入图片 和 设置打印区域
- 代码中设置textView的颜色出现文字消失或颜色设置不上的问题
- poj3372-Candy Distribution
- android热补丁总结
- operator 类型转换符
- 查询Oracle正在执行和执行过的SQL语句
- 在vim中设置将tab自动转化为4个空格
- linux中nfs挂载问题(mount.nfs: access denied by server while mounting )
- WSDL 详解
- POI导出word 文档