Author Topic: Closest text string to a point  (Read 5902 times)

0 Members and 1 Guest are viewing this topic.

A_LOTA_NOTA

  • Guest
Closest text string to a point
« on: September 30, 2010, 06:38:07 PM »
How would I get the closest text string to a given point using lisp?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #1 on: September 30, 2010, 07:14:09 PM »

Depends what you mean by closest.

Closest InsertionPoint ?
Closest to the text Bounding box. ?
 if so, what about rotated Text
         and how would you want to evaluate 'close' to a rectangle ?
                closest to the center, closest to the edge ?
                would you need to somehow 'grade' the options ? 
                    (can imagine 2 strings, with equal distances from the center of one to the point and from the start of the other to the point.)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

A_LOTA_NOTA

  • Guest
Re: Closest text string to a point
« Reply #2 on: September 30, 2010, 07:25:54 PM »
I'm wanting to read text strings that are basicly in the same place in every drawing. I woild like to be able to read the string even if it is not exactly in the calulated spot.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #3 on: September 30, 2010, 07:33:23 PM »
So you want to select the text with the insertion point closest to a predetermined location ?

Does the Insertion scale of a border affect the location ?

What do you consider "Not exactly in the correct spot" ?
How far out of position can the text be and still qualify ?



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

A_LOTA_NOTA

  • Guest
Re: Closest text string to a point
« Reply #4 on: September 30, 2010, 07:46:06 PM »
So you want to select the text with the insertion point closest to a predetermined location ?

Yes, that would work. As long as the text is selected so it can be read it would be fine.

Does the Insertion scale of a border affect the location ?

Yes, the scale of the border affects the location.

What do you consider "Not exactly in the correct spot" ?
How far out of position can the text be and still qualify ?

I have seen the text be as far as 1/16" from where I calculated it should be. Most of the time there are no other objects within 1/8" - 1/4" to the text I'm wanting to read. Maybe that much "fuzz" can't be factored in... I'm not sure
« Last Edit: September 30, 2010, 07:52:17 PM by A_LOTA_NOTA »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #5 on: September 30, 2010, 07:53:50 PM »

Is that 1/16" absolute
or 1/16" multiplied by the scale. ?

If the location is say x:4" y:4" at 1:1 scale can we multiply that by DIMSCALE to determine where the text is expexted. ?

Is the entity Text or MText ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

A_LOTA_NOTA

  • Guest
Re: Closest text string to a point
« Reply #6 on: September 30, 2010, 08:03:55 PM »

Is that 1/16" absolute
or 1/16" multiplied by the scale. ?

If the location is say x:4" y:4" at 1:1 scale can we multiply that by DIMSCALE to determine where the text is expexted. ?

Is the entity Text or MText ?


The drafter has placed the text in the same spot in MOST of the drawings. I have calculated that insertion point based off the scale of the boarder. But in some of the drawing the insertion point varies up to a 1/16" from where it is in the other drawings. It is Text, not MText and most of the time it is just a single character.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #7 on: September 30, 2010, 08:07:30 PM »

Sorry,  that doesn't answer the question.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

A_LOTA_NOTA

  • Guest
Re: Closest text string to a point
« Reply #8 on: September 30, 2010, 08:12:30 PM »
I'm not needing help selecting the text if it is at the calculated point. What I'd like to know is if there is a way to select text if it is within say 1/16" of a specified point.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #9 on: September 30, 2010, 08:13:53 PM »


Yes, just a couple of minutes.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #10 on: September 30, 2010, 08:26:28 PM »
This will select Text within a range of a predetermined point ... allowing for a DIMSCALE factor being applied to both the location point  and the proximity distance.

Will return a list of entitys that meet the criteria.

Code: [Select]

(defun doit () ;; variables left global for testing
  (vl-load-com)
  ;; kdub@theSwamp 2010.10.01
  ;; User Defined variables
  ;;  .... values in Metric units.
  (setq location    (list 10. 10. 0.0)
        HalfBoxSize 5
  )
  ;;
  (setq ScaleFactor (getvar "DIMSCALE")
        ScaleMatrix (list ScaleFactor ScaleFactor ScaleFactor)
  )
  ;;
  (setq sc_loc       (mapcar '* ScaleMatrix location)
        sc_halfBox   (mapcar '* ScaleMatrix (list HalfBoxSize HalfBoxSize 0.0))
        selectBox_ll (mapcar '- sc_loc sc_halfBox)
        selectBox_ur (mapcar '+ sc_loc sc_halfBox)
        entList nil
  )
  ;;
  (if (setq ss (ssget "C" selectBox_ll selectBox_ur '((0 . "TEXT"))))
    (setq entList (mapcar 'vlax-ename->vla-object
                          (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
                  )
    )
    ;;
    (alert "Ooooops")
  )
  entList
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

A_LOTA_NOTA

  • Guest
Re: Closest text string to a point
« Reply #11 on: September 30, 2010, 09:17:49 PM »
Thank you for all your help Kerry. I'll give that a try when I get back to work. Thanks again!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #12 on: September 30, 2010, 09:34:18 PM »

Of course, that only returns a list of options to choose from if there are several entities within the nominated proximity.

I'll have a look at finding the closest at lunchtime ... about 1 hour from now.

Should be relatively simple.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #13 on: September 30, 2010, 10:45:21 PM »
Something to play with ...
For my test ( Metric drawing )
Dimscale was 10
Expected Text location was 10,10,0 at 1:1 scale, so actually at 100,100,0 in the test drawing.
Proximity was 5 at 1:1, so actually 50 in the test drawing.
The routine determined that there were 3 text entities within a legal proximity to the nominated point
and highlighted the closest.

Code: [Select]
[color=green];; closest_text_to_nominatedpoint.lsp[/color]
[color=green];; Proof of concept Code : kdub@theSwamp 2010.10.01
;; http://www.theswamp.org/index.php?topic=35119.0
;; Select the closest TEXT to a nominated location
;; within a prescribed proximity.
;; The location and Proximity shall be factored by the Dimscale value.
;; [/color]
 
(defun [color=blue]c:Doit[/color] (/              [color=blue]_Select[/color]        Location       Halfboxsize
               Halfboxsize    Location       Nearest_Obj    Objlist
               Proximitylist  Scalefactor    Scalematrix    Sc_Halfbox
               Sc_Loc         Selectbox_Ll   Selectbox_Ur   Ss
              )
  (vl-load-com)
  [color=green];; CodeHimBelonga kdub@theSwamp 2010.10.01
  ;; User Defined variables
  ;;  .... values in Metric units.
  ;; these variables will be factored by DIMSCALE
  ;;[/color]
  (setq location    (list 10. 10. 0.0)
        HalfBoxSize 5
  )
  [color=green];;--------------------------------------------------------------- [/color]
  (defun [color=blue]_Select[/color] (/ result)
    (setq ScaleFactor (getvar [color=Maroon]"DIMSCALE"[/color])
          ScaleMatrix (list ScaleFactor ScaleFactor ScaleFactor)
    )
    [color=green];;[/color]
    (setq sc_loc       (mapcar '* ScaleMatrix location)
          sc_halfBox   (mapcar '* ScaleMatrix (list HalfBoxSize HalfBoxSize 0.0))
          selectBox_ll (mapcar '- sc_loc sc_halfBox)
          selectBox_ur (mapcar '+ sc_loc sc_halfBox)
    )
    [color=green];;[/color]
    (if (setq ss (ssget [color=Maroon]"C"[/color] selectBox_ll selectBox_ur '((0 . [color=Maroon]"TEXT"[/color]))))
      (setq result (mapcar 'vlax-ename->vla-object
                           (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
                   )
      )
      [color=green];;[/color]
      (alert [color=Maroon]"Ooooops"[/color])
    )
    result
  )
  [color=green];;---------------------------------------------------------------
  ;; Main Entry Point[/color]
  (if (setq objList (_Select))
    (progn (setq ProximityList '())
           (foreach item objList
             (setq ProximityList
                    (append
                      ProximityList
                      (list
                        ([color=blue]Kdub:Vec:Calculatelength[/color]
                          (mapcar '- (vlax-get item 'InsertionPoint) sc_loc)
                        )
                      )
                    )
             )
           )
           (setq nearest_Obj
                  (nth (vl-position (apply 'MIN ProximityList) ProximityList)
                       objList
                  )
           )
    )
    (setq nearest_Obj nil)
  )
  (if nearest_Obj
    (progn (vla-Highlight nearest_Obj :vlax-true)
           (prompt [color=Maroon]"\nText is Highlighted"[/color])
    )
    (prompt [color=Maroon]"\nNo Text at nominated location"[/color])
  )
  (princ)
)
 
 
 
[color=green];;;-------------------------------------------------------------
;;;-------------------------------------------------------------
;;
;;; -- Function Kdub:Vec:Calculatelength
;;; Calculates length of a vector.
;;; Arguments [Type]:
;;;   Vec = Vector [LIST]
;;; Return [Type]:
;;;   > Length of the vector [REAL]
;;; Notes:
;;;   None
;;[/color]
(defun [color=blue]Kdub:Vec:Calculatelength[/color] (Vector)
  (car ([color=blue]kdub:vec:zeroaxisfuzz[/color]
         (list (sqrt (apply '+ (mapcar '(lambda (a) (* a a)) Vector))))
       )
  )
)
[color=green];;
;;;-------------------------------------------------------------     
;;;-------------------------------------------------------------
;;
;;; -- Function Kdub:Vec:Zeroaxisfuzz
;;; Recursively eliminates floating point precision problems when values are
;;; aproximately 0.
;;; Arguments [Type]:
;;;   Lst = List or nestedlList of floating point numbers [LIST]
;;; Return [Type]:
;;;   > Corrected list [LIST]
;;; Notes:
;;;   None
;;[/color]
(defun [color=blue]Kdub:Vec:Zeroaxisfuzz[/color] (VectorList)
  (mapcar '(lambda (a)
             (if (= 'list (type a))
               ([color=blue]kdub:vec:zeroaxisfuzz[/color] a)
               (if (and (> a (- 1E-13)) (< a 1E-13))
                 0.0
                 a
               )
             )
           )
          VectorList
  )
)
[color=green];;
;;;-------------------------------------------------------------
;;;-------------------------------------------------------------[/color]
(princ)
 
 
 
« Last Edit: January 02, 2011, 06:23:52 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Closest text string to a point
« Reply #14 on: October 01, 2010, 12:15:29 AM »
Just for kicks, what about grabbing the object closest to the picked point? I hope I'm not stepping on your toes, Kerry; I just wanted to throw another thought/direction out there.

Code: [Select]
(defun _selectClosestTextToPoint (pt / ss)
  (if (and (vl-consp pt)
           (setq ss (ssget "_X"
                           (list '(0 . "TEXT")
                                 (cons 410
                                       (if (eq 1 (getvar 'cvport))
                                         (getvar 'ctab)
                                         "Model"
                                       )
                                 )
                           )
                    )
           )
      )
    (caar (vl-sort
            ((lambda (i / e l)
               (while (setq e (ssname ss (setq i (1+ i))))
                 (setq l (cons (cons e (cdr (assoc 10 (entget e)))) l))
               )
             )
              -1
            )
            (function
              (lambda (a b)
                (< (distance (trans pt 1 (car a)) (cdr a)) (distance (trans pt 1 (car b)) (cdr b)))
              )
            )
          )
    )
  )
)

eg.
Code: [Select]
(vla-put-color (vlax-ename->vla-object (_selectClosestTextToPoint (getpoint "\nSpecify point: "))) 3)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #15 on: October 01, 2010, 12:48:08 AM »

Should work fine Alan.
Probably at a severe disadvantage time-wise due to iterating the complete database.
.. also, it won't satisfy the OP's requirement regarding being at the "Correct" location

... but heh, it's Friday afternoon so I'd buy it  :-)

ps : that's a significant sort  statement sir ... very busy !
 I like it
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Closest text string to a point
« Reply #16 on: October 01, 2010, 08:57:12 AM »
I don't know why but I got the sense that this was headed to a ObjectDBX application. :?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Closest text string to a point
« Reply #17 on: October 01, 2010, 03:22:21 PM »


Perhaps, Alan's sneaky like that  :-D

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

johnson

  • Guest
Re: Closest text string to a point
« Reply #18 on: March 14, 2011, 10:48:32 AM »
Just for kicks, what about grabbing the object closest to the picked point? I hope I'm not stepping on your toes, Kerry; I just wanted to throw another thought/direction out there.

Code: [Select]
(defun _selectClosestTextToPoint (pt / ss)
  (if (and (vl-consp pt)
           (setq ss (ssget "_X"
                           (list '(0 . "TEXT")
                                 (cons 410
                                       (if (eq 1 (getvar 'cvport))
                                         (getvar 'ctab)
                                         "Model"
                                       )
                                 )
                           )
                    )
           )
      )
    (caar (vl-sort
            ((lambda (i / e l)
               (while (setq e (ssname ss (setq i (1+ i))))
                 (setq l (cons (cons e (cdr (assoc 10 (entget e)))) l))
               )
             )
              -1
            )
            (function
              (lambda (a b)
                (< (distance (trans pt 1 (car a)) (cdr a)) (distance (trans pt 1 (car b)) (cdr b)))
              )
            )
          )
    )
  )
)

eg.
Code: [Select]
(vla-put-color (vlax-ename->vla-object (_selectClosestTextToPoint (getpoint "\nSpecify point: "))) 3)


how to find the text using contents with highlighting method except qselect method.i want highlight how many my given contents text availble in selected area

hmspe

  • Bull Frog
  • Posts: 362
Re: Closest text string to a point
« Reply #19 on: March 14, 2011, 11:35:20 AM »
What I've used is

Code: [Select]
  (defun getss (apnt adist)
    (ssget "c"
      (list (- (car apnt) adist) (- (cadr apnt) adist))
      (list (+ (car apnt) adist) (+ (cadr apnt) adist))
    )
  )

apnt is the assumed location point.  adist is 1/2 the size of the search box.  Depending on what I'm trying to do I'll set adist to a reasonable size then test the entities returned, or I'll set up a loop that starts with a small value for adist and increments until the correct type entity is found.
"Science is the belief in the ignorance of experts." - Richard Feynman

johnson

  • Guest
Re: Closest text string to a point
« Reply #20 on: March 17, 2011, 10:45:06 AM »
hmspe i have tried with cad2007..but no response

Brick_top

  • Guest
Re: Closest text string to a point
« Reply #21 on: March 17, 2011, 11:11:47 AM »
Curiously enought I made this a couple of days ago

Not exactly what you want but might help.

This will change the Z of a point to the value of the closest text string.
sorry, It wont change. It will create a new point

Code: [Select]
;mudar z de pontos para o valor do texto mais próximo
;

(defun c:tzp (/ polst txtlstf dstf)

  (setq wd (getpoint "\nSeleccionar janela à volta dos objectos a calcular:")
wdc (getcorner wd)
        tss (ssget "_W" wd wdc '((0 . "TEXT")(8 . "T_Z")))[color=red];specific layer name[/color]
        poss (ssget "_W" wd wdc '((0 . "POINT")(8 . "T_PONTOS"))) [color=red];specific layer name[/color]
num 0
num1 0
num2 0
  );setq
  (repeat (sslength poss)
    (setq ent (entget (ssname poss num))
  poip (cdr (assoc 10 ent))
  polst (append (list poip) polst) ;lista dos pontos sem z
  num (+ 1 num)
     );setq
    );repeat
  (repeat (sslength tss)
    (setq txtent (entget (ssname tss num1))
  txtval (atof (cdr (assoc 1 txtent)))
  txtcoord (cdr (assoc 10 txtent))
  txtlst (list txtcoord (list txtval))
  txtlstf (append (list txtlst) txtlstf)
  num1 (+ 1 num1)
    );setq
   );repeat
  (repeat (length txtlstf)
    (setq txtcoordt (nth num2 txtlstf)
  dlst (mapcar '(lambda (x) (list x (list (distance (car txtcoordt) x)) (cadr txtcoordt))) polst)
  dlstmin (vl-sort dlst '(lambda (d1 d2) (< (car (cadr d1))(car (cadr d2)))))
  dstf (append (list (car dlstmin)) dstf)
  num2 (+ 1 num2)
    );setq
  );repeat
  (setq ptf (mapcar '(lambda (x)(entmake (list (cons 0 "POINT")
       (cons 8 "00_Pontos_Z")
                                               (cons 10 (list (car (car x))(cadr (car x))(* (+ (caddr (car x)) 1) (car (caddr x)))))))) dstf)
  );setq

 );defun
 

hmspe

  • Bull Frog
  • Posts: 362
Re: Closest text string to a point
« Reply #22 on: March 17, 2011, 12:14:57 PM »
hmspe i have tried with cad2007..but no response

Here's example code.  There is no error checking.  It changes OSMODE to 0.  It relies on having DIMSCALE set.

Code: [Select]
[font=courier](defun c:test ( / scale point sse counter string)

  (defun dxf (1code 1ent) (cdr (assoc 1code 1ent)))  ; returns the value assigned to a dxf code

  (defun getss (apnt adist)                          ; gets a selection set using a bounding box
                                                     ; based on the apnt point (x,y) passed, box
                                                     ; bounded by (x-adist,y-adist),(x+adist,y+adist)
    (ssget "c"
      (list (- (car apnt) adist) (- (cadr apnt) adist))
      (list (+ (car apnt) adist) (+ (cadr apnt) adist))
    )
  )

  (setq scale (getvar "dimscale"))
  (setq point (getpoint "\nSelect existing entry or insertion point ... "))
  (setvar "OSMODE" 0)                                ; turn off osnaps
  (setq string nil)
  (setq counter 1)
  (while (and (= string nil)
              (< counter 100)
         )
    (if (setq sse (getss point (* scale 0.03 counter)))
                                                     ; Get the entity at the insertion point.
      (progn
        (setq ent (entget (ssname sse 0)))           ; get the entity
        (if (= (dxf 0 ent) "TEXT")
          (setq string (dxf 1 ent))
        )
      )
    )
    (setq counter (1+ counter))
  )
  (print string)
)
[/font]
"Science is the belief in the ignorance of experts." - Richard Feynman