马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 marting 于 2021-1-20 16:52 编辑
四种获取布局的方法及效率测试
- (Defun GetTabs (/ dwg cnt tab typ lst); rtn) not needed
- ;Always localize your variables unless
- ;intentionally using a global variable
- (vl-load-com)
- (setq DWG (vla-get-layouts
- (vla-get-activedocument (vlax-get-acad-object))
- )
- CNT (vla-get-count DWG)
- )
- (while (<= 0 (setq CNT (1- CNT)))
- ;You lose one layout tab right away because you subtract
- ;one before you ever stop. The better place for
- ;subtracting one would be at the end of the while loop.
- (setq TAB (vla-item DWG CNT)
- TYP (strcase (vla-get-name TAB))
- )
- (if (not (eq TYP "MODEL"))
- ;If you use cons instead of append to create your list
- ;then you won't need to test LST first.
- ;(if (= LST Nil)
- ;(setq LST (list TAB))
- ;(setq LST (append LST (list TAB)))
- ;)
- (setq LST (cons TAB LST))
- )
- )
- ;If this is returning a list to another function you don't
- ;need to set this another variable just return the one
- ;already established.
- ;(setq RTN LST)
- LST
- )
- (Defun GetTabs2 (/ dwg typ lst)
- (vl-load-com)
- (setq dwg (vla-get-layouts
- (vla-get-activedocument
- (vlax-get-acad-object))
- )
- )
- (vlax-for lay dwg
- (setq typ (strcase (vla-get-name lay)))
- (if (not (eq typ "MODEL"))
- (setq lst (cons lay lst))
- )
- )
- lst
- )
- (defun gettabs3 ()
- (vl-load-com)
- (defun *layouts* ()
- (vla-get-layouts
- (vla-get-activedocument (vlax-get-acad-object))
- )
- )
- (defun *layout_list* (/ lst)
- (vlax-map-collection
- (*layouts*)
- '(lambda (x) (setq lst (cons x lst)))
- )
- (cdr
- (*sort* lst 'vla-get-taborder)
- )
- )
- (defun *sort* (lst func)
- (vl-sort lst
- '(lambda (e1 e2)
- (< ((eval func) e1) ((eval func) e2))
- )
- )
- )
- (*layout_list*)
- )
- (defun gettabs4 (/ lst)
- (vl-load-com)
- (vlax-map-collection
- (vla-get-layouts
- (vla-get-activedocument (vlax-get-acad-object))
- )
- '(lambda (x) (setq lst (cons x lst)))
- )
- (cdr
- (vl-sort lst
- '(lambda (e1 e2)
- (< (vla-get-taborder e1) (vla-get-taborder e2))
- )
- )
- )
- )
效率测试:
- (defun c:tt ()
- (XD::Quickbench
- '("gettabs" "gettabs2" "gettabs3" "gettabs4")
- )
- (princ)
- )
令: tt
CPU:(2x)Intel(R) Xeon(R) E5-2643 v4 @ 3.40GHz 6Cores / Memory:256G /
OS:WIN10专业工作站版
Benchmarking ......... done for 8192 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
-------------------------------------------------------------------------------
gettabs3 8192 1656 1656 1.06 <fastest>
gettabs2 8192 1657 1657 1.06
gettabs4 8192 1672 1672 1.05
gettabs 8192 1750 1750 1.00 <slowest>
-------------------------------------------------------------------------------
|