Autocad.net: Conditional selection of AutoCAD entities

Source: Internet
Author: User

Self-built http://through-the-interface.typepad.com/through_the_interface/2008/07/conditional-sel.html

This articleArticleIn a previous article, we can findCode. Here we are concerned about how to better select entities in multiple layers: the choice of filtering mechanism of AutoCAD makes this choice easy to implement, at the same time, it can achieve composite selection related to each attribute of the object.

This post was too red 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 mechanisms inside AutoCAD makes this very easy, and can vary with composition of conditions related to varous entity properties.

The basic idea is to add tags for the object features you want to filter. The tags indicate the filter conditions: condition "or", add "<or" and "> or "; condition "and", add "<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's probably the only sentence I 've ever written that has 6 of the last 10 words being "and ". :-)

Let's look at an example: how do we combine conditions to select all straight lines on layer 0 and all circles with a diameter greater than 10?

Let's take a concrete example: Let's say we want to select all lines on layer 0 and all the circles with radii greater than 10. 'S how we wocould compose the conditions, in pseudo-code:

    • <Or

      • <And

        • Layer = "0"
        • Entity type = "line"
      • And>
      • <And
        • Entity type = "circle"
        • Radius >=10.0
      • And>
    • Or>

Convert to C # the following code: for clarity, here I implement the specified attribute/value in the form of hard encoding, and if necessary, the user should directly select from the database.

This translates into the following C # code-for clarity I 've left the specific properties/values hard-coded, but clearly it wocould 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
}

In addition, you can also use "<XOR" and "XOR>" for an advanced "or" selection test.

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 your 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.