Use copy of your mtext entities. First you have to remove all unwanted content of the text string using stripmtext (in attachment).
Then run my code to output text to csv. Pick text in desired order.
(defun c:text_out( / *error* ss file1 f text_ent text )
(defun *error* ()
(close file1)
)
(setq f (getfiled "exit file:" (getvar "dwgprefix") "csv" 3))
(setq file1 (open f "w"))
(setq ss (ssget '((0 . "*text"))) i -1)
(repeat (sslength ss)
(setq text_ent (entget(ssname ss (setq i (1+ i)))))
(setq text (cdr (assoc 1 text_ent)))
(write-line text file1)
)
(close file1)
(princ)
)
Miljenko Hatlak
Thank you very much!
Can i get the value from left to right?
@skchui6159 Here you have it!
(defun c:text_out( / *error* ss file1 f text_ent sort_list)
(defun *error* ()
(close file1)
(princ)
)
(setq f (getfiled "exit file:" (getvar "dwgprefix") "csv" 3))
(setq file1 (open f "w"))
(setq ss (ssget '((0 . "*text"))) i -1)
(repeat (sslength ss)
(setq text_ent (entget(ssname ss (setq i (1+ i)))))
(setq sort_list (cons (list (car(cdr(assoc 10 text_ent)))(cdr(assoc 1 text_ent))) sort_list))
)
(setq sort_list (vl-sort sort_list '(lambda (x y)(<(car x)(car y)))))
(setq sort_list (mapcar 'cadr sort_list) i -1)
(repeat (length sort_list)
(write-line (nth (setq i (1+ i)) sort_list) file1)
)
(close file1)
(princ "\nDone!")
(princ)
)
Miljenko Hatlak
Thanks for helping!!!
This is direct to excel version 1 it needs the text strings to have the mtext formatting removed. The \P needs to be recognised as a line break in Excel so will shows as 2 lines in the cell, possibly the size also set to 1/2 size.
This is now version 2 code updated.
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/can-i-get-the-mtext-value-from-left-to-right-to-excel-using-lisp/td-p/13087327
; Original code provided by Hak_vz Oct 2024
; write to Excel rather than csv modified by AlanH 2024
; Do not have Excel open ! Code will open Excel.
(defun c:text_out( / *error* ss file1 f text_ent txt_ent txt_pt sort_list)
(defun *error* ()
(Alert "an error has occured")
(princ)
)
;; Thanks to fixo ;;
;; = Set Excel cell text = ;;
;; ;;
(defun xlsetcelltext ( row column text)
(setq cells (vlax-get-property (vlax-get-property myxl "ActiveSheet") "Cells"))
(vl-catch-all-apply
'vlax-put-property
(list cells 'Item row column
(vlax-make-variant (vl-princ-to-string text) vlax-vbstring)))
)
(or (setq myxl (vlax-get-object "Excel.Application"))
(setq myxl (vlax-get-or-create-object "excel.Application"))
)
(vla-put-visible myXL :vlax-true)
(vlax-put-property myxl 'ScreenUpdating :vlax-true)
(vlax-put-property myXL 'DisplayAlerts :vlax-true)
(vlax-invoke-method (vlax-get-property myXL 'WorkBooks) 'Add) ; opens a new xl
(setq row 1)
(setq ss (ssget '((0 . "*text"))))
(setq i -1)
(setq sort_list '())
(repeat (sslength ss)
(setq text_ent (ssname ss (setq i (1+ i))))
(setq txt_ent (getpropertyvalue text_ent "Text"))
(setq txt_ent (vl-string-subst "" "\r" Txt_ent))
(setq txt_ent (vl-string-subst "" "\r" Txt_ent))
(setq txt_pt (car (cdr (assoc 10 (entget text_ent)))))
(setq sort_list (cons (list txt_pt txt_ent ) sort_list))
)
(setq sort_list (vl-sort sort_list '(lambda (x y)(<(car x)(car y)))))
(setq sort_list (mapcar 'cadr sort_list) i -1)
(repeat (length sort_list)
(xlsetcelltext (setq row (1+ row)) 1 (nth (setq i (1+ i)) sort_list))
)
(if (not (vlax-object-released-p myXL))(progn(vlax-release-object myXL)(setq myXL nil)))
(Alert "\nDone! \nCheck excel.")
(princ)
)
(c:text_out)
Very good! Many Thanks, The values not be lost as /P of the M text!