Get closest entity when dragging

Get closest entity when dragging

845179011
Enthusiast Enthusiast
1,666 Views
6 Replies
Message 1 of 7

Get closest entity when dragging

845179011
Enthusiast
Enthusiast

[platform win10,AutoCAD 2018]

I want to implement a dynamic drawing tool.DWG files have many Polylines that represent points. When the drawing tool starts and the mouse moves to the point, the coordinates of the point are prompted (extended data is written to the point).
I referred to an article: https://www.keanw.com/2006/11/two_methods_for.html . Gets the entities in range by the SelectCrossingWindow() function, which works when used in a command function. I write a test program to get the text entity near the picked point:

public static void SelectCrossingEditor()
{
            Editor ed =            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                PromptPointOptions ptOpts =
                  new PromptPointOptions(
                    "\nPick a point at which to select all text entities: "
                  );
                PromptPointResult ptRes =
                  ed.GetPoint(ptOpts);
                if (PromptStatus.OK == ptRes.Status)
                {
                    Point3d p = ptRes.Value;
                    Point3d p1 =
                      new Point3d(p.X - 100, p.Y - 100, 0.0);
                    Point3d p2 =
                      new Point3d(p.X + 100, p.Y + 100, 0.0);
                    TypedValue[] values =
                      {
              new TypedValue(
                (int)DxfCode.Start,
                "TEXT"
              )
            };
                    SelectionFilter filter = new SelectionFilter(values);
                    PromptSelectionResult res =
                      ed.SelectCrossingWindow(p1, p2, filter);
                    if(res.Status== PromptStatus.OK)
                    {
                        SelectionSet ss = res.Value;
                        int n = 0;
                        if (ss != null)
                        {
                            n = ss.Count;
                        }
                        ed.WriteMessage(
                          string.Format(
                            "\n{0} text entit{1} selected.",
                            n, 1 == n ? "y" : "ies"
                          )
                        );
                    }
                }
            }
            catch (System.Exception e)
            {
                ed.WriteMessage("\nException {0}.", e);
            }
        }

But when SelectCrossingWindow() function used in DrawJig's WorldDraw function, it always returns an Error, even if the mouse position is in the text position.

 

How to solve this problem?

0 Likes
Accepted solutions (1)
1,667 Views
6 Replies
Replies (6)
Message 2 of 7

timgrote
Enthusiast
Enthusiast

I don't think you can run a window selection while in a jig.

 

I wrote something similar, but I cycle through a subset of modelspace entities to check for points within a distance to the cursor. Depending on the drawing it can be kind of laggy, so be certain to trim down the subset of entities as much as you can.

0 Likes
Message 3 of 7

845179011
Enthusiast
Enthusiast

Thanks, someone told me that the entity under the cursor can be obtained by "reactor" in c++. I just want to get the entity under the cursor, can it be solved by c#?

0 Likes
Message 4 of 7

_gile
Consultant
Consultant

Hi,

 

Maybe you can get some inspiration from this topic.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 7

845179011
Enthusiast
Enthusiast

Thanks so much. I know the monitor,it works well when idle. But when start a Jip, the tooltip display some point like "center of circle",  or display the  data I read from  Entity, it's irregular. I just want to display the data of Entity.

0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor
Accepted solution

You did not describe how your "jig" works like. However, since you want some custom feature that Entity/DrawJig class does not offer directly (identifying entity when mouse cursor hovers during drag, and showing tooltip style prompt, if I were you, I would create my own jig, using Editor.PointMonitor event and Transient Graphics. That is, in PointMonitor event handler, you can easily identify the entity the mouse cursor hover, then you can collect related data and show custom tooltip; you use Transient Graphics as the ghost image of the "dragged" entity.

 

If you have ever coded PointMonitor event handler and used transient graphics, you should be able to achieve your goal with fairly satisfactory effect.

 

@_gile 's reply provides the link to discussion of showing custom tooltips. I have quite a few articles in my blog on transient graphics being used in custom drag, for example, this one:

https://drive-cad-with-code.blogspot.com/2011/01/creating-move-command-with-net-api-take.html 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

845179011
Enthusiast
Enthusiast
I did it with your help. Thank you very much.
0 Likes