using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(MergeCellsTest.MyCommands))]
namespace MergeCellsTest
{
public class MyCommands
{
[CommandMethod("MergeCellsTest")]
public void MergeCellsTestHandler()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
Createtable();
}
public void Createtable()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
ObjectId msId = bt[BlockTableRecord.ModelSpace];
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(msId, OpenMode.ForWrite);
Table tb = new Table();
tb.TableStyle = db.Tablestyle;
btr.AppendEntity(tb);
// Число строк
int RowsNum = 5;
// Число столбцов
int ColumnsNum = 5;
// Высота строки
double rowheight = 3;
// Ширина столбца
double columnwidth = 20;
// Добавляем строки и колонки
tb.InsertRows(0, rowheight, RowsNum -1);
tb.InsertColumns(0, columnwidth, ColumnsNum - 1);
tb.SetRowHeight(rowheight);
tb.SetColumnWidth(columnwidth);
Point3d eMax = db.Extmax;
Point3d eMin = db.Extmin;
double CenterY = (eMax.Y + eMin.Y) * 0.5;
tb.Position = new Point3d(10, 10, 0);
CellRange range = CellRange.Create(tb, 1, 0, 1, 4);
tb.MergeCells(range);
range = CellRange.Create(tb, 3, 0, 3, 4);
tb.MergeCells(range);
range = CellRange.Create(tb, 4, 0, 4, 4);
tb.UnmergeCells(range);
// заполняем по одной все ячейки
for (int i = 0; i < RowsNum; i++)
{
for (int j = 0; j < ColumnsNum; j++)
{
tb.Cells[i, j].TextHeight = 1;
tb.Cells[i, j].TextString = i.ToString() + "," + j.ToString();
tb.Cells[i, j].Alignment = CellAlignment.MiddleCenter;
}
}
tb.GenerateLayout();
tr.AddNewlyCreatedDBObject(tb, true);
tr.Commit();
}
}
}
}