.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 4 (222 Views)

Fill a table inside specified rectangle

222 Views, 3 Replies
06-23-2013 06:20 AM

Let's say I have a rectangle and I want to fill a table inside of it.

The below is the code snippet I wrote.....

 

Table tb = new Table();
tb.NumColumns = 5;
tb.NumRows = 40;
tb.SetRowHeight(C_height / 40);
tb.SetColumnWidth(C_width / 5);
tb.Position = new Point3d(ext.MaxPoint.X, total_MaxPoint.Y, 0);
tb.Width = C_width;
tb.Height = C_height;
tb.SetTextHeight(C_height / 40, (int)RowType.DataRow);
tb.HorizontalCellMargin = 0;
tb.VerticalCellMargin = 0;

btr.AppendEntity(tb);
tr.AddNewlyCreatedDBObject(tb, true);

 

Where C_width and C_height are the width and height of the rectangle respectively.

Then, I got the following

 

table.PNG

 

The red line is the specified rectangle. It seems like SetRowHeight and SetColumnWidth are not working.

FYI, the rectangle size is around width = 3.5m height = 5m.

 

Am I doing something wrong???

 

Thank you in advance.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 4 (183 Views)

Re: Fill a table inside specified rectangle

06-24-2013 10:02 AM in reply to: dynamicscope

Try this scratch, just change table position by calculus of upper left corner 

Also I used syntax for table in A2010, in other release it may be different

        [CommandMethod("rct", CommandFlags.Modal)]
        public void CreateTableByRectangle()
        {
            // based on code written by Kean Walmsley     
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
           
                       PromptPointOptions ppo = new PromptPointOptions("\nFirst corner: ");
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK) return;
            PromptCornerOptions pco = new PromptCornerOptions("\nOther corner: ", ppr.Value);
            PromptPointResult pcr = ed.GetCorner(pco);
            if (pcr.Status != PromptStatus.OK) return;
            Point3d p1 = ppr.Value;
            Point3d p2 = pcr.Value;
            if (p1.X == p2.X || p1.Y == p2.Y)
            {
                ed.WriteMessage("\nInvalid coordinate specification");
                return;
            }//end getcorner
            PromptIntegerOptions pio = new PromptIntegerOptions("");
            pio.Message = "\nEnter the number of rows: ";

            // Restrict input to positive and non-negative values
            pio.AllowZero = false;
            pio.AllowNegative = false;
            // Add default value
            pio.DefaultValue = 10;
            pio.AllowNone = true;

            // Get the value entered by the user
            PromptIntegerResult ires = ed.GetInteger(pio);
            if (ires.Status != PromptStatus.OK) return;

            int row_num = ires.Value;

            ed.WriteMessage("\nRows count entered\t{0}",  row_num);
            pio.Message = "\nEnter the number of columns: ";
            ires = ed.GetInteger(pio);
            if (ires.Status != PromptStatus.OK) return;

            int col_num = ires.Value;
            /*Do your stuff here*/
            double wid = Math.Abs(p1.X - p2.X);
            double hgt = Math.Abs(p1.Y - p2.Y);
            List<string> headers = new List<string>();
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                   
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    Table tbl = new Table();
                    tbl.Width = wid;
                    tbl.Height = hgt;
                    tbl.TableStyle = db.Tablestyle;
                    ppo.Message = "\nPick a table position: ";
                    Point3d pt = ed.GetPoint(ppo).Value;
                    tbl.Position = pt;
                    TableStyle ts = (TableStyle)tr.GetObject(tbl.TableStyle, OpenMode.ForRead);
                    double row_height = hgt / row_num;
                    double col_width = wid / col_num;
                    double textht = row_height * 0.8; 
                    //insert rows          
                    tbl.InsertRows(1, textht * 2,row_num);
                    // insert columns           
                    tbl.InsertColumns(1, textht * 15, col_num - 1);// first column is already exist, thus we'll have 3 columns     
                    //create range to merge the cells in the first row    
                    CellRange range = CellRange.Create(tbl, 0, 0, 0, col_num - 1);
                    tbl.MergeCells(range);
                    // set style for title row      
                    tbl.Cells[0, 0].Style = "Title";
                    tbl.Cells[0, 0].TextString = "Table Title";
                    tbl.Rows[0].Height = row_height;
                    tbl.InsertRows(1, row_height, 1);
                    // set style for header row           
                    tbl.Rows[1].Style = "Header";
                    tbl.Rows[1].Height = row_height;
                    // create dummy headers
                    for (int a =0;a<col_num;a++)
                    headers.Add(((char)(a + (int)('A'))).ToString());
                    //create contents in the first cell and set textstring    
                    tbl.Cells[1, 0].Contents.Add();
                    tbl.Cells[1, 0].Contents[0].TextString = headers[0];
                    for (int c = 1; c < col_num; c++)
                    {
                        //for all of the rest cells just set textstring (or value)       
                        tbl.Cells[1, c].TextString = headers[c];
                    }

                    for (int r = 2; r < row_num + 2; r++)//exact number of data rows + title row + header row    
                    {
                        // set style for data row      
                        tbl.Rows[r].Style = "Data";
                        tbl.Rows[r].Height = row_height;
                    }
                    // set column widths
                    foreach (Column col in tbl.Columns)
                        col.Width = col_width;
                    //change last column values just to show data formatting    
                    // to set string format:     
                    // create DataTypeParameter object        
                    // set data type,set value, then data format for every cell:    
                    DataTypeParameter dtp = new DataTypeParameter();
                    dtp.DataType = DataType.General;
                    dtp.UnitType = UnitType.Unitless;        
                    //populate column with  values:  
                    int ct = 0;
                    for (int r = 2; r < row_num + 2; r++)
                    //exact number of data rows + title row + header row         
                    {
                        for (int c = 0; c < col_num; c++)
                        {
                            tbl.Cells[r, c].Contents.Add();
                            tbl.Cells[r, c].Contents[0].DataFormat = "@";//general
                            tbl.Cells[r, c].Contents[0].DataType = dtp;
                            // add your cell value here
                            tbl.Cells[r, c].Contents[0].SetValue("V", ParseOption.ParseOptionNone);

                        }
                        ct += 1;
                    }
                    tbl.GenerateLayout();
                    btr.AppendEntity(tbl);
                    tr.AddNewlyCreatedDBObject(tbl, true);
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 3 of 4 (164 Views)

