Visual LISP, AutoLISP and General Customization

Reply
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 1 of 16 (285 Views)

TextWidth before Text creation

285 Views, 15 Replies
03-02-2013 05:18 PM

Hi, 

 

I want to know is there any formula/calculation for the width of text object before creation/insertion?

 

at now I used apploximated width size via a simple code:

 

(Defun GetStrWidth (str H W) (fix (*(* H W) (strlen str))))
;;usage: (GetStrWidth "This is a Text." 2.5 0.8)
;;H: TextHeight; W: width factor.

 

 

I know that for the existed text in drawing I can use textbox command, but for the texts that has not been created,

what can I do?

Is there any formula/calculation depands on font name, text height and width factor?

 

Thanks,

 

 

 

*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 2 of 16 (263 Views)

Re: TextWidth before Text creation

03-02-2013 08:59 PM in reply to: aqdam1978

Wow....  It's hard to imagine.  It would require not only the font, height, and width factor, but whatever is doing the calculation would need to know the widths of every possible character, to add them up for the characters used in the text string.  If you were only talking about .SHP/.SHX fonts, maybe something could be made to read the font files and figure all of that out, but as for other font types [TrueType, for the main possibility], I wouldn't have a clue whether it would be possible at all.

Kent Cooper
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 3 of 16 (259 Views)

Re: TextWidth before Text creation

03-02-2013 09:42 PM in reply to: Kent1Cooper

Hi Kent,

 

YES, you are right.

That simple code works fine for MonoSpaced fonts, but for other types of font, NO!

 

In programming languages (like Delphi), there is a "Form" and "Canvas" for project.

we can assign a font to Canvas and it's possible to get width of texts before insertion.

 

I thought maybe there is a solution in AutoLisp like as other visual form-based proframming languages!

 

Thanks,

 

Abbas

 

 

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 4 of 16 (249 Views)

Re: TextWidth before Text creation

03-03-2013 03:59 AM in reply to: aqdam1978

aqdam1978 wrote:

 

 

I know that for the existed text in drawing I can use textbox command, but for the texts that has not been created,

what can I do?

  


Curious what you want to achive here aqdam1978, perhaps there is another way thru textbox function. or is it a general question?

 

*Expert Elite*
hmsilva
Posts: 2,644
Registered: ‎12-17-2004
Message 5 of 16 (240 Views)

Re: TextWidth before Text creation

03-03-2013 06:24 AM in reply to: aqdam1978

aqdam1978 wrote:

Hi,

I want to know is there any formula/calculation for the width of text object before creation/insertion?

...

 

Abbas,

perhaps creating a temporary object, something like

 

(defun test (str TS H W / txt txtobj ll ur llpt urpt brpt Width)
;; usage:(test "This is a Text." "Standard" 2.5 0.8)
;; TS TextStyle; H: TextHeight; W: width factor.
;; returns the text string with or nil
;; if text style doesn't exist...
  (if (tblsearch "style" TS)
    (progn
      (entmake (list
		 '(0 . "TEXT")
		 '(100 . "AcDbEntity")
		 '(8 . "0")
		 '(100 . "AcDbText")
		 '(10 0.0 0.0 0.0)
		 (cons 40 H)
		 '(1 . "This Is A Test")
		 '(50 . 0.0)
		 (cons 41 W)
		 '(51 . 0.0)
		 (cons 7 TS)
		 '(71 . 0)
		 '(72 . 0)
		 '(11 0.0 0.0 0.0)
		 '(210 0.0 0.0 1.0)
		 '(100 . "AcDbText")
		 '(73 . 0)
	       );; list
      );; entmake
      (setq txt (entlast))
      (setq txtobj (vlax-ename->vla-object txt))
      (vla-getboundingbox txtobj 'll 'ur)
      (setq llpt  (vlax-safearray->list ll)
	    urpt  (vlax-safearray->list ur)
	    brpt  (list (car urpt) (cadr llpt) (caddr llpt))
	    Width (distance llpt brpt)
      )
      (entdel txt)
      Width
    );; progn
  );; if
);; test

 

Henrique

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 6 of 16 (235 Views)

Re: TextWidth before Text creation

03-03-2013 06:46 AM in reply to: pbejse

