乐筑天下

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

[编程交流] 简单VBA修改

[复制链接]

44

主题

542

帖子

502

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
220
发表于 2022-7-6 22:13:49 | 显示全部楼层 |阅读模式
我在Augi论坛上找到了Robert Bell的以下VBA脚本(来自8年前最后一个活跃的帖子,因此我在这里发帖),它绝对精彩。
 
我有称为T01、T02、T03、T04、T05、T06、T07、T08、T09、T10、T11等的布局,尽管脚本将它们重命名为T1、T2、T3、T4、T5、T6、T7、T8、T9、T10、T11等。
 
我想保留0到9的布局前的0,例如T01等。
 
有人能帮我修改脚本来实现这一点吗?我不熟悉VBA for AutoCad,尽管我猜想它需要if语句来检查if布局。TabOrder公司
 
干杯
有机的
回复

使用道具 举报

44

主题

542

帖子

502

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
220
发表于 2022-7-6 22:30:21 | 显示全部楼层
我对VBA了解不多,尽管以下黑客似乎达到了我想要的效果:
 
  1. For Each Layout In Layouts
  2.      If Layout.ModelType = False And Layout.TabOrder <= 9 Then
  3.        Layout.Name = prefixName & "0" & CStr(startNum + Layout.TabOrder)
  4.      ElseIf Layout.ModelType = False Then
  5.        Layout.Name = prefixName & CStr(startNum + Layout.TabOrder)
  6.      End If
  7.    Next Layout

 
