明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1534|回复: 4

[表格] 数据写入autoCAD

[复制链接]
发表于 2013-3-29 16:03 | 显示全部楼层 |阅读模式
怎样把dataGridView数据以表格的形式写入AutoCAD,望高手不吝赐教!望能提供点代码供小弟学习,谢谢!
发表于 2013-4-1 21:06 | 显示全部楼层
你是想写实体Table呢还是数据字典?
 楼主| 发表于 2013-4-2 11:39 | 显示全部楼层
是实体Table
发表于 2013-4-2 12:49 | 显示全部楼层
本帖最后由 chmenf087 于 2013-4-2 12:50 编辑
  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. namespace TableCreation
  7. {
  8.   public class Commands
  9.   {
  10.     [CommandMethod("CRT")]
  11.     static public void CreateTable()
  12.     {
  13.       Document doc =
  14.         Application.DocumentManager.MdiActiveDocument;
  15.       Database db = doc.Database;
  16.       Editor ed = doc.Editor;
  17.       PromptPointResult pr =
  18.         ed.GetPoint("\nEnter table insertion point: ");
  19.       if (pr.Status == PromptStatus.OK)
  20.       {
  21.         Table tb = new Table();
  22.         tb.TableStyle = db.Tablestyle;
  23.         tb.NumRows = 5;
  24.         tb.NumColumns = 3;
  25.         tb.SetRowHeight(3);
  26.         tb.SetColumnWidth(15);
  27.         tb.Position = pr.Value;
  28.         // Create a 2-dimensional array
  29.         // of our table contents
  30.         string[,] str = new string[5, 3];
  31.         str[0, 0] = "Part No.";
  32.         str[0, 1] = "Name ";
  33.         str[0, 2] = "Material ";
  34.         str[1, 0] = "1876-1";
  35.         str[1, 1] = "Flange";
  36.         str[1, 2] = "Perspex";
  37.         str[2, 0] = "0985-4";
  38.         str[2, 1] = "Bolt";
  39.         str[2, 2] = "Steel";
  40.         str[3, 0] = "3476-K";
  41.         str[3, 1] = "Tile";
  42.         str[3, 2] = "Ceramic";
  43.         str[4, 0] = "8734-3";
  44.         str[4, 1] = "Kean";
  45.         str[4, 2] = "Mostly water";
  46.         // Use a nested loop to add and format each cell
  47.         for (int i = 0; i < 5; i++)
  48.         {
  49.           for (int j = 0; j < 3; j++)
  50.           {
  51.             tb.SetTextHeight(i, j, 1);
  52.             tb.SetTextString(i, j, str[i, j]);
  53.             tb.SetAlignment(i, j, CellAlignment.MiddleCenter);
  54.           }
  55.         }
  56.         tb.GenerateLayout();
  57.         Transaction tr =
  58.           doc.TransactionManager.StartTransaction();
  59.         using (tr)
  60.         {
  61.           BlockTable bt =
  62.             (BlockTable)tr.GetObject(
  63.               doc.Database.BlockTableId,
  64.               OpenMode.ForRead
  65.             );
  66.           BlockTableRecord btr =
  67.             (BlockTableRecord)tr.GetObject(
  68.               bt[BlockTableRecord.ModelSpace],
  69.               OpenMode.ForWrite
  70.             );
  71.           btr.AppendEntity(tb);
  72.           tr.AddNewlyCreatedDBObject(tb, true);
  73.           tr.Commit();
  74.         }
  75.       }
  76.     }
  77.   }
  78. }
稍加修改就可以了(遍历一下DatagrideView)
这些本版内都有的(我只是复制粘贴而已)
 楼主| 发表于 2013-4-2 16:44 | 显示全部楼层
chmenf087 发表于 2013-4-2 12:49
稍加修改就可以了(遍历一下DatagrideView)
这些本版内都有的(我只是复制粘贴而已)

谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-25 08:03 , Processed in 0.186980 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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