Hi Pbejse, assume that I want to make columns of text like this:
Column1 Column2 ... ColumnN
---------- --------- ..... ---------
---------- --------- ..... ---------
---------- --------- ..... ---------
---------- --------- ..... ---------
---------- --------- ..... ---------
---------- --------- ..... ---------

(setq Column1 (list "str1" "str2" "str3"..... "strN"))
(setq Column2 (list "str1" "str2" "str3"..... "strN"))
.....
(setq ColumnN (list "str1" "str2" "str3"..... "strN"))

I put the fisrt column Texts in drawing via:

 

(setq X 0 Y 100)
(command "text" "s" "romans" "j" "ML" (list X (setq Y (- Y 5))) 0 (nth i Column1))

 

but for the second column:
(setq X (+ X dX) Y 100)
(command "text" "s" "romans" "j" "ML" (list X (setq Y (- Y 5))) 0 (nth i Column2))

 

So, I need dX value, dX is the widest text in Column1
in fact I need to calculate dX value for each column.

 

Thanks,

 

Abbas

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 7 of 16 (217 Views)

Re: TextWidth before Text creation

03-03-2013 07:35 AM in reply to: aqdam1978

I'll have a look-see tomorow Abbas :smileyhappy:

 

 

Mentor
devitg
Posts: 1,692
Registered: ‎03-14-2004
Message 8 of 16 (215 Views)

Re: TextWidth before Text creation

03-03-2013 07:41 AM in reply to: aqdam1978

What about using tables , check for the wide most text , and set column wide as need ?

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 9 of 16 (209 Views)

Re: TextWidth before Text creation

03-03-2013 08:03 AM in reply to: hmsilva

Hi Henrique,

 

Thanks for your code,

I also think there is no way except temporary creation!

 

I updated your code to:

(defun GetTextWidth (str TS H W / txt txtobj ll ur llpt urpt brpt Width fn)
;; usage:(GetTextWidth "This is a Text." "Standard" 2.5 0.8)
;; TS TextStyle; H: TextHeight; W: width factor.
(if (not (tblsearch "style" TS))(progn (setq fn "romans.shx")
	(if (=(setq txt (getstring (strcat "Give the FONT NAME <" fn ">: "))) "") ()(setq fn txt))
	(command "-style" TS fn  2 1 0 "N" "N" "N"))
)
      (entmake (list
		 '(0 . "TEXT")
		 '(100 . "AcDbEntity")
		 '(8 . "0")
		 '(100 . "AcDbText")
		 '(10 0.0 0.0 0.0)
		 (cons 40 H)
		 (cons 1 str)
		 '(50 . 0.0)
		 (cons 41 W)
		 '(51 . 0.0)
		 (cons 7 TS)
		 '(71 . 0)
		 '(72 . 0)
		 '(11 0.0 0.0 0.0)
		 '(210 0.0 0.0 1.0)
		 '(100 . "AcDbText")
		 '(73 . 0)
	       );; list
      );; entmake
      (setq txt (entlast))
      (setq txtobj (vlax-ename->vla-object txt))
      (vla-getboundingbox txtobj 'll 'ur)
      (setq llpt  (vlax-safearray->list ll)
	    urpt  (vlax-safearray->list ur)
	    brpt  (list (car urpt) (cadr llpt) (caddr llpt))
	    Width (distance llpt brpt)
      )
      (entdel txt)
      Width
)

 Thanks,

 

Abbas

 

*Expert Elite*
hmsilva
Posts: 2,644
Registered: ‎12-17-2004
Message 10 of 16 (204 Views)

Re: TextWidth before Text creation

03-03-2013 08:24 AM in reply to: aqdam1978

Abbas,
using

 

(setq lastent (entlast))
(setq ss (ssadd))

;;here your text stuff for Column1
;;(command "text" "s" "romans" "j" "ML" (list X (setq Y (- Y 5))) 0 (nth i Column1))

(while (setq lastent (entnext lastent))
(ssadd lastent ss)
)

 

You get all the new text in the selection set "ss". then, if you use the Kent Cooper's method in

 

"The smallest rectangle enclosing selected set of objects"

 

is easy to calculate "dX" value...

 

hope that helps

Henrique

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community