明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2319|回复: 20

[源码] 文字加减复制的lisp程序

[复制链接]
发表于 2018-6-9 10:10 | 显示全部楼层 |阅读模式
下面的程序是网上下载的一个文字加减复制的lisp程序,3.222这个数,加2.3运行后,结果是5.522,但如果是3.220这样后面有0的数,运行后的结果是05.52,如果是3.200,结果就会是005.5,我想要的结果是0放小数点后面,有高手能改改这个程序吗
  1. (defun C:T11 ( / buchang1 $buchang SS SS1 e0 ent PT i loop ENTL E-1 NEWTX ENT_TMP)
  2.   (if (null $buchang) (setq $buchang 1))
  3.   (setq $buchang (ureal 1 "" "\n增减值(正为增,负为减)" $buchang) buchang1 $buchang)
  4.   (setq ss (ssget '((0 . "*TEXT"))))
  5. (setq e0 (entlast))  
  6.   (setq pt (getpoint "指定基点:"))   
  7.   (command "copy" ss "" pt pause)  
  8.   (setq loop T)
  9.   (while loop   
  10.   (SETQ SS1 (last_ent E0) I 0)
  11.   (repeat (sslength ss1)
  12.     (setq ent (ssname ss1 i)
  13.     entl (entget ent);图元资料
  14.     e-1 (cdr (assoc 1 entl));;文字内容
  15.     i (1+ i)
  16.           NEWTX (Plus1 E-1 BUCHANG1)
  17.     )
  18.    (entmod (subst (cons 1 NEWTX) (assoc 1 entl) entl));更新文字
  19.   );end repeat  
  20. (setq e0 (entlast))
  21. (setq buchang1 (+ buchang1 $buchang))
  22.     (command "copy" ss "" pt pause)
  23.     (setq Point (getvar "LastPoint"))
  24.     (if (= 0 (distance Point pt)) ;判断最后一点是不是pt点.
  25.         (progn
  26.         (setq loop nil) ;Right Button
  27.         (setq ent_tmp (LAST_ENT e0)) ;ent_tmp 是e0后生成的物体.
  28.         (command "_.erase" ent_tmp "" )
  29.         )      
  30.         )   
  31.   );end while
  32. )   
  33. (defun last_ent (en / ss)
  34.    (if en
  35.      (progn
  36.        (setq ss (ssadd))
  37.        (while (setq en (entnext en))
  38.          (if (not (member (cdr (assoc 0 (entget en)))
  39.                           '("ATTRIB" "VERTEX" "SEQEND")
  40.                   )
  41.              )
  42.            (ssadd en ss)
  43.          );if
  44.        );while
  45.        (if (zerop (sslength ss)) (setq ss nil))
  46.        ss
  47.      );progn
  48.      (ssget "_x")
  49.    );if
  50. )
  51. (defun Plus1 (str buchang / d1 d2 h)
  52.   (setq str (vl-string-translate "-" (chr 1) str))
  53.   (setq d1 (last (string_to_numbers str))
  54.         h (vl-string-right-trim d1 str))
  55.   (setq d2 (vl-princ-to-string (+ (read d1) buchang)))
  56.   (while (< (strlen d2) (strlen d1))
  57.     (setq d2 (strcat "0" d2))
  58.   )
  59.   (vl-string-translate (chr 1) "-" (strcat h d2))
  60. )

  61. ;;; Bill Kramer
  62. ;
  63. (defun String_To_Numbers (
  64.         inStr  ;;Input string
  65.         /
  66.         Res    ;;Result list
  67.         Buf    ;;String buffer
  68.         Inx      ;;Character location
  69.         CH     ;;Character
  70.         )
  71.   (setq Inx 1 ;start at the beginning of the string
  72.   Buf "" ;init buffer to empty
  73.   )
  74.   ;
  75.   ; Loop until the end of the string.
  76.   ; (I indicates where we are in the string)
  77.   ;
  78.   (while (<= Inx (strlen inStr))
  79.     ;
  80.     ; Get the character at position Inx, increment position indicator
  81.     (setq CH (substr inStr Inx 1)
  82.     Inx (1+ Inx)
  83.     )
  84.     ;
  85.     (cond
  86.       ; Test to see if character is a digit.
  87.       ((wcmatch CH "[0-9.]")
  88.         (if (= CH ".") ;is it decimal?
  89.     (if (not (wcmatch Buf "*`.*")) ;not already in there
  90.       (setq Buf (strcat Buf CH))
  91.       (Flush_Buf))
  92.     ;
  93.           (setq Buf (strcat Buf CH)))
  94.       )
  95.       ((= Buf "") ;is the buffer empty?
  96.          ;Is CH minus
  97.   (if (= CH "-")
  98.     (setq Buf CH) ;Yes, save in Buf
  99.   )
  100.       )
  101.       ('T ;else buffer is not empty
  102.         (Flush_Buf)
  103.         (if (= CH "-")
  104.     (setq Buf CH))
  105.       )
  106.     ); End of COND
  107.   ); End of WHILE
  108.   ;
  109.   (if (and (/= Buf "")
  110.      (not (wcmatch Buf "[+-.]"))
  111.      )
  112.     (Flush_Buf))
  113.   (reverse Res)
  114. )
  115. (defun Flush_Buf ()
  116.   (if (not (wcmatch Buf "[+-.]")) ;is it not just +-.?
  117.     (progn
  118.       ;Clean it up first
  119.       (if (= (substr Buf 1 1) ".")
  120.   (setq Buf (strcat "0" Buf))) ;;add zero to front if .#
  121.       (if (= (substr Buf (strlen Buf)) ".")
  122.   (setq Buf (substr Buf 1 (1- (strlen Buf))))) ;;remove decimal if #.
  123.       ;Add to RES list
  124.       (setq RES (cons Buf RES))))
  125.   (setq Buf "") ;;reset Buf
  126. )




"觉得好,就打赏"
还没有人打赏,支持一下
 楼主| 发表于 2018-6-14 08:37 | 显示全部楼层
lostbalance 发表于 2018-6-13 19:41
你把程序再发一遍看看
  1. (defun C:T11 ( / buchang1 $buchang SS SS1 e0 ent PT i loop ENTL E-1 NEWTX ENT_TMP)
  2. (setvar "dimzin" 0)
  3.   (if (null $buchang) (setq $buchang 1))
  4.   (setq $buchang (ureal 1 "" "\n增减值(正为增,负为减)" $buchang) buchang1 $buchang)
  5.   (setq ss (ssget '((0 . "*TEXT"))))
  6. (setq e0 (entlast))  
  7.   (setq pt (getpoint "指定基点:"))   
  8.   (command "copy" ss "" pt pause)  
  9.   (setq loop T)
  10.   (while loop   
  11.   (SETQ SS1 (last_ent E0) I 0)
  12.   (repeat (sslength ss1)
  13.     (setq ent (ssname ss1 i)
  14.           entl (entget ent);图元资料
  15.           e-1 (cdr (assoc 1 entl));;文字内容
  16.           i (1+ i)
  17.           (setq NEWTX (rtos (+ (atof E-1) BUCHANG1)) 2 3)
  18.     )
  19.    (entmod (subst (cons 1 NEWTX) (assoc 1 entl) entl));更新文字
  20.   );end repeat  
  21. (setq e0 (entlast))
  22. (setq buchang1 (+ buchang1 $buchang))
  23.     (command "copy" ss "" pt pause)
  24.     (setq Point (getvar "LastPoint"))
  25.     (if (= 0 (distance Point pt)) ;判断最后一点是不是pt点.
  26.         (progn
  27.         (setq loop nil) ;Right Button
  28.         (setq ent_tmp (LAST_ENT e0)) ;ent_tmp 是e0后生成的物体.
  29.         (command "_.erase" ent_tmp "" )
  30.         )      
  31.         )   
  32.   );end while
  33. )   
  34. (defun last_ent (en / ss)
  35.    (if en
  36.      (progn
  37.        (setq ss (ssadd))
  38.        (while (setq en (entnext en))
  39.          (if (not (member (cdr (assoc 0 (entget en)))
  40.                           '("ATTRIB" "VERTEX" "SEQEND")
  41.                   )
  42.              )
  43.            (ssadd en ss)
  44.          );if
  45.        );while
  46.        (if (zerop (sslength ss)) (setq ss nil))
  47.        ss
  48.      );progn
  49.      (ssget "_x")
  50.    );if
  51. )
  52. (defun Plus1 (str buchang / d1 d2 h)
  53.   (setq str (vl-string-translate "-" (chr 1) str))
  54.   (setq d1 (last (string_to_numbers str))
  55.         h (vl-string-right-trim d1 str))
  56.   (setq d2 (vl-princ-to-string (+ (read d1) buchang)))
  57.   (while (< (strlen d2) (strlen d1))
  58.   (setq d2 (strcat  d2 "0" ))
  59.    ; (setq d2 (strcat "0" d2))
  60.   )
  61.   (vl-string-translate (chr 1) "-" (strcat h d2))
  62. )

  63. ;;; Bill Kramer
  64. ;
  65. (defun String_To_Numbers (
  66.                           inStr  ;;Input string
  67.                           /
  68.                           Res    ;;Result list
  69.                           Buf    ;;String buffer
  70.                           Inx      ;;Character location
  71.                           CH     ;;Character
  72.                           )
  73.   (setq Inx 1 ;start at the beginning of the string
  74.         Buf "" ;init buffer to empty
  75.         )
  76.   ;
  77.   ; Loop until the end of the string.
  78.   ; (I indicates where we are in the string)
  79.   ;
  80.   (while (<= Inx (strlen inStr))
  81.     ;
  82.     ; Get the character at position Inx, increment position indicator
  83.     (setq CH (substr inStr Inx 1)
  84.           Inx (1+ Inx)
  85.           )
  86.     ;
  87.     (cond
  88.       ; Test to see if character is a digit.
  89.       ((wcmatch CH "[0-9.]")
  90.         (if (= CH ".") ;is it decimal?
  91.           (if (not (wcmatch Buf "*`.*")) ;not already in there
  92.             (setq Buf (strcat Buf CH))
  93.             (Flush_Buf))
  94.           ;
  95.           (setq Buf (strcat Buf CH)))
  96.       )
  97.       ((= Buf "") ;is the buffer empty?
  98.                ;Is CH minus
  99.         (if (= CH "-")
  100.           (setq Buf CH) ;Yes, save in Buf
  101.         )
  102.       )
  103.       ('T ;else buffer is not empty
  104.         (Flush_Buf)
  105.         (if (= CH "-")
  106.           (setq Buf CH))
  107.       )
  108.     ); End of COND
  109.   ); End of WHILE
  110.   ;
  111.   (if (and (/= Buf "")
  112.            (not (wcmatch Buf "[+-.]"))
  113.            )
  114.     (Flush_Buf))
  115.   (reverse Res)
  116. )
  117. (defun Flush_Buf ()
  118.   (if (not (wcmatch Buf "[+-.]")) ;is it not just +-.?
  119.     (progn
  120.       ;Clean it up first
  121.       (if (= (substr Buf 1 1) ".")
  122.         (setq Buf (strcat "0" Buf))) ;;add zero to front if .#
  123.       (if (= (substr Buf (strlen Buf)) ".")
  124.         (setq Buf (substr Buf 1 (1- (strlen Buf))))) ;;remove decimal if #.
  125.       ;Add to RES list
  126.       (setq RES (cons Buf RES))))
  127.   (setq Buf "") ;;reset Buf
  128. )
发表于 2018-6-19 10:49 | 显示全部楼层
szx025 发表于 2018-6-19 09:35
这样改对纯数字没有问题,但带字母数字,比如"TB-1"这样的数字,运行后前面的字母就没有了,要想保留原程 ...

。。。。。。要求要说清楚啊。。。。
如果只是后面补零,在plus1里,对d2参照newtx进行修改
(setq d2 (vl-princ-to-string (+ (read d1) buchang)))
  (while (< (strlen d2) (strlen d1))
  (setq d2 (strcat  d2 "0" ))
   ; (setq d2 (strcat "0" d2))
  )
上面的删掉,改成
(setq odimzin (getvar "dimzin")) ;;取得原来dimzin
(setvar "dimzin" 0) ;;设置dimzin为0
(setq d2 (rtos (+ (read d1) buchang) 2 3))
(setvar "dimzin" odimzin) ;;恢复dimzin
如果前面要补零,那把原来的那个while加到后面
发表于 2018-6-20 09:02 | 显示全部楼层
szx025 发表于 2018-6-19 13:54
这里还有一个问题,按你这样改所有数字都是保留小数点3位。但我的要求是,如果原来的数是整数,没有小数 ...

加一个判断啊
(setq d2 (+ (read d1) buchang))
(if (= d2 (fix d2)) ;;判断d2是不是整数
    (setq d2 (itoa d2)) ;;转字符串
    (progn
        (setq odimzin (getvar "dimzin")) ;;取得原来dimzin
        (setvar "dimzin" 0) ;;设置dimzin为0
        (setq d2 (rtos d2 2 3)) ;;转字符串
        (setvar "dimzin" odimzin) ;;恢复dimzin
    )
)
发表于 2018-6-9 15:10 | 显示全部楼层
没细看,按下面改试试
  (while (< (strlen d2) (strlen d1))
    (setq d2 (strcat "0" d2))
  )
中第二条改为
    (setq d2 (strcat d2 "0"))

 楼主| 发表于 2018-6-9 16:56 | 显示全部楼层
lostbalance 发表于 2018-6-9 15:10
没细看,按下面改试试
  (while (< (strlen d2) (strlen d1))
    (setq d2 (strcat "0" d2))

谢谢,可以了
发表于 2018-6-9 21:12 | 显示全部楼层
@lostbalance 谢谢! 我的也可以了。
 楼主| 发表于 2018-6-11 08:24 | 显示全部楼层
好像还有点问题,3.222这个数,加2.3运行后,结果是5.522,但3.200加2.3连续运行后,5.500 7.800都没有问题,但到了10.100出现问题了,变成了10.10.而不是我想要的10.100
发表于 2018-6-12 09:16 | 显示全部楼层
szx025 发表于 2018-6-11 08:24
好像还有点问题,3.222这个数,加2.3运行后,结果是5.522,但3.200加2.3连续运行后,5.500 7.800都没有问题 ...

如果只是要保留和的三位小数的话,没必要整的台麻烦啊。。。。
上面的NEWTX用
(setq NEWTX (rtos (+ (atof E-1) BUCHANG1)) 2 3)
就可以
如果结果的小数长度有问题,可能是系统变量dimzin的关系,在前面加个(setvar "dimzin" 8)
 楼主| 发表于 2018-6-12 14:27 | 显示全部楼层
lostbalance 发表于 2018-6-12 09:16
如果只是要保留和的三位小数的话,没必要整的台麻烦啊。。。。
上面的NEWTX用
(setq NEWTX (rtos (+ (a ...

按你的改了,但小数点还是两位
发表于 2018-6-13 08:57 | 显示全部楼层
szx025 发表于 2018-6-12 14:27
按你的改了,但小数点还是两位

上面提到dimzin参数搞错了,为0的时候才可以
用下面这段
(setq odimzin (getvar "dimzin")) ;;取得原来dimzin
(setvar "dimzin" 0) ;;设置dimzin为0
(setq NEWTX (rtos (+ (atof E-1) BUCHANG1)) 2 3)
(setvar "dimzin" odimzin) ;;恢复dimzin
 楼主| 发表于 2018-6-13 11:28 | 显示全部楼层
lostbalance 发表于 2018-6-13 08:57
上面提到dimzin参数搞错了,为0的时候才可以
用下面这段
(setq odimzin (getvar "dimzin")) ;;取得原来 ...

dimzin 为0,还是不行,问题依旧
发表于 2018-6-13 19:41 | 显示全部楼层
szx025 发表于 2018-6-13 11:28
dimzin 为0,还是不行,问题依旧

你把程序再发一遍看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-20 20:04 , Processed in 0.361971 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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