使用DWGDirect插件提取DWG中的块属性,“db = new Database(false, false);”报未将对象引用设置到对象的实例,不知道原因,各位给个意见吧!using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Runtime;namespace CadDwgEdit
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Database db = null;
getCAD(db);
} private void getCAD(Database db)
{
//Document doc = Document.FromAcadDocument("C:\abc.dwg");
//string path = "../Web/imgfile/201207/201207171030120000.dwg";
//string path = "C:\abc.dwg";
//db = doc.Database;
try
{
//把DWG文件读入到一个临时的内存数据库中
db = new Database(false, false);
db.ReadDwgFile("C:\abc.dwg", System.IO.FileShare.ReadWrite, true, null);
//现在进入数据库并获得数据库的块表引用
Transaction trans = db.TransactionManager.StartTransaction();
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
//从块表的模型空间特性中获得块表记录,块表记录对象包含DWG文件数据库实体
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false, true);
foreach (ObjectId btrId in btr)
{
DBObject entBlock = (DBObject)trans.GetObject(btrId, OpenMode.ForRead, false, true);
if (entBlock.GetRXClass().DxfName.ToUpper() == "INSERT")
{
BlockReference bRef = (BlockReference)entBlock;
if (bRef.AttributeCollection.Count != 0)
{
System.Collections.IEnumerator bRefEnum = bRef.AttributeCollection.GetEnumerator();
while (bRefEnum.MoveNext())
{
ObjectId aId = (ObjectId)bRefEnum.Current;//这一句极其关键
AttributeReference aRef = (AttributeReference)trans.GetObject(aId, OpenMode.ForRead, false, true);
this.textBox1.Text = aRef.TextString;//此语句即获得属性单行文本,请自行在此语句前添加 属性单行文本 赋于的变量
}
}
}
}
trans.Commit(); //提交事务处理
btr.Dispose();
bt.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show("\n出错啦: " + ex.Message);
}
finally
{
db.Dispose();
}
}
}
}
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Runtime;namespace CadDwgEdit
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Database db = null;
getCAD(db);
} private void getCAD(Database db)
{
//Document doc = Document.FromAcadDocument("C:\abc.dwg");
//string path = "../Web/imgfile/201207/201207171030120000.dwg";
//string path = "C:\abc.dwg";
//db = doc.Database;
try
{
//把DWG文件读入到一个临时的内存数据库中
db = new Database(false, false);
db.ReadDwgFile("C:\abc.dwg", System.IO.FileShare.ReadWrite, true, null);
//现在进入数据库并获得数据库的块表引用
Transaction trans = db.TransactionManager.StartTransaction();
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
//从块表的模型空间特性中获得块表记录,块表记录对象包含DWG文件数据库实体
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false, true);
foreach (ObjectId btrId in btr)
{
DBObject entBlock = (DBObject)trans.GetObject(btrId, OpenMode.ForRead, false, true);
if (entBlock.GetRXClass().DxfName.ToUpper() == "INSERT")
{
BlockReference bRef = (BlockReference)entBlock;
if (bRef.AttributeCollection.Count != 0)
{
System.Collections.IEnumerator bRefEnum = bRef.AttributeCollection.GetEnumerator();
while (bRefEnum.MoveNext())
{
ObjectId aId = (ObjectId)bRefEnum.Current;//这一句极其关键
AttributeReference aRef = (AttributeReference)trans.GetObject(aId, OpenMode.ForRead, false, true);
this.textBox1.Text = aRef.TextString;//此语句即获得属性单行文本,请自行在此语句前添加 属性单行文本 赋于的变量
}
}
}
}
trans.Commit(); //提交事务处理
btr.Dispose();
bt.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show("\n出错啦: " + ex.Message);
}
finally
{
db.Dispose();
}
}
}
}
解决方案 »
- 求高人指教,我是C#初学者,想提取txt文件中的内容
- 连接SQL2000问题
- 关于C#写的移动设备程序问题
- 求助:做一个登录窗体.身份验证问题
- 问个OpenGL的问题
- 如何动态统计datagrid某几个列的值?并显示在最后一行中
- FileUpload控件点击浏览按钮没反应
- DockingManager使用难题
- winform求助,计时器滚动5秒(上限引用之前的全局变量)然后停止,再显示之前的结果变量(多个,每次一个不重复)停留2秒,重复运行,到1分钟后自动结束
- 弹出窗口显示提示(WebForm)
- AJAX 无刷新实现级联效果利用xmlHttpRequest,客户端全选删除效果
- 如何让控件绑定到DataTable的 属性上 而 不是数据上?
不太明白这两个参数都是false表示什么?
显然应该写成:"C:\\abc.dwg"
1.没有实例化对象
2.某个对象的值是null
using (Teigha.Runtime.Services srv = new Teigha.Runtime.Services())
{
//读取DWG文件
}