AutoLISP Solutions: Change Your Tables9 May, 2006 By: Tony Hotchkiss
Easily replace text, attributes and cells in an AutoCAD table
Kelley Marshall of Richmond, Virginia, requested a routine that performs a number of text-replacement functions, including:
- switching selected values of text, attributes and cells in a table object;
- replacing selected values of text, attributes and cells in a table object with the value of the first one selected; and
- switching groups of cells in a table object by selecting them with a window.
This month's AutoLISP Solution includes multiline text objects as well as text, attribute values and the contents of cells in tables. Figure 1 shows a test drawing that has all of the objects that can be used to replace or switch.
Figure 1. This test drawing includes all the objects that can be replaced or switched: text, attributes, mtext and table objects.
Two versions of the program are available -- TSWITCH-SHX.LSP and TSWITCH-TTF.LSP. The SHX designation is for the standard shape file fonts, and the TTF designation is for TrueType fonts. I made this distinction because the command prompt is slightly different for setting the different types fonts. The programs run only in AutoCAD 2005 and 2006 because earlier versions don't contain table objects and they haven't been tested.
Get the Code
Download the TSWITCH-SHX.LSP and TSWITCH-TTF.LSP files Cadalyst's CAD Tips site. Save the file in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then use the browser to select the file.
How to Use the TSWITCH Code
To start the program, enter TSE, and the [Replace/Multiple-switch/
Figure 2. The [Replace/Multiple-switch/<Switch>] prompt.
The prompts are shown on the screen for AutoCAD 2006 and in the command line for AutoCAD 2005. The default selection is Switch, and you can press Enter (or type S) to select it. You are prompted to Select text, attribute, or table cell (Esc or Enter to stop). If you don't select an object, you are prompted with No entity selected, try again. To select text, the cursor must be on a character of the text. It's possible to select a position in between the letters of the text and that produces the No entity warning.
After a successful selection, the text is highlighted and the prompt Switch with text, attribute, or table cell: displays. When you select the text, it's automatically switched with the first text selected, and the process is repeated so that you can continue to switch text, attributes and table cells in any combination until you press Enter or ESC. The highlighting for Text, Mtext and Attribute entities is done in the conventional way with a dotted-line appearance. Table cell contents are highlighted by changing their color temporarily to the gray color (number 8).
If you're using the TrueType font version, when the program is active, all text fonts temporarily appear as TXT as shown in figure 2 because it's easier to allow for text selection of a table cell if the font is TXT rather than a more complex font. The table cells can only be selected if they have not been merged.
If you select the Replace option, you see the prompt Source text, attribute, or table cell:, and if you select empty space, the program shows the No entity warning. A successful selection prompts Destination text, attribute, or cell (
If you select the Multiple-switch option, you are prompted to Select table. If a table is not selected, you are warned with the prompt You must select a table, try again. When you select the table, the prompt Select cells with a window displays, followed by Specify opposite corner in the conventional manner
Figure 3. The Number of cells not equal alert.
The number of cells must be the same in each case, otherwise an alert box displays with the warning Number of cells not equal (figure 3).
The program was written in AutoCAD 2006. I tested it in AutoCAD 2006 and Kelley Marshall tested it in AutoCAD 2005. (Thanks for the assistance, Kelley!)
The versions TSWITCH-SHX.LSP and TSWITCH-TTF.LSP are almost identical with the difference being three lines of code commented out in the SHX version. The main function that follows the error handler and system variable setting functions is TXTSTR-EDIT, and it calls the functions DO-SWITCH, DO-REPLACE and DO-MULTIPLE, depending on the user response to the call to GETKWORD that gives those options.
DO-SWITCH uses NENTSELP to select the Text, Mtext, Attribute and Table cell entities. The challenging part here was to highlight the first entities when they are selected. The standard method to highlight objects in Visual LISP is to use:
(vla-Highlight obj1 :vlax-True)
This works only with simple objects that are not part of a group. It doesn't highlight attribute values or the contents of a table cell, so I wrote a HILITE function as follows:
(defun hilite (en obj p)
(setq txt (vla-get-TextString obj)
height (vla-get-Height obj)
) ;_ end of setq
(setq tempobj (vla-AddText *modelspace* txt p height))
(vla-Highlight tempobj :vlax-True)
) ;_ end of hilite
HILITE creates a new temporary TEXT object and highlights it using the standard VLA-HIGHLIGHT function as before. The origin point P in the function is the insertion point of the attribute text. Unfortunately, this function does not work with table cells because the insertion point of the cell contents is not the same as the insertion point of a TEXT object, so I used the following code to change the color of the table cell content object:
(setq idx (vla-get-ColorIndex col))
(vla-put-ColorIndex col 8)
(vla-Put-TrueColor obj1 col)
(vla-regen *thisdrawing* acActiveViewport)
I chose color number 8, a gray, as shown, and I had to regenerate the drawing to show the new color on the screen. After the switching had been done, the color changed back to its original number.
The actual switching of text was easily done by using the following to get the text values:
(setq txt1 (vla-get-TextString obj1)
txt2 (vla-get-TextString obj2)
) ;_ end of setq
Then the switch itself used the following VLA-PUT functions:
(vla-put-TextString obj1 txt2)
(vla-put-TextString obj2 txt1)
The DO-REPLACE function uses very similar logic to that of DO-SWITCH, except that for DO-SWITCH the entire process was contained in a loop, whereas the DO-REPLACE function only looped the destination text.
The DO-MULTIPLE function is fairly short because it calls other functions as shown in the following:
(defun do-Multiple ()
(setv "OSMODE" 0)
(setq tableobj (get-tableobj))
(setq cells1 (get-cell-contents tableobj))
(prompt "\nSwitch with cells ")
(setq cells2 (get-cell-contents tableobj))
(if (= (length cells2) (length cells1))
(switch-cells tableobj cells1 cells2)
(alert "Number of cells not equal")
) ;_ end of if
) ;_ end of do-Multiple
I didn't spend much energy in designating local variables in these routines, but no conflicts in values appear anywhere.
As always, I look forward to receiving your comments and requests for AutoLISP Solutions. Contact me using the link below.
Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's Tips & Tricks Tuesdays free e-newsletter and we'll notify you every time a new video tip is available. All exclusively from Cadalyst!
Autodesk Gallery Powers of Design Exhibit: 10**15 Cat's Eye Nebula 23 May, 2013
Load ‘Em Up! Stackers, Conveyors, and Advanced Assembly 23 May, 2013
Excel Hyperlinks & Document Management Tricks 22 May, 2013
New Certification Logos And Certificate Designs Available 23 May, 2013
Cool Tools of Doom n’ Stuff: Episode 22 23 May, 2013