Unmerging cells in an AutoCAD table using .NET

A question came in, last week, via a comment on this post. Daniel wanted to unmerge the title cells of a table:

Very nice tutorial. based on this I manange to create the VB. NET code for my table style, but I face with a problem that I could not change the Title to be unmerged.
In this moment, as default, it is merged and I want it to have it not merged.
What is code for this ? I tried to find it out by

ts.SetCellClass(RowType.TitleRow, MergeCellStyleOption.None

but is not working. i get error on:

sd.UpgradeOpen() line

I took a quick look at the Table class and found that this C# code did the job:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

 

namespace TableStyleEditing

{

  public class Commands

  {

    [CommandMethod("UTT")]

    public void UnmergeTableTitle()

    {

      var doc = Application.DocumentManager.MdiActiveDocument;

      if (doc == null) return;

      var ed = doc.Editor;

 

      // Select a table

 

      var peo = new PromptEntityOptions("\nSelect table");

      peo.SetRejectMessage("\nMust be a table.");

      peo.AddAllowedClass(typeof(Table), false);

      var per = ed.GetEntity(peo);

      if (per.Status != PromptStatus.OK)

        return;

 

      using (var tr = doc.TransactionManager.StartTransaction())

      {

        var table = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Table;

        if (table != null)

        {

          // Get the first row

 

          var row = table.Rows[0];

 

          // If it is merged, unmerge it

 

          if (row.IsMerged.HasValue && row.IsMerged.Value)

          {

            table.UnmergeCells(row);

            ed.WriteMessage("\nUnmerged first row.");

          }

          else

          {

            ed.WriteMessage("\nFirst row is not merged.");

          }

        }

        tr.Commit();

      }

    }

  }

}

 

Here's what happens when you run the UTT command and select a table with a merged title row:

Unmerge Table Title

The UTT command is currently hardcoded to unmerge the first row in the selected table, but it could of course be generalised to work on any range of merged cells.

5 responses to “Unmerging cells in an AutoCAD table using .NET”

  1. Nice. Needed this myself.

  2. Snehal Takawale Avatar
    Snehal Takawale

    How to draw table in inventor application using c# code

  3. Thank you so much.
    Life saver.

  4. How to create a table with a fixed block size?

    1. Hi Elvis,

      Please post your support requests to the AutoCAD .NET forum.

      Thank you,

      Kean

Leave a Reply to Kean Walmsley Cancel reply

Your email address will not be published. Required fields are marked *