autocad中新建一个table,设置表格方向的记录

以下是一个创建table,并设置表头方向的示例代码

关键是:
TableStyle style = new TableStyle();
style.FlowDirection = FlowDirection.LeftToRight;
style.IsTitleSuppressed = true;
table.TableStyle = style.Id;
table.FlowDirection = FlowDirection.LeftToRight;

另: 设置表格行格式是表头, 还是标题列名, 还是数据设置的行属性是Style,例如
table.Rows[i].Style = "_DATA",

  [CommandMethod("addtable")]
    public  void addtable()
    {
        #region 例子bomDataList
        List<BomData> bomDataList = new List<BomData>
    {
        new BomData
        {
            ProjectId = 1,
            ProjectCode = "PC001",
            ProjectNum = "PN001",
            ProjectName = "Project Alpha",
            DrawingId = 101,
            DrawingName = "Drawing Alpha",
            Quantity = 10,
            UnitWeight = 2.5,
            Material = "Steel",
            Remarks = "Initial batch",
            Status = 'A', // Active
            CreateBy = "User1",
            CreateTime = DateTime.Now,
            UpdateBy = "User1",
            UpdateTime = DateTime.Now
        },
        new BomData
        {
            ProjectId = 2,
            ProjectCode = "PC002",
            ProjectNum = "PN002",
            ProjectName = "Project Beta",
            DrawingId = 102,
            DrawingName = "Drawing Beta",
            Quantity = 20,
            UnitWeight = 1.8,
            Material = "Aluminum",
            Remarks = "Revised design",
            Status = 'R', // Revised
            CreateBy = "User2",
            CreateTime = DateTime.Now.AddDays(-1),
            UpdateBy = "User2",
            UpdateTime = DateTime.Now.AddDays(-1)
        },
        new BomData
        {
            ProjectId = 3,
            ProjectCode = "PC003",
            ProjectNum = "PN003",
            ProjectName = "Project Gamma",
            DrawingId = 103,
            DrawingName = "Drawing Gamma",
            Quantity = 5,
            UnitWeight = 3.2,
            Material = "Copper",
            Remarks = "Special order",
            Status = 'S', // Special
            CreateBy = "User3",
            CreateTime = DateTime.Now.AddDays(-2),
            UpdateBy = "User3",
            UpdateTime = DateTime.Now.AddDays(-2)
        },
        new BomData
        {
            ProjectId = 4,
            ProjectCode = "PC004",
            ProjectNum = "PN004",
            ProjectName = "Project Delta",
            DrawingId = 104,
            DrawingName = "Drawing Delta",
            Quantity = 15,
            UnitWeight = 2.0,
            Material = "Plastic",
            Remarks = "Standard parts",
            Status = 'P', // Pending
            CreateBy = "User4",
            CreateTime = DateTime.Now.AddDays(-3),
            UpdateBy = string.Empty,
            UpdateTime = DateTime.MinValue // Not updated yet
        },
        new BomData
        {
            ProjectId = 5,
            ProjectCode = "PC005",
            ProjectNum = "PN005",
            ProjectName = "Project Epsilon",
            DrawingId = 105,
            DrawingName = "Drawing Epsilon",
            Quantity = 8,
            UnitWeight = 4.5,
            Material = "Composite",
            Remarks = "High priority",
            Status = 'H', // High
            CreateBy = "User5",
            CreateTime = DateTime.Now.AddDays(-4),
            UpdateBy = "User5",
            UpdateTime = DateTime.Now.AddDays(-4)
        }
    };
        #endregion
        using var tr = new DBTrans();
        var table =AddTable(new Point3d(0, 0, 0), bomDataList,"J6BW");
        table.Layer = "J1";
    }

    private Table AddTable(Point3d insertPoint, List<BomData> bomDataList,string layer)
    {

        var tr = DBTrans.Top;
        List<List<string>> bomRowInfos = new List<List<string>>
        {
            new List<string>{"顺序","代号","名称","数量","单重","总重","材料", "备注"
        }};
        List<BomInfo> bomInfos = bomDataList.Select(originalItem => new BomInfo
        {
            DrawingId = originalItem.DrawingId,
            DrawingName = originalItem.DrawingName,
            Quantity = originalItem.Quantity,
            UnitWeight = originalItem.UnitWeight,
            TotalWeight = originalItem.Quantity * originalItem.UnitWeight,
            Material = originalItem.Material,
            Remarks = originalItem.Remarks
        }).ToList();

        for (int i = 0; i < bomInfos.Count; i++)
        {
            var bomInfo = bomInfos[i];
            var itemList = new List<string>
            {
                (i + 1).ToString(), // 序号从1开始
                bomInfo.DrawingId.ToString(),
                bomInfo.DrawingName,
                bomInfo.Quantity.ToString(),
                ((int)bomInfo.UnitWeight).ToString(),
                ((int)bomInfo.TotalWeight).ToString(),
                bomInfo.Material,
                bomInfo.Remarks
            };
            bomRowInfos.Add(itemList);
        }

        var rowsCount = bomDataList.Count + 1;
        var colsCount = 8;
        var rowHeights = new List<double>() { 700 };
        List<double> colWidths = new List<double>() { 820, 4000, 5000, 800, 1500, 1500, 1700, 2800 };
        var table = createTable(rowsCount, colsCount, rowHeights, colWidths, false);
        AddRowDatas(table, 0, bomRowInfos, rowHeights[0]/3);
        table.Position = insertPoint;

        table.FlowDirection = Autodesk.AutoCAD.DatabaseServices.FlowDirection.LeftToRight;
        var id = tr.CurrentSpace.AddEntity(table);
        table.Layer = layer;
        
        return table;
    }
