如何替换块?
原来图里的符号要替换成新符号,仅仅是符号替换,而不改变xdata和attribute,如何实现?谢谢 <p>在BlockTable中找到对应的记录,打开</p><p>遍历,删除属性定义外的实体</p><p>然后加入新实体</p><p></p><p></p> 有没有代码示例?谢谢 <p>今天一天都在外面飘,简单描述下吧</p><p>获取BlockTable->bt</p><p>btr = bt</p><p>foreach(ObjectId id in btr)</p><p>{</p><p>Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite)</p><p>if(!(ent is AttDef))</p><p>{</p><p>ent.earse</p><p>}</p><p>}</p><p>然后加入实体,这部分简化:)</p> 是blockreference 以为是替换全部呢:)public static void tt1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptEntityOptions optsEnt = new PromptEntityOptions("\nSelect a Block:");
optsEnt.SetRejectMessage("\nNot a Block!");
optsEnt.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult resEnt = ed.GetEntity(optsEnt);
if (resEnt.Status == PromptStatus.OK)
{
PromptResult resStr = ed.GetString("\nInput BlockName:");
if(resStr.Status == PromptStatus.OK)
{
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
string blkname = resStr.StringResult;
if(bt.Has(blkname))
{
BlockReference blkref = (BlockReference)tr.GetObject(resEnt.ObjectId, OpenMode.ForWrite);
BlockReference blkrefnew = new BlockReference(blkref.Position, bt);
blkrefnew.BlockTransform = blkref.BlockTransform;
btr.AppendEntity(blkrefnew);
tr.AddNewlyCreatedDBObject(blkrefnew, true);
foreach(ObjectId attid in blkref.AttributeCollection)
{
AttributeReference att = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead);
AttributeReference attnew = (AttributeReference)att.Clone();
blkrefnew.AttributeCollection.AppendAttribute(attnew);
tr.AddNewlyCreatedDBObject(attnew, true);
}
blkrefnew.XData = blkref.XData;
blkref.Erase();
tr.Commit();
}
}
}
}
}
试试,谢谢先
页:
[1]