乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 72|回复: 10

[编程交流] LISP, find and replace (Specia

[复制链接]

5

主题

22

帖子

17

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 06:45:29 | 显示全部楼层 |阅读模式
Hi,
i'm having a little poblem. I want to find and replace some "special characters" in allot of drawings, without a dialog box. i'm having problems with the square bracktes.
 
for example:
Before,   After
============
"]",        "A"
"[",        "B"
"/",        "C"
"{",        "D"
 
 
i tried to modify this script...
http://www.cadtutor.net/forum/showthread.php?19333-Find-and-replace-text-without-a-dialog-box
  1. (defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list st);; local defun  ;; by Michael Puckett(defun Replace ( oldText newText text / i )(if (/= oldText newText)(while (setq i (vl-string-search oldText text))(setq text(vl-string-substnewTextoldTexttexti))))text) (or (vl-load-com)) (setq    fn           (getfiled "Select *ANY .DWG FILE* in a desired folder : "                ""                "dwg"                4              )   fold           (vl-filename-directory fn)   full_name_list (vl-directory-files fold "*.dwg" 1)   full_name_list (mapcar (function (lambda (x)                      (strcat fold "\" x)                    )                  )                  full_name_list              ) ) (setq    acapp (vlax-get-acad-object)   adocs (vla-get-documents acapp) ) (foreach fl full_name_list   (setq adoc (vla-open adocs fl :vlax-false))   (setq acsp (vla-get-modelSpace adoc))   (vlax-for lt (vla-get-layouts adoc)     (vlax-for    obj (vla-get-block lt)   (if (eq "AcDbText" (vla-get-objectname obj))     (if (wcmatch (setq st (vla-get-textstring obj)) "*D.F.*")       (vla-put-textstring obj (Replace "DF" "D.F." st))     )   )     )   )   (vla-save adoc)   (vla-close adoc) ))(princ "\n\t***\tStart command with RT\t***")(princ)
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 06:52:57 | 显示全部楼层
You may need to use the (chr and (ascii functions you can find stuff like "/" basicly any key on the keyboard
 
  1. (ascii "{") = 123(chr 123) = "{"(Replace (chr 123) "A" st)
回复

使用道具 举报

5

主题

22

帖子

17

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 07:02:14 | 显示全部楼层
 
thx, i'll try that
回复

使用道具 举报

5

主题

22

帖子

17

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 07:08:01 | 显示全部楼层
How to modify script to be able to replace multiple characters...
 
Script:

[code](defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list st) (defun Replace ( oldText newText text / i )   (if (/= oldText newText)     (while (setq i (vl-string-search oldText text))       (setq text         (vl-string-subst           newText           oldText           text           i         )       )     )   )   text ) (or (vl-load-com)) (setq fn   (getfiled "Select *ANY .DWG FILE* in a desired folder : "     ""     "dwg"     4   )   fold (vl-filename-directory fn)   full_name_list (vl-directory-files fold "*.dwg" 1)   full_name_list (mapcar     (function        (lambda         (x)         (strcat fold "\\" x)       )     )     full_name_list   ) ) (setq acapp (vlax-get-acad-object)       adocs (vla-get-documents acapp) ) (foreach fl full_name_list   (setq adoc (vla-open adocs fl :vlax-false))   (setq acsp (vla-get-modelSpace adoc))   (vlax-for lt (vla-get-layouts adoc)     (vlax-for obj (vla-get-block lt)(if (eq "AcDbText" (vla-get-objectname obj))  (if (wcmatch (setq st (vla-get-textstring obj)) "*")    (vla-put-textstring obj (Replace (chr 91) "Å" st))    ;(vla-put-textstring obj (Replace (chr 93) "Ä" st))
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 07:14:48 | 显示全部楼层
Try this
 

[code](if (wcmatch (setq st (vla-get-textstring obj)) "*")(progn     (vla-put-textstring obj (Replace (chr 91) "Å" st))    (vla-put-textstring obj (Replace (chr 93) "Ä" st))
回复

使用道具 举报

5

主题

22

帖子

17

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 07:16:20 | 显示全部楼层
Thx bigal,
but still doesn't work.. (no errors but only replaces the last letter "(Replace (chr 92) "Ö" st)"
i think the problem is in the first part.. if i understand the code correctly (i'm more used to C# programming)
 
  1. ... (defun Replace ( oldText newText text / i )...
 
complete code:
  1. (defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list st) (defun Replace ( oldText newText text / i )   (if (/= oldText newText)     (while (setq i (vl-string-search oldText text))       (setq text         (vl-string-subst           newText           oldText           text           i         )       )     )   )   text ) (or (vl-load-com)) (setq    fn   (getfiled "Select *ANY .DWG FILE* in a desired folder : "     ""     "dwg"     4   )   fold (vl-filename-directory fn)   full_name_list (vl-directory-files fold "*.dwg" 1)   full_name_list (mapcar     (function        (lambda         (x)         (strcat fold "\" x)       )     )     full_name_list   ) ) (setq acapp (vlax-get-acad-object)       adocs (vla-get-documents acapp) ) (foreach fl full_name_list   (setq adoc (vla-open adocs fl :vlax-false))   (setq acsp (vla-get-modelSpace adoc))   (vlax-for lt (vla-get-layouts adoc)     (vlax-for    obj (vla-get-block lt)   (if (eq "AcDbText" (vla-get-objectname obj))     (if (wcmatch (setq st (vla-get-textstring obj)) "*")       (progn              (vla-put-textstring obj (Replace (chr 91) "Å" st))          (vla-put-textstring obj (Replace (chr 93) "Ä" st))             (vla-put-textstring obj (Replace (chr 92) "Ö" st))         )     )   )     )   )      (vla-save adoc)   (vla-close adoc) ))(princ "\n\t***\tStart command with RT\t***")(princ)
回复

使用道具 举报

5

主题

22

帖子

17

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 07:22:10 | 显示全部楼层
Trying to understand how the script works... how to modify it to do someting simple like Zoom Extents, dont get it to work, just opens all the drawings and saves them... so i must have missed something
 
  1. (defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list)  (vl-load-com) (setq fn   (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4)   ;Location   fold (vl-filename-directory fn)   ;Filename   full_name_list (vl-directory-files fold "*.dwg" 1)   ;change  to Location + Filename   full_name_list (mapcar (function (lambda (x) (strcat fold "\" x))) full_name_list) ) (setq acapp (vlax-get-acad-object)       adocs (vla-get-documents acapp) ) (foreach fl full_name_list   (setq adoc (vla-open adocs fl :vlax-false))   (setq acsp (vla-get-modelSpace adoc))   (command ".ZOOM" "Extents")   (command ".ZOOM" "0.95x")      (vla-save adoc)   (vla-close adoc) ))(princ "\n\t***\tStart command with RT\t***")(princ)
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 07:29:32 | 显示全部楼层
Script is as below
 
open dwg1 (load "MYRT")(c:RT) CLOSE y
Open dwg2 (load "MYRT")(c:RT) CLOSE y
 
as you will run multitimes you can add the (Load "MYrt.lsp) to your acaddoc.lsp this way its ready to be used any time
 
the its just
open dwg1 (c:RT) CLOSE y
回复

使用道具 举报

5

主题

22

帖子

17

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 07:35:43 | 显示全部楼层
 
thx for the tip,
now is the problem i want to open every drawing in a specific folder.. when i rewrite the code like this than it opens every drawing in the folder, but i also want to run a lisp from the acaddoc.lsp, and i dont know how to do that in this cript.
 
  1. (defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list) (or (vl-load-com)) ;ensure that AutoLISP ActiveX support is loaded (setq fn   (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4)   fold (vl-filename-directory fn) ;Location   full_name_list (vl-directory-files fold "*.dwg" 1) ;Filename   full_name_list (mapcar (function (lambda (x) (strcat fold "\" x))) full_name_list) ;change  to Location + Filename ) (setq acapp (vlax-get-acad-object) ;Retrieves the top level AutoCAD application object for the current AutoCAD session       adocs (vla-get-documents acapp) ) (foreach drawing full_name_list   (setq vgao (vlax-get-acad-object))   (setq vgad (vla-get-activedocument vgao))   (setq vgd (vla-get-documents vgao))   (setq cd drawing)    (if     (= 0 (getvar "SDI"))     (vla-activate (vla-open vgd cd))     (vla-sendcommand vgad (strcat "(command "_open")\n" cd "\n"))   )    ))(princ "\n\t***\tStart command with RT\t***")(princ)
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 07:39:44 | 显示全部楼层
A simple test is add (C:RT) as the last item in your foreach not sure if it will work you may need also a extra bit to make the last opened dwg the current one. I just use the old fashioned way with a script, You can get your file list and get the lisp to write a script  like above example.
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-10 15:04 , Processed in 0.420622 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表