明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1002|回复: 7

[源码] [分享]lee-mac的文字按字符打断

[复制链接]
发表于 2021-8-15 00:15 | 显示全部楼层 |阅读模式
本帖最后由 tigcat 于 2021-8-15 00:18 编辑

这个程序可以实现文字按字符打断,但不适合中文字符,比如有个ABC,输入B后,变成了2行字符A和C,A和C的位置不变化.
  1. ;;--------------------=={ Text to Words }==-------------------;;
  2. ;;                                                            ;;
  3. ;;  Prompts the user for a selection of Text objects and      ;;
  4. ;;  converts each object in the selection into separate Text  ;;
  5. ;;  objects for each word, retaining all properties of the    ;;
  6. ;;  original objects.                                         ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Author: Lee Mac, Copyright ?2013 - www.lee-mac.com       ;;
  9. ;;------------------------------------------------------------;;

  10. (defun c:t2w (/        _splitwords     _textwidth      ang     dxf
  11.         ent     enx     fun     inc     lst     pnt     sel
  12.         spc     tot     wid
  13.        )

  14.   (defun _splitwords (str / pos)
  15.     (if  (setq pos (vl-string-position chrx str))
  16.   ;|获取要分割的字符串|;
  17.       (cons (cons 1 (substr str 1 pos))
  18.       ;|组成字首到切割符号前的字符串点对|;
  19.       (_splitwords (substr str (+ pos 2)))
  20.       ;|组成切割字符串后面字符|;
  21.       )
  22.       (list (cons 1 str))
  23.     )
  24.   )

  25.   (defun _textwidth (enx)
  26.     ((lambda (lst) (- (caadr lst) (caar lst))) (textbox enx))
  27.   )

  28.   (setq chrx (ascii (LM:editbox "-")))
  29. ;| (setq chrx (ascii "你"))|;
  30.   (if (setq sel  (ssget
  31.            '((0 . "TEXT")
  32.        (-4 . "<NOT")
  33.        (-4 . "<OR")
  34.        (72 . 3)
  35.        (72 . 4)
  36.        (72 . 5)
  37.        (-4 . "OR>")
  38.        (-4 . "NOT>")
  39.       )
  40.     )
  41.       )
  42.     (repeat (setq inc (sslength sel))
  43.       (setq ent  (ssname sel (setq inc (1- inc)))
  44.       enx  (entget ent)
  45.       tot  0.0
  46.       lst  nil
  47.       )
  48.       (foreach item (_splitwords (cdr (assoc 1 enx)))
  49.   (if (= "" (cdr item))
  50.     (setq lst (cons '(nil . 0.0) lst))
  51.     (setq  dxf (entget (entmakex (subst item (assoc 1 enx) enx)))
  52.         ;|获取新生成字符串的数据表,entmakex函数最后会返回一个图元名,所以用entget获取数据表|;
  53.     wid (_textwidth dxf)
  54.     tot (+ tot wid)
  55.     lst (cons (cons dxf wid) lst)
  56.     )
  57.   )
  58.       )
  59.       (if (< 1 (length lst))
  60.   (progn
  61.     (setq  wid (_textwidth enx)
  62.     spc (/ (- wid tot) (float (1- (length lst))))
  63.     lst (reverse lst)
  64.     ang (cdr (assoc 50 enx))
  65.     )
  66.     (if
  67.       (and
  68.         (= 0 (cdr (assoc 72 enx)))
  69.         (= 0 (cdr (assoc 73 enx)))
  70.       )
  71.        (setq pnt (cdr (assoc 10 enx)))
  72.        (setq pnt (cdr (assoc 11 enx)))
  73.     )
  74.     (cond
  75.       ((= (cdr (assoc 72 enx)) 0)
  76.        (setq fun (lambda (a b) (+ spc (cdr a))))
  77.       )
  78.       ((= (cdr (assoc 72 enx)) 1)
  79.        (setq fun (lambda (a b) (+ spc (/ (+ (cdr a) (cdr b)) 2.0)))
  80.        pnt (polar pnt (+ ang pi) (/ (- wid (cdar lst)) 2.0))
  81.        )
  82.       )
  83.       ((= (cdr (assoc 72 enx)) 2)
  84.        (setq fun (lambda (a b) (+ spc (cdr b)))
  85.        pnt (polar pnt (+ ang pi) (- wid (cdar lst)))
  86.        )
  87.       )
  88.     )
  89.     (mapcar
  90.       (function
  91.         (lambda (a b / dxf)
  92.     (if (setq dxf (car a))
  93.       (entmod
  94.         (subst (cons 10 pnt)
  95.          (assoc 10 dxf)
  96.          (subst (cons 11 pnt) (assoc 11 dxf) dxf)
  97.         )
  98.       )
  99.     )
  100.     (setq pnt (polar pnt ang (fun a b)))
  101.         )
  102.       )
  103.       lst
  104.       (append (cdr lst) '((nil . 0.0)))
  105.     )
  106.     (entdel ent)
  107.   )
  108.       )
  109.     )
  110.   )
  111.   (princ)
  112. )
  113. (princ)
  114. (defun LM:editbox ( str1 / han )
  115.   (and (< 0 (setq han (load_dialog "acad")))
  116.     (new_dialog  "acad_txtedit" han)
  117.     (set_tile    "text_edit"    str1)
  118.     (action_tile "text_edit" "(setq str1 $value)")
  119.     (if (zerop (start_dialog)) (setq str1 nil))
  120.   )
  121.   (if (< 0 han) (unload_dialog han))
  122.   str1
  123. )

"觉得好,就打赏"
    共1人打赏
 楼主| 发表于 2021-9-13 21:19 | 显示全部楼层

中文不支持,可能需要正则匹配,但我不会改,有哪位前辈愿意改改,在此感谢!
发表于 2024-4-27 17:11 | 显示全部楼层
楼主你好,这个功能搞定了吗
 楼主| 发表于 2024-4-27 18:22 | 显示全部楼层
664571221 发表于 2024-4-27 17:11
楼主你好,这个功能搞定了吗

没有搞定中文呢
发表于 2024-4-27 19:43 | 显示全部楼层
用正则表达式修改成功。收2币。勿喷!

本帖子中包含更多资源

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

x

点评

辛苦了,收点明经币正常的.  发表于 2024-4-27 20:29

评分

参与人数 1明经币 +1 收起 理由
tigcat + 1 非常感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-20 00:44 , Processed in 0.410746 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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