明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1513|回复: 3

[基础] 怎样修改entity的属性

[复制链接]
发表于 2010-12-17 15:09 | 显示全部楼层 |阅读模式
目的:选择一个entity,当这个entity是Mtext的时候,改变Mtext的内容
加载,运行,提示这里不对--btr.AppendEntity(ent);
代码如下:
        [CommandMethod("selectent")]
        public void secectent()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed=doc.Editor;
            PromptEntityOptions opts = new PromptEntityOptions("\nPlease select a Mtext");
            opts.AllowNone = true;
            PromptEntityResult res = ed.GetEntity(opts);
            if (res.Status == PromptStatus.OK)
            {
                changes(res.ObjectId);
            }
        }
        public void changes(ObjectId id)
        {
            BlockTableRecord btr;
            BlockTable bt;
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction tran;
            tran = db.TransactionManager.StartTransaction();
            Entity ent = (Entity)tran.GetObject(id, OpenMode.ForWrite);
            bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForRead);
            btr = (BlockTableRecord)tran.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
            if (ent.GetType() == typeof(MText))
            {
                MText mt = new MText();
                mt = (MText)ent;
                mt.Contents = "AAAAA";
            }
            btr.AppendEntity(ent);
            tran.AddNewlyCreatedDBObject(ent, true);
            tran.Commit();
            tran.Dispose();
        }

 楼主| 发表于 2010-12-17 15:14 | 显示全部楼层
上面的有错误
if (ent.GetType() == typeof(MText))
            {
                MText mt = new MText();
                mt = (MText)ent;
                mt.Contents = "AAAAA";
            }
            btr.AppendEntity(ent);
            tran.AddNewlyCreatedDBObject(ent, true);
            tran.Commit();
改为
MText mt = new MText();

            if (ent.GetType() == typeof(MText))
            {
               
                mt = (MText)ent;
                mt.Contents = "AAAAA";
            }
            btr.AppendEntity(mt);
            tran.AddNewlyCreatedDBObject(mt, true);

异常提示是eAlreadyDB什么的,想想也是database中已存在这个ID的entity,再append就不对了,那怎样modify呢?
发表于 2010-12-17 15:14 | 显示全部楼层
去掉
            btr.AppendEntity(ent);
            tran.AddNewlyCreatedDBObject(ent, true);
只有新建的图元才需要添加到数据库
已存在的不能重复添加
 楼主| 发表于 2010-12-17 15:17 | 显示全部楼层
呃……我怎么这么笨呢!
谢谢版主
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-17 10:20 , Processed in 0.222919 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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