Through the Interface: Highlighting an AutoCAD entity in a nested block using .NET

April 2015

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    


« DWFx & Freewheel | Main | Highlighting a nested AutoCAD block using .NET »

December 13, 2006

Highlighting an AutoCAD entity in a nested block using .NET

I'd like to thank two people for this post: Kerry Brown, an ADN member in Australia and regular contributor to TheSwamp, pointed me to a response he'd received from a member of the DevTech team in India, Sreekar Devatha. So thanks to both Kerry and Sreekar for providing the material for this post.

The problem was to highlight the nested entity returned by the Editor.GetNestedEntity() method. Sreekar's solution was to create a sub-entity path through the nested block structure, and use that to highlight the entity in question. To create the sub-entity path he retrieved the containers of the nested entity, then reversed them and added the entity's ObjectId to the end of the list. This served as input to the constructor of the FullSubentityPath object, which could then be used in the call to Highlight() on the root container entity.

Here's the C# code:

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Runtime;

namespace MySelectionApp


  public class MySelectionCmds



    static public void HighlightNestedEntity()


      Document doc =


      Editor ed = doc.Editor;

      PromptNestedEntityResult rs =

        ed.GetNestedEntity("\nSelect nested entity: ");

      if (rs.Status == PromptStatus.OK)


        ObjectId[] objIds = rs.GetContainers();

        ObjectId ensel = rs.ObjectId;

        int len = objIds.Length;

        // Reverse the "containers" list

        ObjectId[] revIds = new ObjectId[len + 1];

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


          ObjectId id =

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

          revIds.SetValue(id, i);


        // Now add the selected entity to the end

        revIds.SetValue(ensel, len);

        // Retrieve the sub-entity path for this entity

        SubentityId subEnt =

          new SubentityId(SubentityType.Null, 0);

        FullSubentityPath path =

          new FullSubentityPath(revIds, subEnt);

        Transaction tr =


        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);







Create a nested block and run the HLNESTED command to see the code in action. You'll need to REGEN at the end for the highlighting to disappear (you would typically use ent.Unhighlight(path, false) to do this programmatically).


TrackBack URL for this entry:

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

blog comments powered by Disqus


10 Random Posts