天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 101|回复: 0

[表格] 关于表格的代码

[复制链接]
  • TA的每日心情
    开心
    昨天 08:01
  • 签到天数: 82 天

    [LV.6]常住居民II

    1585

    主题

    204

    回帖

    214748万

    积分

    管理员

    积分
    2147483647
    发表于 2024-3-9 11:13:56 | 显示全部楼层 |阅读模式
    1. using Autodesk.AutoCAD.ApplicationServices;
    2. using Autodesk.AutoCAD.DatabaseServices;
    3. using Autodesk.AutoCAD.EditorInput;
    4. using Autodesk.AutoCAD.Geometry;
    5. using Autodesk.AutoCAD.Runtime;
    6. using Autodesk.AutoCAD.Colors;
    7. namespace TableCreation
    8. {
    9.     public class Commands
    10.     {
    11.         [CommandMethod("CRT1")]
    12.         static public void CreateTable1()
    13.         {
    14.             Document doc = Application.DocumentManager.MdiActiveDocument;
    15.             Database db = doc.Database;
    16.             Editor ed = doc.Editor;
    17.             ObjectId appDictId = ObjectId.Null;
    18.             //DBDictionary appDict = new DBDictionary();
    19.             TableStyle myTableStyle = new TableStyle();
    20.             PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
    21.             if (pr.Status == PromptStatus.OK)
    22.             {
    23.                 Transaction tr = doc.TransactionManager.StartTransaction();
    24.                 using (tr)
    25.                 {
    26.                     BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
    27.                     Table tb = new Table();
    28.                     DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);//获取数据库object,并转换为DBDictionary
    29.                     if (nod.Contains("ACAD_TABLESTYLE"))//包含这个字典
    30.                     {
    31.                         DBDictionary compDict = (DBDictionary)tr.GetObject(nod.GetAt("ACAD_TABLESTYLE"), OpenMode.ForRead);
    32.                         if (!compDict.Contains("Standard"))
    33.                         {
    34.                             compDict.UpgradeOpen();
    35.                             appDictId = compDict.SetAt("Standard", myTableStyle);
    36.                             tr.AddNewlyCreatedDBObject(myTableStyle, true);
    37.                         }
    38.                         else
    39.                         {
    40.                             appDictId = compDict.GetAt("Standard");
    41.                             myTableStyle = (TableStyle)tr.GetObject(compDict.GetAt("Standard"), OpenMode.ForRead);
    42.                            
    43.                         }
    44.                         myTableStyle.UpgradeOpen();//这个是必须的,否则会有问题的
    45.                         myTableStyle.FlowDirection = FlowDirection.NotSet;//LeftToRight从下往上,NotSet从上往下,如果下面表格再设置就会不按照样式来
    46.                         myTableStyle.IsTitleSuppressed = false;// tb.TableStyle = myTableStyle.ObjectIdb必须要有这个语句才起作用
    47.                         myTableStyle.IsHeaderSuppressed = false;
    48.                         
    49.                         //myTableStyle.DowngradeOpen();
    50.                     }
    51.                     tb.TableStyle = myTableStyle.ObjectId;//这个语句很关键,如果没有下面tb的很多设置不起作用
    52.                     tb.IsTitleSuppressed = false;
    53.                     tb.IsHeaderSuppressed = false;
    54.                     tb.FlowDirection = FlowDirection.LeftToRight;//如果没有tb.TableStyle = myTableStyle.ObjectId,则这个无论怎么设置似乎都不起作用
    55.                     //FlowDirection myFlowDirection = FlowDirection.BottomToTop;//BottomToTop,不存在的,似乎桌子公司错了
    56.                     
    57.                     tb.NumRows = 7;
    58.                     // Added an additional column for the block image
    59.                     // and one for the "is dynamic" flag
    60.                     tb.NumColumns = 9;
    61.                     tb.SetRowHeight(3);
    62.                     tb.SetColumnWidth(15);
    63.                     tb.Rows[0].Height = 10;
    64.                     tb.Rows[1].Height = 20;
    65.                     tb.Rows[2].Height = 30;
    66.                     tb.Columns[0].Width = 30;
    67.                     tb.Columns[1].Width = 40;
    68.                     tb.Columns[2].Width = 60;
    69.                     tb.Position = pr.Value;
    70.                     // Create a 2-dimensional array
    71.                     // of our table contents
    72.                     string[,] str = new string[5, 3];
    73.                     str[0, 0] = "Part No.";
    74.                     str[0, 1] = "Name";
    75.                     str[0, 2] = "Material ";
    76.                     str[1, 0] = "1876-1";
    77.                     str[1, 1] = "Flange";
    78.                     str[1, 2] = "Perspex";
    79.                     str[2, 0] = "0985-4";
    80.                     str[2, 1] = "Bolt";
    81.                     str[2, 2] = "Steel";
    82.                     str[3, 0] = "3476-K";
    83.                     str[3, 1] = "Tile";
    84.                     str[3, 2] = "Ceramic";
    85.                     str[4, 0] = "8734-3";
    86.                     str[4, 1] = "Kean";
    87.                     str[4, 2] = "Mostly water";
    88.                     // Use a nested loop to add and format each cell
    89.                     for (int i = 0; i < 5; i++)
    90.                     {
    91.                         for (int j = 0; j < 3; j++)
    92.                         {
    93.                             tb.Cells[i, j].TextHeight = 1;//tb.SetTextHeight(i, j, 1);
    94.                             tb.Cells[i, i].TextString = str[i, j];//tb.SetTextString(i, j, str[i, j]);
    95.                             tb.Cells[i, i].Alignment = CellAlignment.MiddleCenter; //tb.SetAlignment(i, j, CellAlignment.MiddleCenter);
    96.                         }
    97.                         // Adding title information for additional columns
    98.                         if (i == 0)
    99.                         {
    100.                             tb.SetTextHeight(i, 3, 1);
    101.                             tb.Cells[i, 3].TextString = "Block Preview"; //tb.SetTextString(i, 3, "Block Preview");
    102.                             tb.SetAlignment(i, 3, CellAlignment.MiddleCenter);
    103.                             tb.SetTextHeight(i, 4, 1);
    104.                             tb.Cells[i, 4].TextString = "Is Dynamic?"; //tb.SetTextString(i, 4, "Is Dynamic?");
    105.                             tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
    106.                         }
    107.                         // If a block definition exists for a block of our
    108.                         // "name" field, then let's set it in the 4th column
    109.                         if (bt.Has(str[i, 1]))
    110.                         {
    111.                             ObjectId objId = bt[str[i, 1]];
    112.                             Cell c=tb.Cells[i,3];
    113.                             c.Contents.Add();
    114.                             // Set the horizontal margins
    115.                             c.Borders.Left.Margin =0.5;// horMarg;
    116.                             c.Borders.Right.Margin = 0.5;//horMarg;
    117.                             // Set the vertical margins
    118.                             c.Borders.Top.Margin = 0.8;//verMarg;
    119.                             c.Borders.Bottom.Margin = 0.8;//verMarg;
    120.                             //CellBorder myCellBorder = c.Borders.Bottom;
    121.                             //myCellBorder.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
    122.                             c.Borders.Bottom.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);//1代表红色
    123.                             c.Contents[0].BlockTableRecordId = objId;
    124.                            
    125.                             //tb.SetBlockTableRecordId(i, 3, objId, true);
    126.                            
    127.                             // And then we use a field to check on whether
    128.                             // it's a dynamic block or not
    129.                             string strObjId = objId.ToString();
    130.                             strObjId = strObjId.Trim(new char[] { '(', ')' });
    131.                             tb.SetTextHeight(i, 4, 1);
    132.                             tb.SetTextString(i, 4, "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).IsDynamicBlock \\f "%bl2">%");
    133.                             tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
    134.                         }
    135.                     }
    136.                     //非常重要,根据当前样式更新表格,不加此句,会导致AutoCAD崩溃
    137.                     tb.GenerateLayout();
    138.                     //tb.FlowDirection = Autodesk.AutoCAD.DatabaseServices.FlowDirection.TopToBottom;
    139.                     BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    140.                     btr.AppendEntity(tb);
    141.                     tr.AddNewlyCreatedDBObject(tb, true);
    142.                     tr.Commit();
    143.                 }
    144.             }
    145.         }

    146.         [CommandMethod("CRT")]
    147.         static public void CreateTable()
    148.         {
    149.             Document doc = Application.DocumentManager.MdiActiveDocument;
    150.             Database db = doc.Database;
    151.             Editor ed = doc.Editor;
    152.             ObjectId appDictId = ObjectId.Null;
    153.             //DBDictionary appDict = new DBDictionary();
    154.             TableStyle myTableStyle = new TableStyle();
    155.             PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
    156.             if (pr.Status == PromptStatus.OK)
    157.             {
    158.                 Transaction tr = doc.TransactionManager.StartTransaction();
    159.                 using (tr)
    160.                 {
    161.                     BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
    162.                     Table tb = new Table();
    163.                     DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);//获取数据库object,并转换为DBDictionary
    164.                     if (nod.Contains("ACAD_TABLESTYLE"))//包含这个字典
    165.                     {
    166.                         DBDictionary compDict = (DBDictionary)tr.GetObject(nod.GetAt("ACAD_TABLESTYLE"), OpenMode.ForRead);
    167.                         if (!compDict.Contains("Standard1"))
    168.                         {
    169.                             compDict.UpgradeOpen();
    170.                             appDictId = compDict.SetAt("Standard1", myTableStyle);
    171.                             tr.AddNewlyCreatedDBObject(myTableStyle, true);
    172.                         }
    173.                         else
    174.                         {
    175.                             appDictId = compDict.GetAt("Standard1");
    176.                             myTableStyle = (TableStyle)tr.GetObject(compDict.GetAt("Standard1"), OpenMode.ForRead);
    177.                         }
    178.                         myTableStyle.UpgradeOpen();//这个是必须的,否则会有问题的
    179.                         myTableStyle.FlowDirection = FlowDirection.NotSet;//LeftToRight从下往上,NotSet从上往下,如果下面表格再设置就会不按照样式来
    180.                         myTableStyle.IsTitleSuppressed = false;// tb.TableStyle = myTableStyle.ObjectIdb必须要有这个语句才起作用
    181.                         myTableStyle.IsHeaderSuppressed = false;
    182.                         //myTableStyle.DowngradeOpen();
    183.                     }
    184.                     tb.TableStyle = myTableStyle.ObjectId;//这个语句很关键,如果没有下面tb的很多设置不起作用
    185.                     tb.IsTitleSuppressed = true;
    186.                     tb.IsHeaderSuppressed = true;
    187.                     tb.FlowDirection = FlowDirection.LeftToRight;//如果没有tb.TableStyle = myTableStyle.ObjectId,则这个无论怎么设置似乎都不起作用
    188.                     //FlowDirection myFlowDirection = FlowDirection.BottomToTop;//BottomToTop,不存在的,似乎桌子公司错了
    189.                     tb.SetSize(10, 5);//设置行列数目   //旧的方式 tb.NumRows = 7 //tb.NumColumns = 7;
    190.                     tb.SetRowHeight(5);//设置行高
    191.                     tb.SetColumnWidth(15);// 设置列宽
    192.                     //tb.Columns[0].Width = 20; // 设置第一列宽度为20
    193.                     tb.Position = pr.Value;
    194.                     // Create a 2-dimensional array
    195.                     // of our table contents
    196.                     string[,] str = new string[5, 4];
    197.                     str[0, 0] = "Part No.";
    198.                     str[0, 1] = "Name";
    199.                     str[0, 2] = "Material ";
    200.                     str[1, 0] = "1876-1";
    201.                     str[1, 1] = "Flange";
    202.                     str[1, 2] = "Perspex";
    203.                     str[2, 0] = "0985-4";
    204.                     str[2, 1] = "Bolt";
    205.                     str[2, 2] = "Steel";
    206.                     str[3, 0] = "3476-K";
    207.                     str[3, 1] = "Tile";
    208.                     str[3, 2] = "Ceramic";
    209.                     str[4, 0] = "8734-3";
    210.                     str[4, 1] = "Kean";
    211.                     str[4, 2] = "Mostly water";
    212.                     // Use a nested loop to add and format each cell
    213.                     for (int i = 0; i < 5; i++)
    214.                     {
    215.                         for (int j = 0; j < 3; j++)
    216.                         {
    217.                             tb.SetTextHeight(i, j, 1);
    218.                             tb.Cells[i, j].TextString = str[i, j];  //旧的方式,tb.SetTextString(i, j, str[i, j]);
    219.                             tb.SetAlignment(i, j, CellAlignment.MiddleCenter);
    220.                             tb.Cells.Borders.Bottom.Color=Color.FromColorIndex(ColorMethod.ByAci, 1);//1代表红色
    221.                         }
    222.                         // Adding title information for additional columns
    223.                         if (i == 0)
    224.                         {
    225.                             tb.SetTextHeight(i, 3, 1);
    226.                             tb.Cells[i, 3].TextString = "Block Preview";
    227.                             tb.SetAlignment(i, 3, CellAlignment.MiddleCenter);
    228.                             tb.SetTextHeight(i, 4, 1);
    229.                             tb.Cells[i, 4].TextString = "Is Dynamic?";
    230.                             tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
    231.                         }
    232.                         // If a block definition exists for a block of our
    233.                         // "name" field, then let's set it in the 4th column
    234.                         if (bt.Has(str[i, 1]))
    235.                         {
    236.                             ObjectId objId = bt[str[i, 1]];
    237.                             tb.SetBlockTableRecordId(i, 3, objId, true);
    238.                             //tb.Cells[i, 3].Contents[0].BlockTableRecordId = objId;
    239.                             // And then we use a field to check on whether
    240.                             // it's a dynamic block or not
    241.                             string strObjId = objId.ToString();
    242.                             strObjId = strObjId.Trim(new char[] { '(', ')' });
    243.                             tb.SetTextHeight(i, 4, 1);
    244.                             tb.Cells[i, 4].TextString = "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).IsDynamicBlock \\f "%bl2">%";
    245.                             //tb.SetTextString(i, 4, "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).IsDynamicBlock \\f "%bl2">%");
    246.                            
    247.                             tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
    248.                         }
    249.                     }
    250.                     //非常重要,根据当前样式更新表格,不加此句,会导致AutoCAD崩溃
    251.                     tb.GenerateLayout();
    252.                     //tb.FlowDirection = Autodesk.AutoCAD.DatabaseServices.FlowDirection.TopToBottom;
    253.                     BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    254.                     btr.AppendEntity(tb);
    255.                     tr.AddNewlyCreatedDBObject(tb, true);
    256.                     tr.Commit();
    257.                 }
    258.             }
    259.         }

    260.         [CommandMethod("t8")]
    261.         public static void t8()//选择table,读取属性
    262.         {
    263.             Document doc = Application.DocumentManager.MdiActiveDocument;
    264.             Editor ed = doc.Editor;
    265.             Database db = doc.Database;
    266.             Table myTable = new Table();
    267.             PromptEntityOptions opts = new PromptEntityOptions("\n选择一个table:");
    268.             opts.SetRejectMessage("错误的选择!");
    269.             opts.AddAllowedClass(typeof(Table), false);
    270.             PromptEntityResult res = ed.GetEntity(opts);
    271.             if (res.Status != PromptStatus.OK)
    272.                 return;
    273.             using (Transaction tr = db.TransactionManager.StartTransaction())
    274.             {
    275.                 myTable = tr.GetObject(res.ObjectId, OpenMode.ForRead) as Table;
    276.                 myTable.FlowDirection = FlowDirection.LeftToRight;
    277.                 tr.Commit();
    278.             }
    279.         }

    280.     }
    281. }
    复制代码

     

     

     

     

    [表格] 关于表格的代码
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网_中国空间膜结构协会

    GMT+8, 2024-5-17 17:41 , Processed in 0.058805 second(s), 22 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表