AutoCAD.net:有条件选择AutoCAD实体

来源:互联网 发布:审计软件 过程 编辑:程序博客网 时间:2024/06/05 02:29

AutoCAD.net:有条件选择AutoCAD实体

Posted on 2008-08-08 13:23 无锋不起浪 阅读(1093) 评论(3) 编辑 收藏 所属分类: AutoCAD.net

引自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. 

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.

 

原创粉丝点击