<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Select all text object with a particular color in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5277425#M42693</link>
    <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is some quick code (not tested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;[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&amp;lt;ObjectId&amp;gt; ids = new List&amp;lt;ObjectId&amp;gt;();

    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;
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Sep 2014 13:57:25 GMT</pubDate>
    <dc:creator>norman.yuan</dc:creator>
    <dc:date>2014-09-16T13:57:25Z</dc:date>
    <item>
      <title>Select all text object with a particular color</title>
      <link>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5276443#M42692</link>
      <description>&lt;P&gt;Hi, I need to select all text objects in model space but only those who have a particular string value in Xdata attached. I can get a selectionset and filter out those without xdata but how can I get those with xdata that match the criteria to show seleccted on screen (or at least highlighted on screen)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for any suggestion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;emiz&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 05:01:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5276443#M42692</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-09-16T05:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Select all text object with a particular color</title>
      <link>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5277425#M42693</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is some quick code (not tested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;[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&amp;lt;ObjectId&amp;gt; ids = new List&amp;lt;ObjectId&amp;gt;();

    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;
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 13:57:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5277425#M42693</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-09-16T13:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Select all text object with a particular color</title>
      <link>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5293519#M42694</link>
      <description>&lt;P&gt;Hi Norman, thank you very much, your solution helped me solved the problem. I went even further, for other type of xdata, i wanted to delete the text but to have them selected before deletion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've used Autodesk.AutoCAD.Internal.Utils.SelectObjects(ids) to select the object ids, then pressing delete to delete them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;emiz&lt;/P&gt;</description>
      <pubDate>Tue, 23 Sep 2014 19:14:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5293519#M42694</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-09-23T19:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: Select all text object with a particular color</title>
      <link>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5608778#M42695</link>
      <description>&lt;P&gt;still learning .net - can't remember the exact method, but cannot a SetimpliedSelection or implied selection do the trick: with the appropriate filter and prompt selection options placed in as a parameter if pertinent?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Apr 2015 01:04:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5608778#M42695</guid>
      <dc:creator>BKSpurgeon</dc:creator>
      <dc:date>2015-04-27T01:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select all text object with a particular color</title>
      <link>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5608779#M42696</link>
      <description>&lt;P&gt;Would like to add that in order to use Lists one would have to add, I believe&amp;nbsp;using System.Collections.Generic; - this is for intellisense in visual studio to pick it up.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hope this helps someone.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;rgds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BK&lt;/P&gt;</description>
      <pubDate>Mon, 27 Apr 2015 01:05:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/select-all-text-object-with-a-particular-color/m-p/5608779#M42696</guid>
      <dc:creator>BKSpurgeon</dc:creator>
      <dc:date>2015-04-27T01:05:38Z</dc:date>
    </item>
  </channel>
</rss>

