Author Topic: Method to Find text elements near a specified point  (Read 2284 times)

0 Members and 1 Guest are viewing this topic.

autogis

  • Guest
Method to Find text elements near a specified point
« on: May 08, 2014, 10:57:43 AM »
Does anyone know of a method to find all text elements near a certain distance from a specified point?

Thanks Before Hand.
« Last Edit: May 08, 2014, 11:00:53 AM by autogis »

BillZndl

  • Guest
Re: Method to Find text elements near a specified point
« Reply #1 on: May 08, 2014, 11:14:12 AM »
Not sure what you are trying to accomplish but an idea from the top of my head
would be to check the text.Position or text.AlignmentPoint or text in the drawing against your test point.
Quick example:
Code: [Select]
BlockTable table = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)transaction.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForRead);

                                    foreach (ObjectId oid in btr)
                                    {
                                        if (oid.ObjectClass.DxfName == "TEXT")
                                        {
                                            DBText dbt = (DBText)transaction.GetObject(oid, OpenMode.ForRead);

                                            if (dbt.Position == testpoint)
                                            {
                                                Do your work here...........
                                            }
                                        }
                                    }

autogis

  • Guest
Re: Method to Find text elements near a specified point
« Reply #2 on: May 08, 2014, 11:27:43 AM »
Basically this is what I am trying to accomplish:  There is a drawing (I cannot modify the drawing) with leaders.  Then once I find those leaders,  there is text at the end of the leader.  I need to find those element(s) (Usually one or two).  (this is the method I am looking for that will find all elements within a certain distance from the point I pass it).   It would be at a certain distance from the leader.  (see attached image)

// Start Transaction
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                    foreach (ObjectId id in modelSpace)
                    {
                        // Pick only Leaders
                        if (id.ObjectClass.DxfName == "LEADER")
                        {
                            // Pick only HOMES
                            Leader leader = (Leader)tr.GetObject(id, OpenMode.ForRead);
                            if (leader.Layer == "HOMES")
                            {

                                // Leader Points (Just use the last point)
                                Point3dCollection pts = new Point3dCollection();
                                leader.GetStretchPoints(pts);       // NOTE: pts will have all the points of the leader

                                // HERE I would call a method
                                // that would get all text elements within a certain distance of:   pts[pts.Count-1]  <<The last point of the leader
                                 

BillZndl

  • Guest
Re: Method to Find text elements near a specified point
« Reply #3 on: May 08, 2014, 01:12:23 PM »
Is the text part of the leaders Annotation?

Sorry, I don't have time right now to do it but
I would look into the Annotation/ObjectID and find a way to read the Mtext contained therein.
There's lots of examples of leader code out there.
Here's one: http://www.acadnetwork.com/index.php?action=profile;area=showposts;u=26


BillZndl

  • Guest
Re: Method to Find text elements near a specified point
« Reply #4 on: May 08, 2014, 01:22:15 PM »
Oh wait!
This seems to work as a quick test:
Code: [Select]
if (oid.ObjectClass.DxfName == "LEADER")
  {
       Leader ldr = (Leader)transaction.GetObject(oid, OpenMode.ForRead);
       ObjectId AnnoId = ldr.Annotation;
           if (!AnnoId.IsNull)
            {
               MText AnnoTxt = (MText)transaction.GetObject(AnnoId, OpenMode.ForRead);
               editor.WriteMessage("Hello world: " + AnnoTxt.Text);
            }

    }
« Last Edit: May 08, 2014, 01:34:29 PM by BillZndl »

autogis

  • Guest
Re: Method to Find text elements near a specified point
« Reply #5 on: May 08, 2014, 01:29:03 PM »
No it is not a leader annotation.  It is a standalone element.  That is the main problem I am having.  :-o  And sometimes it is two separate elements, that is the reason I need to find all the text elements near that specific point.

BillZndl

  • Guest
Re: Method to Find text elements near a specified point
« Reply #6 on: May 08, 2014, 01:44:38 PM »
Ah, ok.
well one improvement I can see right away is that the leader has an end point property,
so that may be easier to use than looping through points.

Then I would try something like I first posted and compare Text alignment or position points with leaderEndpoint,
then collect according to what you think is in the correct position.

There maybe someone else here that has a better way and I don't think you can "select" text with a window crossing but I may be wrong about that.

Sorry I couldn't be of more help.



MickD

  • King Gator
  • Posts: 3657
  • (x-in)->[process]->(y-out) ... simples!
Re: Method to Find text elements near a specified point
« Reply #7 on: May 08, 2014, 07:41:23 PM »
...
There maybe someone else here that has a better way and I don't think you can "select" text with a window crossing but I may be wrong about that.


that's the way I'd do it, use a window selection with a filter for text/mtext (or what ever) that defines your 'selection area' rectangle and for each item within the area do your work.

edit:

here's the basic set up, look up the doc's for more info on filters etc:
PromptSelectionResult res = editor.SelectWindow(btmLeftPoint, topRightPoint, selectionfilter);

hth
Mick.
« Last Edit: May 08, 2014, 07:45:56 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

autogis

  • Guest
Re: Method to Find text elements near a specified point
« Reply #8 on: May 09, 2014, 10:05:37 AM »
Mick, that sounds like a very good idea!   Also, I am trying something I found called the "Haversine Formula" that seems promising:

http://www.stormconsultancy.co.uk/blog/development/code-snippets/the-haversine-formula-in-c-and-sql/

I was able to use it by creating two lists in C#, one of the leader objects and one of the text objects I am looking for.  Then with a for each outside I loop through all the leaders, a foreach inside I loop through the texts (getting the distance of each one in relation to the leader).  Finally with a linq as mentioned in above link, I am able to retrieve the X closest elements that I need.

MickD

  • King Gator
  • Posts: 3657
  • (x-in)->[process]->(y-out) ... simples!
Re: Method to Find text elements near a specified point
« Reply #9 on: May 09, 2014, 07:30:26 PM »
cool, another option may be something like BSP (binary space partitioning) to build a node tree of all points, that way you can query any point at any time and find its closet neighbor very quickly.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Method to Find text elements near a specified point
« Reply #10 on: May 10, 2014, 10:27:24 AM »
Hi,

If you have a big amount of text points, you can, as MickD suggested, use a Point3dTree.
This class provides a method which can be usefull for you: NearestNeighbours which returns a Point3dCollection containing all points in the tree within the specified distance from the input point.

Pseudo code:
  • Iterate through model space
  • Store the found texts in a Dictionary<Point3d, DBText>
  • Store the found leaders in a Dictionary<Point3d, Leader>
  • Build a Point3dTree instance with the texts Dictionary.Keys collection
  • Foreach point in the leaders Dictionary.Keys call the Point3dTree.NearestNeighbours() method
  • Get the DBText entity(s) related to the returned point(s) from the Dictionary<Point3d, DBText
« Last Edit: May 10, 2014, 10:35:16 AM by gile »
Speaking English as a French Frog