Month: October 2016

  • Checking the type of an Entity? (AutoCAD .net)

    Ok, so you’ve got a bunch of entities in a collection. You only want to deal with circles. You need to iterate through the collection and consider only the circles. But how will you identify the circles from the other objects.

     

    1. Casting.

    You can cast

    Entity en = en as Circle

    And then you can test whether entity is null.

    If (en == null )

    { // throw new Exception etc. etc. }

     

    Or you can try the equivalent:

    If (en is Circle)

    { // Perform operation etc. etc.}

     

    What is the catch with this approach?

    • The benefits are that it is really quick and dirty.
    • ………most important that you gotta watch out for is that it tests for Circles and subsequent sub classes of circles. You may not want that so watch out!

     

    1. GetType

    I’ve also seen folks on the forums use the Gettype to  check for the type of the entity. It goes something like this:

    en.GetType() == typeOf(Circle)

     

    The Catch with this approach

    • It’s painful to read.
    • Two computations involved, just like the first approach. I can’t see the performance being too much better or worse.

     

    Another approach is to use Dxf codes to check for the name. But this is overcomplicated. I don’t see many people using it on the forums and you need the object ID of the relevant entity and all the overhead associated with it.

    In my opinion, keep it simple. Casting, after all things considered, is probably the best option, but you have to watch out – all subclasses will return true. So you need to use the most granular class you can if that is at all important.

  • Get SelectionFilter by block Name

    It is very common that you will need to create selection filters for different types of blocks. Why repeat yourself?

     

    There is a very simple utility that I wrote that is amazingly handy for solving this very issue. Best part is that it accepts wildcards, so you can search for block references which, for example, start with Fire by passing in “FIRE*” as the argument:

    public static SelectionFilter GetSSFilterBlockReferenceByName(string name)
    {
    TypedValue[] filterlist = new TypedValue[2];
    filterlist[0] = new TypedValue(0, “INSERT”);
    filterlist[1] = new TypedValue(2, name);
    SelectionFilter filter = new SelectionFilter(filterlist);
    return filter;
    }

    So simple!

  • Convert ObjectID[] to ObjectIDCollections!

    When dealing with selection sets we can obtain the object ids of the objects contained within. The method though, returns an array.

     

    But what if we want an ObjectIDCollection?

     

    We can simply pass the ObjectID[] array into the ObjectIDCollection constructor.

     

    Simple. The last thing you want to do is iterate through the array and add it to a collection individually. A simple yet handy hint which can save you a lot of effort.