明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 13274|回复: 5

求助:用C#读取CAD的dwg文件图层信息

[复制链接]
发表于 2012-3-11 22:33 | 显示全部楼层 |阅读模式
请问怎样用C#语言读取CAD的dwg格式文件,想要读取里面图层的信息,比如读取里面的圆,线,矩形等,然后放在sql2008数据库中,我该安装什么[url=]插件[/url]吗?要加载什么包吗?
      我有C#语言基础,以前没有接触过CAD文件的读取。请高手指点方向。
发表于 2012-3-17 17:28 | 显示全部楼层
  1. [CommandMethod("GetLayerPro")]
  2.         public static void GetLayerPro()
  3.         {
  4.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  5.             //新建一个数据库对象以读取Dwg文件
  6.             Database db = new Database(false, true);
  7.             string fileName = "C:\\Drawing3.dwg";
  8.             //如果指定文件名的文件存在
  9.             if (System.IO.File.Exists(fileName))
  10.             {
  11.                 //把文件读入到数据库中
  12.                 db.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
  13.                 using (Transaction trans = db.TransactionManager.StartTransaction())
  14.                 {
  15.                     //获取数据库的图层表对象
  16.                     LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId,OpenMode.ForRead);
  17.                     //循环遍历每个图层
  18.                     foreach (ObjectId layerId in lt)
  19.                     {
  20.                         LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForRead);
  21.                         if (ltr != null)
  22.                         {
  23.                             Autodesk.AutoCAD.Colors.Color layerColor = ltr.Color;
  24.                             ed.WriteMessage("\n图层名称为:" + ltr.Name);
  25.                             ed.WriteMessage("\n图层颜色为:" + layerColor.ToString());
  26.                         }
  27.                     }
  28.                     trans.Commit();
  29.                 }
  30.             }
  31.         }

  32.         [CommandMethod("GetEntitiesFromLayer")]
  33.         public static void GetEntitiesFromLayer(string layerName)
  34.         {
  35.             Database db = HostApplicationServices.WorkingDatabase;
  36.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  37.             using(Transaction trans = db.TransactionManager.StartTransaction())
  38.             {
  39.                 LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
  40.                 if (lt.Has(layerName))
  41.                 {
  42.                     BlockTableRecord ltr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
  43.                     foreach (ObjectId objId in ltr)
  44.                     {
  45.                         Entity ent = (Entity)trans.GetObject(objId, OpenMode.ForRead);
  46.                         // 假设该图层中有一个圆
  47.                         Circle myCircle = (Circle)ent;
  48.                         if (ent.Layer == layerName && myCircle != null)
  49.                         {
  50.                             Point3d cirCenter = myCircle.Center;
  51.                             ed.WriteMessage("\n圆心为:" + cirCenter.ToString());
  52.                         }
  53.                     }
  54.                 }
  55.                 trans.Commit();
  56.             }
  57.         }

评分

参与人数 1明经币 +1 金钱 +20 收起 理由
东风秀 + 1 + 20 谢谢你的代码。我已经给你加了能最多加的分.

查看全部评分

 楼主| 发表于 2012-3-19 10:21 | 显示全部楼层
菜鸟Liu 发表于 2012-3-17 17:28

