- UID
- 244763
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2005-4-16
- 最后登录
- 1970-1-1
|
发表于 2005-12-3 21:32:32
|
显示全部楼层
;| Example programms based on VLXLS By Koz Jono Yeoh | KozMos Inc.
Copyright(C) 1994-2005 by KozMos Inc. All rights reserved
============================================================================
VirtualTableModifier2005
========================
This is a example file to show data exchange between Excel and AutoCAD.
This routine will send contents of user selected TEXTS into Excel so that
they can be modified within Excel. When the modification is done, the program
will read data from Excel and modify the contents of TEXTS in AutoCAD auto-
matically. Thus we can use some Excel features such as sort, number adding to
change AutoCAD TEXTS very quickly.
All selected TEXTS will be treated as cell contents of a [Virtual Table]
in AutoCAD. The [Virtual Table] will be re-vive in Excel so that the contents
of TEXTS lying on correct Excel cells.
|;
(Defun c:vtm (/ vllist-sort-n vllist-nth-subst
xxx-build-list xxx-sort-x xxx-sort-y
xxx-get-max xxx-build-array xxx-subst-array
xxx-sset->data ss rng
xls data *xl* dat1
dat2 x y txt
obj
)
;;; Sub-function: Make list atom subst
(defun vllist-nth-subst (listx id val / i rtn)
(setq i -1)
(repeat (length listx)
(if (= (setq i (1+ i)) id)
(setq rtn (cons val rtn))
(setq rtn (cons (nth i listx) rtn))
)
)
(reverse rtn)
)
;;; Sub-function: Sort via certain list ID
(defun vllist-sort-n (sql n / p1 p2)
(vl-sort sql '(lambda (p1 p2) (< (nth n p1) (nth n p2))))
)
;;; Sub-function: Build data list of TEXTS, syntax:
;;; ((TextObject LocationX TextWidth LocationY TextHeight ExcelID-X ExcelID-Y)...)
(Defun xxx-build-list (sset / i txt vlo ll ur tw th rtn)
(setq i -1)
(repeat (sslength sset)
(setq txt (ssname sset (setq i (1+ i)))
vlo (vlax-ename->vla-object txt)
)
(vla-getboundingbox vlo 'll 'ur)
(if vlax-safearray->list
(setq ll (vlax-safearray->list ll)
ur (vlax-safearray->list ur)
)
)
(setq tw (- (car ur) (car ll))
th (- (cadr ur) (cadr ll))
rtn (cons (list txt (car ll) tw (cadr ll) th 1 1) rtn)
)
)
rtn
)
;;; Sub-function: Sort X direction of built data list and reset ExcelID-X
(Defun xxx-sort-x (data / dx ox xx rtn)
(setq dx 1)
(foreach item (vllist-sort-n data 1)
(setq ox (nth 1 item))
(if (null xx)
(setq xx ox)
)
(if (> (- ox xx) (* 0.9 (nth 2 item)))
(setq dx (1+ dx)
xx ox
)
)
(setq rtn (cons (vllist-nth-subst item 5 dx) rtn))
)
rtn
)
;;; Sub-function: Sort Y direction of built data list and reset ExcelID-Y
(Defun xxx-sort-y (data / dy yy oy rtn)
(setq dy 1)
(foreach item (reverse (vllist-sort-n data 3))
(setq oy (nth 3 item))
(if (null yy)
(setq yy oy)
)
(if (> (abs (- oy yy)) (* 0.9 (nth 4 item)))
(setq dy (1+ dy)
yy oy
)
)
(setq rtn (cons (vllist-nth-subst item 6 dy) rtn))
)
rtn
)
;;; Sub-function: Get Max Excel Cell ID's X or Y
(Defun xxx-get-max (listx id)
(nth id (last (vllist-sort-n listx id)))
)
;;; Sub-function: Build an empty 2-dimension list
(Defun xxx-build-array (x y / dat rtn)
(repeat y
(setq dat nil)
(repeat x
(setq dat (cons "" dat))
)
(setq rtn (cons dat rtn))
)
rtn
)
;;; Sub-function: fill data in 2-dimension list
(defun xxx-subst-array (ary x y val / dat)
(setq dat (nth y ary)
dat (vllist-nth-subst dat x val)
ary (vllist-nth-subst ary y dat)
)
ary
)
;;; Sub-function: Get final data from text selectionset
;;; The return value contain two same-dimension lists, one for contents, one for TEXT object
(Defun xxx-sset->data (sset / data mx my txt item rtn obj xls)
(setq data (xxx-sort-y (xxx-sort-x (xxx-build-list sset)))
mx (xxx-get-max data 5)
my (xxx-get-max data 6)
xls (xxx-build-array mx my)
obj (xxx-build-array mx my)
)
(foreach item data
(setq txt (cdr (assoc 1 (entget (car item))))
mx (1- (nth 5 item))
my (1- (nth 6 item))
xls (xxx-subst-array xls mx my txt)
obj (xxx-subst-array obj mx my (car item))
)
)
(list xls obj)
)
(command "_.Undo" "_Group")
(princ
"\n Select [Virtual Table TEXTS] to be edit in Excel <Exit>:"
)
(if (setq ss (ssget '((0 . "text"))))
(progn
(setq data (xxx-sset->data ss)
xls (car data)
data (cadr data)
*xl* (vlxls-app-new T)
)
(vlxls-cell-put-value *xl* nil xls)
(vlxls-range-autofit (setq rng (vlxls-sheet-get-usedrange *xl* nil)))
(alert
"Switching to Excel to edit [Virtual Table TEXTS]\nSwitching back to AutoCAD when done\nAnd press OK button to perform synchonization"
)
(setq xls (vlxls-variant->list (vlax-get-property rng "Value2"))
y -1
)
(vlxls-app-quit *xl* nil)
(setq *xl* nil)
(repeat (length xls)
(setq y (1+ y)
dat1 (nth y xls)
dat2 (nth y data)
x -1
)
(repeat (length dat1)
(setq x (1+ x)
txt (nth x dat1)
)
(if (/= (setq obj (nth x dat2)) "")
(setq obj (entget obj)
obj (subst (cons 1 txt) (assoc 1 obj) obj)
obj (entmod obj)
)
)
)
)
)
)
(command "_.Undo" "_End")
(princ)
) |
|