Author Topic: Why is this table merged?  (Read 1781 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Why is this table merged?
« on: August 27, 2015, 12:34:45 AM »
For some reason this creates a table with the cells merged. Anyone know how I can fix it?

Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6.  
  7. namespace TableStuff
  8.  
  9. {
  10.     public class CreateTable
  11.     {
  12.         static public void CreateMyTable(Point3d mypoint)
  13.  
  14.         {
  15.             Document doc = Application.DocumentManager.MdiActiveDocument;
  16.             Database db = doc.Database;
  17.             Editor ed = doc.Editor;
  18.  
  19.             Table tb = new Table();
  20.  
  21.             tb.TableStyle = db.Tablestyle;
  22.             tb.SetSize(1, 5);
  23.             tb.SetRowHeight(3);
  24.             tb.SetColumnWidth(3);
  25.             tb.Position = mypoint;
  26.  
  27.             // Create a 2-dimensional array
  28.  
  29.             // of our table contents
  30.  
  31.             string[,] str = new string[1, 5];
  32.  
  33.             str[0, 0] = "Column1";
  34.             str[0, 1] = "Column2";
  35.             str[0, 2] = "Column3";
  36.             str[0, 3] = "Column4";
  37.             str[0, 4] = "Column5";
  38.  
  39.             // Use a nested loop to add and format each cell
  40.             for (int i = 0; i < 1; i++)
  41.             {
  42.                 for (int j = 0; j < 5; j++)
  43.  
  44.                 {                    
  45.                     tb.Cells[i, j].TextHeight = 1;
  46.                     tb.Cells[i, j].TextString = str[i, j];
  47.                     tb.Cells[i, j].Alignment = CellAlignment.MiddleCenter;
  48.  
  49.                 }
  50.             }
  51.  
  52.             tb.GenerateLayout();
  53.  
  54.             Transaction tr = doc.TransactionManager.StartTransaction();
  55.  
  56.             using (tr)
  57.  
  58.             {
  59.                 BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
  60.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  61.                 btr.AppendEntity(tb);
  62.                 tr.AddNewlyCreatedDBObject(tb, true);
  63.                 tr.Commit();
  64.  
  65.             }
  66.  
  67.         }
  68.  
  69.     }
  70. }
  71.  

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Why is this table merged?
« Reply #1 on: August 27, 2015, 07:42:26 AM »
I don't see any where in your code that you are adding new columns to the table, tb.InsertColumns().
Revit 2019, AMEP 2019 64bit Win 10

ChrisCarlson

  • Guest
Re: Why is this table merged?
« Reply #2 on: August 27, 2015, 08:22:04 AM »
I would guess the issue is with the tb.SetSize, what happens if you use tb.NumRows and tb.NumColumns?

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Why is this table merged?
« Reply #3 on: August 27, 2015, 11:41:52 AM »
You are creating a Table with just one row. Your Tablestyle is set to have a Title and to merge the cells in the Title row. Change the table to have 2 rows and give the first cell the Title. Be sure to adjust the value of i to i-1 when filling in the columns.


nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Why is this table merged?
« Reply #4 on: August 27, 2015, 12:02:31 PM »
Thank you.
You are creating a Table with just one row. Your Tablestyle is set to have a Title and to merge the cells in the Title row. Change the table to have 2 rows and give the first cell the Title. Be sure to adjust the value of i to i-1 when filling in the columns.