I'm trying to build a table in my Net code and success is kinda spotty at this point.
I could find NO code examples in the sdk, no C# example in this ng and just a couple
of vb examples (here). So I took what I needed from the vb stuff and translated it to
C# and thats how I got the below code.
My biggest problem is that after the grid is constructed, if I try to add text to any
cells I get an access violation exception. Im sure Im using the settext function right,
according to the less-than-spectacular documentation, so Im at a loss as to whats wrong.
As long as I dont attempt to put any data in the table, it appears ok.
Other, less harmfull problems is with table styles. I created a style, which does appear
in the styles table and is applied to my table but the style is pretty spotty too.
For example, adjusting lineweights seems to do nothing. "Thick" lines show up in the styles
editor dialogs preview (most of them at least), but the table does not have any changed
thicknesses, nor does the drop-down list in the styles dialog show a thick line, it remains
"byblock". Also, as you can see I want a title row and headers in my table, but I end up
with a title row in every column! even though the "preview" shows a single title spanning
the width of the table.
Pretty frustrating. Sure would be nice to have better documentation, with code examples
and possible pitfalls.
Has anyone done some nice tables with C#? preferably with custom styles?
private ObjectId makeTableStyle()
ObjectId styDef = ObjectId.Null;
Color color1 = Color.FromColorIndex(ColorMethod.ByAci, 1);
Color color2 = Color.FromColorIndex(ColorMethod.ByAci, 2);
Color color3 = Color.FromColorIndex(ColorMethod.ByAci, 3);
using (Database dBase = HostApplicationServices.WorkingDatabase)
using (Transaction trans = dBase.TransactionManager.StartTransaction())
TableStyle listStyle = new TableStyle();
//First, get the tablestyle dictionary
using (DBDictionary tblStyle = (DBDictionary)trans.GetObject(dBase.TableStyleDictionaryId, OpenMode.ForRead, false))
//if it exists, just get it
listStyle = (TableStyle)trans.GetObject(tblStyle.GetAt("DWG-LIST"), OpenMode.ForRead);
//Doesn't exist, so create it
styDef = tblStyle.SetAt("DWG-LIST", listStyle);
listStyle.Name = "drawing list";
listStyle.Description = "list style for cover sheet";
listStyle.IsHeaderSuppressed = false;
listStyle.IsTitleSuppressed = false;
//make a heavy "box" around the table
listStyle.SetGridLineWeight(LineWeight.LineWeight100, 1, 2);
listStyle.SetGridLineWeight(LineWeight.LineWeight100, 1, 1);
listStyle.SetGridLineWeight(LineWeight.LineWeight100, 8, 1);
listStyle.SetGridLineWeight(LineWeight.LineWeight100, 0x20, 1);
listStyle.SetTextHeight(0.2, 2);//title row
listStyle.SetTextHeight(0.15, 4);//header row
listStyle.SetTextHeight(0.1, 1);//data row
listStyle.SetAlignment(CellAlignment.MiddleCenter, 2);//title row
listStyle.SetAlignment(CellAlignment.MiddleCenter, 4);//header row
listStyle.SetAlignment(CellAlignment.MiddleCenter, 1);//data row
listStyle.SetColor(color1, 2);//title row
listStyle.SetColor(color2, 4);//header row
listStyle.SetColor(color3, 1);//data row
I found that creating a table style in autocad itself and saving it was the easiest option, just insert your table into the drawing and get your user to select the empty (except for title and headers) table. Add some rows and fill them.
They are a beastie and don't quite work as you would expect (ie. ref cells by row/column number) and had me guessing for quite a while!
here's my simple solution. It reads it's data from a dataset using ado.
PromptEntityOptions entOpt = new PromptEntityOptions("\nSelect Table: ");
entOpt.SetRejectMessage("\nOnly Tables may be selected.");
Mick Duprez wrote:
> I found that creating a table style in autocad itself and saving it was the easiest option, just insert your table into the drawing and get your user to select the empty (except for title and headers) table. Add some rows and fill them.
> They are a beastie and don't quite work as you would expect (ie. ref cells by row/column number) and had me guessing for quite a while!
> here's my simple solution. It reads it's data from a dataset using ado.
Thanks for the tip Mick. As this function I'm working on is part of a larger app I really want to do it
programatically. I could live without creating the table style but if the table itself wont construct
properly then its a lost cause. So far, no one can tell me why I cant insert text into my table. Wonder
if it would make any difference if I added rows via "insertRow" as opposed to using the properties of the
the solution for me was to insert the function Table.GenerateLayout();
before any insertion of text and after you have defined the proper sizes (row and columns).
I'm too working to get tables created programmatically.