Re: Fill a table inside specified rectangle

06-24-2013 07:03 PM in reply to: Hallex

Thank you.

I am using Object ARX 2007.

And it looks like 2007 does not support CellRange class and Rows, Columns, and Cells properties.

 

So this is what I tried...

 

using (Transaction tr = db.TransactionManager.StartTransaction())
{

	BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
	Table tbl = new Table();
	tbl.Width = wid;
	tbl.Height = hgt;
	//tbl.TableStyle = db.Tablestyle;
	ppo.Message = "\nPick a table position: ";
	Point3d pt = ed.GetPoint(ppo).Value;
	tbl.Position = pt;
	//TableStyle ts = (TableStyle)tr.GetObject(tbl.TableStyle, OpenMode.ForRead);
	double row_height = hgt / row_num;
	double col_width = wid / col_num;
	double textht = row_height * 0.8;

	tbl.SetRowHeight(row_height);
	tbl.SetColumnWidth(col_width);
	//insert rows          
	tbl.InsertRows(1, row_height, row_num - 1);
	// insert columns           
	tbl.InsertColumns(1, col_width, col_num - 1);// first column is already exist, thus we'll have 3 columns

	for (int r = 0; r < row_num; r++)
	{
		for (int c = 0; c < col_num; c++)
		{
			tbl.SetTextHeight(r, c, textht);
		}
	}

	tbl.GenerateLayout();
	btr.AppendEntity(tbl);
	tr.AddNewlyCreatedDBObject(tbl, true);
	tr.Commit();
}

 

And this is what I got when the rectangle is big enough...

 

2013-06-25 10;59;47.PNG

 

Almost fit into the rectangle... Except the last row...

 

But this is what I get when the rectangle is small....

 

2013-06-25 11;00;47.PNG

 

Is there some kind of minimum size of a cell???

Hmmmm......

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 4 (155 Views)

Re: Fill a table inside specified rectangle

06-24-2013 10:33 PM in reply to: dynamicscope
I could not use A2007, try to find in the older answers
on this forum
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.