If the drawing is opened in AutoCAD, you can use Editor.SelectAll(SelectionFilter filter) to partially achieve what you need. Being "partially", because you can filter on entity type (DBText and/or MText), you can filter on space (LayoutName="Model"), you can also filter on XData's registered application name. However, you cannot define a filter based on particular XData value.
So, you would build a filter on DBText/MText, LayoutName and XData application name, pass the filter to Editor.SelectAll() method to get a SelectionSet. Then you loop through the SelectionSet and read XData from each entity in the SelectionSet to see if the entity is the targeted one.
Here is some quick code (not tested):
[CommandMethod("MyText")]
public static void FindAllTextsWithXData()
{
Document dwg = CadApp.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
ObjectId[] myTexts = null;
try
{
ObjectId[] selected = SelectEntities(ed);
if (selected != null)
{
using (Transaction tran = dwg.TransactionManager.StartTransaction())
{
myTexts = HighlightEntsByXDataValue(selected, tran);
tran.Commit();
}
}
if (myTexts != null)
{
try
{
//Do whay you need with the highlighted Texts
}
finally
{
//You may want to eventually unhighlight those text before ending the command
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage("\nError: {0}\n{1}", ex.Message, ex.StackTrace);
}
finally
{
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
}
private static ObjectId[] SelectEntities(Editor ed)
{
TypedValue[] vals = new TypedValue[]
{
new TypedValue((int)DxfCode.Start,"TEXT"),
new TypedValue((int)DxfCode.LayoutName,"MODEL"),
new TypedValue((int)DxfCode.ExtendedDataRegAppName,"MyXDataAppName")
};
PromptSelectionResult res = ed.SelectAll(new SelectionFilter(vals));
if (res.Status==PromptStatus.OK)
{
return res.Value.GetObjectIds();
}
else
{
return null;
}
}
private static ObjectId[] HighlightEntsByXDataValue(ObjectId[] objIds, Transaction tran)
{
List<ObjectId> ids = new List<ObjectId>();
foreach (var id in objIds)
{
Entity ent = (Entity)tran.GetObject(id, OpenMode.ForRead);
ResultBuffer buffer = ent.GetXDataForApplication("MyXDataAppName");
// Or you can simply
// ResultBuffer buffer = ent.XData;
// Because the entity was selected by a filter on the XData app name
if (buffer!=null)
{
if (IsTarget(buffer))
{
ids.Add(id);
ent.Highlight();
}
}
}
return ids.ToArray();
}
private static bool IsTarget(ResultBuffer buffer)
{
//Your logic to determine if the XData has particular piece of data
return true;
}