Vous êtes sur la page 1sur 16

Visual LISP Table Magic

Peter Jamtgaard P.E. Cordeck Sales, Inc.

CP311-3

This class will present and explain several routines for creating, populating, and manipulating tables using Visual LISP. These routines can be used for developing custom bills of material, schedules, or spreadsheets automatically.

About the Speaker:


Peter has 21 years of experience running AutoCAD in a multidiscipline environment, and is now focused on heavy steel construction and structural engineering. His current position is senior engineer and CAD manager for Cordeck Sales, Inc. He has served on the AUGI Board of Directors and is a registered Autodesk developer. Peter has been programming AutoCAD for 21 years with AutoLISP, VisualLISP, and VB(A). He holds a Bachelor of Science degree in Civil and Environmental Engineering and is a licensed professional engineer in the state of Wisconsin. peter@cordecksales.com

Introduction: It is my intention to present in this class the fundamentals for manipulating AutoCAD Tables and Table Styles using Visual LISP. This class is for programmers of Visual LISP that have some understanding of ActiveX programming techniques. The course will include Visual LISP functions that create a table style, modify a table style, create a table, populate a table, adjust a table, perform a schedule of blocks in a drawingetc. Before we can create tables, it is necessary to create a table style. To create a table style in AutoCAD you can navigate From the TABLE Command > Insert Table Dialog Box > Table Style Settings Button > Table Style Dialog New Button. Shown below are the dialog boxes used to create tables and table styles for reference.

Figure 1 - Insert Table Dialog

Figure 2 - Table Style Dialog

Figure 3 - Create New Table Style Dialog

Figure 4 - Modify Table Style Dialog AU07:GetOrCreateTableStyle function


This first function is used to get or create a tablestyle object. It accepts one argument strTableStyleName which is the name of the new or existing tablestyle. (If you are not familiar with my variable naming style, I use a style similar to the VBA Reddick naming convention that includes a two or three letter prefix indicating the type of variable and then capitalize the words in the descriptive name. In this case I used str for string and the variable holds the Table Style Name.) The table styles are stored inside the drawing within the ACAD_TABLESTYLE dictionary. To create a new table style the addobject method is used. Since this function will get or create a table style there is the possibility that the table style already exists, an error will occur. To trap this error and prevent the routine from crashing, I am utilizing my standard error trap function. It will return a nil, if an error is encountered, or a True or returned value if no error occurs. So if the table style exists, it will return a nil and then the item method can be used to select the existing table style.

The setvariable method of the activedocument object is used to set the system variable CTableStyle or the current table style. The setvariable method is similar to the setvar lisp expression but can be used in reactor callback functions. This function does a vlax-dump-object of the table style so you can become familiar with the properties and methods of the table style object. I would recommend you commenting out or removing the vlax-dump-object expression after you have reviewed them.

AU07:ErrorTrap function
It will return a nil, if an error is encountered, or a True or returned value if no error occurs. To utilize this function just wrap a lisp expression with a quote expression and pass it to this function like (au07:Errotrap (quote (/ 1.0 0.0))) This function utilizes the vl-catch-all-apply expression and its ability to trap errors.

AU07:SetTableStyleDefaults function
If you look at Figure 4 Modify Table Style Dialog box, you can begin to see that the properties and methods of the table styles listed on the previous page correlate to the options on the dialog box. This next function will set the table style to be proportional to the current text size. Make sure that the textsize in your drawing is not set to zero. The routine starts by changing the description to be the same as the name of the table style, the flow direction puts the title on top (1 would be from the bottom), It does not suppress the title or headers. I then get the textsize system variable to adjust the tables proportions. The horzcellmargin and vertcellmargin methods change the horizontal and vertical margins to be the text size. There are 3 types of rows in a table. The data, title, and header row types. The unknown type is all of the above. It sets the textstyle and textsize of the different types of rows. It also sets the alignment of the headers and title rows to middle center and the data rows to left center using the AutoCAD Cell alignments shown below.

