heidong2002 发表于 2010-5-14 10:27:00

菜鸟请教

从官方例题改的,不知道哪里错了,插入块后,我想得到块名:myblock1,但我只得到:*Model_Space,为什么?请高手帮我看下,谢谢
      //插入块
      public void createb()
      {
            Database db = HostApplicationServices.WorkingDatabase; //数据库
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Transaction trans = db.TransactionManager.StartTransaction();//使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
            try
            {
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(CreateBlock("myblock1"), OpenMode.ForWrite);
                Ellipse ellipse = new Ellipse(new Point3d(200, 200, 0), Vector3d.ZAxis, new Vector3d(300, 0, 0), 0.5, 0, 0);
                ellipse.ColorIndex = 1;
                btr.AppendEntity(ellipse);
                trans.AddNewlyCreatedDBObject(ellipse, true);
                trans.Commit();
            }
            catch
            {
                ed.WriteMessage("Error");
            }
            finally
            {
                trans.Dispose();
            }
      }
      public ObjectId CreateBlock(string blockname)
      {
            ObjectId blockId = new ObjectId(); //它返回函数的值
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Transaction trans = db.TransactionManager.StartTransaction();
            try
            {
                //首先取得块表……
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
                //检查blockname块是否存在……
                if (bt.Has(blockname))
                {
                  blockId = bt;
                }
                else
                {
                  //如果blockname块不存在,就创建它
                  BlockTableRecord btr = new BlockTableRecord();
                  btr.Name = blockname; //设置块的名字
                  blockId = bt.Add(btr);
                  trans.AddNewlyCreatedDBObject(btr, true);
                }
                trans.Commit();
            }
            catch
            {
                ed.WriteMessage("Error");
            }
            finally
            {
                trans.Dispose();
            }
            return blockId;
      }
      
      public void PrintoutData()//CreateDivision
      {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();
            try
            {
                //首先,获取块表和模型空间块表记录
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId id in btr)
                {
                  Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //打开当前对象!
                  if (ent.GetType() == typeof(BlockReference))
                  {
                        ed.WriteMessage(ent.BlockName);
                  }
                }
            trans.Commit();
            }
            catch
            {
                ed.WriteMessage("Error");
            }
            finally
            {
                trans.Dispose();
            }
      }

雪山飞狐_lzh 发表于 2010-5-14 22:57:00

块操作的概念要搞清楚
首先在块表中创建块定义(BlockTableRecord)
然后在模型空间中按块定义生成块引用(BlockReference)

heidong2002 发表于 2010-5-15 12:59:00

我在ShowData()中用了BlockReference呀
lzh741206能帮我看看错在哪里吗,不胜感激

雪山飞狐_lzh 发表于 2010-5-15 13:36:00

BlockName:
Accesses the name of the owner block.
块引用是放在模型空间中的,返回的当然是:*Model_Space
你应该引用Name属性

heidong2002 发表于 2010-5-15 17:07:00

Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);
ent没有Name属性

雪山飞狐_lzh 发表于 2010-5-15 17:17:00

                  BlockReference ent = trans.GetObject(id, OpenMode.ForRead, false) as BlockReference; //打开当前对象!
                  if (ent != null)
                  {
                        ed.WriteMessage(ent.Name);
                  }
另外,如果是在模型空间中选择特定实体,用选择集更方便

heidong2002 发表于 2010-5-15 22:06:00

谢谢非常感谢
页: [1]
查看完整版本: 菜鸟请教