求助:用C#读取CAD的dwg文件图层信息
请问怎样用C#语言读取CAD的dwg格式文件,想要读取里面图层的信息,比如读取里面的圆,线,矩形等,然后放在sql2008数据库中,我该安装什么插件吗?要加载什么包吗?我有C#语言基础,以前没有接触过CAD文件的读取。请高手指点方向。
public static void GetLayerPro()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//新建一个数据库对象以读取Dwg文件
Database db = new Database(false, true);
string fileName = "C:\\Drawing3.dwg";
//如果指定文件名的文件存在
if (System.IO.File.Exists(fileName))
{
//把文件读入到数据库中
db.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//获取数据库的图层表对象
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId,OpenMode.ForRead);
//循环遍历每个图层
foreach (ObjectId layerId in lt)
{
LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(layerId, OpenMode.ForRead);
if (ltr != null)
{
Autodesk.AutoCAD.Colors.Color layerColor = ltr.Color;
ed.WriteMessage("\n图层名称为:" + ltr.Name);
ed.WriteMessage("\n图层颜色为:" + layerColor.ToString());
}
}
trans.Commit();
}
}
}
public static void GetEntitiesFromLayer(string layerName)
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using(Transaction trans = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
if (lt.Has(layerName))
{
BlockTableRecord ltr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
foreach (ObjectId objId in ltr)
{
Entity ent = (Entity)trans.GetObject(objId, OpenMode.ForRead);
// 假设该图层中有一个圆
Circle myCircle = (Circle)ent;
if (ent.Layer == layerName && myCircle != null)
{
Point3d cirCenter = myCircle.Center;
ed.WriteMessage("\n圆心为:" + cirCenter.ToString());
}
}
}
trans.Commit();
}
} 菜鸟Liu 发表于 2012-3-17 17:28 static/image/common/back.gif
你好,首先谢谢你的代码。这是我的本科课程设计的研究课题,我对于C#语言的基本有了大致了解。请问能够编写上面你的代码,我需要参考哪些书?需要下载哪些包?怎样配置环境?希望您能给我列一个需要学习的书目或推荐的教程,谢谢。 你好,我使用的开发环境是VS2008+AutoCAD2010,环境安装百度里面搜都有的,至于参考书我用的是《AutoCAD VBA & VB.Net开发》,祝你学习顺利。 #region 提取一个图层上的各类元素
public void BlockInLayerCAD()
{
//PromptStringOptions pStringOption = new PromptStringOptions("\n 输入一个图层名");
//PromptResult layerName = pDocument.Editor.GetString(pStringOption);
List<string> layerNames = new List<string>();
using (Transaction tran = pDatabase.TransactionManager.StartTransaction())
{
#region 获取图层名字
LayerTable pLayerTable = tran.GetObject(pDatabase.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId pObjectId in pLayerTable)
{
LayerTableRecord pLayerTableRecord = tran.GetObject(pObjectId, OpenMode.ForRead) as LayerTableRecord;
layerNames.Add(pLayerTableRecord.Name);
}
#endregion
string layerName = string.Empty;
string typeResult = string.Empty;
FrmLayer frm = new FrmLayer(layerNames);
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
{
layerName = frm.selectLayer;
typeResult = frm.blockType;
}
else
{
return;
}
TypedValue[] pTypedValue = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, layerName) };
SelectionFilter pSelectFilter = new SelectionFilter(pTypedValue);
PromptSelectionResult pSelectionResult = pDocument.Editor.SelectAll(pSelectFilter);
SelectionSet pSelectionSet = pSelectionResult.Value;
StreamWriter txt = new StreamWriter(Stream.Null, Encoding.UTF8);
if (typeResult != "全部")
{
if (File.Exists("C:\\cad\\cadMessage.txt"))
{
File.Delete("C:\\cad\\cadMessage.txt");
}
txt = File.AppendText("C:\\cad\\cadMessage.txt");
}
Point3d startPoint = new Point3d();
Point3d endPoint = new Point3d();
if (typeResult != "全部")
{
PromptPointOptions txtPoint = new PromptPointOptions("\n 选择两个点作为文字取值范围");
txtPoint.Message = "\n 选择第一个点:";
PromptPointResult txtStartPoint = pDocument.Editor.GetPoint(txtPoint);
startPoint = txtStartPoint.Value;
txtPoint.Message = "\n 选择第二个点:";
PromptPointResult txtEndPoint = pDocument.Editor.GetPoint(txtPoint);
endPoint = txtEndPoint.Value;
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(startPoint.X.ToString() + "," + startPoint.Y.ToString() + ";" + endPoint.X.ToString() + "," + endPoint.Y.ToString());
}
foreach (ObjectId selectedId in pSelectionSet.GetObjectIds())
{
Entity pEntity = tran.GetObject(selectedId, OpenMode.ForRead) as Entity;
switch (typeResult)
{
case "全部":
pEntity.ColorIndex = 4;
break;
case "文字":
if ((pEntity as MText) != null)
{
MText mText = pEntity as MText;
if (mText.Location.X > startPoint.X && mText.Location.Y < startPoint.Y && mText.Location.X < endPoint.X && mText.Location.Y > endPoint.Y)
{
txt.WriteLine((pEntity as MText).Contents.ToString());
}
}
if ((pEntity as DBText) != null)
{
DBText pDBText = pEntity as DBText;
//txtList.Add(pDBText);//这个留着后面测试分析用
if (pDBText.Position.X > startPoint.X && pDBText.Position.X < endPoint.X)
{
if (pDBText.Position.Y < startPoint.Y && pDBText.Position.Y > endPoint.Y)
{
txt.WriteLine((pEntity as DBText).TextString.ToString());
pDocument.Editor.WriteMessage(pDBText.TextString+"\n");
txtList.Add(pDBText);//这个留着后面测试分析用
}
}
}
break;
case "多段线":
if ((pEntity as Polyline) != null)
{
Polyline pPolyline = pEntity as Polyline;
txt.WriteLine("起点:" + pPolyline.StartPoint.X.ToString() + "," + pPolyline.StartPoint.Y.ToString());
txt.WriteLine("终点:" + pPolyline.EndPoint.X.ToString() + "," + pPolyline.EndPoint.Y.ToString());
}
break;
case "直线":
if ((pEntity as Line) != null)
{
Line pLine = pEntity as Line;
txt.WriteLine("起点:" + pLine.StartPoint.X.ToString() + "," + pLine.StartPoint.Y.ToString());
txt.WriteLine("终点:" + pLine.EndPoint.X.ToString() + "," + pLine.EndPoint.Y.ToString());
}
break;
case "圆":
if ((pEntity as Circle) != null)
{
Circle pCircle = pEntity as Circle;
txt.WriteLine("圆心:(" + pCircle.Center.X.ToString() + "," + pCircle.Center.Y.ToString() + "),半径:" + pCircle.Radius.ToString());
}
break;
default:
break;
}
}
tran.Commit();
txt.Flush();
txt.Close();
}
}
#endregion上面是我写的取出DWG里面指定图层,指定类型的方法,然后涉及到一个窗体帮助选择图层和类型的,代码我附上,就不贴代码了哈。希望你有用...using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CADappolication
{
public partial class FrmLayer : Form
{
public FrmLayer()
{
InitializeComponent();
}
List<string> layerName = new List<string>();
public string selectLayer;
public string blockType;
public FrmLayer(List<string> layers)
{
layerName = layers;
InitializeComponent();
}
public void FrmLayer_Load(object sender, EventArgs e)
{
if (layerName != null)
{
foreach (string layer in layerName)
{
comboBox1.Items.Add(layer);
}
comboBox2.Items.Add("全部");
comboBox2.Items.Add("多段线");
comboBox2.Items.Add("直线");
comboBox2.Items.Add("文字");
comboBox2.Items.Add("圆");
comboBox2.SelectedIndex = 0;
}
else
{
MessageBox.Show("图层读取失败...");
}
}
public void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
selectLayer = comboBox1.SelectedItem.ToString();
blockType = comboBox2.SelectedItem.ToString();
}
else
{
MessageBox.Show("请选择个图层");
return;
}
}
}
}
本帖最后由 hjki_i 于 2016-9-30 09:48 编辑
楼主,解决问题没有,C#语言读取CAD的dwg格式文件,想要读取里面图层的信息,比如读取里面的圆,线,矩形等,我不用存到数据库中,直接获取信息,在窗体上显示,怎么做,发一下你的Demo到邮箱里,万分感谢!! 1246462177@qq.com
页:
[1]