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


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

  • 相关阅读:
    20190613 安装Anaconda
    20190613 一个SQL问题
    20190604 宠物引发的乱想
    windows下的vim打开文件乱码解决
    【题解】 「NOI2019」弹跳 KDT优化建图+最短路+剪枝+卡空间 LOJ3159
    【题解】 [AH/HNOI2017]单旋 LCT+splay
    【题解】 CF1400E Clear the Multiset 笛卡尔树+贪心+dp
    【题解】 CF1404C Fixed Point Removal 线段树+树状数组+带悔贪心
    【题解】 CF1251E2 Voting (Hard Version) 带悔贪心
    【题解】 CF1418G Three Occurrences hash+双指针+差分
  • 原文地址:https://www.cnblogs.com/wf225/p/1263674.html
Copyright © 2020-2023  润新知