.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Select all text object with a particular color

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
emiz
1856 Views, 4 Replies

Select all text object with a particular color

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

 

Thank you very much for any suggestion.

 

emiz

4 REPLIES 4
Message 2 of 5
norman.yuan
in reply to: emiz

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

 

Message 3 of 5
emiz
in reply to: norman.yuan

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.

 

I've used Autodesk.AutoCAD.Internal.Utils.SelectObjects(ids) to select the object ids, then pressing delete to delete them.

 

Thank you very much,

 

emiz

Message 4 of 5
BKSpurgeon
in reply to: emiz

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?

Message 5 of 5
BKSpurgeon
in reply to: emiz

Would like to add that in order to use Lists one would have to add, I believe using System.Collections.Generic; - this is for intellisense in visual studio to pick it up.

 

hope this helps someone.

 

rgds

 

BK

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost