Author Topic: Closest text string to a point  (Read 5924 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