Lee mac的一个解析数,用于分离T1等
  1. (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  2. (vlax-for lay (vla-get-Layouts doc)
  3. (setq plotabs (cons (vla-get-name lay) plotabs))
  4. )
  5. ; need to now do this (vla-put-name lay)

 
T01 v的T1检查
  1. ; change the 410 to layout name
  2. ;;-------------------=={ Parse Numbers }==--------------------;;
  3. ;;                                                            ;;
  4. ;;  Parses a list of numerical values from a supplied string. ;;
  5. ;;------------------------------------------------------------;;
  6. ;;  Author: Lee Mac, Copyright © 2011 - [url="http://www.lee-mac.com"]www.lee-mac.com[/url]       ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Arguments:                                                ;;
  9. ;;  s - String to process                                     ;;
  10. ;;------------------------------------------------------------;;
  11. ;;  Returns:  List of numerical values found in string.       ;;
  12. ;;------------------------------------------------------------;;
  13. (defun LM:ParseNumbers ( s )
  14. (
  15.    (lambda ( l )
  16.      (read
  17.        (strcat "("
  18.          (vl-list->string
  19.            (mapcar
  20.              (function
  21.                (lambda ( a b c )
  22.                  (if
  23.                    (or
  24.                      (< 47 b 58)
  25.                      (and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
  26.                      (and (= 46 b) (< 47 a 58) (< 47 c 58))
  27.                    )
  28.                    b 32
  29.                  )
  30.                )
  31.              )
  32.              (cons nil l) l (append (cdr l) (list nil))
  33.            )
  34.          )
  35.          ")"
  36.        )
  37.      )
  38.    )
  39.    (vl-string->list s)
  40. )
  41. )
回复

使用道具 举报

44

主题

542

帖子

502

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
220
发表于 2022-7-6 22:36:39 | 显示全部楼层
 
我建议如下,以增强(或至少在我的系统中-Autocad 2010和W7-它可以工作)这项伟大的Robert Bell工作
 
就在“Option Explicit”代码行位置之后
 
  1. ; if less than 10
  2. (if (< (car dwgnum) 10.0)
  3.      (setq newstr2 (strcat dwgname "-D0"  (rtos sheetnum 2 0)))
  4.      (setq newstr2 (strcat dwgname "-D"  (rtos sheetnum 2 0)))
  5. )

 
并且,就在“If CommandName=“LAYOUT\u CONTROL”Then“代码行之后,放置
 
  1. '-------------------------------------
  2. ' to prevent the main routine from acting when user only switched from one Layout to another
  3. Dim JustSwitched As Integer
  4. Private Sub AcadDocument_LayoutSwitched(ByVal LayoutName As String)
  5.    JustSwitched = 1
  6. End Sub
  7. '-------------------------------------

 
然而,对于尾随零问题,遵循dbroada的建议只是一种替代
 
  1.     '-----------------------
  2.    ' if only switching between Layouts, then do nothing
  3.    If JustSwitched = 1 Then
  4.        JustSwitched = 0
  5.        Exit Sub
  6.    End If
  7.    '-----------------------

 
具有
 
  1. 7

 
 
最后,在卸载VBA脚本时,我没有遇到“搞砸”的问题。也许是上面的东西解决了它,或者也许我只需要知道一点点。。。
 
PS:无法理解为什么布局中有两个“For Each Layout In Layouts”循环,这两个循环都以不同的方式更改了布局名称。我想一个人必须选择其中一个,然后评论另一个?
回复

使用道具 举报

48

主题

1073

帖子

1043

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
238
发表于 2022-7-6 22:42:20 | 显示全部楼层
回复

使用道具 举报

29

主题

519

帖子

477

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
163
发表于 2022-7-6 22:48:10 | 显示全部楼层
Dave's Suggestion of using Format would work but your suggestion is fine.
 
Have you tried saving your drawing immediately after running the macro? If it fixes the problem You could add a Save in the macro code. Why do you need to unload the macro? It's not necessary, it is not saved in the drawing and when you end your AutoCAD it is automatically removed. I'm not on my computer at the moment But I can look at it tomorrow.
回复

使用道具 举报

44

主题

542

帖子

502

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
220
发表于 2022-7-6 23:01:20 | 显示全部楼层
I'm not familiar with Format although will take a look at it.
 
Yes, saving the drawing after running the macro does work. The macro always runs though (it is not a on/off macro) in the background. If I leave it running and save/close the drawing, it is all good. It only screws up when I unload the script while still in the drawing (as presumably it is halfway through its continuous loop checking process).
 
The reason I was trying to unload it is that I don't like scripts running all the time and would rather control when they run
I can work around this as above though.
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 23:12:13 | 显示全部楼层
I have something almost what you want done in VL lisp so I have cut the relevant bits out If I can find time I will do the rename layouts. The guys here often use all sorts of naming layout1-xx D01-xxx a mixture it would help me instantly have one standard.
 
There are two ways to retrieve the layouts creation order and view order I am pretty sure Lee-mac helped me with this will try to find the two ways to post here as well. Found "You have to be carefull the tab list is different to the tab display order". Now for the code.
 
This gets the layout names you can then do a opposite and PUT the layout name
  1. (setq doc (vla-get-activedocument (vlax-get-acad-object)))(vlax-for lay (vla-get-Layouts doc) (setq plotabs (cons (vla-get-name lay) plotabs))); need to now do this (vla-put-name lay)
 
a parse number by Lee-mac to pull apart the T1 etc
  1. ; change the 410 to layout name;;-------------------=={ Parse Numbers }==--------------------;;;;                                                            ;;;;  Parses a list of numerical values from a supplied string. ;;;;------------------------------------------------------------;;;;  Author: Lee Mac, Copyright © 2011 - [url="http://www.lee-mac.com"]www.lee-mac.com[/url]       ;;;;------------------------------------------------------------;;;;  Arguments:                                                ;;;;  s - String to process                                     ;;;;------------------------------------------------------------;;;;  Returns:  List of numerical values found in string.       ;;;;------------------------------------------------------------;;(defun LM:ParseNumbers ( s ) (   (lambda ( l )     (read       (strcat "("         (vl-list->string           (mapcar             (function               (lambda ( a b c )                 (if                   (or                     (< 47 b 58)                     (and (= 45 b) (< 47 c 58) (not (< 47 a 58)))                     (and (= 46 b) (< 47 a 58) (< 47 c 58))                   )                   b 32                 )               )             )             (cons nil l) l (append (cdr l) (list nil))           )         )         ")"       )     )   )   (vl-string->list s) ))
 
The check for T01 v's T1
  1. ; if less than 10(if (< (car dwgnum) 10.0)      (setq newstr2 (strcat dwgname "-D0"  (rtos sheetnum 2 0)))     (setq newstr2 (strcat dwgname "-D"  (rtos sheetnum 2 0))))
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2022-7-6 23:21:38 | 显示全部楼层
 
I'd suggest the following, to enhance (or at least, in my system - Autocad 2010 and W7 - it works) this great Robert Bell work
 
just after 'Option Explicit' codeline place
 
  1. '-------------------------------------' to prevent the main routine from acting when user only switched from one Layout to anotherDim JustSwitched As IntegerPrivate Sub AcadDocument_LayoutSwitched(ByVal LayoutName As String)   JustSwitched = 1End Sub'-------------------------------------
 
and, just after the 'If CommandName = "LAYOUT_CONTROL" Then' codeline, place
 
  1.     '-----------------------   ' if only switching between Layouts, then do nothing   If JustSwitched = 1 Then       JustSwitched = 0       Exit Sub   End If   '-----------------------
 
while, as for the trailing zeros issue, following dbroada's advice just subsitute
 
  1.       If Layout.ModelType = False Then Layout.Name = prefixName & CStr(startNum + Layout.TabOrder)
 
with
 
  1.       If Layout.ModelType = False Then Layout.Name = prefixName & CStr(Format(startNum + Layout.TabOrder, "00")) ' keep trailing zeros  
 
 
finally I didn't bumped into the "screwing up" issue while unloading the VBA script. maybe what above fixes it, or maybe I only have to witi a little bit...
 
PS: couldn't catch the reason why there are two 'For Each Layout In Layouts' loops both changing layouts name in a different manner. I guess one have to choose one of them and comment the other?
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 08:35 , Processed in 0.874836 second(s), 68 queries .

© 2020-2025 乐筑天下

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