你好,首先谢谢你的代码。这是我的本科课程设计的研究课题,我对于C#语言的基本有了大致了解。请问能够编写上面你的代码,我需要参考哪些书?需要下载哪些包?怎样配置环境?希望您能给我列一个需要学习的书目或推荐的教程,谢谢。
发表于 2012-3-19 13:02 | 显示全部楼层
你好,我使用的开发环境是VS2008+AutoCAD2010,环境安装百度里面搜都有的,至于参考书我用的是《AutoCAD VBA & VB.Net开发》,祝你学习顺利。
发表于 2012-7-20 16:19 | 显示全部楼层
  1. #region 提取一个图层上的各类元素

  2.         [CommandMethod("BlockInLayerCAD")]
  3.         public void BlockInLayerCAD()
  4.         {
  5.             //PromptStringOptions pStringOption = new PromptStringOptions("\n 输入一个图层名");
  6.             //PromptResult layerName = pDocument.Editor.GetString(pStringOption);

  7.            

  8.             List<string> layerNames = new List<string>();
  9.             using (Transaction tran = pDatabase.TransactionManager.StartTransaction())
  10.             {
  11.                 #region 获取图层名字
  12.                 LayerTable pLayerTable = tran.GetObject(pDatabase.LayerTableId, OpenMode.ForRead) as LayerTable;
  13.                 foreach (ObjectId pObjectId in pLayerTable)
  14.                 {
  15.                     LayerTableRecord pLayerTableRecord = tran.GetObject(pObjectId, OpenMode.ForRead) as LayerTableRecord;
  16.                     layerNames.Add(pLayerTableRecord.Name);
  17.                 }

  18.                 #endregion

  19.                 string layerName = string.Empty;
  20.                 string typeResult = string.Empty;

  21.                 FrmLayer frm = new FrmLayer(layerNames);
  22.                 frm.ShowDialog();
  23.                 if (frm.DialogResult == DialogResult.OK)
  24.                 {
  25.                     layerName = frm.selectLayer;
  26.                     typeResult = frm.blockType;
  27.                 }
  28.                 else
  29.                 {
  30.                     return;
  31.                 }

  32.                 TypedValue[] pTypedValue = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, layerName) };
  33.                 SelectionFilter pSelectFilter = new SelectionFilter(pTypedValue);
  34.                 PromptSelectionResult pSelectionResult = pDocument.Editor.SelectAll(pSelectFilter);
  35.                 SelectionSet pSelectionSet = pSelectionResult.Value;

  36.                 StreamWriter txt = new StreamWriter(Stream.Null, Encoding.UTF8);
  37.                 if (typeResult != "全部")
  38.                 {
  39.                     if (File.Exists("C:\\cad\\cadMessage.txt"))
  40.                     {
  41.                         File.Delete("C:\\cad\\cadMessage.txt");
  42.                     }
  43.                     txt = File.AppendText("C:\\cad\\cadMessage.txt");
  44.                 }
  45.                 Point3d startPoint = new Point3d();
  46.                 Point3d endPoint = new Point3d();
  47.                 if (typeResult != "全部")
  48.                 {
  49.                     PromptPointOptions txtPoint = new PromptPointOptions("\n 选择两个点作为文字取值范围");
  50.                     txtPoint.Message = "\n 选择第一个点:";
  51.                     PromptPointResult txtStartPoint = pDocument.Editor.GetPoint(txtPoint);
  52.                     startPoint = txtStartPoint.Value;
  53.                     txtPoint.Message = "\n 选择第二个点:";
  54.                     PromptPointResult txtEndPoint = pDocument.Editor.GetPoint(txtPoint);
  55.                     endPoint = txtEndPoint.Value;

  56.                     Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(startPoint.X.ToString() + "," + startPoint.Y.ToString() + ";" + endPoint.X.ToString() + "," + endPoint.Y.ToString());
  57.                 }

  58.                 foreach (ObjectId selectedId in pSelectionSet.GetObjectIds())
  59.                 {
  60.                     Entity pEntity = tran.GetObject(selectedId, OpenMode.ForRead) as Entity;

  61.                     switch (typeResult)
  62.                     {
  63.                         case "全部":
  64.                             pEntity.ColorIndex = 4;
  65.                             break;
  66.                         case "文字":
  67.                             if ((pEntity as MText) != null)
  68.                             {
  69.                                 MText mText = pEntity as MText;
  70.                                 if (mText.Location.X > startPoint.X && mText.Location.Y < startPoint.Y && mText.Location.X < endPoint.X && mText.Location.Y > endPoint.Y)
  71.                                 {
  72.                                     txt.WriteLine((pEntity as MText).Contents.ToString());
  73.                                 }
  74.                             }
  75.                             if ((pEntity as DBText) != null)
  76.                             {
  77.                                 DBText pDBText = pEntity as DBText;

  78.                                 //txtList.Add(pDBText);//这个留着后面测试分析用

  79.                                 if (pDBText.Position.X > startPoint.X && pDBText.Position.X < endPoint.X)
  80.                                 {
  81.                                     if (pDBText.Position.Y < startPoint.Y && pDBText.Position.Y > endPoint.Y)
  82.                                     {
  83.                                         txt.WriteLine((pEntity as DBText).TextString.ToString());
  84.                                         pDocument.Editor.WriteMessage(pDBText.TextString+"\n");

  85.                                         txtList.Add(pDBText);//这个留着后面测试分析用
  86.                                     }
  87.                                 }
  88.                             }

  89.                             break;
  90.                         case "多段线":
  91.                             if ((pEntity as Polyline) != null)
  92.                             {
  93.                                 Polyline pPolyline = pEntity as Polyline;
  94.                                 txt.WriteLine("起点:" + pPolyline.StartPoint.X.ToString() + "," + pPolyline.StartPoint.Y.ToString());
  95.                                 txt.WriteLine("终点:" + pPolyline.EndPoint.X.ToString() + "," + pPolyline.EndPoint.Y.ToString());
  96.                             }
  97.                             break;
  98.                         case "直线":
  99.                             if ((pEntity as Line) != null)
  100.                             {
  101.                                 Line pLine = pEntity as Line;
  102.                                 txt.WriteLine("起点:" + pLine.StartPoint.X.ToString() + "," + pLine.StartPoint.Y.ToString());
  103.                                 txt.WriteLine("终点:" + pLine.EndPoint.X.ToString() + "," + pLine.EndPoint.Y.ToString());
  104.                             }
  105.                             break;
  106.                         case "圆":
  107.                             if ((pEntity as Circle) != null)
  108.                             {
  109.                                 Circle pCircle = pEntity as Circle;
  110.                                 txt.WriteLine("圆心:(" + pCircle.Center.X.ToString() + "," + pCircle.Center.Y.ToString() + "),半径:" + pCircle.Radius.ToString());
  111.                             }
  112.                             break;
  113.                         default:
  114.                             break;
  115.                     }

  116.                 }
  117.                 tran.Commit();
  118.                 txt.Flush();
  119.                 txt.Close();
  120.             }
  121.             
  122.         }

  123.         #endregion
