Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Select polyline in point

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
2185 Views, 2 Replies

Select polyline in point

Hi. I have a folowwing problem:

I have a lot of polylines. Some polylines are in one point and I want to select these then delete selected objects.

Example point:

Dim c As Double = 359.1275
        Dim point As New Point2d(Val(c), Val(c))

How I would like to select these polyline:

        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        Using acLckDoc As DocumentLock = acDoc.LockDocument
            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
                Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection(""Select all In point"")
                If acSSPrompt.Status = PromptStatus.OK Then
                    Dim acSSet As SelectionSet = acSSPrompt.Value
                    For Each acSSObj As SelectedObject In acSSet
                        If Not IsDBNull(acSSObj) Then
                            Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)
                            If Not IsDBNull(acEnt) Then
                                acEnt.Erase()
                            End If
                        End If
                    Next
                    acTrans.Commit()
                End If
            End Using
        End Using

but in the "" "" i wrote what i want to select but i don't know how to do it.

 

Then I would like to choose everything that has not been erase and join it

Tags (1)
2 REPLIES 2
Message 2 of 3
_gile
in reply to: Anonymous

Hi,

 

If you want to select the polylines crossing a previouly known point without user interaction, you can use the Editor.SelectCrossingWindow passing it the point twice (or two very closed points).

 

        [CommandMethod("TEST1")]
        public static void Test1()
        {
            var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;

            // selection filter to select only Polyline
            var filter = new SelectionFilter(new[] { new TypedValue(0, "LWPOLYLINE") });

            var point = new Point3d(359.1275, 359.1275, 0.0);
            var result = ed.SelectCrossingWindow(point, point, filter);

            // highlight the result
            if (result.Status == PromptStatus.OK)
                ed.SetImpliedSelection(result.Value);
        }

If you want the user to pick a point and select all polylines within the cusor aperture, you can set some PromptSelectionOptions (this also requires to deactivate the selection cycling)

 

        [CommandMethod("TEST2")]
        public static void Test2()
        {
            var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;

            // selection filter to select only Polyline
            var filter = new SelectionFilter(new[] { new TypedValue(0, "LWPOLYLINE") }); ;

            // selection options to select everything in aperture by single pick
            var options = new PromptSelectionOptions();
            options.MessageForAdding = "\nSelect Polylines by point: ";
            options.SelectEverythingInAperture = true;
            options.SingleOnly = true;

            // get the current SELECTIONCYCLING and deactivate it
            var selectionCycling = Application.GetSystemVariable("SELECTIONCYCLING");
            Application.SetSystemVariable("SELECTIONCYCLING", 0);

            try
            {
                // prompt the user for selection
                var result = ed.GetSelection(options, filter);

                // highlight the result
                if (result.Status == PromptStatus.OK)
                    ed.SetImpliedSelection(result.Value);
            }

            // restore the previous value of SELECTIONCYCLING
            finally { Application.SetSystemVariable("SELECTIONCYCLING", selectionCycling); }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3
_gile
in reply to: _gile

Another way without user interaction could be get all entities ObjectId in current space, filter polylines and check for the distance between the search point and the closest point on polyline to the searche point.

 

        [CommandMethod("Test3")]
        public static void Test3()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var point = new Point3d(359.1275, 359.1275, 0.0);
            var ids = new ObjectIdCollection();
            using (var tr = new OpenCloseTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                foreach (ObjectId id in curSpace)
                {
                    if (id.ObjectClass.DxfName == "LWPOLYLINE")
                    {
                        var pl = (Polyline)tr.GetObject(id, OpenMode.ForRead);
                        if (pl.GetClosestPointTo(point, false).DistanceTo(point) < Tolerance.Global.EqualPoint)
                        {
                            ids.Add(id);
                        }
                    }
                }
                var array = new ObjectId[ids.Count];
                ids.CopyTo(array, 0);
                ed.SetImpliedSelection(array);
                tr.Commit();
            }
        }

This can be written in a more declarative way by using Linq with lambda functions

 

        [CommandMethod("TEST4")]
        public static void Test4()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var point = new Point3d(359.1275, 359.1275, 0.0);
            using (var tr = new OpenCloseTransaction())
            {
                var ids = ((BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))
                    .Cast<ObjectId>()
                    .Where(id => id.ObjectClass.DxfName == "LWPOLYLINE")
                    .Select(id => (Polyline)tr.GetObject(id, OpenMode.ForRead))
                    .Where(pl => pl.GetClosestPointTo(point, false).DistanceTo(point) < Tolerance.Global.EqualPoint)
                    .Select(pl => pl.ObjectId)
                    .ToArray();
                ed.SetImpliedSelection(ids);
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report