找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1366|回复: 2

[分享] How to show a block when positioning it, using C#?

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-26 17:58:28 | 显示全部楼层 |阅读模式

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

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

×
  1. using Autodesk.AutoCAD.Geometry;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Runtime;

  6. namespace MySamples
  7. {
  8.   public class BlockJigCmds
  9.   {
  10.     // Ask the user to place the block named "TEST", showing it
  11.     //   during the dragging.
  12.     // Make sure that a block with this name exists in the drawing.
  13.     //===============================================================
  14.     [CommandMethod("BlockJig")]
  15.     static public void BlockJig()
  16.     {
  17.       Editor ed =
  18.               Application.DocumentManager.MdiActiveDocument.Editor;
  19.       Database db = ed.Document.Database;
  20.       try
  21.       {
  22.         using (Transaction tr =
  23.                            db.TransactionManager.StartTransaction())
  24.         {
  25.           // Find the "TEST" block in the current drawing
  26.           BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,
  27.                                                    OpenMode.ForRead);
  28.           BlockTableRecord block = (BlockTableRecord)tr.GetObject(
  29.                                        bt["TEST"], OpenMode.ForRead);
  30.           // Create the Jig and ask the user to place the block
  31.           //----------------------------------------------------
  32.           MyBlockJig blockJig = new MyBlockJig();
  33.           Point3d point;
  34.           PromptResult res = blockJig.DragMe( block.ObjectId,
  35.                                               out point);
  36.           if (res.Status == PromptStatus.OK)
  37.           {
  38.             // Now we need to do the usual steps to place
  39.             //   the block insert at the position where the user
  40.             //   did the click
  41.             BlockTableRecord curSpace =
  42.               (BlockTableRecord)tr.GetObject(
  43.                                db.CurrentSpaceId, OpenMode.ForWrite);
  44.             BlockReference insert = new BlockReference(point,
  45.                                                      block.ObjectId);
  46.             curSpace.AppendEntity(insert);
  47.             tr.AddNewlyCreatedDBObject(insert, true);
  48.           }
  49.           tr.Commit();
  50.         } // using
  51.       }
  52.       catch (System.Exception ex)
  53.       {
  54.         ed.WriteMessage(ex.ToString());
  55.       }
  56.     } // BlockJig()
  57.   } // class BlockJigCmds
  58.   // This Jig will show the given block during the dragging.
  59.   //=================================================================
  60.   public class MyBlockJig : DrawJig
  61.   {
  62.     public Point3d _point;
  63.     private ObjectId _blockId = ObjectId.Null;
  64.     // Shows the block until the user clicks a point.
  65.     // The 1st parameter is the Id of the block definition.
  66.     // The 2nd is the clicked point.
  67.     //---------------------------------------------------------------
  68.     public PromptResult DragMe(ObjectId i_blockId, out Point3d o_pnt)
  69.     {
  70.       _blockId = i_blockId;
  71.       Editor ed =
  72.               Application.DocumentManager.MdiActiveDocument.Editor;
  73.       PromptResult jigRes = ed.Drag(this);
  74.       o_pnt = _point;
  75.       return jigRes;
  76.     }
  77.     // Need to override this method.
  78.     // Updating the current position of the block.
  79.     //--------------------------------------------------------------
  80.     protected override SamplerStatus Sampler(JigPrompts prompts)
  81.     {
  82.       JigPromptPointOptions jigOpts = new JigPromptPointOptions();
  83.       jigOpts.UserInputControls =
  84.                           (UserInputControls.Accept3dCoordinates |
  85.                            UserInputControls.NullResponseAccepted);
  86.       jigOpts.Message = "Select a point:";
  87.       PromptPointResult jigRes = prompts.AcquirePoint(jigOpts);
  88.       Point3d pt = jigRes.Value;
  89.       if (pt == _point)
  90.         return SamplerStatus.NoChange;
  91.       _point = pt;
  92.       if (jigRes.Status == PromptStatus.OK)
  93.         return SamplerStatus.OK;
  94.       return SamplerStatus.Cancel;
  95.     }
  96.     // Need to override this method.
  97.     // We are showing our block in its current position here.
  98.     //--------------------------------------------------------------
  99.     protected override bool WorldDraw(
  100.                   Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  101.     {
  102.       BlockReference inMemoryBlockInsert =
  103.                                new BlockReference(_point, _blockId);
  104.       draw.Geometry.Draw(inMemoryBlockInsert);
  105.       inMemoryBlockInsert.Dispose();
  106.       return true;
  107.     } // WorldDraw()
  108.   } // class BlockJig
  109. } // namespace MySamples

评分

参与人数 1D豆 +2 收起 理由
ScmTools + 2 很给力!经验;技术要点;资料分享奖!

查看全部评分

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

已领礼包: 1632个

财富等级: 堆金积玉

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

使用道具 举报

已领礼包: 146个

财富等级: 日进斗金

发表于 2013-11-11 03:01:00 | 显示全部楼层
如果播入块的同时  可以动态镜像或旋转  如何在此基础上改进?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-7 09:52 , Processed in 0.221960 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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