明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: 958620832

[源码] 动态引线标注(改版)

    [复制链接]
发表于 2014-4-10 10:52 | 显示全部楼层
冒昧的在楼主的基础上进行了修改,希望不要介意。
因为我们作图的要求是白色字,绿色线。 而且每个图比例不同,需要的大小也不同。
修改过后,字高2.5,按照当前标注比例的全局比例进行修改,线为绿色,字为白色。

;;;动态引线标注
;;;支持线上下写字,字体由当前字体决定
;;;字高2.5,按标注比例标注
;;;字为白色,线为绿色
;;;作者:明经958620832
;;;修改:Bella             命令:bz          日期:2014年04月

(defun bz (/ *error* name1 name2 name3)
    (defun *error* (msg)                ;将描述错误的字符串存入变量msg
        (entdel name1)
        (entdel name2)
        (if name3
            (entdel name3)
        )
        (princ "错误: ")
        (princ msg)
    )                                        ;打印错误信息
    (setq Scale (getvar "dimscale"));
    (setq ty (getvar "TEXTSTYLE"))
    (setq kd1 (caadr (textbox (list '(0 . "text")
                                    (cons 1 txt1)
                                    (cons 40 (* 2.5 Scale))
                                    (cons 41 1)
                                    (cons 7 ty)
                                    (cons 62 7)
                              )
                     )
              )
    )
                                        ;字高2.5*当前比例,字宽高比1,可以自己设置,字体为当前字体
    (setq kd2 (caadr (textbox (list '(0 . "text")
                                    (cons 1 txt2)
                                    (cons 40 (* 2.5 Scale))
                                    (cons 41 1)
                                    (cons 7 ty)
                                    (cons 62 7)
                              )
                     )
              )
    )
                                        ;字高2.5*当前比例,字宽高比1,可以自己设置,字体为当前字体
    (setq kd (max kd1 kd2)
          kd (+ kd (* 0.50 Scale))
    )
    (setq p (getpoint "\n输入基点:"))
    (setq pd t)
    (while pd
        (setq gr   (grread t 4 1)
              mode (car gr)
              pt   (cadr gr)
        )
        (if (= kd3 0)
            (setq kd kd1)
        )
        (if (and (listp pt) (>= (car pt) (car p)))
            (progn
                (setq p0 (polar pt 0 kd))
                (setq p1 (polar pt 0 (/ (- kd kd1) 2))
                      p1 (polar p1 (angtof "90") (* 0.70 Scale))
                )
                (setq p2 (polar pt 0 (/ (- kd kd2) 2))
                      p2 (polar p2 (angtof "270") (* 3.20 Scale))
                )
            )
        )
        (if (and (listp pt) (< (car pt) (car p)))
            (progn
                (setq p0 (polar pt pi kd))
                (setq p1 (polar p0 0 (/ (- kd kd1) 2))
                      p1 (polar p1 (angtof "90") (* 0.70 Scale))
                )
                (setq p2 (polar p0 0 (/ (- kd kd2) 2))
                      p2 (polar p2 (angtof "270") (* 3.20 Scale))
                )
            )
        )
        (if (= mode 5)
            (progn
                (if name1
                    (entdel name1)
                )
                (entmake (list '(0 . "LWPOLYLINE")
                               '(100 . "AcDbEntity")
                               '(100 . "AcDbPolyline")
                               '(90 . 3)
                               (cons 10 p)
                               (cons 10 pt)
                               (cons 10 p0)
                               (cons 62 3)
                         )
                )
                (setq name1 (entlast))
                (if name2
                    (entdel name2)
                )
                (entmake (list '(0 . "text")
                               (cons 1 txt1)
                               (cons 40 (* 2.5 Scale))
                               (cons 41 1)
                               (cons 10 p1)
                               (cons 7 ty)(cons 62 7)
                         )
                )
                                        ;字高2.5*当前比例,字宽高比1,可以自己设置,字体为当前字体
                (setq name2 (entlast))
                (if name3
                    (entdel name3)
                )
                (if (= kd3 1)
                    (entmake (list '(0 . "text")
                                   (cons 1 txt2)
                                   (cons 40 (* 2.5 Scale))
                                   (cons 41 1)
                                   (cons 10 p2)
                                   (cons 7 ty)(cons 62 7)
                             )
                    )
                )
                                        ;字高2.5*当前比例,字宽高比1,可以自己设置,字体为当前字体
                (if (= kd3 1)
                    (setq name3 (entlast))
                )
            )
        )
        (if (= mode 3)
            (setq pd nil)
        )
        (if (or (= mode 2) (= mode 25))
            (progn (setq pd nil)
                   (entdel name1)
                   (entdel name2)
                   (if name3
                       (entdel name3)
                   )
            )
        )
    )
    (princ)
)

