简单VBA修改
我在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公司
干杯
有机的 我对VBA了解不多,尽管以下黑客似乎达到了我想要的效果:
For Each Layout In Layouts
If Layout.ModelType = False And Layout.TabOrder <= 9 Then
Layout.Name = prefixName & "0" & CStr(startNum + Layout.TabOrder)
ElseIf Layout.ModelType = False Then
Layout.Name = prefixName & CStr(startNum + Layout.TabOrder)
End If
Next Layout
Lee mac的一个解析数,用于分离T1等
(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)
T01 v的T1检查
; change the 410 to layout name
;;-------------------=={ Parse Numbers }==--------------------;;
;; ;;
;;Parses a list of numerical values from a supplied string. ;;
;;------------------------------------------------------------;;
;;Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
;;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)
)
)
我建议如下,以增强(或至少在我的系统中-Autocad 2010和W7-它可以工作)这项伟大的Robert Bell工作
就在“Option Explicit”代码行位置之后
; 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)))
)
并且,就在“If CommandName=“LAYOUT\u CONTROL”Then“代码行之后,放置
'-------------------------------------
' to prevent the main routine from acting when user only switched from one Layout to another
Dim JustSwitched As Integer
Private Sub AcadDocument_LayoutSwitched(ByVal LayoutName As String)
JustSwitched = 1
End Sub
'-------------------------------------
然而,对于尾随零问题,遵循dbroada的建议只是一种替代
'-----------------------
' if only switching between Layouts, then do nothing
If JustSwitched = 1 Then
JustSwitched = 0
Exit Sub
End If
'-----------------------
具有
7
最后,在卸载VBA脚本时,我没有遇到“搞砸”的问题。也许是上面的东西解决了它,或者也许我只需要知道一点点。。。
PS:无法理解为什么布局中有两个“For Each Layout In Layouts”循环,这两个循环都以不同的方式更改了布局名称。我想一个人必须选择其中一个,然后评论另一个? 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. 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. 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
(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
; change the 410 to layout name;;-------------------=={ Parse Numbers }==--------------------;;;; ;;;;Parses a list of numerical values from a supplied string. ;;;;------------------------------------------------------------;;;;Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;;;------------------------------------------------------------;;;;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
; 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))))
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
'-------------------------------------' 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
'----------------------- ' 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
If Layout.ModelType = False Then Layout.Name = prefixName & CStr(startNum + Layout.TabOrder)
with
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?
页:
[1]