.NET

Reply
Valued Contributor
cnicholas
Posts: 55
Registered: ‎03-26-2007
Message 1 of 5 (204 Views)
Accepted Solution

Filtering entities by name

204 Views, 4 Replies
04-24-2014 06:34 AM

Hi

I've borrowed and modified the filtering code from the online help to create a selection set. Here's the code.

 

        private SelectionSet FilterSelectionSet(string entityTypeName)
        {
            // Create a TypedValue array to define the filter criteria
            TypedValue[] acTypValAr = new TypedValue[1];
            acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, entityTypeName), 0);

            // Assign the filter criteria to a SelectionFilter object
            SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);

            // Select all objects according to the filter in the drawing area
            PromptSelectionResult acSSPrompt;
            acSSPrompt = Active.Editor.SelectAll(acSelFtr);

            // If the prompt status is OK, objects were selected
            if (acSSPrompt.Status == PromptStatus.OK)
            {
                SelectionSet acSSet = acSSPrompt.Value;
                return acSSet;
            }
            else
                return null;
        }

 

I pass in the entityName using code like this

 

SelectionSet ss = FilterSelectionSet(typeof(Polyline).Name);

 

In the case of polylines which have been drawn using AutoCAD, it does not find any entities. After looking around I found the use of LWPOLYLINE so if I now introduce this test in the method it finds the polylines.

 

            if (entityTypeName.ToUpper() == "POLYLINE")
                entityTypeName = "LWPOLYLINE";

 

Is this to be expected?

When would "POLYLINE" need to be left as "POLYLINE"?

Does this occur for any other entities?

 

Craig 

 

*Expert Elite*
dgorsman
Posts: 5,277
Registered: ‎10-12-2006
Message 2 of 5 (185 Views)

Re: Filtering entities by name

04-24-2014 08:54 AM in reply to: cnicholas

AutoCAD has several types of polylines.  Most of them will be lightweight, or LWPOLYLINE objects.  Older files may have the old "heavy" polylines, which have slightly different data.  There may also be 3D polylines as well.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 5 (173 Views)

Re: Filtering entities by name

04-24-2014 10:27 AM in reply to: cnicholas

Try this very basic example, you will see how to detect

polyline type anyway:

        //using System.Text;// for StringBuilder
        [CommandMethod("spt", CommandFlags.UsePickSet)]
        public void testPolySelect()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            StringBuilder sb = new StringBuilder();

            using (DocumentLock docloc = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        // select line, lwpolyline,polyline3d, polyline2d
                        TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "LINE,*POLY*") };
                        SelectionFilter flt = new SelectionFilter(tv);
                        //select on screen
                        PromptSelectionResult selRes = ed.GetSelection(flt);
                        SelectionSet selSet = selRes.Value;
                        ObjectId[] ids = selSet.GetObjectIds();
                        foreach (ObjectId entId in ids)
                        {
                            Line ln = tr.GetObject(entId, OpenMode.ForRead) as Line;
                            if (ln != null) 
                            {
                                sb.AppendLine(string.Format("{0:f3}\t{1:f3}", ln.StartPoint, ln.EndPoint));
                               
                            }
                            Polyline poly = tr.GetObject(entId, OpenMode.ForRead) as Polyline;
                            if (poly != null)
                            {
                                sb.AppendLine(string.Format("{0:f3}\t{1:f3}", poly.StartPoint,poly.EndPoint));
                             
                            }
                            Polyline3d poly3d = tr.GetObject(entId, OpenMode.ForRead) as Polyline3d;
                            if (poly3d != null) 
                            {
                                sb.AppendLine(string.Format("{0:f3}\t{1:f3}", poly3d.StartPoint, poly3d.EndPoint));
                           
                            }
                            Polyline2d poly2d = tr.GetObject(entId, OpenMode.ForRead) as Polyline2d;
                            if (poly2d != null) 
                            {
                                sb.AppendLine(string.Format("{0:f3}\t{1:f3}", poly2d.StartPoint, poly2d.EndPoint));
                            
                            }
                            
                        }
                       
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.StackTrace);
                    }
                    finally
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(sb.ToString());
                    }
                }
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
cnicholas
Posts: 55
Registered: ‎03-26-2007
Message 4 of 5 (152 Views)

Re: Filtering entities by name

04-25-2014 01:21 AM in reply to: cnicholas

Thanks for the hint. I've simply used the wildcard character in the entity name as you have done and it simplifies what I need to do.

 

            if (entityTypeName.ToUpper() == "POLYLINE")
                entityTypeName = "*POLYLINE*";

 

 

Craig

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 5 (137 Views)

Re: Filtering entities by name

04-25-2014 03:38 AM in reply to: cnicholas
You're welcome
Cheers, Craig
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.