明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1476|回复: 6

菜鸟请教

[复制链接]
发表于 2010-5-14 10:27:00 | 显示全部楼层 |阅读模式

从官方例题改的,不知道哪里错了,插入块后,我想得到块名:myblock1,但我只得到:*Model_Space,为什么?请高手帮我看下,谢谢

        [CommandMethod("createblock1")]  //插入块
        public void createb()
        {
            Database db = HostApplicationServices.WorkingDatabase; //AutoCAD数据库
            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[blockname];
                }
                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;
        }

       [CommandMethod("ShowData")]
        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();
            }
        }

发表于 2010-5-14 22:57:00 | 显示全部楼层

块操作的概念要搞清楚

首先在块表中创建块定义(BlockTableRecord)

然后在模型空间中按块定义生成块引用(BlockReference)

 楼主| 发表于 2010-5-15 12:59:00 | 显示全部楼层

我在ShowData()中用了BlockReference呀

lzh741206能帮我看看错在哪里吗,不胜感激

发表于 2010-5-15 13:36:00 | 显示全部楼层

BlockName:

Accesses the name of the owner block.

块引用是放在模型空间中的,返回的当然是:*Model_Space

你应该引用Name属性

 楼主| 发表于 2010-5-15 17:07:00 | 显示全部楼层

Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);

ent没有Name属性

发表于 2010-5-15 17:17:00 | 显示全部楼层

                    BlockReference ent = trans.GetObject(id, OpenMode.ForRead, false) as BlockReference; //打开当前对象!
                    if (ent != null)
                    {
                        ed.WriteMessage(ent.Name);
                    }

另外,如果是在模型空间中选择特定实体,用选择集更方便

 楼主| 发表于 2010-5-15 22:06:00 | 显示全部楼层
谢谢非常感谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-29 13:48 , Processed in 0.199312 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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