明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7274|回复: 25

[提问] 多行文字转单行文字

[复制链接]
发表于 2014-2-9 10:13 | 显示全部楼层 |阅读模式
请问多行文字如何转为单行文字
是否有插件可以使用?谢谢大家了
发表于 2018-2-14 03:31 | 显示全部楼层
Here is an old LISP routine from 1998 that can be used for learning
I hope it helps
~Greg
[code = lisp]
Tip1455.LSP: MMTEXT.LSP Mtext to Text (c) 1998, Jim Houser
(defun C: MTT (/        en ent nent lyr        ipt tsz        txt sty        elv spc        ts pt pty npty
              ptx ptz npt)
  (setq         en (car (entsel "\ nSelect MTEXT to change to regular text"))
        ent (entget en)
        nent (car ent)
  )
  (setq         lyr (assoc 8 ent)
        ipt (assoc 10 ent)
        tsz (assoc 40 ent)
        txt (assoc 1 ent)
        sty (assoc 7 ent)
        elv (assoc 210 ent)
        spc (assoc 67 ent)
  )
  (setq         ts (cdr tsz)
        pt (cdr ipt)
        pty (cadr pt)
        npty (- pty ts)
        ptx (car pt)
        ptz (caddr pt)
        npt (list ptx npty ptz)
        ipt (cons 10 npt)
  )
  (setq         nent
         (list nent
               (cons 0 "TEXT")
               (cons 100 "AcDbEntity")
               spc
               lyr
               (cons 100 "AcDbText")
               ipt
               tsz
               txt
               (cons 50 0.0)
               (cons 41 1.0)
               (cons 51 0.0)
               sty
               (cons 71 0)
               (cons 72 0)
               (cons 11 (list 0.0 0.0 0.0))
               elv
               (cons 73 0)
         )
  )
  (entdel en)
  (entmake nent)
  (princ)
)[/ code]
发表于 2020-10-11 18:25 | 显示全部楼层
;;;;;;取多行文字函数(e---多行文字图元对象名称,调用MtoT e))
(defun MtoT(e / ee str n n1 nn str0 str1 stra)
    (vl-load-com)
    (setq    n nil nn 0 str0 "" str1 "" stra "")
    (setq   ee (entget e))
    (setq  str (cdr(assoc 1 ee)))
    (while
         (setq   n (vl-string-search ";" str))
         (setq str (substr str (+ n 2))) ;;;;;;取格式符以后的字符串,无格式符则忽略
    )
    (setq  str (vl-string-subst "" "}" str)) ;;;;;去掉尾符号,无格式符则忽略
    (setq   nn (strlen str) n1 1 stra "")
     ;;;;;以下循环去除换行符"\P"但保留不属于换行的大写字符P——高手们可将下面的代码简化或优化
    (repeat nn
         (setq str1 "" str0 "")
         (setq str0 (substr str n1 1) n1 (1+ n1))
         (if (and(> n1 2)(<= n1 nn))(setq str1 (substr str (- n1 2) 1)))
      (cond ((and(= str0 "")(/= str1 "\\"))(setq stra (strcat stra str0)))
            ((and(/= str0 "\\")(/= str0 ""))(setq stra (strcat stra str0)))
      )
    )
       stra ;;;;;单行文字全字符串
)  
发表于 2020-10-3 15:21 | 显示全部楼层
  1. ;;;;;;;分解法取多行文字
  2. (defun c:text()
  3.     (setq f (open "C:/aaa.txt" "w"))
  4.     (setq e (ssget "X" '((0 . "MTEXT"))))
  5.     (setq n (sslength e) m 0 strb '())
  6.   (repeat n
  7.     (setq e1 (ssname e m) m (1+ m) d1 nil d2 nil)
  8.     (setq e2 (entget e1))
  9.     (setq  p (cdr(assoc 10 e2)));;;左上角坐标
  10.     (setq d1 (cdr(assoc 42 e2)));;;宽度
  11.     (setq d2 (cdr(assoc 43 e2)));;;高度
  12.     (setq p1 (polar p 4.71238898 d2))
  13.     (setq p2 (polar p 0.0 d1))
  14.     (command  "_.explode" e1)
  15.     (setq  s (ssget "c" p1 p2 '((0 . "TEXT"))))
  16.     (setq nn (sslength s) mm 0 hh '())
  17.     (repeat nn
  18.       (setq s1 (entget(ssname s mm)) mm (1+ mm))
  19.       (setq h1 (cdr(assoc 1 s1)))
  20.       (setq hh (cons (list h1) hh))
  21.     )
  22.        (setq str "")
  23.      (foreach z hh
  24.        (setq str (strcat str (car z)))
  25.      )
  26.        (setq strb (cons (list str) strb))
  27.        (command "_.undo" 1)
  28.   );;;;;;end n
  29.      (foreach z strb
  30.         (setq pp (car z))
  31.         (write-line pp f)
  32.      )
  33.      (close f)
  34. )  

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2014-2-9 10:24 | 显示全部楼层
本帖最后由 自贡黄明儒 于 2014-2-9 10:26 编辑

爆破就可以了,(command "_.explode"
 楼主| 发表于 2014-2-9 10:30 | 显示全部楼层
自贡黄明儒 发表于 2014-2-9 10:24
爆破就可以了,(command "_.explode"

谢谢了
发表于 2014-2-10 07:46 | 显示全部楼层
;----------------------------------
;----------------------------------
;       多行文字转单行文字
;----------------------------------
;----------------------------------
(defun c:xx( / myosmode cmdmode sset ssl nsset temp ssl1 total txt_h txt_p txt_s txt_en txt_tx txt_tx1 txt_tx2 txt_tx3 txt1_l txt1_s txt1_c i_1)
(if *error* quit)
(setq cmdmode (getvar "cmdecho"))  
(setvar "cmdecho" 0)  
(setq myosmode (getvar "osmode"))  
(setvar "osmode" 0)  
(prompt "\n选择需要求和的数据: ")
(setq sset (ssget))
(if (null sset)
(princ "\n错误: 没有选择对象!\n")
;过滤出选中的“text”实体,并报告有多少“text”实体被选中。
(progn
(setq ssl (sslength sset))
(setq nsset (ssadd))
(while (> ssl 0)
(setq temp (ssname sset (setq ssl(1- ssl))))
(if (= (cdr (assoc 0 (entget temp))) "MTEXT")
(ssadd temp nsset)
)
)
(setq ssl (sslength nsset))
(princ "共选择了 ")
(princ ssl)
(princ " 个多行文字.")
;选出所有可转化为数的“TEXT”,并求和。
(setq ssl1 ssl)
(if (= (CDR (ASSOC '3 (TBLSEARCH "STYLE" "多转单"))) nil)
    (command "style" "多转单" "宋体" "" "" "" "" ""))
(while (> ssl 0)
(setq temp (ssname nsset (setq ssl (1- ssl))))
(setq txt_h (cdr (assoc 40 (entget temp))))
(setq txt_p (cdr (assoc 10 (entget temp))))
(setq txt_s (cdr (assoc 71 (entget temp))))
(setq txt_en (/ (* (cdr (assoc 50 (entget temp))) 180) Pi))
(setq txt_tx (cdr (assoc 1 (entget temp))))
(setq txt1_s 1)
(setq txt1_l (strlen txt_tx))
(setq i_1 txt1_l)
(setq txt_tx1 0)
(setq txt_tx2 0)
(setq txt_tx3 0)
(while (> i_1 0)
;如果文字有多行,则不转换
   (if (= (substr txt_tx i_1 1) "P")
       (if (> i_1 1)
           (if (= (substr txt_tx (- i_1 1) 1) "\\")
               (setq txt1_s 0))))

;找到“{”符号位置
   (if (= (substr txt_tx i_1 1) "{")
       (if (> i_1 1)
           (if (/= (substr txt_tx (- i_1 1) 1) "\\")
               (setq txt_tx1 i_1)
               (setq txt1_s 0))
           (setq txt_tx1 i_1)))

;找到“}”符号位置
   (if (= (substr txt_tx i_1 1) "}")
       (if (> i_1 1)
           (if (/= (substr txt_tx (- i_1 1) 1) "\\")
               (setq txt_tx3 i_1)
               (setq txt1_s 0))
           (setq txt_tx3 i_1)))

   (setq i_1 (1- i_1))
)

;找到“{”“}”符号中间的“;”位置
(if (and (> txt_tx1 0) (> txt_tx3 txt_tx1))
   (progn
       (setq i_1 txt_tx3)
       (while (> i_1 txt_tx1)
           (if (= (substr txt_tx i_1 1) ";")
               (setq txt_tx2 i_1))
           (setq i_1 (1- i_1)))
))


(if (= txt1_s 1)
(progn

;形如“{*****}”
(if (and (= txt_tx1 1) (= txt_tx3 txt1_l))
    (setq txt_tx (substr txt_tx (+ txt_tx2 1) (- txt_tx3 txt_tx2 1))))

;形如“{*****}***”
(if (and (= txt_tx1 1) (< txt_tx3 txt1_l))
    (setq txt_tx (strcat (substr txt_tx (+ txt_tx2 1) (- txt_tx3 txt_tx2 1)) (substr txt_tx (+ txt_tx3 1) (- txt1_l txt_tx3)) )))

;形如“***{*****}”
(if (and (> txt_tx1 1) (= txt_tx3 txt1_l))
    (setq txt_tx (strcat (substr txt_tx 1 (- txt_tx1 1)) (substr txt_tx (+ txt_tx2 1) (- txt_tx3 txt_tx2 1)) )))

;形如“***{*****}***”
(if (and (> txt_tx1 1) (< txt_tx3 txt1_l))
    (setq txt_tx (strcat (substr txt_tx 1 (- txt_tx1 1)) (substr txt_tx (+ txt_tx2 1) (- txt_tx3 txt_tx2 1)) (substr txt_tx (+ txt_tx3 1) (- txt1_l txt_tx3)) )))

(entdel temp)
(cond ((= txt_s 1) (command "text" "s" "多转单" "j" "tl" txt_p txt_h txt_en txt_tx))
      ((= txt_s 2) (command "text" "s" "多转单" "j" "tc" txt_p txt_h txt_en txt_tx))
      ((= txt_s 3) (command "text" "s" "多转单" "j" "tr" txt_p txt_h txt_en txt_tx))
      ((= txt_s 4) (command "text" "s" "多转单" "j" "ml" txt_p txt_h txt_en txt_tx))
      ((= txt_s 5) (command "text" "s" "多转单" "j" "mc" txt_p txt_h txt_en txt_tx))
      ((= txt_s 6) (command "text" "s" "多转单" "j" "mr" txt_p txt_h txt_en txt_tx))
      ((= txt_s 7) (command "text" "s" "多转单" "j" "bl" txt_p txt_h txt_en txt_tx))
      ((= txt_s 8) (command "text" "s" "多转单" "j" "bc" txt_p txt_h txt_en txt_tx))
      ((= txt_s 9) (command "text" "s" "多转单" "j" "br" txt_p txt_h txt_en txt_tx)))
))
(setq ssl1 (1- ssl1))
) ) )

(setvar "OSMODE" myosmode)
(setvar "CMDECHO" cmdmode)
(prin1)
)
发表于 2014-2-10 08:26 | 显示全部楼层
lxdz443 发表于 2014-2-10 07:46
;----------------------------------
;----------------------------------
;       多行文字转单行文字 ...

explode不方便?干嘛还整个代码?你确定这样能行?
发表于 2014-2-10 09:11 | 显示全部楼层
很久以前在网上找的画通信杆路里面自带的,就收藏到硬盘里面了,一直没有使用过。
发表于 2014-2-11 10:58 | 显示全部楼层
编程精神可嘉,可是,适用意义并不大。

点评

有无意义决定于是否需要  发表于 2014-9-7 10:37
发表于 2014-9-4 10:14 | 显示全部楼层
自贡黄明儒 发表于 2014-2-9 10:24
爆破就可以了,(command "_.explode"

爆破会有个问题,爆破后的单行文字,中文英文啥的都各自变成一个个单体了
发表于 2014-9-7 10:37 | 显示全部楼层
lxdz443 发表于 2014-2-10 07:46
;----------------------------------
;----------------------------------
;       多行文字转单行文字 ...

谢谢分享,有空测试后收藏
发表于 2016-5-27 09:00 | 显示全部楼层
怎样,我使用没有反应的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-28 21:39 , Processed in 0.276840 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表