明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1039|回复: 10

[讨论] (国外搬运)用CSV文件重命名图块和图层

[复制链接]
发表于 2022-12-26 17:00 | 显示全部楼层 |阅读模式
本帖最后由 panliang9 于 2022-12-26 17:18 编辑

这是一个用CSV文件来重命名所有图块或者图层的程序,我一段段的粘到CAD里可以有用,但一整个调用就不对了。谁有空帮我调一下。

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-rename-layers-blocks-based-on-a-csv-txt-file/m-p/9713623#M403905



原贴附件



;; gc:str2lst;; Transforme un chaine avec séparateur en liste de chaines;;;; Arguments;; str : la chaîne;; sep : le séparateur(defun gc:str2lst (str sep / pos)  (if (setq pos (vl-string-search sep str))    (cons (substr str 1 pos) (gc:str2lst (substr str (+ (strlen sep) pos 1)) sep))    (list str)  ))(defun oe:RenBlock (old new) (if   (and     (tblsearch "BLOCK" old)     (not (tblsearch "BLOCK" new))     (snvalid new)   )    (progn       (if         (vl-catch-all-error-p           (vl-catch-all-apply             'vla-put-name             (list               (vla-item (vla-get-blocks                           (cond (*AcadDoc*)                                 ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))                           )                         )                         old               )               new             )           )         )          (princ (strcat "\nBlock: " old " could not be renamed to: " new))          (princ (strcat "\nBlock: " old " renamed to: " new))      )    )  ))(defun oe:RenLayer (old new) (if   (and     (tblsearch "LAYER" old)     (not (tblsearch "LAYER" new))     (snvalid new)   )    (progn       (if         (vl-catch-all-error-p           (vl-catch-all-apply             'vla-put-name             (list               (vla-item (vla-get-layers                           (cond (*AcadDoc*)                                 ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))                           )                         )                         old               )               new             )           )         )          (princ (strcat "\nLayer: " old " could not be renamed to: " new))          (princ (strcat "\nLayer: " old " renamed to: " new))      )    )  ))(defun C:RenBlock ( / sFile oFile sInfos)  (if (and        (setq sFile (getfiled "Select a block CSV File" (getvar "DWGPREFIX") "CSV" 8))        (setq sFile (findfile sFile))      )    (progn      (setq sSep (vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList"))      (setq oFile (open sFile "r"))      (while (setq sLine (read-line oFile))        (setq sInfos (gc:str2lst sLine sSep))        (if (= (length sInfos) 2)          (oe:RenBlock (car sInfos) (cadr sInfos))        )      )      (close oFile)    )  )  (princ))(defun C:RenLayer ( / sFile oFile sInfos)  (if (and        (setq sFile (getfiled "Select a layer CSV File" (getvar "DWGPREFIX") "CSV" 8))        (setq sFile (findfile sFile))      )    (progn      (setq sSep (vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList"))      (setq oFile (open sFile "r"))      (while (setq sLine (read-line oFile))        (setq sInfos (gc:str2lst sLine sSep))        (if (= (length sInfos) 2)          (oe:RenLayer (car sInfos) (cadr sInfos))        )      )      (close oFile)    )  )  (princ))

本帖子中包含更多资源

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

x
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2022-12-26 18:53 来自手机 | 显示全部楼层
本帖最后由 lxl217114 于 2022-12-26 18:58 编辑

