using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Globalization;
using System.Linq;
namespace AcadTest
{
public class TestCellStyle
{
[CommandMethod("TestCellStyle")]
public void RunCommand()
{
int rowsNum = 5;
int columnsNum = 2;
double rowHeight = 30;
double columnWidth = 200;
long handle = -1;
Document adoc = Application.DocumentManager.MdiActiveDocument;
Database db = adoc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Table table = new Table();
table.SetDatabaseDefaults(); // !
table.TableStyle = db.Tablestyle; // !
TableStyle tableStyle
= db.Tablestyle.GetObject(OpenMode.ForRead) as TableStyle;
string[] cellStyles = tableStyle
.CellStyles
.Cast<string>()
.ToArray();
string cellStyle = cellStyles
.FirstOrDefault(item => item.ToLower
(CultureInfo.InvariantCulture).Contains("data"));
table.InsertRows(0, rowHeight, rowsNum);
table.InsertColumns(0, columnWidth, columnsNum);
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
{
Cell cell = table.Cells[i, j];
cell.TextString = $"{i} : {j}";
// cell.Alignment = CellAlignment.BottomRight; // Бесполезное действие т.к. ниже изменяется стиль
}
if (!string.IsNullOrEmpty(cellStyle))
table.Rows[i].Style = cellStyle;
}
table.SetRowHeight(rowHeight);
table.SetColumnWidth(columnWidth);
table.Position = new Point3d(100, 100, 0);
BlockTableRecord modelSpace
= db.CurrentSpaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
modelSpace.AppendEntity(table);
tr.AddNewlyCreatedDBObject(table, true);
handle = table.Handle.Value;
tr.Commit();
}
}
}
}