.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

the trouble with tables

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
727 Views, 4 Replies

the trouble with tables

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();
}
}
}
4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

So nobody has used tables in C# eh?
Message 3 of 5
Mick Duprez
in reply to: Anonymous

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.");
entOpt.AddAllowedClass(typeof(Table), true);

//Start the selection ...

PromptEntityResult entRes = ed.GetEntity(entOpt);

//If a Table was successfully selected, proceed

if (entRes.Status != PromptStatus.OK)
{
return;
}

Table mTable = tr.GetObject(entRes.ObjectId,OpenMode.ForWrite)as Table;

int j = 1;
string item = "n/a";
foreach(DataRow dr in dt.Rows)
{
if(dr["itemno"].ToString() == "tbc")
{
item = j.ToString();
j++;
}
else
{
item = dr["itemno"].ToString();
}
mTable.InsertRows(2,1.0,1);
ed.WriteMessage("\n" + item + " " + dr["qty"]);
mTable.SetTextString(2, 0, item );
mTable.SetTextString(2, 1, dr["qty"].ToString());
mTable.SetTextString(2, 2, dr["material"].ToString());
mTable.SetTextString(2, 3, dr["length"].ToString());
mTable.SetTextString(2, 4, dr["mass"].ToString());
}
Message 4 of 5
Anonymous
in reply to: Anonymous

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
table.
Perry
Message 5 of 5
jp
in reply to: Anonymous

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost