Author Topic: TextBox function in AutoLisp  (Read 6914 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
TextBox function in AutoLisp
« on: July 08, 2010, 03:17:28 AM »
Has anyone played with the TextBox function in AutoLisp
to determine the size of text that hasn't yet been drawn.

I have a situation where I want the user to draw a quickleader with dynamically created text that could be ANY length.

I wanted to show the text outline [boxed] to the user so that he can place the text correctly first time, without needing to move it to clear previously drawn features.

The code I have works fine EXCEPT where the text has a newline character in the string ie "\\P"

I'm brain dead and may be missing the obvious ...  :|


Code: [Select]

(defun c:doit ()
  (FOODLES "THIS IS A TEXT STRING THAT COULD BE ANY LENGTH")
  (FOODLES (getstring t "\nPlease enter text."))
  (FOODLES "THIS IS another TEXT STRING\\PTHAT COULD BE ANY LENGTH")
  (princ)
)



Code: [Select]

(defun FOODLES (DescText / _Draw_rect arrowPoint corners boxsize bw bh wpx done woff ll lr ul ur ip gr key data tp)
  ;;
  ;; Codehimbelonga kdub 2010.07.08
  ;;
  (setq arrowPoint (getpoint "\nSelect Leader Arrow Point"))
  (setq corners (textbox (list (cons 1 DescText)
                               (cons 40 (* (getvar "DIMTXT") (getvar "DIMSCALE")))
                               (cons 7 (getvar "DIMTXSTY"))
                         )
                )
        boxsize (mapcar '- (cadr corners) (car corners))
        bw      (car boxsize)
        bh      (cadr boxsize)
        wpx     (car arrowPoint)
        done    nil
  )
  ;;----------------------
  (defun _Draw_rect (/)
    (if (< (car data) wpx)
      (setq woff (- bw))
      (setq woff 0)
    )
    (setq ll (mapcar '+ (list woff 0. 0.) data)
          lr (mapcar '+ (list (+ bw woff) 0. 0.) data)
          ul (mapcar '+ (list woff bh 0.) data)
          ur (mapcar '+ (list (+ bw woff) bh 0.) data)
    )
    (redraw)
    (grdraw arrowPoint data -1)
    (grdraw ll lr -1)
    (grdraw lr ur -1)
    (grdraw ur ul -1)
    (grdraw ul ll -1)
  )
  ;;----------------------
  (prompt "\nSelect Text location")
  (while (not done)
    (setq gr   (grread 't 9 1)
          key  (car gr)
          data (cadr gr)
    )
    (cond ((and (= 5 key) (listp data))
           ;; pointing device and 3Dpoint data
           (_Draw_rect)
          )
          ((and (= 3 key) (listp data))
           ;; Point Selected and 3Dpoint data
           (setq tp data)
           (setq done t)
          )
          ((= 2 key) (setq done t))
    )
  )
  (if tp
    (vl-cmdf "qLEADER"
             arrowPoint
             (mapcar '+ (list 0.0 (* 0.5 (getvar "DIMTXT") (getvar "DIMSCALE")) 0.) tp)
             ""
             "0"                                                      ; width
             DescText                                                 ; text String
             ""
    )
  )
  (redraw)
  (princ)
)

« Last Edit: July 08, 2010, 03:39:50 AM by Kerry Brown »
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: TextBox function in AutoLisp
« Reply #1 on: July 08, 2010, 03:44:00 AM »

If it is necessary I could parse and split the textstring and test each component : then determine the textbox size by extrapolation.

.... but I'll wait for a possible better solution :-)
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.

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: TextBox function in AutoLisp
« Reply #2 on: July 08, 2010, 09:12:14 AM »
Couldn't you just provide a width for the mtext string - the "length" would be automatically adjusted.  I do this with my Dynamic Keynotes routine with mleaders, but prior to that I used a qleader and the user predetermined the box during qleader creation.  I don't have AutoCAD on my laptop so I can't check - sorry.

jb
James Buzbee
Windows 8

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: TextBox function in AutoLisp
« Reply #3 on: July 08, 2010, 09:46:25 AM »
Hi,

As usual, I'm not certain to understand the request, but here's a little routine wich returns the 4 points WCS coordinates of the 'bounding frame' of a Mtext.
It works whatever the mtext justification, rotation, and building frame.

Code: [Select]
;; MTextBoundingFrame (gile)
;; Returns the bounding frame of a Mtext (WCS coordinates)
;;
;; Argument
;; mtext: the mtext ename

(defun MtextBoundingFrame
       (mtext / elst nor ref rot wid hgt jus org lst mat)
  (setq elst (entget mtext)
nor  (cdr (assoc 210 elst))
ref  (trans (cdr (assoc 10 elst)) 0 nor)
rot  (angle '(0 0 0) (trans (cdr (assoc 11 elst)) 0 nor))
wid  (cdr (assoc 42 elst))
hgt  (cdr (assoc 43 elst))
jus  (cdr (assoc 71 elst))
org  (list
       (cond
((member jus '(2 5 8)) (/ wid -2))
((member jus '(3 6 9)) (- wid))
(T 0.0)
       )
       (cond
((member jus '(1 2 3)) (- hgt))
((member jus '(4 5 6)) (/ hgt -2))
(T 0.0)
       )
     )
lst (mapcar
       (function
(lambda (p)
   (mapcar '+ org p)
)
       )
       (list
(list 0. 0.)
(list wid 0.)
(list wid hgt)
(list 0. hgt)
       )
     )
mat  (list (list (cos rot) (- (sin rot)) 0)
   (list (sin rot) (cos rot) 0)
   '(0 0 1)
     )
lst (mapcar
       (function
(lambda (p)
   (trans (mapcar '+ (mxv mat p) (list (car ref) (cadr ref)))
  nor
  0
   )
)
       )
       lst
     )
  )
)

;; Apply a transformation matrix to a vector by Vladimir Nesterovsky
(defun mxv (m v)
  (mapcar (function (lambda (r) (apply '+ (mapcar '* r v)))) m)
)
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: TextBox function in AutoLisp
« Reply #4 on: July 08, 2010, 04:51:34 PM »
Thanks guys

The TEXTBOX function will calculate the bounding box without the text actually being drawn  ie: from the string, size-height and style info.

That is what I want to do.
It works well, except for with multiline text.

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.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: TextBox function in AutoLisp
« Reply #5 on: July 08, 2010, 04:57:28 PM »
I thought TextBox was only for DText anyway  :|

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: TextBox function in AutoLisp
« Reply #6 on: July 08, 2010, 05:08:30 PM »

That's the conclusion I'm arriving at.

I'll look later at the option of building a temporary MText with the same Style and Height as the dimension Style, evaluating the boungingbox, and using that.

Not critical, 'cause the current solution works for single line MText without special formatting.
 
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: TextBox function in AutoLisp
« Reply #7 on: July 08, 2010, 08:58:13 PM »
My text box routines are within this lisp but mtext must be created first.
http://www.theswamp.org/index.php?topic=7003.0
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: TextBox function in AutoLisp
« Reply #8 on: July 08, 2010, 09:23:20 PM »

Thanks for the input Alan.

This piccy shows what I was trying to do.
Just something to assist with the placement of programmatically generated leaders.

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.