找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1189|回复: 1

[分享] Move Entity from Model Space to Block

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-28 22:23:35 来自手机 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 csharp 于 2014-5-29 07:53 编辑

http://spiderinnet1.typepad.com/blog/2014/02/autocad-net-add-entity-in-model-space-to-block.html



In this article, let us see how to select an entity in the model space either WCS or UCS and append it to a particular block referenced by an INSERT (block reference), which is picked at the same time, maintaining the same relative position and rotation.

  1.         [CommandMethod("AddEntToBlock")]
  2.         public static void AddEntToBlock_Method()
  3.         {
  4.             Database db = HostApplicationServices.WorkingDatabase;
  5.             Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;

  6.             try
  7.             {
  8.                 PromptEntityResult prEntRes1 = ed.GetEntity("Select an entity: ");
  9.                 PromptEntityResult prEntRes2 = ed.GetEntity("Select a block reference (INSERT) to add the entity to: ");

  10.                 if (prEntRes1.Status == PromptStatus.OK && prEntRes2.Status == PromptStatus.OK)
  11.                 {
  12.                     using (Transaction tr = db.TransactionManager.StartTransaction())
  13.                     {
  14.                         Entity ent = tr.GetObject(prEntRes1.ObjectId, OpenMode.ForWrite) as Entity;
  15.                         BlockReference insert = tr.GetObject(prEntRes2.ObjectId, OpenMode.ForRead) as BlockReference;

  16.                         if(insert == null)
  17.                         {
  18.                             ed.WriteMessage("\nThe second selected is not a block reference (INSERT).");
  19.                             return;
  20.                         }

  21.                         BlockTableRecord hostBlk = hostBlk = tr.GetObject(insert.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;

  22.                         Matrix3d mat = insert.BlockTransform.Inverse();

  23.                         Entity clone = ent.Clone() as Entity;
  24.                         clone.TransformBy(mat);
  25.                         hostBlk.AppendEntity(clone);
  26.                         tr.AddNewlyCreatedDBObject(clone, true);

  27.                         ent.Erase();

  28.                         tr.Commit();
  29.                     }
  30.                 }
  31.             }
  32.             catch (System.Exception ex)
  33.             {
  34.                 ed.WriteMessage(ex.ToString());
  35.             }
  36.         }
If we insert a sample block into the current drawing, draw a standalone entity such as circle in red, run the command, pick the entity and the INSERT (block reference) in turn, the entity (red circle here) will be moved from the model space regardless of WCS or UCS to the block space.
  AddEntToBlock
0.jpg

Next time, when the same block is inserted, we will notice it has the red circle nicely added. Once again, the block can be any one defined in the current database; the block reference can have any insertion point and rotation angle; the entity and the INSERT can be in WCS or UCS; the entity can be of any type and can be anywhere as well.

Some readers may have noticed that the transformation Matrix3d does not seem to take into the current UCS, which is the case as demonstrated, in the code, but the command nicely handles both UCS and WCS. If wonder why, please keep on reading. We will reveal it soon.

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-29 22:32:18 来自手机 | 显示全部楼层
仅限等比块
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-18 23:52 , Processed in 0.406047 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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