明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1556|回复: 7

[基础] 修改多选择性替换

[复制链接]
发表于 2010-12-12 15:28 | 显示全部楼层 |阅读模式
本帖最后由 chpmould 于 2010-12-12 17:09 编辑

以下块替换程序是我在论坛看到由狐哥编写的,此程序实现一次单选一个块来进行替换,
我现将此程序再贴出来,是想请老师帮助修改成一次性可以选择多个块进行替换。。。

 楼主| 发表于 2010-12-12 15:29 | 显示全部楼层
  1. public static void tt1()
  2.         {
  3.             Document doc = Application.DocumentManager.MdiActiveDocument;
  4.             Editor ed = doc.Editor;
  5.             PromptEntityOptions optsEnt = new PromptEntityOptions("\nSelect a Block:");
  6.             optsEnt.SetRejectMessage("\nNot a Block!");
  7.             optsEnt.AddAllowedClass(typeof(BlockReference), false);
  8.             PromptEntityResult resEnt = ed.GetEntity(optsEnt);
  9.             if (resEnt.Status == PromptStatus.OK)
  10.             {
  11.                 PromptResult resStr = ed.GetString("\nInput BlockName:");
  12.                 if(resStr.Status == PromptStatus.OK)
  13.                 {
  14.                     Database db = doc.Database;
  15.                     using (Transaction tr = db.TransactionManager.StartTransaction())
  16.                     {
  17.                         BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  18.                         BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  19.                         string blkname = resStr.StringResult;
  20.                         if(bt.Has(blkname))
  21.                         {
  22.                             BlockReference blkref = (BlockReference)tr.GetObject(resEnt.ObjectId, OpenMode.ForWrite);
  23.                             BlockReference blkrefnew = new BlockReference(blkref.Position, bt[blkname]);
  24.                             blkrefnew.BlockTransform = blkref.BlockTransform;
  25.                             btr.AppendEntity(blkrefnew);
  26.                             tr.AddNewlyCreatedDBObject(blkrefnew, true);
  27.                             foreach(ObjectId attid in blkref.AttributeCollection)
  28.                             {
  29.                                 AttributeReference att = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead);
  30.                                 AttributeReference attnew = (AttributeReference)att.Clone();
  31.                                 blkrefnew.AttributeCollection.AppendAttribute(attnew);
  32.                                 tr.AddNewlyCreatedDBObject(attnew, true);
  33.                             }
  34.                             blkrefnew.XData = blkref.XData;
  35.                             blkref.Erase();
  36.                             tr.Commit();
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.         }
 楼主| 发表于 2010-12-12 16:42 | 显示全部楼层
自己先顶一下...
发表于 2010-12-12 18:37 | 显示全部楼层
似乎用.NET也不能绝对化,例如这种问题,lisp一行就能解决,且特性、XData、EDictionary自动继承。
(vla-put-Name objRef BlockName)

发表于 2010-12-12 18:49 | 显示全部楼层
本帖最后由 lzh741206 于 2010-12-12 19:35 编辑

汗,不知道什么时候的代码
其实NetApi也是一句话
            using (var tr = new DBTransaction())
            {
                var ed = tr.Editor;
                var resEnt = ed.GetEntity("\n选择块参照");
                var bref = tr.GetObject<BlockReference>(resEnt.ObjectId, OpenMode.ForWrite);
               
                bref.BlockTableRecord = tr.BlockTable.GetRecorId("2");
            }
 楼主| 发表于 2010-12-12 21:19 | 显示全部楼层
本帖最后由 chpmould 于 2010-12-12 23:03 编辑

谢谢老师! 你上面这代码是如何用的啊!
发表于 2010-12-13 20:03 | 显示全部楼层
把块名为“2”的块定义的Id赋值给块参照的BlockTableRecord 属性
 楼主| 发表于 2010-12-13 22:46 | 显示全部楼层
本帖最后由 chpmould 于 2010-12-13 22:47 编辑

请问老师,这个DBTransaction函数是你自己写的吧...
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-17 13:06 , Processed in 0.269998 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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