Author Topic: How to select Lines and DBTexts only?  (Read 3304 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
How to select Lines and DBTexts only?
« on: December 21, 2012, 09:38:31 AM »
Hi.
I think there are two text classes: the MText class and the DBText class, right? I want to select the Lines and DBTexts, I don't want to select MText. How to exclude others. Here is what I have now:
Code: [Select]
public bool select()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    //How to define a Filter to select Line and DBText only?
                    TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "LINE") };

                    SelectionFilter flt = new SelectionFilter(tv);

                    PromptSelectionOptions optSel = new PromptSelectionOptions();
                    optSel.MessageForAdding = "Drag the cursor to select";
                    PromptSelectionResult resSel = ed.GetSelection(optSel, flt);

                    if (resSel.Status != PromptStatus.OK)
                    {
                        return false;
                    }

                    SelectionSet selSet = resSel.Value;
                    ObjectId[] ids = selSet.GetObjectIds();     
             
                    //I want to collect the Strings of the DBTexts, so I define a List<> to do it
                    List<string> TextLst = new List<string>();

                    foreach (ObjectId sSetEntId in ids)
                    {
                        Entity ent = (Entity)trans.GetObject(sSetEntId, OpenMode.ForRead);

                        //How to tell the Lines and DBTexts apart?
                        if (ent.GetType().Name == "Line")
                        {
                            //This means the loop entity is a line?
                        }
                        else if (ent.GetType().Name == "DBText")
                        {
                            //This means the loop entity is a single line Text?

                            DBText myText = (DBText)trans.GetObject(sSetEntId, OpenMode.ForRead);

                            string textStr = myText.TextString;     //get the String
                            TextLst.Add(textStr);       //collect teh string
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                    return false;
                }

                trans.Commit();
               
                return true;
            }
        }
How to fix the filter? Is my solution good? If not, are there any better solutions?

BlackBox

  • King Gator
  • Posts: 3770
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to select Lines and DBTexts only?
« Reply #2 on: December 21, 2012, 12:13:24 PM »
Hi,

You can have more than one entity type in the same TypedValue:
Code - C#: [Select]
  1. SelectionFilter flt = new SelectionFilter(new TypedValue[] { new TypedValue(0, "TEXT,LINE") });
Speaking English as a French Frog

TheMaster

  • Guest
Re: How to select Lines and DBTexts only?
« Reply #3 on: December 22, 2012, 06:04:43 AM »
Add Complexity to Your Filter List Conditions

Some of those examples are downright horrifying. No doubt, a manager with no real programming experience must have been responsible for allowing them to get into the online docs.

No one uses Array.SetValue() to assign elements to a strongly-typed array.


Keith Brown

  • Swamp Rat
  • Posts: 601
Re: How to select Lines and DBTexts only?
« Reply #4 on: December 22, 2012, 02:07:44 PM »
Hi Tony,

Could you provide a snippet of code on the correct way to assign an element to a strongly-typed array?  The examples I have seen have used array.setvalue() and I would rather learn the correct way to do it instead of a method that is considered incorrect.  Thank you.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to select Lines and DBTexts only?
« Reply #5 on: December 22, 2012, 05:37:07 PM »
Hi Keith,

you can simply use the array indexor:
Code - C#: [Select]
  1. TypedValue[] acTypValAr = new TypedValue[3];
  2. acTypValAr[0] = new TypedValue(0, "CIRCLE");
  3. acTypValAr[1] = new TypedValue(-4, ">=");
  4. acTypValAr[2] = new TypedValue(40, 5);
or directlly fill the array:
Code - C#: [Select]
  1. TypedValue[] acTypValAr =
  2. {
  3.     new TypedValue(0, "CIRCLE"),
  4.     new TypedValue(-4, ">="),
  5.     new TypedValue(40, 5)
  6. };
Speaking English as a French Frog