(defun getdata ()
    (setq txt1 (get_tile "a1"))
    (setq txt2 (get_tile "a2"))
    (if        (= (get_tile "a3") "0")
        (setq kd3 0)
        (setq kd3 1)
    )
)

(defun c:bz ()
                                        ;(步骤1)建立临时对话框
    (setq tempname (vl-filename-mktemp "temp.dcl")
          filen           (open tempname "w")
    )
    (foreach stream
             '("yxbz:dialog{"
               "\n  label = "
               动态引线标注
               ";"
               "\n  :edit_box {key = \"a1\"; label = \"线上文字:\"; width = 40 ;}"
               "\n  :toggle {key = \"a3\"; label = \"增加线下文字\"; value = "
               0
               ";}"
               "\n  :edit_box {key = \"a2\"; label = \"线下文字:\"; width = 40; is_enabled = false;}"
               "\n  ok_cancel;}"
              )
        (princ stream filen)
    )
    (close filen)
    (setq dclname tempname)
                                        ;(步骤2)加载并显示对话框
    (setq dcl_re (load_dialog dclname))
    (if        (not (new_dialog "yxbz" dcl_re))
        (exit)
    )
                                        ;(步骤3)定义对话框控件(运用set_tile、action_tile、mode_tile、get_tile等函数)
    (if        txt1
        (set_tile "a1" txt1)
        (set_tile "a1" "动态标注")
    )
    (if        txt2
        (set_tile "a2" txt2)
        (set_tile "a2" "动态标注")
    )
    (if        kd3
        (set_tile "a3" (rtos kd3))
    )                                        ;注意set_tile函数中赋值均为字符串(带双引号),就连关键词也要加上双引号。
    (if        (= kd3 0)
        (mode_tile "a2" 1)
    )
    (if        (= kd3 1)
        (mode_tile "a2" 0)
    )
    (action_tile
        "a3"
        "(if (= (get_tile \"a3\") \"0\") (mode_tile \"a2\" 1) (mode_tile \"a2\" 0))"
    )                                        ;点击时才起作用
    (action_tile "accept" "(getdata)(done_dialog 1)")
    (action_tile "cancel" "(done_dialog)")
                                        ;(步骤4)激活并卸载对话框,并进行对话框隐藏后的操作。
    (setq std (start_dialog))
    (unload_dialog dcl_re)
    (vl-file-delete dclname)
    (if        (= std 1)
        (bz)
    )
    (princ)
)
发表于 2014-4-17 01:30 | 显示全部楼层
Bellahx 发表于 2014-4-10 10:52
冒昧的在楼主的基础上进行了修改,希望不要介意。
因为我们作图的要求是白色字,绿色线。 而且每个图比例不 ...

楼主如果能改成在DCL中直接设字高就好了
发表于 2014-4-17 10:46 | 显示全部楼层
lucas_3333 发表于 2014-4-17 01:30
楼主如果能改成在DCL中直接设字高就好了

