怎样修改entity的属性
目的:选择一个entity,当这个entity是Mtext的时候,改变Mtext的内容加载,运行,提示这里不对--btr.AppendEntity(ent);
代码如下:
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();
}
上面的有错误
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呢? 去掉
btr.AppendEntity(ent);
tran.AddNewlyCreatedDBObject(ent, true);
只有新建的图元才需要添加到数据库
已存在的不能重复添加 呃……我怎么这么笨呢!
谢谢版主
页:
[1]