October 2014

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  










« Highlighting an AutoCAD entity in a nested block using .NET | Main | Merry Christmas, Joyeux Noël, Fröhliche Weihnachten, Buon Natale »

December 15, 2006

Highlighting a nested AutoCAD block using .NET

OK, I have to admit I feel a bit cheeky making a separate post out of this one, but then I did promise our friends at TheSwamp that I'd post this additional code, once more provided by Sreekar Devatha in response to an ADN support request.

The code is basically the same as that in my last post. The only change is that after we reverse the list of containers we don't go and add in the selected entity. Which means that when we get our path it stops at the container of the selected entity, not the entity itself.

I've extended the code to define two commands - HLNESTEDENT and HLNESTEDBLK - using the same internal function, InternalHighlightNested(). Check the use of the isEntity flag in the code, to see how it differs for the two commands.

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Runtime;


namespace MySelectionApp

{

  public class MySelectionCmds

  {

    [CommandMethod("HLNESTEDBLK")]

    static public void HighlightNestedBlock()

    {

      InternalHighlightNested(false);

    }


    [CommandMethod("HLNESTEDENT")]

    static public void HighlightNestedEntity()

    {

      InternalHighlightNested(true);

    }


    static private void InternalHighlightNested(bool isEntity)

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;


      PromptNestedEntityResult rs =

        ed.GetNestedEntity(

          "\nSelect nested " +

          (isEntity ? "entity" : "block") +

          ": "

        );


      if (rs.Status == PromptStatus.OK)

      {

        ObjectId[] objIds = rs.GetContainers();

        ObjectId ensel = rs.ObjectId;

        int len = objIds.Length;


        // Reverse the "containers" list

        ObjectId[] revIds;

        if (isEntity)

          revIds = new ObjectId[len + 1];

        else

          revIds = new ObjectId[len];


        for (int i = 0; i < len; i++)

        {

          ObjectId id =

            (ObjectId)objIds.GetValue(len - i - 1);

          revIds.SetValue(id, i);

        }

        // Add the selected entity to the end, if needed

        if (isEntity)

          revIds.SetValue(ensel, len);


        // Check to make sure a single entity has not been

        // picked (in the case of a block)

        if (revIds.Length > 0)

        {

          // Retrieve the sub-entity path for this entity

          SubentityId subEnt =

            new SubentityId(SubentityType.Null, 0);

          FullSubentityPath path =

            new FullSubentityPath(revIds, subEnt);


          Transaction tr =

            doc.TransactionManager.StartTransaction();

          using (tr)

          {

            // Open the outermost container...

            ObjectId id = (ObjectId)revIds.GetValue(0);

            Entity ent =

              (Entity)tr.GetObject(id, OpenMode.ForRead);

            // ... and highlight the nested entity

            ent.Highlight(path, false);

            tr.Commit();

          }

        }

      }

    }

  }

}

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200d834d211c853ef

Listed below are links to weblogs that reference Highlighting a nested AutoCAD block using .NET:

blog comments powered by Disqus

Feed/Share

10 Random Posts