试着改了一下,我这边测没什么大问题,有点小问题希望大家指点。
1.在输入字高不合法的时候怎么直接报错跳出程序。
2.在对话框中输入后不能直接回车进行下一步,只能点击确认按钮,如果能的话会方便很多。
(再次,弱弱的希望楼主不要介意修改你的代码,一起学习


  1. ;;;动态引线标注
  2. ;;;支持线上下写字,字体由当前字体决定
  3. ;;;字高2.5,按标注比例标注
  4. ;;;字为白色,线为绿色
  5. ;;;作者:明经958620832
  6. ;;;修改:             命令:yxbz          日期:2014年04月

  7. (defun bz (/ *error* name1 name2 name3)
  8.     (defun *error* (msg)    ;将描述错误的字符串存入变量msg
  9.   (entdel name1)
  10.   (entdel name2)
  11.   (if name3
  12.       (entdel name3)
  13.   )
  14.   (princ "错误: ")
  15.   (princ msg)
  16.     )          ;打印错误信息
  17.    
  18.     (setq ty (getvar "TEXTSTYLE"))
  19.     (setq ht (atof txtht))
  20.     (if (= ht 0)
  21.   (progn
  22.   (prompt "字高为0!")
  23.   (terpri)
  24.   );progn
  25.   );if
  26.     (setq Scale (/ ht 2.5));
  27.     (setq kd1 (caadr (textbox (list '(0 . "text")
  28.             (cons 1 txt1)
  29.             (cons 40 ht)
  30.             (cons 41 1)
  31.             (cons 7 ty)
  32.             (cons 62 7)
  33.             )
  34.          )
  35.         )
  36.     )
  37.           ;字高ht,字宽高比1,可以自己设置,字体为当前字体
  38.     (setq kd2 (caadr (textbox (list '(0 . "text")
  39.             (cons 1 txt2)
  40.             (cons 40 ht)
  41.             (cons 41 1)
  42.             (cons 7 ty)
  43.             (cons 62 7)
  44.             )
  45.          )
  46.         )
  47.     )
  48.           ;字高ht,字宽高比1,可以自己设置,字体为当前字体
  49.     (setq kd (max kd1 kd2)
  50.     kd (+ kd (* 0.50 Scale))
  51.     )
  52.     (setq p (getpoint "\n输入基点:"))
  53.     (setq pd t)
  54.     (while pd
  55.   (setq gr   (grread t 4 1)
  56.         mode (car gr)
  57.         pt   (cadr gr)
  58.   )
  59.   (if (= kd3 0)
  60.       (setq kd kd1)
  61.   )
  62.   (if (and (listp pt) (>= (car pt) (car p)))
  63.       (progn
  64.     (setq p0 (polar pt 0 kd))
  65.     (setq p1 (polar pt 0 (/ (- kd kd1) 2))
  66.           p1 (polar p1 (angtof "90") (* 0.70 Scale))
  67.     )
  68.     (setq p2 (polar pt 0 (/ (- kd kd2) 2))
  69.           p2 (polar p2 (angtof "270") (* 3.20 Scale))
  70.     )
  71.       )
  72.   )
  73.   (if (and (listp pt) (< (car pt) (car p)))
  74.       (progn
  75.     (setq p0 (polar pt pi kd))
  76.     (setq p1 (polar p0 0 (/ (- kd kd1) 2))
  77.           p1 (polar p1 (angtof "90") (* 0.70 Scale))
  78.     )
  79.     (setq p2 (polar p0 0 (/ (- kd kd2) 2))
  80.           p2 (polar p2 (angtof "270") (* 3.20 Scale))
  81.     )
  82.       )
  83.   )
  84.   (if (= mode 5)
  85.       (progn
  86.     (if name1
  87.         (entdel name1)
  88.     )
  89.     (entmake (list '(0 . "LWPOLYLINE")
  90.              '(100 . "AcDbEntity")
  91.              '(100 . "AcDbPolyline")
  92.              '(90 . 3)
  93.              (cons 10 p)
  94.              (cons 10 pt)
  95.              (cons 10 p0)
  96.              (cons 62 3)
  97.        )
  98.     )
  99.     (setq name1 (entlast))
  100.     (if name2
  101.         (entdel name2)
  102.     )
  103.     (entmake (list '(0 . "text")
  104.              (cons 1 txt1)
  105.              (cons 40 ht)
  106.              (cons 41 1)
  107.              (cons 10 p1)
  108.              (cons 7 ty)(cons 62 7)
  109.        )
  110.     )
  111.           ;字高ht,字宽高比1,可以自己设置,字体为当前字体
  112.     (setq name2 (entlast))
  113.     (if name3
  114.         (entdel name3)
  115.     )
  116.     (if (= kd3 1)
  117.         (entmake (list '(0 . "text")
  118.            (cons 1 txt2)
  119.            (cons 40 ht)
  120.            (cons 41 1)
  121.            (cons 10 p2)
  122.            (cons 7 ty)(cons 62 7)
  123.            )
  124.         )
  125.     )
  126.           ;字高ht,字宽高比1,可以自己设置,字体为当前字体
  127.     (if (= kd3 1)
  128.         (setq name3 (entlast))
  129.     )
  130.       )
  131.   )
  132.   (if (= mode 3)
  133.       (setq pd nil)
  134.   )
  135.   (if (or (= mode 2) (= mode 25))
  136.       (progn (setq pd nil)
  137.        (entdel name1)
  138.        (entdel name2)
  139.        (if name3
  140.            (entdel name3)
  141.        )
  142.       )
  143.   )
  144.     )
  145.     (princ)
  146. )

  147. (defun getdata ()
  148.     (setq txt1 (get_tile "a1"))
  149.     (setq txt2 (get_tile "a2"))
  150.     (if  (= (get_tile "a3") "0")
  151.   (setq kd3 0)
  152.   (setq kd3 1)
  153.     )
  154.     (setq txtht(get_tile "a4"))
  155. )

  156. (defun c:yxbz ()
  157.           ;(步骤1)建立临时对话框
  158.     (setq tempname (vl-filename-mktemp "temp.dcl")
  159.     filen     (open tempname "w")
  160.     )
  161.     (foreach stream
  162.        '("yxbz:dialog{"
  163.          "\n  label = "
  164.          动态引线标注
  165.          ";"
  166.          "\n  :edit_box {key = \"a1\"; label = \"线上文字:\"; width = 40 ;}"
  167.          "\n  :toggle {key = \"a3\"; label = \"增加线下文字\"; value = "
  168.          0
  169.          ";}"
  170.          "\n  :edit_box {key = \"a2\"; label = \"线下文字:\"; width = 40; is_enabled = false;}"
  171.          "\n  :edit_box {key = \"a4\"; label = \"文字高度:\"; width = 20; }"
  172.          "\n  ok_cancel;}"
  173.         )
  174.   (princ stream filen)
  175.     )
  176.     (close filen)
  177.     (setq dclname tempname)
  178.           ;(步骤2)加载并显示对话框
  179.     (setq dcl_re (load_dialog dclname))
  180.     (if  (not (new_dialog "yxbz" dcl_re))
  181.   (exit)
  182.     )
  183.           ;(步骤3)定义对话框控件(运用set_tile、action_tile、mode_tile、get_tile等函数)
  184.     (if  txt1
  185.   (set_tile "a1" txt1)
  186.   (set_tile "a1" "动态标注")
  187.     )
  188.     (if  txt2
  189.   (set_tile "a2" txt2)
  190.   (set_tile "a2" "动态标注")
  191.     )
  192.     (if  kd3
  193.   (set_tile "a3" (rtos kd3))
  194.     )          ;注意set_tile函数中赋值均为字符串(带双引号),就连关键词也要加上双引号。
  195.     (if  (= kd3 0)
  196.   (mode_tile "a2" 1)
  197.     )
  198.     (if  (= kd3 1)
  199.   (mode_tile "a2" 0)
  200.     )
  201.     (if  txtht
  202.   (set_tile "a4" txtht)
  203.   (set_tile "a4" "2.5")
  204.     )
  205.     (action_tile
  206.   "a3"
  207.   "(if (= (get_tile \"a3\") \"0\") (mode_tile \"a2\" 1) (mode_tile \"a2\" 0))"
  208.     )          ;点击时才起作用
  209.     (action_tile "accept" "(getdata)(done_dialog 1)")
  210.     (action_tile "cancel" "(done_dialog)")
  211.           ;(步骤4)激活并卸载对话框,并进行对话框隐藏后的操作。
  212.     (setq std (start_dialog))
  213.     (unload_dialog dcl_re)
  214.     (vl-file-delete dclname)
  215.     (if  (= std 1)
  216.   (bz)
  217.     )
  218.     (princ)
  219. )

