修改多选择性替换
本帖最后由 chpmould 于 2010-12-12 17:09 编辑以下块替换程序是我在论坛看到由狐哥编写的,此程序实现一次单选一个块来进行替换,
我现将此程序再贴出来,是想请老师帮助修改成一次性可以选择多个块进行替换。。。
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();
}
}
}
}
}
自己先顶一下... 似乎用.NET也不能绝对化,例如这种问题,lisp一行就能解决,且特性、XData、EDictionary自动继承。
(vla-put-Name objRef BlockName)
本帖最后由 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");
}
本帖最后由 chpmould 于 2010-12-12 23:03 编辑
谢谢老师! 你上面这代码是如何用的啊!
把块名为“2”的块定义的Id赋值给块参照的BlockTableRecord 属性 本帖最后由 chpmould 于 2010-12-13 22:47 编辑
请问老师,这个DBTransaction函数是你自己写的吧...
页:
[1]