public override float DrawTable(TableModel tableModel, Document targetDocument, string workPath)
{
var database = targetDocument.Database;
CellHeight = tableModel.Template.CellHeight > 0 ? tableModel.Template.CellHeight : 8f;
CellTextHeight = tableModel.Template.TextHeight > 0 ? tableModel.Template.TextHeight : 5f;
//Создаем и позиционируем таблицу в начале координат + смещение по оси x
Table table = null;
using (var transaction = database.TransactionManager.StartTransaction())
{
string textStyle = tableModel.Template.TextStyle;
var textStyleTable = (TextStyleTable)transaction.GetObject(database.TextStyleTableId, OpenMode.ForRead);
if (!string.IsNullOrEmpty(textStyle) && textStyleTable.Has(textStyle))
_textStyleId = textStyleTable[textStyle];
table = _graphicsRepository.GetTableGraphics(tableModel.Id, transaction);
if (table == null)
{
if (!double.IsNaN(tableModel.CoordinateX))
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
using (acDoc.LockDocument()) // this is needed from a modeless form
{
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
// Prompt for the start point
pPtOpts.Message = "\n Укажите место, в которое вы хотите добавить таблицу \"" + tableModel.Name + "\" (его верхний левый улог): ";
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
if (pPtRes.Status == PromptStatus.OK)
{
Point3d ptStart = pPtRes.Value;
tableModel.CoordinateX = ptStart.X;
tableModel.CoordinateY = ptStart.Y;
tableModel.CoordinateZ = ptStart.Z;
}
}
}
table = new Table
{
Position = new Point3d(tableModel.CoordinateX, tableModel.CoordinateY, tableModel.CoordinateZ),
LineWeight = (LineWeight)tableModel.Template.LineWeight
};
}
else
{
var position = table.Position;
_graphicsRepository.DeleteTableGraphics(tableModel.Id, transaction);
table = new Table()
{
Position = position,
LineWeight = (LineWeight)tableModel.Template.LineWeight
};
}
transaction.Commit();
}
//Задаем таблице размер в соответсвии с числом колонок и строк
IList<Item> rows = Item.GetFlatList(tableModel.Items);
var templateColumns = tableModel.Template.Columns;
table.SetSize(rows.Count + 1, templateColumns.Count);
#region Инициализация шапки
table.Rows[0].Height = 15;
table.Cells.TopLeft.Borders.Horizontal.Margin = _cellMargin;
table.Cells.TopLeft.Borders.Top.Margin = _cellMargin;
table.Cells.TopLeft.Borders.Bottom.Margin = _cellMargin;
for (var i = 0; i < table.Columns.Count; i++)
{
var column = table.Columns[i];
string heasderText = ChangeSpecialSymbols(tableModel.Template.Columns[i].Header);
column.TopLeft.TextString = heasderText;
column.TopLeft.TextHeight = CellTextHeight;
column.TopLeft.Alignment = CellAlignment.MiddleCenter;
if (_textStyleId != ObjectId.Null)
column.TopLeft.TextStyleId = _textStyleId;
var templateWidth = templateColumns.Single(c => c.ColumnNumber == i)?.Width ?? 0;
column.Width = templateWidth > 0 ? templateWidth : 40;
if (tableModel.Template.Columns[i].IsHeaderTextVerticalOrientation)
table.Cells[0, i].Contents[0].Rotation = 1.57;
table.Cells[0, i].TopLeft.Borders.Horizontal.Margin = 0;
table.Cells[0, i].TopLeft.Borders.Top.Margin = 0;
table.Cells[0, i].TopLeft.Borders.Bottom.Margin = 0;
}
#endregion
#region Заполнение таблицы
for (var i = 1; i < table.Rows.Count; i++)
{
var attributes = rows[i - 1].Attributes;
table.Rows[i].Height = CellHeight;
for (var j = 0; j < table.Columns.Count; j++)
{
var column = templateColumns.Single(c => c.ColumnNumber == j);
var cell = table.Cells[i, j];
cell.Alignment = CellAlignment.MiddleCenter;
cell.TextHeight = CellTextHeight;
cell.Borders.Horizontal.Margin = _cellMargin;
cell.Borders.Vertical.Margin = _cellMargin;
cell.Borders.Top.Margin = _cellMargin;
cell.TextString = ChangeSpecialSymbols(AttributeOfItem.GetFirstValueByAttributeIds(attributes, column.AttributeIds) ?? "");
cell.TextHeight = CellTextHeight;
cell.Alignment = CellAlignment.MiddleLeft;
}
table.Rows[i].State = CellStates.ContentLocked;
}
#endregion
using (var transaction = database.TransactionManager.StartTransaction())
{
Matrix3d transform = Matrix3d.Scaling(tableModel.Scale, table.Position);
table.TransformBy(transform);
transaction.Commit();
}
#region Сохранение таблицы в чертеже
using (var transaction = database.TransactionManager.StartTransaction())
{
BlockTable blockTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
blockTableRecord.AppendEntity(table);
transaction.AddNewlyCreatedDBObject(table, true);
transaction.Commit();
}
#endregion
_graphicsRepository.PutTableGraphics(tableModel.Id, new List<Handle> { table.Handle });
return (float)table.Width;
}