谢谢大佬分享  实用工具
发表于 2022-12-26 20:02 | 显示全部楼层
谢谢楼主分享
发表于 2022-12-27 13:07 | 显示全部楼层
谢谢楼主分享
发表于 2022-12-27 13:21 | 显示全部楼层
  1. ;; (gc:str2lst "CAP;block capacit" ";") → ("CAP" "block capacit")
  2. (defun gc:str2lst (str sep / pos)
  3.   (if (setq pos (vl-string-search sep str))
  4.     (cons (substr str 1 pos)
  5.           (gc:str2lst (substr str (+ (strlen sep) pos 1)) sep)
  6.     )
  7.     (list str)
  8.   )
  9. )

  10. (defun oe:RenBlock (old new)
  11.   (if (and (tblsearch "BLOCK" old)
  12.            (not (tblsearch "BLOCK" new))
  13.            (snvalid new)
  14.       )
  15.     (if        (vl-catch-all-error-p
  16.           (vl-catch-all-apply
  17.             'vla-put-name
  18.             (list
  19.               (vla-item        (vla-get-blocks
  20.                           (cond        (*AcadDoc*)
  21.                                 ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object)))
  22.                                 )
  23.                           )
  24.                         )
  25.                         old
  26.               )
  27.               new
  28.             )
  29.           )
  30.         )
  31.       (princ
  32.         (strcat "\nBlock: " old " could not be renamed to: " new)
  33.       )
  34.       (princ (strcat "\nBlock: " old " renamed to: " new))
  35.     )
  36.   )
  37. )
  38. (defun oe:RenLayer (old new)
  39.   (if (and (tblsearch "LAYER" old)
  40.            (not (tblsearch "LAYER" new))
  41.            (snvalid new)
  42.       )
  43.     (if        (vl-catch-all-error-p
  44.           (vl-catch-all-apply
  45.             'vla-put-name
  46.             (list
  47.               (vla-item        (vla-get-layers
  48.                           (cond        (*AcadDoc*)
  49.                                 ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object)))
  50.                                 )
  51.                           )
  52.                         )
  53.                         old
  54.               )
  55.               new
  56.             )
  57.           )
  58.         )
  59.       (princ
  60.         (strcat "\nLayer: " old " could not be renamed to: " new)
  61.       )
  62.       (princ (strcat "\nLayer: " old " renamed to: " new))
  63.     )
  64.   )
  65. )
  66. (defun C:RenBlock (/ sFile oFile sInfos)
  67.   (if (and (setq sFile (getfiled "Select a block CSV File"
  68.                                  (getvar "DWGPREFIX")
  69.                                  "CSV"
  70.                                  8
  71.                        )
  72.            )
  73.            (setq sFile (findfile sFile))
  74.       )
  75.     (progn
  76.       (setq oFile (open sFile "r"))
  77.       (while (setq sLine (read-line oFile))
  78.         (setq sInfos (gc:str2lst sLine ";"))
  79.         (if (= (length sInfos) 2)
  80.           (oe:RenBlock (car sInfos) (cadr sInfos))
  81.         )
  82.       )
  83.       (close oFile)
  84.     )
  85.   )
  86.   (princ)
  87. )

  88. (defun C:RenLayer (/ sFile oFile sInfos)
  89.   (if (and (setq sFile (getfiled "Select a layer CSV File"
  90.                                  (getvar "DWGPREFIX")
  91.                                  "CSV"
  92.                                  8
  93.                        )
  94.            )
  95.            (setq sFile (findfile sFile))
  96.       )
  97.     (progn
  98.       (setq oFile (open sFile "r"))
  99.       (while (setq sLine (read-line oFile))
  100.         (setq sInfos (gc:str2lst sLine ";"))
  101.         (if (= (length sInfos) 2)
  102.           (oe:RenLayer (car sInfos) (cadr sInfos))
  103.         )
  104.       )
  105.       (close oFile)
  106.     )
  107.   )
  108.   (princ)
  109. )
 楼主| 发表于 2022-12-27 17:11 来自手机 | 显示全部楼层
谢谢  “xyp1964”!!!!
发表于 2022-12-27 19:41 | 显示全部楼层
论坛里面真的是藏龙卧虎啊
发表于 2022-12-28 10:03 | 显示全部楼层
谢大佬分享  实用工具
发表于 2023-4-21 10:34 | 显示全部楼层

加载了无gc命令?

点评

RenBlock  发表于 2023-4-21 22:13
发表于 2023-8-18 18:26 | 显示全部楼层
试了下  不能用吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-28 02:36 , Processed in 0.355075 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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