#if !NET35
    /// <summary>
    /// 创建表格
    /// </summary>
    /// <param name="rowsCount">行数</param>
    /// <param name="colsCount">列数</param>
    /// <param name="rowHeight">行高</param>
    /// <param name="colWidth">列宽</param>
    /// <returns></returns>
    private Table createTable(int rowsCount, int colsCount, List<double> rowHeights, List<double> colWidths, bool hasTitle)
    {
        TableStyle style = new TableStyle();
        style.FlowDirection = FlowDirection.LeftToRight;
        style.IsTitleSuppressed = true;
        var table = new Table();
        table.SetSize(rowsCount, colsCount);
        if (rowHeights.Count == 1)
        {
            table.SetRowHeight(rowHeights[0]);
        }
        else
        {
            for (int i = 0; i < rowHeights.Count; i++)
            {
                table.Rows[i].Height = rowHeights[i];
            }

        }
        if (colWidths.Count == 1)
        {
            table.SetColumnWidth(colWidths[0]);
        }
        else
        {
            for (int i = 0; i < colWidths.Count; i++)
            {
                table.Columns[i].Width = colWidths[i];
            }
        }

        table.TableStyle = style.Id;
        table.FlowDirection = FlowDirection.LeftToRight;
        
        if (!hasTitle)
        {
            for (int i = 0; i < (table.Rows.Count<4? table.Rows.Count :4); i++) 
            {
                table.Rows[i].Style = "_DATA";
            }
        }
       
        return table;
    }
    private static void AddRow(Table table, int count = 1)
    {
        if (table == null) return;

        int c = table.Rows.Count();
        table.InsertRows(c, table.Rows[c - 1].Height, count);
    }

    public static void InsertData(Table table, List<string> ls)
    {
        if (table == null) return;
        AddRow(table);
        int c = table.Rows.Count();
        for (int i = 0; i < table.Columns.Count(); i++)
        {
            if (i < ls.Count)
            {
                table.Cells[c - 1, i].TextString = ls[i];
            }
        }
    }

    public static void AddRowData(Table table,int row,  List<string> ls)
    {
        if (table == null) return;
        for (int i = 0; i < table.Columns.Count(); i++)
        {
            if (i < ls.Count)
            {
                table.Cells[row, i].TextString = ls[i];
            }
        }
    }
    public static void AddRowDatas(Table table,  int startrow, List<List<string>> bomRowInfos, double TextHeight)
    {
        if (table == null) return;
        for (int i = 0; i < bomRowInfos.Count; i++)
        {
            var row = startrow + i;
            if (row> table.Rows.Count)
            {
                AddRow(table);
            }
            var bomInfo = bomRowInfos[i];
            for (int j = 0; j < table.Columns.Count(); j++)
            {
                if (j < bomInfo.Count)
                {
                    table.Cells[row, j].TextString = bomInfo[j];
                    table.Cells[row, j].Alignment= CellAlignment.MiddleCenter;
                    table.Cells[row, j].TextHeight= TextHeight;
                }
            }
        }
    }


#endif

}

public class BomData
{
    public int ProjectId { get; set; } 
    public string ProjectCode { get; set; } = string.Empty; 
    public string ProjectNum { get; set; } = string.Empty; 
    public string ProjectName { get; set; } = string.Empty; 
    public int DrawingId { get; set; } 
    public string DrawingName { get; set; } = string.Empty; 
    public int Quantity { get; set; } 
    public double UnitWeight { get; set; } 
    public double TotalWeight
    {
        get { return Quantity * UnitWeight; }
    }
    public string Material { get; set; } = string.Empty;
    public string Remarks { get; set; } = string.Empty;
    public char Status { get; set; } = ' '; 
    public string CreateBy { get; set; } = string.Empty;
    public DateTime CreateTime { get; set; } 
    public string UpdateBy { get; set; } = string.Empty;
    public DateTime UpdateTime { get; set; } 
    
}
public class BomInfo 
{

    public int DrawingId { get; set; }
    public string DrawingName { get; set; } = string.Empty;
    public int Quantity { get; set; }
    public double UnitWeight { get; set; }
    public double TotalWeight { get; set; }
    public string Material { get; set; } = string.Empty;
    public string Remarks { get; set; } = string.Empty;


}
posted @   elepeipei  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示