COT Convert Old TableV1.4
COT Convert Old TableV1.4
2010
;-------------------------------------------------------------------------------------------------------------------------------;-------------------------------------- GATHERING TABLE INFORMATION ----------------------------------;-------------------------------------------------------------------------------------------------------------------------------(defun tableinfo ( ss / n entlist)
(setq n 0)
(repeat (sslength ss)
(setq entlist (entget (ssname ss n)))
(cond ((member (cdr (assoc 0 entlist)) '("LINE" "POLYLINE"))
(getlinepts entlist)(setq linelist (cons (ssname ss n) linelist)))
((member (cdr (assoc 0 entlist)) '("TEXT" "MTEXT"))
(setq textlist (cons (ssname ss n) textlist)))
((member (cdr (assoc 0 entlist)) '("INSERT"))
(setq blocklist (cons (ssname ss n) blocklist)))
)
(setq n (1+ n))
)
)
;-------------------------- Cell Count/Height/Width Determination --------------------;;Gathers x and y positions of lines and polylines in separate lists
;;This is used to determine height/width & # of rows/columns
;;Line info must be gathered first in order to determine
;;cell position of any other gathered information
;-------------------------------------------------------------------------------------(defun getlinepts (alist / x xpt ypt)
(foreach x alist
(if (member (car x) '(10 11))
(progn
(if (not (vl-position (setq xpt (atof (rtos (car (trans (cdr x) 0 1))
2 8))) lpxlist))
(setq lpxlist (cons xpt lpxlist)))
(if (not (vl-position (setq ypt (atof (rtos (cadr (trans (cdr x) 0 1)
) 2 8))) lpylist))
(setq lpylist (cons ypt lpylist)))
)
)
)
);defun
;---------------------------- Text Info and Cell Position ---------------------------------------------------;;Determine cell position by insertionpoint of text objects
;;(Using text center is probably more reliable)
;;Create list of lists containing row, column, textstring and textheight
;;to be used to fill acad table after creation
;;If row and column is already in list, replace with combined string
;------------------------------------------------------------------------------------------------------------(defun gettxtinfo (alist / x vlaobj pos rpos cpos expos)
(setq vlaobj (vlax-ename->vla-object txt)
pos (trans (vlax-safearray->list (vlax-variant-value (vla-get-insertionp
oint vlaobj))) 0 1)
rpos (1- (vl-position (cadr pos)(vl-sort (cons (cadr pos) lpylist) '>)))
cpos (if (zerop rpos) 0 (1- (vl-position (car pos) (vl-sort (cons (car p
os) lpxlist) '<)))))
(if (setq expos (vl-position (list rpos cpos) (mapcar '(lambda (x)(list (car (ca
(setq tstylelst2
(mapcar
'(lambda (x / txt)
(setq txt x spos 0)
(while (setq spos (vl-string-position (ascii " ") txt spos))
(setq txt (vl-string-subst "" " " txt spos))
)
txt
)
tstylelst
)
)
(initget (setq tstylelst3 (apply 'strcat (mapcar '(lambda (x)
(if (nth (1+ (setq i (1+ i))) tstylelst2)
(strcat x " ") (strcat x))) tstylelst2))))
(setq spos -3)
(while (setq spos (vl-string-position (ascii " ") tstylelst3 (+ spos 3)))
(setq tstylelst3 (vl-string-subst " / " " " tstylelst3 spos))
)
(setq kword
(if (setq kword (getkword (strcat "\nSelect Table Style: [ " tstylelst3 " ]:<Sta
ndard>"))) kword "Standard"))
(setq kword (nth (vl-position kword tstylelst2) tstylelst))
(initget "Yes No")
(setq mergekword (if (setq mergekword (getkword "\nSearch for merged cells? [ Ye
s / No ]:<Yes>")) mergekword "Yes"))
(vla-put-horzcellmargin (vla-item tstyle kword) 0.0)
(vla-put-vertcellmargin (vla-item tstyle kword) 0.0)
(setq otcontents (ssget))
(princ "\nSorting Line Info...")
(tableinfo otcontents)
(setq lpxlist (vl-sort lpxlist '<) lpylist (vl-sort lpylist '>))
(princ "\nSorting Text Info...")
(mapcar '(lambda (txt)(gettxtinfo (entget txt))) textlist)
(princ "\nSorting Block Info...")
(mapcar '(lambda (blk)(getblockinfo blk)) blocklist)
(setq colwidths (mapcar '(lambda (x)(- (nth (1+ (vl-position x lpxlist)) lpxlist
) x))(reverse (cdr (reverse lpxlist))))
rowheights (mapcar '(lambda (x)(- x (nth (1+ (vl-position x lpylist)) lpyl
ist)))(reverse(cdr (reverse lpylist)))))
(setq p0 (vlax-3d-point (trans (list (car lpxlist) (car lpylist) 0.0) 1 0)));;<--Table Placement (Currently using Top Left corner)
(if (eq mergekword "Yes")
(progn
(princ "\nSearching for merged cells...")
(princ)
(setvar 'nomutt 1)
;-----------------------------------Method to determine which cells to merge------------------------------------------;Method fails if missed selection is not possible at zoom level.
;Currently only merges horizontally or vertically;
;To determine which cells to merge, a selection at point is used.
;For each row, a selection is attempted at each vertical line at row's center.
;If no selection is made, the point is at the center or left of horizontally mer
ged cells.
;For each column, a selection is attempted at each horizontal line at column's c
enter.
;If no selection is made, the point is at the center or upper region of vertical
ly merged cells.
);if
(setq check nil mlist nil)
));lambda
(cdr rheights)
);mapcar
);lambda
lpxlist
colwidths
);mapcar
(setvar 'nomutt 0)
);progn
);if
(mapcar '(lambda (x)(entdel x)) linelist);;Delete all lines in selection set
;-------------------------------------- Table Creation and Info Placement----------------------------------------------;;Create table object
;;Fill table with gathered text and block info and set selected style.
;----------------------------------------------------------------------------------------------------------------------(princ "\nCreating Table...")
(setq tblobj
(vla-addtable
*Space* p0
(float (1- (length lpylist)))
(float (1- (length lpxlist)))
(apply 'max rowheights)
(apply 'max colwidths)))
(vla-put-regeneratetablesuppressed tblobj :vlax-true)
(princ "\nProcessing Text Info...")
(mapcar
'(lambda (x)
(vla-settext tblobj (caar x) (cadar x) (cadr x))
(vla-setcelltextheight tblobj (caar x) (cadar x) (caddr x))
)
tinfo
)
(princ "\nProcessing Block Info...")
(mapcar
'(lambda (x)
(vla-setcelltype tblobj (caar x) (cadar x) acBlockCell)
(vla-SetBlockTableRecordId tblobj (caar x) (cadar x) (cadr x) :vlax-false)
(mapcar '(lambda (y)
(vla-setblockattributevalue tblobj (caar x) (cadar x) (car y) (c
dr y)))
(nth 2 x))
(vla-SetBlockScale tblobj (caar x)(cadar x) (car (reverse x)))
)
binfo
)
(vla-put-StyleName tblObj kword)
(if (eq mergekword "Yes")
(progn
(princ "\nProcessing Merge Info")
;---------------------------------------- Method used to merge cells ---------------------------------------------------;For each list of cells to merge
;All cell content is combined and placed in the first cell
;The max cell text height found in the cells to merge is applied to the first ce
ll
;Cells are merged and content of first cell is displayed.
;-----------------------------------------------------------------------------------------------------------------------(mapcar
'(lambda (x / newstring cnumb thlist)
(setq newstring "" cnumb (1- (cadr x)))
(repeat (- (1+ (cadddr x)) (cadr x))
(setq newstring (strcat newstring (if (eq newstring "") "" " ") (vla-gett
ext tblobj (car x) (setq cnumb (1+ cnumb)))))
(if (/= (vla-gettext tblobj (car x) cnumb) "")
(setq thlist (cons (vla-getcelltextheight tblobj (car x) cnumb) thlis
t)))
)
(vla-settext tblobj (car x) (cadr x) newstring)
(if thlist (vla-setcelltextheight tblobj (car x)(cadr x)(apply 'max thlist)))
(vla-mergecells tblobj (car x) (caddr x) (cadr x) (cadddr x))
)
hmergelist
)
(mapcar
'(lambda (x / newstring rnumb thlist)
(setq newstring "" rnumb (1- (car x)))
(repeat (- (1+ (caddr x)) (car x))
(setq newstring (strcat newstring (if (eq newstring "") "" " ")(vla-gette
xt tblobj (setq rnumb (1+ rnumb)) (cadr x))))
(if (/= (vla-gettext tblobj rnumb (cadr x)) "")
(setq thlist (cons (vla-getcelltextheight tblobj rnumb (cadr x)) thlis
t)))
)
(vla-settext tblobj (car x) (cadr x) newstring)
(if thlist (vla-setcelltextheight tblobj (car x)(cadr x)(apply 'max thlist)))
(vla-mergecells tblobj (car x) (caddr x) (cadr x) (cadddr x))
)
vmergelist
)
));if
;-----------------------------------------------------------------------------------------------------------------------(setq i -1)
(mapcar
'(lambda (x)
(vla-setcolumnwidth tblobj (setq i (1+ i)) x)
)
colwidths
)
(setq i -1)
(mapcar
'(lambda (x)
(vla-setrowheight tblobj (setq i (1+ i)) x)
)
rowheights
)
(vla-put-regeneratetablesuppressed tblobj :vlax-false)
(vla-rotate tblobj p0 (- (* 2 pi) (getvar 'viewtwist)))
(princ "\nConversion Complete")
(mapcar '(lambda (x)(and x (not (vlax-object-released-p x))(vlax-release-object
x))) (list ssitem))