评分

参与人数 1明经币 +1 收起 理由
lucas_3333 + 1 不错,字高应限定为数字,现在字母也可输入

查看全部评分

发表于 2014-4-17 11:38 | 显示全部楼层
Bellahx 发表于 2014-4-17 10:46
试着改了一下,我这边测没什么大问题,有点小问题希望大家指点。
1.在输入字高不合法的时候怎么直接报错 ...

Bellahx 你好!
如果不考虑字高出错的问题,仅仅只需将源程序中的字高通过对话框更改,就像你上面的程序一下,需要更改哪些地方,能不能单独贴出来?谢谢!
发表于 2014-4-17 15:17 | 显示全部楼层
lucas_3333 发表于 2014-4-17 11:38
Bellahx 你好!
如果不考虑字高出错的问题,仅仅只需将源程序中的字高通过对话框更改,就像你上面的程序 ...

没太理解你的问题(⊙o⊙)…麻烦详细说一下~
发表于 2014-7-20 12:11 | 显示全部楼层
好厉害 啊学习了
发表于 2014-7-20 13:53 | 显示全部楼层
如果能在程序中事先写入自己常用的词,标注文字时从事先写入自己常用的词中选择就好了,就是需要程序能读*.txt文件
发表于 2014-11-20 22:55 | 显示全部楼层
Bellahx 发表于 2014-4-10 10:52
冒昧的在楼主的基础上进行了修改,希望不要介意。
因为我们作图的要求是白色字,绿色线。 而且每个图比例不 ...

一个疑问是将字宽高比1修改0.7,线在哪里可以修改同步缩短?
发表于 2014-11-22 21:32 | 显示全部楼层
很不错...................
发表于 2014-12-10 11:57 | 显示全部楼层
Bellahx 发表于 2014-4-10 10:52
冒昧的在楼主的基础上进行了修改,希望不要介意。
因为我们作图的要求是白色字,绿色线。 而且每个图比例不 ...

确实好用多了,继续
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-25 10:23 , Processed in 0.316150 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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