AU07:CreateTable function
Now that we have the functions to create table styles, we need a function to create a table. This function accepts three arguments the insertion point, number of rows and number of columns. . The textsize is also used to set the initial width and height of the cells and then uses the addtable method of the block collection of the active layout to create the table. The function does a vlax-dump-object of the new table object and then returns the new table object. Again I have provided a vlax-dump-object expression so you can become more familiar with the properties and methods of the table object. I would recommend commenting out or removing it after you have reviewed them.

AU07:CSVFileToList function
Now we need to get some data. One place to get data is from a spreadsheet, or more simply from a comma separated value (CSV) ASCII text file, that can be created from Excel. Here is a function that can convert a CSV file into a list of sublists. It accepts two arguments including the name (with path) of the CSV file and the character(s) used to parse the strings like a comma or semicolon.

AU07:CSVStringToList
The AU07:CSVStringToList function will parse the CSV strings into sublists. Like: (AU07:CSVStringToList Peter,Bob,Kathy ,) returns (Peter Bob Kathy)

AU07:ListToTable function
We are almost there! We still need a function to populate a table from a list of sublists. This function accepts three arguments, including the insertion point of the table, the title of the table and the data in the form of a list of sublists. From the list of sublists we can determine the number of rows using the length expression. The number of columns is the max length of the list of sublists. This function assumes that the first sub-list is the headers. The settext method is used to assign the value of the title, and then with a couple nested foreach expressions, each of the data row values are set similarly.

C:CSVFileToTable Routine
Lets pull it all together and cook our first command line function to create a table. It first creates our Table Style, then sets up our defaults. It then prompts to select a CSV file and converts it to a list of sublists. Next it prompts for the title of the table and then asks for the insertion point and creates our table.

AU07:CountAllBlocks Function
This function accepts two arguments including the drawing document object and a wildcard string of the names of the layouts to search. This can search the activedocument or another open document in the documents collection or use a dbx module (not presented here) to search a drawing that is not even open. It uses two nested vlax-for expressions to iterate thought the layouts and block items in each layout searching for blocks, ignoring blocks starting with * like dimensions. When it finds a block it either adds its effective name to the list or increases its count by one. It ends by sorting the list blocks alphabetically and returns the list.

AU07:GetBlockCostAndShow Function
When you create a schedule of blocks with in a drawing there may be blocks that you might want to exclude from the table. Along with this group of functions I created a dialog box control that allow the user to indicate what blocks will be displayed, (Hide or Show) and also attach a unit price for a block that can be used to create a Bill of Materials. In the count all blocks function, it uses this function to filter the blocks. The block cost and show information is stored as xdata on the block definition object.

AU07:SetBlockCostAndShow Function
The set block cost and show attaches the xdata to the block definition.

Before we create a Schedule, it would be nice to have a function that would place an image of the block into the table.

AU07:ConvertBlockNameToSymbol Function
The way that the symbols are displayed is by first setting the text value of the cell to be the name of the block and then changing the cell type from an acTextCell to an acBlockCell. This function changes the text value in the cell to be the objectId of the block definition block. Then it uses the SetBlockObjectId method to convert the objectId text into a symbol. This function accepts two arguments including the table object and the column index where the symbols are to be displayed. (0 is the first column, etc) I am using the and expression and several error traps to make sure it doesnt crash.

C:Schedule Routine
This routine creates a schedule of blocks for a drawing in a table that includes each block name, the block symbol and the total number of instances of each block.

As you may notice the columns in the Schedule are not well proportioned. The names of the blocks take up two lines, the symbols and count columns widths could be much narrower.

AU07:ProportionTable Function
This function has one argument, the table object. To proportion the table first this routine proportions the columns by creating a text object in the activelayout and then setting the properties of that text object to match the properties of the text in each cell. By then taking the bounding box of that text, the width of the column can be determined by the widest bounding box. After iterating through each column and setting the width of each, this routine iterates through each row and sets its height to be three times the textsize.

In the C:Schedule function we change the third column (index 2) to be the count of each block. For that column I decided to change the justification to be right justified for numbers. The following function has three arguments including the table object, the column and the Autocad justification symbol. The routine iterates through each data row of the specified column and uses the SetCellAlignment method to change the justification.

AU07:AlignColumn Function

Thank you for attending, and please remember to fill out the evaluation. Thank you Peter Jamtgaard

Vous aimerez peut-être aussi