上面是我写的取出DWG里面指定图层,指定类型的方法,然后涉及到一个窗体帮助选择图层和类型的,代码我附上,就不贴代码了哈。希望你有用...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;

  8. namespace CADappolication
  9. {
  10.     public partial class FrmLayer : Form
  11.     {
  12.         public FrmLayer()
  13.         {
  14.             InitializeComponent();
  15.         }

  16.         List<string> layerName = new List<string>();
  17.         public string selectLayer;
  18.         public string blockType;

  19.         public FrmLayer(List<string> layers)
  20.         {
  21.             layerName = layers;
  22.             InitializeComponent();
  23.         }

  24.         public void FrmLayer_Load(object sender, EventArgs e)
  25.         {
  26.             if (layerName != null)
  27.             {
  28.                 foreach (string layer in layerName)
  29.                 {
  30.                     comboBox1.Items.Add(layer);
  31.                 }
  32.                 comboBox2.Items.Add("全部");
  33.                 comboBox2.Items.Add("多段线");
  34.                 comboBox2.Items.Add("直线");
  35.                 comboBox2.Items.Add("文字");
  36.                 comboBox2.Items.Add("圆");

  37.                 comboBox2.SelectedIndex = 0;

  38.             }
  39.             else
  40.             {
  41.                 MessageBox.Show("图层读取失败...");
  42.             }
  43.         }

  44.         public void button1_Click(object sender, EventArgs e)
  45.         {
  46.             if (comboBox1.SelectedItem != null)
  47.             {
  48.                 selectLayer = comboBox1.SelectedItem.ToString();
  49.                 blockType = comboBox2.SelectedItem.ToString();
  50.             }
  51.             else
  52.             {
  53.                 MessageBox.Show("请选择个图层");
  54.                 return;
  55.             }
  56.         }

  57.     }
  58. }
发表于 2016-9-30 09:45 | 显示全部楼层
本帖最后由 hjki_i 于 2016-9-30 09:48 编辑

楼主,解决问题没有,C#语言读取CAD的dwg格式文件,想要读取里面图层的信息,比如读取里面的圆,线,矩形等,我不用存到数据库中,直接获取信息,在窗体上显示,怎么做,发一下你的Demo到邮箱里,万分感谢!!                  1246462177@qq.com
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-3-29 08:35 , Processed in 0.186503 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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