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
{
[CommandMethod("HLNESTED")]
static public void HighlightNestedEntity()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
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 =
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();
}
}
}
}
}
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).