Jump to content

Find Nearest Text/Mtext from Specified point


m4rdy

Recommended Posts

Hi all,

How can we find nearest specified Text/Mtext object from specified point?

For example: I want to find nearest TB2A from point A.

Thank you.

 

mardi

 

Nearest_Text.jpg

Link to comment
Share on other sites

Quickly written:

 

(defun NearestTextFromPoint ( pt str / d e i l m r s )
   (if
       (setq s
           (ssget "_X"
               (list
                  '(0 . "TEXT,MTEXT")
                   (cons 1 str)
                   (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
               )
           )
       )
       (repeat (setq i (sslength s))
           (setq e (ssname s (setq i (1- i)))
                 l (entget e)
           )
           (if
               (<
                   (setq d
                       (distance pt
                           (if
                               (or (eq "MTEXT" (cdr (assoc 0 l)))
                                   (and
                                       (zerop (cdr (assoc 72 l)))
                                       (zerop (cdr (assoc 73 l)))
                                   )
                               )
                               (cdr (assoc 10 l))
                               (cdr (assoc 11 l))
                           )
                       )
                   )
                   (cond (m) ((1+ d)))
               )
               (setq m d r e) r
           )
       )
   )
)

(defun c:test ( / s p )
   (setq s (getstring t "\nString: "))
   (if (setq p (getpoint "\nPoint: "))
       (sssetfirst nil (ssadd (NearestTextFromPoint (trans p 1 0) s)))
   )
   (princ)
)

Link to comment
Share on other sites

  • 3 years later...

sorry for replying to an old thread. I need this too, but after selecting a point, got this error: "Point: ; error: bad argument type: lentityp nil"

Link to comment
Share on other sites

sorry for replying to an old thread. I need this too, but after selecting a point, got this error: "Point: ; error: bad argument type: lentityp nil"

 

Change:

(defun c:test ( / s p )
   (setq s (getstring t "\nString: "))
   (if (setq p (getpoint "\nPoint: "))
       (sssetfirst nil (ssadd (NearestTextFromPoint (trans p 1 0) s)))
   )
   (princ)
)

to:

(defun c:test ( / e s p )
   (setq s (getstring t "\nString: "))
   (if (setq p (getpoint "\nPoint: "))
       (if (setq e (NearestTextFromPoint (trans p 1 0) s))
           (sssetfirst nil (ssadd e))
           (princ (strcat "\nNo text/mtext with content \"" s "\" found in the current layout."))
       )
   )
   (princ)
)

  • Thanks 1
Link to comment
Share on other sites

thanks Lee, that works great! um, could you please modify it so that it finds any nearest text? edit: never mind, it works with wildcard.

Edited by orange
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...