ObjectARX

Reply
Active Contributor
chikito1990
Posts: 40
Registered: ‎06-26-2013
Message 1 of 2 (160 Views)

Merging two cells in a row?

160 Views, 1 Replies
08-12-2013 03:16 AM

Hi all,

 

I am trying to merge two cells in one but i cannot find proper method to do that. Any suggestions?

 

Example:
I am creating a table with dynamic number of rows and columns: 

 for (int i = 3; i < tbRight.NumRows; i++)
                {
                    for (int j = 0; j < tbRight.NumColumns; j++)
                    {
                        if (num2 == numCircles - 1)
                        {
                            Cell c = new Cell(tbRight, i, j);
                            c.TextHeight = 450;
                            tbRight.SetTextString(i, j, (num2 + 1).ToString() + "# - " + rightConn[num2]);
                            num2++;
                            
                        }
                        else if (num2 > numCircles - 1)
                        {   }
                        else
                        {
                            Cell c = new Cell(tbRight, i, j);
                            c.TextHeight = 450;
                            tbRight.SetTextString(i, j, (num2 + 1).ToString() + "# - " + rightConn[num2]);
                            num2++;
                        }
                    }
                }

 First row of the table is the header and i have two free rows with two columns each. I want to merge all of them to look like one row.

 

 

On the other hand, is it possible to align the text in the table in the middle and also the vertical align to be in the middle.

 

Thank you in advance!

ADN Support Specialist
philippe.leefsma
Posts: 562
Registered: ‎06-02-2009
Message 2 of 2 (94 Views)

Re: Merging two cells in a row?

08-21-2013 08:33 AM in reply to: chikito1990

Hi,

 

The following sample illustrates how to merge cells. Also it creates a table style that allows to control the text alignment inside the cells.

 

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

    PromptPointOptions ppo = new PromptPointOptions("\nSpecify insertion point: ");

    PromptPointResult ppr = ed.GetPoint(ppo);

    if (ppr.Status != PromptStatus.OK)
        return;

    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        Table table = new Table();
                
        table.Position = ppr.Value;

        table.InsertColumns(1, 2.0, 6);
        table.InsertRows(1, 1.0, 5);

        CellRange cr = CellRange.Create(table, 0, 0, 1, 1);

        table.MergeCells(cr);

        table.Cells[0, 0].Value = "This is cell[0][0]";

        table.Cells[0, 0].State = CellStates.ContentModifiedAfterUpdate;

        string TableStyleName = "MyTableStyle1";

        table.TableStyle = createTableStyle(db, TableStyleName);

        BlockTableRecord btr = Tx.GetObject(
            db.CurrentSpaceId, 
            OpenMode.ForWrite) 
                as BlockTableRecord;

        btr.AppendEntity(table);
        Tx.AddNewlyCreatedDBObject(table, true);
        Tx.Commit();
    }
}

public ObjectId createTableStyle(Database db, string TableStyleName)
{
    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        DBDictionary dico = Tx.GetObject(
            db.TableStyleDictionaryId, 
            OpenMode.ForWrite) as DBDictionary;

        if (dico.Contains(TableStyleName))
        {
            //Already exists
            return dico.GetAt(TableStyleName);
        }

        DbDictionaryEnumerator enumerator = dico.GetEnumerator();
        enumerator.MoveNext();

        //Get first style in the TableStyleDictionary and clone it
        TableStyle style = Tx.GetObject(
            enumerator.Current.Value, 
            OpenMode.ForRead) as TableStyle;

        TableStyle newStyle = style.Clone() as TableStyle;

        ObjectId newStyleId = dico.SetAt(TableStyleName, newStyle);

        newStyle.Name = TableStyleName;
        newStyle.TextHeight(RowType.DataRow);
        newStyle.SetTextHeight(10, 1);
        newStyle.SetAlignment(CellAlignment.MiddleLeft, 1);

        Tx.AddNewlyCreatedDBObject(newStyle, true);
        Tx.Commit();

        return newStyleId;
    }
}

 

Regards,

Philippe.

 



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community