使用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();
            }   
        }
    }
}

解决方案 »

  1.   

    我一步步调过来的,try后面走了一句,就跑catch了。这个是关于提取dwg文件的!我用的是DWGDirect插件做。这个就是DWGDirect插件中的类,我实例化的时候出错,这是什么原因呢!
      

  2.   

    db = new Database(false, false);
    不太明白这两个参数都是false表示什么?
      

  3.   

    问题不在这里,这两个false,第一个false表示是否建立默认的绘图,第二个表示是否新建文件。我把这两个false去掉,也报同样的错。这个dwg文件的读取。
      

  4.   

    要把那个.gs的文件放到Bin目录
      

  5.   

    这个错误是因为没new对象就开始用了。。java中叫做空指针
      

  6.   

    这里也错了哦:"C:\abc.dwg"
    显然应该写成:"C:\\abc.dwg"
      

  7.   

    话说lz你的DataBase在外面有用不到,为什吗要当参数从外面传进来呢,劝你还是再在你的getCAD方法里
      

  8.   

    “未将对象引用设置到对象的实例”这种异常一般都是这两种情况:
    1.没有实例化对象
    2.某个对象的值是null
      

  9.   

    100%  是有个东西为NULL了  然后你去点他····
      

  10.   

    需要先初始化Teigha,再读图形文件,否则报错,如下:
    using (Teigha.Runtime.Services srv = new Teigha.Runtime.Services())
    {
        //读取DWG文件
    }
      

  11.   

    请问你用的是Teigha.NET那个版本?
      

  12.   

    楼上有位兄弟说的很正确,你没有使用Service
      

  13.   

    这个能放到asp.net里用吗?我把TD_Mgd_3.03_9.dll引用到web项目里就总弹出这样的错误:Could not load file or assembly 'TD_Mgd_3.03_9' or one of its dependencies. An attempt was made to load a program with an incorrect format. 当然,我已经试过http://www.cnblogs.com/lifuyun/archive/2011/09/23/lifuyun2011092301.html和权限等方法了,都没能解决。在WINFORM里,我读取和写入都可以,就是bs中出现上面问题,根本跟不进去啊
      

  14.   

    有人说TD_Mgd_3.03_9.dll是GDI程序dll,不能在bs项目里引用~~~
      

  15.   

    希望楼主能联系我,我的QQ:38172679,email:[email protected]