明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2542|回复: 6

如何替换块?

[复制链接]
发表于 2009-9-18 09:10 | 显示全部楼层 |阅读模式
原来图里的符号要替换成新符号,仅仅是符号替换,而不改变xdata和attribute,如何实现?谢谢
发表于 2009-9-18 09:48 | 显示全部楼层

在BlockTable中找到对应的记录,打开

遍历,删除属性定义外的实体

然后加入新实体

 楼主| 发表于 2009-9-18 11:30 | 显示全部楼层
有没有代码示例?谢谢
发表于 2009-9-18 12:31 | 显示全部楼层

今天一天都在外面飘,简单描述下吧

获取BlockTable->bt

btr = bt[yourblockname]

foreach(ObjectId id in btr)

{

Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite)

if(!(ent is AttDef))

{

ent.earse

}

}

然后加入实体,这部分简化:)

 楼主| 发表于 2009-9-18 14:59 | 显示全部楼层
是blockreference
发表于 2009-9-18 18:36 | 显示全部楼层
以为是替换全部呢:)
  1.         [CommandMethod("ts1")]
  2.         public static void tt1()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             PromptEntityOptions optsEnt = new PromptEntityOptions("\nSelect a Block:");
  7.             optsEnt.SetRejectMessage("\nNot a Block!");
  8.             optsEnt.AddAllowedClass(typeof(BlockReference), false);
  9.             PromptEntityResult resEnt = ed.GetEntity(optsEnt);
  10.             if (resEnt.Status == PromptStatus.OK)
  11.             {
  12.                 PromptResult resStr = ed.GetString("\nInput BlockName:");
  13.                 if(resStr.Status == PromptStatus.OK)
  14.                 {
  15.                     Database db = doc.Database;
  16.                     using (Transaction tr = db.TransactionManager.StartTransaction())
  17.                     {
  18.                         BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  19.                         BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  20.                         string blkname = resStr.StringResult;
  21.                         if(bt.Has(blkname))
  22.                         {
  23.                             BlockReference blkref = (BlockReference)tr.GetObject(resEnt.ObjectId, OpenMode.ForWrite);
  24.                             BlockReference blkrefnew = new BlockReference(blkref.Position, bt[blkname]);
  25.                             blkrefnew.BlockTransform = blkref.BlockTransform;
  26.                             btr.AppendEntity(blkrefnew);
  27.                             tr.AddNewlyCreatedDBObject(blkrefnew, true);
  28.                             foreach(ObjectId attid in blkref.AttributeCollection)
  29.                             {
  30.                                 AttributeReference att = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead);
  31.                                 AttributeReference attnew = (AttributeReference)att.Clone();
  32.                                 blkrefnew.AttributeCollection.AppendAttribute(attnew);
  33.                                 tr.AddNewlyCreatedDBObject(attnew, true);
  34.                             }
  35.                             blkrefnew.XData = blkref.XData;
  36.                             blkref.Erase();
  37.                             tr.Commit();
  38.                         }
  39.                     }
  40.                 }
  41.             }
  42.         }
 楼主| 发表于 2009-9-18 19:50 | 显示全部楼层
试试,谢谢先
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-10 18:52 , Processed in 0.142487 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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