I'm have a routine which reads text lines out of a text file into a list
of text strings. Example '("string 1" "string 2" "string 3")
I wanted a way to convert the list to mText. Because the lines of text from
the file are already formatted I decided to use mtext with Width set to zero.
This way all you have to do is add the \P at the end of each line.
The difficult part for me was dealing with the case where the text could
exceed 250 characters. I think I've solved that.
Questions:
Are there other ways to convert the list to mtext?
Are there any improvements to be had for this routine?
CAB
;;;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
;;;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
;;; Txt_List->Mtext.lsp
;;;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
;;;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
;;;
;;; convert a list of strings to a mtext entity
;;; make the entity using current layer & text
;;; style and with insert point of (0,0)
;;; mtext is NoWrap with \P paragraph code at
;;; the end of each line
;;;
;;; ARGUMENTS : list of strings one for each line
;;; ("first line" "second line" third line")
;;;
;;; RETURNS : <EntName> if successful else nil
;;;
;; MText entity format note
;; Group Code Description
;; 1 Text string. If the text string is less than 250 characters,
;; all characters appear in group 1. If the text string is
;; greater than 250 characters, the string is divided
;; into 250 character chunks, which appear in one or more
;; group 3 codes. If group 3 codes are used, the last group
;; is a group 1 and has fewer than 250 characters.
;;
;; 3 Additional text (always in 250 character chunks) (optional)
(defun txt_list->mtext (txtlist / mtxtlist tmptxt entList tmpstr ptr itm)
(setq mtxtlist '()
tmptxt ""
entlist (list
'(0 . "MTEXT")
'(100 . "AcDbEntity")
'(100 . "AcDbMText")
(cons 10 (list 0 0 0)) ; insert point
(cons 7 (getvar "TextStyle")) ; Current Style
(cons 40 (getvar "TextSize")) ; Current height
(cons 41 0) ; 0 Width = no wrap
)
); end setq
;;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;; Create a list of the text formatted for mtext entity
(foreach itm txtlist
(if (> (strlen (strcat tmptxt itm)) 248)
;; use 248 because we are adding \P
(progn ; break text into 250 char groups
;; pointer to number of chrs needed to make 250
(setq ptr (- 250 (strlen tmptxt)))
;; characters to be added to group
(setq tmpstr (strcat tmptxt (substr itm 1 ptr)))
;; add the group to the list of groups w/ code 3
(setq mtxtlist (cons (cons 3 tmpstr) mtxtlist))
;; characters that did not make it into the group
(setq tmptxt (strcat (substr itm (1+ ptr)) "\\P"))
) ; end progn
;; New line, add paragraph code, txt 2 small for group
(setq tmptxt (strcat tmptxt itm "\\P"))
) ; endif
) ; end foreach Done
;; Add the remaining text to the list of groups w/ code 1
(setq mtxtlist (reverse (cons (cons 1 tmptxt) mtxtlist)))
;; Combine entity list with text list
(setq entlist (append entlist mtxtlist))
;; make mtext & return name if ok, else return nil
(if (entmake entlist) (entlast))
) ; end defun
;;;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
;;;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
(defun c:test (/ str)
(setq str (list
"Line 1 123456789012345678901234567890123456789 L01"
"Line 2 123456789012345678901234567890123456789 L02"
"Line 3 123456789012345678901234567890123456789 L03"
"Line 4 123456789012345678901234567890123456789 L04"
"Line 5 123456789012345678901234567890123456789 L05"
"Line 6 123456789012345678901234567890123456789 L06"
"Line 7 123456789012345678901234567890123456789 L07"
"Line 8 123456789012345678901234567890123456789 L08")
)
(txt_list->mtext str)
(princ)
)