Author Topic: How to Highlight list Pline is Square in the drawing?  (Read 182 times)

0 Members and 1 Guest are viewing this topic.

Sieu khung khiep

  • Mosquito
  • Posts: 13
How to Highlight list Pline is Square in the drawing?
« on: November 12, 2019, 04:26:06 AM »
Hi, I have searched for answers in many forums but have not found the answer yet.
This is the code I wrote, however my list pline have not been highlighted as expected

Code - vb.net: [Select]
  1. Dim lstRec As New List (Of Polyline)
  2. Dim Prsel As PromptSelectionResult = ed.GetSelection (New PromptSelectionOptions () With {
  3.           .MessageForAdding = "Select Pline:"})
  4.  
  5.   With cadTrans
  6.                  For Each ID As ObjectId In Prsel.Value.GetObjectIds
  7.                      Dim pli As Polyline = DirectCast (.GetObject (ID, OpenMode.ForRead), Polyline)
  8.                      If isSquare (pli) Then
  9.                          lstRec.Add (pli)
  10.                      End If
  11.                  next
  12. ...
  13. End with
  14.   ed.SetImpliedSelection (lstRec. [Select] (Function (ent) ent.ObjectId) .ToArray ()) 'from gile
  15. ed.regen
  16. ed.UpdateScreen ()
  17. 'Continue code...
  18.  

I want only the pline is Square to be highlighted
Can you give me more instructions?
Thank for all.
« Last Edit: November 12, 2019, 04:51:39 AM by Sieu khung khiep »

Atook

  • Swamp Rat
  • Posts: 942
Re: How to Highlight list Pline is Square in the drawing?
« Reply #1 on: November 12, 2019, 12:05:36 PM »
I can't see anything offhand, you might try Autodesk.AutoCAD.Internal.Utils.SelectObjects() instead of SetImpliedSelection()

Unless you need properties of the rectangles somewhere else, I'd recommend storing the lstRec as a list of ObjectIDs. I've found that storing and passing ObjectIDs is superior to storing and passing Entities.

I don't write VB.NET, but it might look something like this:
Code - vb.net: [Select]
  1. Dim lstRec As New List (Of ObjectId)' <-store ObjectID instead of rectangle
  2. Dim Prsel As PromptSelectionResult = ed.GetSelection (New PromptSelectionOptions () With {
  3.   .MessageForAdding = "Select Pline:"})
  4.  
  5.   With cadTrans
  6.     For Each ID As ObjectId In Prsel.Value.GetObjectIds
  7.         Dim pli As Polyline = DirectCast (.GetObject (ID, OpenMode.ForRead), Polyline)
  8.         If isSquare (pli)
  9.             lstRec.Add (ID)
  10.         End If
  11.     next
  12. ...
  13. End with
  14.   ed.SetImpliedSelection (lstRec.ToArray()) 'from gile
  15. ed.regen
  16. ed.UpdateScreen ()
  17. 'Continue code...

« Last Edit: November 12, 2019, 12:10:21 PM by Atook »

gile

  • Water Moccasin
  • Posts: 2233
  • Marseille, France
Re: How to Highlight list Pline is Square in the drawing?
« Reply #2 on: November 12, 2019, 12:45:57 PM »
Hi,

This should work:

Code - C#: [Select]
  1.             var options = new PromptSelectionOptions(){ MessageForAdding = "\nSelect Polylines" };
  2.             var filter = new SelectionFilter(new[] { new TypedValue(0, "LWPOLYLINE") });
  3.             var selection = ed.GetSelection(options, filter);
  4.             if (selection.Status == PromptStatus.OK)
  5.             {
  6.                 ObjectId[] ids;
  7.                 using (var tr = new OpenCloseTransaction())
  8.                 {
  9.                     ids = selection.Value.GetObjectIds()
  10.                         .Select(id => (Polyline)tr.GetObject(id, OpenMode.ForRead))
  11.                         .Where(pl => IsSquare(pl))
  12.                         .Select(pl => pl.ObjectId)
  13.                         .ToArray();
  14.                     tr.Commit();
  15.                 }
  16.                 ed.SetImpliedSelection(ids);
  17.             }
Speaking English as a French Frog

Sieu khung khiep

  • Mosquito
  • Posts: 13
Re: How to Highlight list Pline is Square in the drawing?
« Reply #3 on: November 13, 2019, 04:22:20 AM »
Unless you need properties of the rectangles somewhere else, I'd recommend storing the lstRec as a list of ObjectIDs. I've found that storing and passing ObjectIDs is superior to storing and passing Entities.
Hi Atook, It actually worked, the code ran a lot faster than before. Thank you for letting me know .

Sieu khung khiep

  • Mosquito
  • Posts: 13
Re: How to Highlight list Pline is Square in the drawing?
« Reply #4 on: November 13, 2019, 04:27:13 AM »
                {    ids = selection.Value.GetObjectIds()
                        .Select(id => (Polyline)tr.GetObject(id, OpenMode.ForRead))
                        .Where(pl => IsSquare(pl))
                        .Select(pl => pl.ObjectId)
                        .ToArray();
                    tr.Commit();
                }
Hi Gile,  :yay!:
Very concise and easy to understand, I learned a little more to code better from you. It worked for me. thank you very much.