Code generated tables not visible in AutoCAD on giving the Insertion Point

Code generated tables not visible in AutoCAD on giving the Insertion Point

priya.mishraTPDMG
Participant Participant
690 Views
4 Replies
Message 1 of 5

Code generated tables not visible in AutoCAD on giving the Insertion Point

priya.mishraTPDMG
Participant
Participant

Hi All,

 

I am creating 15 tables with the help of the below code. I am passing a 2d string array for every table. The 2d arrays are getting created and passed correctly. There's a prompt for the user to select the insertion point of the table for each table. When I run the code, the user is asked to enter the insertion point 15 times i.e. for each table, but there's no table created in the model space. 

 

Any help would be appreciated. @norman.yuan  @Jeff_M 

 

public static void CreateTable(string[,] str)
{

Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
if (pr.Status == PromptStatus.OK)
{
Autodesk.AutoCAD.DatabaseServices.Table tb = new Autodesk.AutoCAD.DatabaseServices.Table();
tb.TableStyle = db.Tablestyle;
tb.SetSize(str.GetLength(0), str.GetLength(1));
tb.SetRowHeight(50);
tb.SetColumnWidth(130);
tb.Position = pr.Value;

// Use a nested loop to add and format each cell
for (int i = 0; i < tb.Rows.Count; i++)
{
for (int j = 0; j < tb.Columns.Count; j++)
{
string item = str[i, j];
tb.Cells[i, j].TextHeight = 10;
tb.Cells[i, j].TextString = item;
tb.Cells[i, j].Alignment = CellAlignment.MiddleCenter;
tb.GenerateLayout();
}
}

using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(tb);
tr.AddNewlyCreatedDBObject(tb, true);
tr.Commit();
}
}
}

 

 

Thankyou in advance.

 

Regards,

Priya Mishra

0 Likes
Accepted solutions (2)
691 Views
4 Replies
Replies (4)
Message 2 of 5

Jeff_M
Consultant
Consultant
Accepted solution

Your code works to add a single table. Does the calling function have an uncommitted Transaction?

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 5

priya.mishraTPDMG
Participant
Participant

Thankyou @Jeff_M You were right. The calling function had an uncommitted Transaction. Now its working perfectly! 

 

@Jeff_M @norman.yuan 

Can you please help me in knowing how to generate a table of the following format through code: 

I tried the tb.UnmergeCells() and tb.MergeCells() but could not get the desired results. 

 

priyamishraTPDMG_1-1626772610733.png

 

Thanks in advance.

 

-Regards

Priya Mishra

0 Likes
Message 4 of 5

Jeff_M
Consultant
Consultant
Accepted solution

Something like this?

2021-07-20_12-42-42.png

 

        [CommandMethod("TableTest")]
        public static void CreateTable()
        {
            
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
            if (pr.Status == PromptStatus.OK)
            {
                Autodesk.AutoCAD.DatabaseServices.Table tb = new Autodesk.AutoCAD.DatabaseServices.Table();
                tb.TableStyle = db.Tablestyle;
                tb.SetSize(8,13);
                tb.SetRowHeight(50);
                tb.SetColumnWidth(80);
                tb.Position = pr.Value;

                var r1 = CellRange.Create(tb, 1, 1, 1, 12);
                var r2 = CellRange.Create(tb, 2, 1, 2, 12);
                var r3 = CellRange.Create(tb, 2, 0, 7, 0);

                tb.MergeCells(r1);
                tb.MergeCells(r2);
                tb.MergeCells(r3);


                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    btr.AppendEntity(tb);
                    tr.AddNewlyCreatedDBObject(tb, true);
                    tr.Commit();
                }
            }
        }
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 5 of 5

priya.mishraTPDMG
Participant
Participant

Thankyou so much @Jeff_M 
This is exactly what I asked for! Thanks again for the help! 

0 Likes