admin 发表于 2024-3-9 11:13:56

[表格] 关于表格的代码

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
namespace TableCreation
{
    public class Commands
    {
      
      static public void CreateTable1()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            ObjectId appDictId = ObjectId.Null;
            //DBDictionary appDict = new DBDictionary();
            TableStyle myTableStyle = new TableStyle();
            PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
            if (pr.Status == PromptStatus.OK)
            {
                Transaction tr = doc.TransactionManager.StartTransaction();
                using (tr)
                {
                  BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
                  Table tb = new Table();
                  DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);//获取数据库object,并转换为DBDictionary
                  if (nod.Contains("ACAD_TABLESTYLE"))//包含这个字典
                  {
                        DBDictionary compDict = (DBDictionary)tr.GetObject(nod.GetAt("ACAD_TABLESTYLE"), OpenMode.ForRead);
                        if (!compDict.Contains("Standard"))
                        {
                            compDict.UpgradeOpen();
                            appDictId = compDict.SetAt("Standard", myTableStyle);
                            tr.AddNewlyCreatedDBObject(myTableStyle, true);
                        }
                        else
                        {
                            appDictId = compDict.GetAt("Standard");
                            myTableStyle = (TableStyle)tr.GetObject(compDict.GetAt("Standard"), OpenMode.ForRead);
                           
                        }
                        myTableStyle.UpgradeOpen();//这个是必须的,否则会有问题的
                        myTableStyle.FlowDirection = FlowDirection.NotSet;//LeftToRight从下往上,NotSet从上往下,如果下面表格再设置就会不按照样式来
                        myTableStyle.IsTitleSuppressed = false;// tb.TableStyle = myTableStyle.ObjectIdb必须要有这个语句才起作用
                        myTableStyle.IsHeaderSuppressed = false;
                        
                        //myTableStyle.DowngradeOpen();
                  }
                  tb.TableStyle = myTableStyle.ObjectId;//这个语句很关键,如果没有下面tb的很多设置不起作用
                  tb.IsTitleSuppressed = false;
                  tb.IsHeaderSuppressed = false;
                  tb.FlowDirection = FlowDirection.LeftToRight;//如果没有tb.TableStyle = myTableStyle.ObjectId,则这个无论怎么设置似乎都不起作用
                  //FlowDirection myFlowDirection = FlowDirection.BottomToTop;//BottomToTop,不存在的,似乎桌子公司错了
                  
                  tb.NumRows = 7;
                  // Added an additional column for the block image
                  // and one for the "is dynamic" flag
                  tb.NumColumns = 9;
                  tb.SetRowHeight(3);
                  tb.SetColumnWidth(15);
                  tb.Rows.Height = 10;
                  tb.Rows.Height = 20;
                  tb.Rows.Height = 30;
                  tb.Columns.Width = 30;
                  tb.Columns.Width = 40;
                  tb.Columns.Width = 60;
                  tb.Position = pr.Value;
                  // Create a 2-dimensional array
                  // of our table contents
                  string[,] str = new string;
                  str = "Part No.";
                  str = "Name";
                  str = "Material ";
                  str = "1876-1";
                  str = "Flange";
                  str = "Perspex";
                  str = "0985-4";
                  str = "Bolt";
                  str = "Steel";
                  str = "3476-K";
                  str = "Tile";
                  str = "Ceramic";
                  str = "8734-3";
                  str = "Kean";
                  str = "Mostly water";
                  // Use a nested loop to add and format each cell
                  for (int i = 0; i < 5; i++)
                  {
                        for (int j = 0; j < 3; j++)
                        {
                            tb.Cells.TextHeight = 1;//tb.SetTextHeight(i, j, 1);
                            tb.Cells.TextString = str;//tb.SetTextString(i, j, str);
                            tb.Cells.Alignment = CellAlignment.MiddleCenter; //tb.SetAlignment(i, j, CellAlignment.MiddleCenter);
                        }
                        // Adding title information for additional columns
                        if (i == 0)
                        {
                            tb.SetTextHeight(i, 3, 1);
                            tb.Cells.TextString = "Block Preview"; //tb.SetTextString(i, 3, "Block Preview");
                            tb.SetAlignment(i, 3, CellAlignment.MiddleCenter);
                            tb.SetTextHeight(i, 4, 1);
                            tb.Cells.TextString = "Is Dynamic?"; //tb.SetTextString(i, 4, "Is Dynamic?");
                            tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
                        }
                        // If a block definition exists for a block of our
                        // "name" field, then let's set it in the 4th column
                        if (bt.Has(str))
                        {
                            ObjectId objId = bt];
                            Cell c=tb.Cells;
                            c.Contents.Add();
                            // Set the horizontal margins
                            c.Borders.Left.Margin =0.5;// horMarg;
                            c.Borders.Right.Margin = 0.5;//horMarg;
                            // Set the vertical margins
                            c.Borders.Top.Margin = 0.8;//verMarg;
                            c.Borders.Bottom.Margin = 0.8;//verMarg;
                            //CellBorder myCellBorder = c.Borders.Bottom;
                            //myCellBorder.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
                            c.Borders.Bottom.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);//1代表红色
                            c.Contents.BlockTableRecordId = objId;
                           
                            //tb.SetBlockTableRecordId(i, 3, objId, true);
                           
                            // And then we use a field to check on whether
                            // it's a dynamic block or not
                            string strObjId = objId.ToString();
                            strObjId = strObjId.Trim(new char[] { '(', ')' });
                            tb.SetTextHeight(i, 4, 1);
                            tb.SetTextString(i, 4, "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).IsDynamicBlock \\f \"%bl2\">%");
                            tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
                        }
                  }
                  //非常重要,根据当前样式更新表格,不加此句,会导致AutoCAD崩溃
                  tb.GenerateLayout();
                  //tb.FlowDirection = Autodesk.AutoCAD.DatabaseServices.FlowDirection.TopToBottom;
                  BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
                  btr.AppendEntity(tb);
                  tr.AddNewlyCreatedDBObject(tb, true);
                  tr.Commit();
                }
            }
      }

      
      static public void CreateTable()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            ObjectId appDictId = ObjectId.Null;
            //DBDictionary appDict = new DBDictionary();
            TableStyle myTableStyle = new TableStyle();
            PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
            if (pr.Status == PromptStatus.OK)
            {
                Transaction tr = doc.TransactionManager.StartTransaction();
                using (tr)
                {
                  BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
                  Table tb = new Table();
                  DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);//获取数据库object,并转换为DBDictionary
                  if (nod.Contains("ACAD_TABLESTYLE"))//包含这个字典
                  {
                        DBDictionary compDict = (DBDictionary)tr.GetObject(nod.GetAt("ACAD_TABLESTYLE"), OpenMode.ForRead);
                        if (!compDict.Contains("Standard1"))
                        {
                            compDict.UpgradeOpen();
                            appDictId = compDict.SetAt("Standard1", myTableStyle);
                            tr.AddNewlyCreatedDBObject(myTableStyle, true);
                        }
                        else
                        {
                            appDictId = compDict.GetAt("Standard1");
                            myTableStyle = (TableStyle)tr.GetObject(compDict.GetAt("Standard1"), OpenMode.ForRead);
                        }
                        myTableStyle.UpgradeOpen();//这个是必须的,否则会有问题的
                        myTableStyle.FlowDirection = FlowDirection.NotSet;//LeftToRight从下往上,NotSet从上往下,如果下面表格再设置就会不按照样式来
                        myTableStyle.IsTitleSuppressed = false;// tb.TableStyle = myTableStyle.ObjectIdb必须要有这个语句才起作用
                        myTableStyle.IsHeaderSuppressed = false;
                        //myTableStyle.DowngradeOpen();
                  }
                  tb.TableStyle = myTableStyle.ObjectId;//这个语句很关键,如果没有下面tb的很多设置不起作用
                  tb.IsTitleSuppressed = true;
                  tb.IsHeaderSuppressed = true;
                  tb.FlowDirection = FlowDirection.LeftToRight;//如果没有tb.TableStyle = myTableStyle.ObjectId,则这个无论怎么设置似乎都不起作用
                  //FlowDirection myFlowDirection = FlowDirection.BottomToTop;//BottomToTop,不存在的,似乎桌子公司错了
                  tb.SetSize(10, 5);//设置行列数目   //旧的方式 tb.NumRows = 7 //tb.NumColumns = 7;
                  tb.SetRowHeight(5);//设置行高
                  tb.SetColumnWidth(15);// 设置列宽
                  //tb.Columns.Width = 20; // 设置第一列宽度为20
                  tb.Position = pr.Value;
                  // Create a 2-dimensional array
                  // of our table contents
                  string[,] str = new string;
                  str = "Part No.";
                  str = "Name";
                  str = "Material ";
                  str = "1876-1";
                  str = "Flange";
                  str = "Perspex";
                  str = "0985-4";
                  str = "Bolt";
                  str = "Steel";
                  str = "3476-K";
                  str = "Tile";
                  str = "Ceramic";
                  str = "8734-3";
                  str = "Kean";
                  str = "Mostly water";
                  // Use a nested loop to add and format each cell
                  for (int i = 0; i < 5; i++)
                  {
                        for (int j = 0; j < 3; j++)
                        {
                            tb.SetTextHeight(i, j, 1);
                            tb.Cells.TextString = str;//旧的方式,tb.SetTextString(i, j, str);
                            tb.SetAlignment(i, j, CellAlignment.MiddleCenter);
                            tb.Cells.Borders.Bottom.Color=Color.FromColorIndex(ColorMethod.ByAci, 1);//1代表红色
                        }
                        // Adding title information for additional columns
                        if (i == 0)
                        {
                            tb.SetTextHeight(i, 3, 1);
                            tb.Cells.TextString = "Block Preview";
                            tb.SetAlignment(i, 3, CellAlignment.MiddleCenter);
                            tb.SetTextHeight(i, 4, 1);
                            tb.Cells.TextString = "Is Dynamic?";
                            tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
                        }
                        // If a block definition exists for a block of our
                        // "name" field, then let's set it in the 4th column
                        if (bt.Has(str))
                        {
                            ObjectId objId = bt];
                            tb.SetBlockTableRecordId(i, 3, objId, true);
                            //tb.Cells.Contents.BlockTableRecordId = objId;
                            // And then we use a field to check on whether
                            // it's a dynamic block or not
                            string strObjId = objId.ToString();
                            strObjId = strObjId.Trim(new char[] { '(', ')' });
                            tb.SetTextHeight(i, 4, 1);
                            tb.Cells.TextString = "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).IsDynamicBlock \\f \"%bl2\">%";
                            //tb.SetTextString(i, 4, "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).IsDynamicBlock \\f \"%bl2\">%");
                           
                            tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
                        }
                  }
                  //非常重要,根据当前样式更新表格,不加此句,会导致AutoCAD崩溃
                  tb.GenerateLayout();
                  //tb.FlowDirection = Autodesk.AutoCAD.DatabaseServices.FlowDirection.TopToBottom;
                  BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
                  btr.AppendEntity(tb);
                  tr.AddNewlyCreatedDBObject(tb, true);
                  tr.Commit();
                }
            }
      }

      
      public static void t8()//选择table,读取属性
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Table myTable = new Table();
            PromptEntityOptions opts = new PromptEntityOptions("\n选择一个table:");
            opts.SetRejectMessage("错误的选择!");
            opts.AddAllowedClass(typeof(Table), false);
            PromptEntityResult res = ed.GetEntity(opts);
            if (res.Status != PromptStatus.OK)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                myTable = tr.GetObject(res.ObjectId, OpenMode.ForRead) as Table;
                myTable.FlowDirection = FlowDirection.LeftToRight;
                tr.Commit();
            }
      }

    }
}
页: [1]
查看完整版本: [表格] 关于表格的代码