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?
Perry
--------------------------------------------------------------------------------
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))
{
try
{
//if it exists, just get it
listStyle = (TableStyle)trans.GetObject(tblStyle.GetAt("DWG-LIST"), OpenMode.ForRead);
}
catch
{
//Doesn't exist, so create it
tblStyle.UpgradeOpen();
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.SetTextStyle(objectid, 1);
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
trans.AddNewlyCreatedDBObject(listStyle, true);
}
}
trans.Commit();
}
}
return styDef;
}
private void addTable()
{
#if (DEBUG)
Utilities.prompt("\nIn cover sheet add.");
#endif
using (Database dBase = HostApplicationServices.WorkingDatabase)
{
using (Transaction trans = dBase.TransactionManager.StartTransaction())
{
if (dwgList.Count > 0)
{
try
{
Point3d position = new Point3d(2.0, 2.0, 0.0);
int numRows = 3;
int numColumns = 3;
double rowHeight = 0.2;
BlockTable blkTbl = (BlockTable)trans.GetObject(dBase.BlockTableId, OpenMode.ForRead);
BlockTableRecord blkTblRec = (BlockTableRecord)trans.GetObject(blkTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
ObjectId styDef = makeTableStyle();
Table myTable = new Table();
myTable.SetRowHeight(rowHeight);
myTable.NumColumns = numColumns;
myTable.NumRows = numRows;
myTable.Height = numColumns * rowHeight;
myTable.Position = position;
myTable.TableStyle = styDef;
//myTable.SetTextString(0, 0, "bozo"); //Exception
//Add the reference to PaperSpace
blkTblRec.AppendEntity(myTable);
//Let the transaction know about it
trans.AddNewlyCreatedDBObject(myTable, true);
myTable.Dispose();
blkTbl.Dispose();
blkTblRec.Dispose();
}
catch (System.Exception ex)
{
Utilities.Message(ex);
}
}
trans.Commit();
}
}
}