找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 538|回复: 8

[每日一码] 删除TEXT实体最后一个字符的几种方法

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2016-9-9 11:50:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
  1. (defun c:foo (/ ss)
  2.   (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
  3.     ((lambda (i / e ed s)
  4.        (while (setq e (ssname ss (setq i (1+ i))))
  5.          (setq ed (entget e))
  6.          (setq s (assoc 1 ed))
  7.          (setq ed (subst
  8.                     (cons 1 (substr (cdr s) 1 (1- (strlen (cdr s)))))
  9.                     s
  10.                     ed
  11.                   )
  12.          )
  13.          (entmod ed)
  14.        )
  15.      ) -1
  16.     )
  17.     (prompt "\n** Nothing selected ** ")
  18.   )
  19.   (princ)
  20. )



  1. (defun c:tt( / e i n s x )   
  2.     (setq n 1) ;; Number of characters to remove from end of string

  3.     (if (setq s (ssget "_:L" '((0 . "TEXT"))))
  4.         (repeat (setq i (sslength s))
  5.             (setq e (ssname s (setq i (1- i)))
  6.                   x (cdr (assoc 1 (entget e)))
  7.             )
  8.             (if (< n (strlen x))
  9.                 (entmod (list (cons -1 e) (cons 1 (substr x 1 (- (strlen x) n)))))
  10.             )
  11.         )
  12.     )
  13.     (princ)
  14. )



  1. (defun c:tt ( / e i n s x ) ;; define function, declare local variables

  2.     (setq n 1) ;; Number of characters to remove from end of string

  3.     (if ;; If the following expression returns a non-nil value
  4.         (setq s ;; Assign the following value to the symbol 's'
  5.             (ssget ;; Collect a selection set of
  6.                 "_:L" ;; Object on unlocked layers
  7.                 '((0 . "TEXT")) ;; whose entity type (DXF Group 0) = TEXT
  8.             ) ; end ssget
  9.         ) ;; end setq
  10.         (repeat ;; repeat a set of expressions a number of times
  11.             (setq i ;; Assign the following value to the symbol 'i'
  12.                 (sslength s)  ;; Number of items in the selection set
  13.             ) ;; end setq
  14.             (setq e ;; Assign the following value to the symbol 'e'
  15.                 (ssname s ;; Retrieve the entity name at a specific index in the set
  16.                     (setq i (1- i)) ;; Decrement the counter variable 'i'
  17.                 ) ;; end ssname
  18.                 x ;; Assign the following value to the symbol 'x'
  19.                 (cdr ;; Return the second item of the dotted pair
  20.                     (assoc 1 ;; Retrieve the dotted pair with DXF Group 1 (the text string)
  21.                         (entget e) ;; Retrieve the DXF data for the entity assigned to the symbol 'e'
  22.                     ) ;; end assoc
  23.                 ) ;; end cdr
  24.             ) ;; end setq
  25.             (if ;; If the following expression returns a non-nil value
  26.                 (< n (strlen x)) ;; If the length of the text string is greater than the number of characters to be subtracted
  27.                 (entmod ;; Modify the following DXF data
  28.                     (list ;; Contruct a list of DXF data
  29.                         (cons -1 e) ;; Create a dotted pair with the entity to be modified (1 . <entity-name>)
  30.                         (cons 1 ;; Create a dotted pair with the new string value (DXF Group 1)
  31.                             (substr x ;; Return a substring of the entity text string
  32.                                 1 ;; from the first character
  33.                                 (- (strlen x) n) ;; spanning the length of the string minus the number of characters to be removed
  34.                             ) ;; end substr
  35.                         ) ;; end cons
  36.                     ) ;; end list
  37.                 ) ;; end entmod
  38.             ) ;; end if
  39.         ) ;; end repeat
  40.     ) ;; end if
  41.     (princ) ;; Suppress the return of the last evaluated expression
  42. )



VLISP

[it618postdisplay>0]
  1. (defun c:tt (/ *error* oldnomutt ss odoc s)
  2.   (vl-load-com)
  3.   (princ "\rtt ")
  4.   (defun *error* (msg)
  5.     (and
  6.       oldnomutt
  7.       (setvar 'nomutt oldnomutt)
  8.     )
  9.     (if odoc
  10.       (vla-endundomark odoc)
  11.     )
  12.     (cond
  13.       ((not msg))                       ; normal exit
  14.       ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or
  15.                                        ; (quit)
  16.       ((princ (strcat "\n** Error: " msg " ** ")))
  17.     )                                       ; fatal error, display it
  18.     (princ)
  19.   )
  20.   (prompt "\nSelect text objects to remove last character: ")
  21.   (and
  22.     (setq oldnomutt (getvar 'nomutt))
  23.     (setvar 'nomutt 1)
  24.   )
  25.   (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
  26.     (progn
  27.       (vla-startundomark (setq odoc (vla-get-activedocument
  28.                                                             (vlax-get-acad-object)
  29.                                     )
  30.                          )
  31.       )
  32.       (vlax-for x (setq ss (vla-get-activeselectionset odoc))
  33.                 (setq s (vla-get-textstring x))
  34. ;|
  35.         (vla-put-textstring
  36.           x
  37.           (vl-string-subst
  38.             ""
  39.             (substr s (strlen s) 1)
  40.             s)))
  41.             |;
  42.                 (vla-put-textstring x (substr s 1 (1- (strlen s))))
  43.       )
  44.       (vla-endundomark odoc)
  45.       (vla-delete ss)
  46.       (setvar 'nomutt oldnomutt)
  47.     )
  48.     (progn
  49.       (setvar 'nomutt oldnomutt)
  50.       (prompt "\n** Nothing selected ** ")
  51.     )
  52.   )
  53.   (princ)
  54. )


[/it618postdisplay]

上面的代码都不支持中文,中文还是按2个字节处理,如果最后一个字符是汉字,那么上面代码最后字符会乱码。下面代码支持中文,中文字符长度按1处理

  1. (defun c:tt()
  2.     (setq n 1) ;; Number of characters to remove from end of string

  3.     (if (setq s (ssget "_:L" '((0 . "TEXT"))))
  4.         (repeat (setq i (sslength s))
  5.             (setq e (ssname s (setq i (1- i)))
  6.                   x (cdr (assoc 1 (entget e)))
  7.             )
  8.             (if (< n (strlen x))
  9.                 (entmod (list (cons -1 e) (cons 1 (xdrx_string_substr x 1 (xdrx_string_length x)))))
  10.             )
  11.         )
  12.     )
  13.     (princ)
  14. )



上面代码使用了XDRX API的 字符串处理函数
全部用API改写的话,下面:

  1. (defun c:tt ()
  2.   (if (setq s (ssget "_:L" '((0 . "TEXT"))))
  3.     (mapcar
  4.       '(lambda (x)
  5.          (setq old (xdrx_getpropertyvalue x "textstring")
  6.                newstr (xdrx_string_substr old 1 (xdrx_string_length old))
  7.          )
  8.          (xdrx_setpropertyvalue x "textstring" newstr)
  9.        )
  10.       (xdrx_pickset->ents s)
  11.     )
  12.   )
  13.   (princ)
  14. )


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2016-9-9 14:44:39 | 显示全部楼层
N版这段时间都在大总结,good job!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 8727个

财富等级: 富甲天下

发表于 2016-9-9 17:03:46 | 显示全部楼层
学习!学习!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

已领礼包: 5586个

财富等级: 富甲天下

发表于 2016-9-9 17:05:22 | 显示全部楼层
版主热心,向版主致敬!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 2476个

财富等级: 金玉满堂

发表于 2016-9-9 20:32:03 | 显示全部楼层
还从来没用VLSP方法..学习..
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 19个

财富等级: 恭喜发财

发表于 2016-9-9 21:13:39 | 显示全部楼层
谢谢N版,向N版学习。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 6056个

财富等级: 富甲天下

发表于 2020-6-29 19:55:55 | 显示全部楼层
正好用得上,感谢。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 216个

财富等级: 日进斗金

发表于 2020-11-17 00:00:01 | 显示全部楼层
感谢版主大作
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 4个

财富等级: 恭喜发财

发表于 2020-11-17 06:30:25 | 显示全部楼层
回复学习,谢谢大师的分享。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-27 01:00 , Processed in 0.433680 second(s), 44 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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