Jump to content

Width factor of text to fit at a certain distance


Stratos

Recommended Posts

Hello,

 

I have a code that asks the user to give text (text1) and select two points (p1 and p2). If the text length is larger than the x distance between these two points, the code should change the width factor of the text to make it smaller than this distance. Is it possible? Here is my code so far , pretty much nothing since I am stuck...

 

    (setq p1 (getpoint "\nFirst point ")) 
    (setq p2 (getcorner p1 "\nOpposite point "))

    (setq text1 (getstring T "\nEnter text:"))  
 

Link to comment
Share on other sites

So I guess the next stage is to calculate the width of the text in 'standard' for - no new width factor?

 

Something similar came up earlier today to use a bounding box around some text, and here Lee Macs Bounding Box LISP can work (you'll find it with a simple internet search) - if you remove the part that draws the box but get the coordinates of points 1 and 2 - following his example and some thinking.

 

If the text is at an angle you could look to his BT (text Box) LISP which does the same but for texts at an angle. Again points 1 and 2.

 

Should work out the distance from them and also distance of the 2 selected points - and from that can work out if you need to apply a width factor?

 

 

Is that enough to frustrate you but make something up or do you want a more finished code? (Busy in work at the moment to make something up for you)

Edited by Steven P
Link to comment
Share on other sites

How do I apply the width factor I want e.g 0.7?

 

I use this command to write my text:
(setq PT3 (list 0 0 0))

(command "_text" "m" "c" PT3 "1" "0" "lalalalala")

Link to comment
Share on other sites

(command "_.text" "_j" "_m" "_non" (list 0.0 0.0 0.0) "1.0" "0.0" "lalalalala")

(setpropertyvalue (entlast) "WidthFactor" 0.7)

Edited by marko_ribar
  • Like 1
Link to comment
Share on other sites

If you look at 'entmod' to change the text, create your text as above.

 

Example here based on AutoCAD help (select an entity to change), assoc 41 is for width factor

(setq ed (entget (car (entsel))))
(setq ed (subst (cons 41 -NEWWIDTHFACTOR-) (assoc 41 ed) ed ))
(entmod ed)

 

though in your case (using entlast - the last entity)

 

(setq ed (entget (entlast)))
(setq ed (subst (cons 41 -NEWWIDTHFACTOR-) (assoc 41 ed) ed ))
(entmod ed)

 

(though it is slightly more efficient to use entmake to create the whole text including width factor, if it works this way, it works)

 

EDIT

Marko_Ribar is of course correct, however this way can be used on existing texts as the first snippet - so place the text, find bounding box area, alter width using entmod

Edited by Steven P
  • Like 1
Link to comment
Share on other sites

6 hours ago, Steven P said:

Something similar came up earlier today to use a bounding box around some text, and here Lee Macs Bounding Box LISP can work (you'll find it with a simple internet search) - if you remove the part that draws the box but get the coordinates of points 1 and 2 - following his example and some thinking.

 

use textbox command.

  • Like 1
Link to comment
Share on other sites

Thanks - that's a new command for me.

 

 

 

 

Edit:

For my reference later

(textbox (list (assoc 1 (entget (car (entsel))))))

 

Edited by Steven P
Link to comment
Share on other sites

Should work as desired...

 

(defun c:txtby2pts ( / p1 p2 txt txtbox v w h )
  (initget 1)
  (setq p1 (getpoint "\nFirst point : "))
  (initget 1)
  (setq p2 (getcorner p1 "\nSecond point : "))
  (setq txt (getstring t "\nText : "))
  (vl-cmdf "_.text" "_j" "_l" "_non" p1 1.0 0.0 txt)
  (setq txtbox (textbox (list (assoc 1 (entget (entlast))))))
  (setq v (mapcar '- p2 p1))
  (setq w (car v) h (cadr v))
  (vl-cmdf "_.scale" (entlast) "" "_non" p1 h)
  (setpropertyvalue (entlast) "WidthFactor" (/ (/ w h) (- (caadr txtbox) (caar txtbox))))
  (princ)
)

 

HTH.

M.R.

Edited by marko_ribar
  • Like 2
Link to comment
Share on other sites

If you need to write text in UCS/=WCS at some 3D angle, you should firstly align view to UCS with PLAN command... Next step is start and write text with above posted code...

 

Regards,

M.R.

Link to comment
Share on other sites

18 hours ago, Steven P said:

If you look at 'entmod' to change the text, create your text as above.

 

Example here based on AutoCAD help (select an entity to change), assoc 41 is for width factor

(setq ed (entget (car (entsel))))
(setq ed (subst (cons 41 -NEWWIDTHFACTOR-) (assoc 41 ed) ed ))
(entmod ed)

 

though in your case (using entlast - the last entity)

 

(setq ed (entget (entlast)))
(setq ed (subst (cons 41 -NEWWIDTHFACTOR-) (assoc 41 ed) ed ))
(entmod ed)

 

(though it is slightly more efficient to use entmake to create the whole text including width factor, if it works this way, it works)

 

EDIT

Marko_Ribar is of course correct, however this way can be used on existing texts as the first snippet - so place the text, find bounding box area, alter width using entmod

 

This was pure gold. Thank you very much!!!!

  • Like 1
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...