Tables are one of the most complex objects, after dimensions, to create and edit through the APIs that AutoCAD offers. The appearance of a table is controlled by its properties and methods, along with the table style that it is assigned. Tables are made up of individual cells which can contain text, blocks, and formulas along with additional formatting.
In the previous posting, Creating a Table Style with AutoLISP and the ActiveX API, I showed an exmple of how to create a custom table style.
In this article, I show for to create a table and then edit the value of a cell by picking a point within a table cell.
(vl-load-com)
;; Example adds a table to model space that is 5 rows by 3 columns
;; with a row height of 10 units and column width of 30 units
(defun c:AddTable( / acadObj curDoc insPt mSpaceObj tableObj)
(setq acadObj (vlax-get-acad-object))
(setq curDoc (vla-get-ActiveDocument acadObj))
(setq insPt (vlax-3d-point 0 0 0))
(setq mSpaceObj (vla-get-ModelSpace curDoc))
(setq tableObj (vla-Addtable mSpaceObj insPt 5 3 10 30))
(vla-put-StyleName tableObj "MyTableStyle")
)
;; Example shows how to pick a single table cell on screen and change its value.
;; This example demonstrates the ActiveX properties/methods HitTest,
;; GetCellType, GetText and SetText.
(defun c:SelectTableCell ( / pick vHeight vWidth lwrLeft uprRight vector
SS_TABLES cnt eMax tableObj row col cellValueOrg)
;; Ask the user for a point on screen
(if (/= (setq pick (vlax-3d-point (getpoint "\nSelect Cell to edit: "))) nil)
(progn
;; Get the corners of the screen display to build our selection set
(setq vHeight (getvar "viewsize"))
(setq vWidth (* (/ (nth 0 (getvar "screensize")) (nth 1 (getvar "screensize"))) vHeight))
(setq lwrLeft (list (- (nth 0 (getvar "viewctr")) (/ vWidth 2)) (- (nth 1 (getvar "viewctr")) (/ vHeight 2)) 0))
(setq uprRight (list (+ (nth 0 (getvar "viewctr")) (/ vWidth 2)) (+ (nth 1 (getvar "viewctr")) (/ vHeight 2)) 0))
;; Get the current display orientation
(setq vector (vlax-make-safearray vlax-vbDouble '(0 . 2)))
(vlax-safearray-fill vector '(1 1 1))
(setq vector (vlax-make-variant vector))
;; Select all the table objects visible on screen
(if (setq SS_TABLES (ssget "C" lwrleft uprright (list (cons 0 "ACAD_TABLE"))))
(progn
(setq cnt 0
eMax (sslength SS_TABLES)
)
;; Step through all the items in the selection set
(while (> eMax cnt)
;; Geta table object from the selection set
(setq tableObj (vlax-ename->vla-object (ssname SS_TABLES cnt)))
;; Return values for what cell was picked in
(setq row 0
col 0)
;; Below is also a sample to see if a valid cell is picked
;; (vla-select table pick vector vector 5 5 :vlax-false 'row 'col)
;; Check to see if a valid cell was picked
(if (= (vla-hittest tableObj pick vector 'row 'col) :vlax-true)
(progn
;; Get out of the loop
(setq cnt (1+ eMax))
;; Check to see what the Cell Type is (Text or Block)
(if (= (vla-GetCellType tableObj row col) acTextCell)
(progn
;; Let's get the value out
(setq cellValueOrg (vla-GetText tableObj row col))
;; Change the current value
(vla-SetText tableObj row col "Revised Text")
(vla-Update tableObj)
(alert "Cell text was changed.")
;; Restore the original value
(vla-SetText tableObj row col cellValueOrg)
(vla-Update tableObj)
(alert "Cell text was changed back to the original value.")
(setq cnt eMax)
)
)
)
)
(setq cnt (1+ cnt))
)
)
)
)
)
(princ)
)
Sincerely,
Lee
I have been looking at this routine which works great except I want to change the color of the text in the cell, is there a way to do that, I have tried using SetColor but am having no luck with it.
Posted by: Christos | Sunday, April 03, 2016 at 11:05 PM
Sorry about the delay... I am currently on sabbatical, I will try and take a look at this tonight. You might have to get the actual MText object in the cell and change its color directly.
Posted by: Lee Ambrosius | Tuesday, April 05, 2016 at 02:35 PM
At the moment we edit the cell and highlight all of the text and just change the text manually...pretty slow process
Posted by: Christos | Thursday, April 07, 2016 at 06:44 PM