找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2135|回复: 0

[分享] Exploding nested AutoCAD blocks using .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-11-11 15:18:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Exploding nested AutoCAD blocks using .NET   Some time ago I posted about how to use Entity.Explode() to do something similar to AutoCAD’s EXPLODE command. At the time it was mentioned in the comments that BlockReference.ExplodeToOwnerSpace() had some relative benefits, but it’s taken me some time to code up a simple sample to show how you might use it (Patrick’s recent comment reminded me I ought to, though).
Anyway, to end the week I thought I’d throw together a quick sample. BlockReference.ExplodeToOwnerSpace() doesn’t return a list of created objects, so I opted to capture this using a Database.ObjectAppended event handler and then recursively call our custom ExplodeBlock() function for any nested blocks that get created. We also then erase the originating entity (or entities, if called recursively), just as the EXPLODE command might.
Here’s the C# code:

  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. namespace Explosions
  6. {
  7.   public class Commands
  8.   {
  9.     [CommandMethod("EB")]
  10.     public void ExplodeBock()
  11.     {
  12.       var doc = Application.DocumentManager.MdiActiveDocument;
  13.       if (doc == null)
  14.         return;
  15.       var ed = doc.Editor;
  16.       var db = doc.Database;
  17.       // Ask the user to select the block
  18.       var peo = new PromptEntityOptions("\nSelect block to explode");
  19.       peo.SetRejectMessage("Must be a block.");
  20.       peo.AddAllowedClass(typeof(BlockReference), false);
  21.       var per = ed.GetEntity(peo);
  22.       if (per.Status != PromptStatus.OK)
  23.         return;
  24.       using (var tr = db.TransactionManager.StartTransaction())
  25.       {
  26.         // Call our explode function recursively, starting
  27.         // with the top-level block reference
  28.         // (you can pass false as a 4th parameter if you
  29.         // don't want originating entities erased)
  30.         ExplodeBlock(tr, db, per.ObjectId);
  31.         tr.Commit();
  32.       }
  33.     }
  34.     private void ExplodeBlock(
  35.       Transaction tr, Database db, ObjectId id, bool erase = true
  36.     )
  37.     {
  38.       // Open out block reference - only needs to be readable
  39.       // for the explode operation, as it's non-destructive
  40.       var br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
  41.       // We'll collect the BlockReferences created in a collection
  42.       var toExplode = new ObjectIdCollection();
  43.       // Define our handler to capture the nested block references
  44.       ObjectEventHandler handler =
  45.         (s, e) =>
  46.         {
  47.           if (e.DBObject is BlockReference)
  48.           {
  49.             toExplode.Add(e.DBObject.ObjectId);
  50.           }
  51.         };
  52.       // Add our handler around the explode call, removing it
  53.       // directly afterwards
  54.       db.ObjectAppended += handler;
  55.       br.ExplodeToOwnerSpace();
  56.       db.ObjectAppended -= handler;
  57.       // Go through the results and recurse, exploding the
  58.       // contents
  59.       foreach (ObjectId bid in toExplode)
  60.       {
  61.         ExplodeBlock(tr, db, bid, erase);
  62.       }
  63.       // We might also just let it drop out of scope
  64.       toExplode.Clear();
  65.       // To replicate the explode command, we're delete the
  66.       // original entity
  67.       if (erase)
  68.       {
  69.         br.UpgradeOpen();
  70.         br.Erase();
  71.         br.DowngradeOpen();
  72.       }
  73.     }
  74.   }
  75. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-18 23:14 , Processed in 0.385104 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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