找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1757|回复: 0

[原创] 练习 Editor.CommandAsync模拟Jig

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-10 02:36:10 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 csharp 于 2014-6-10 02:47 编辑

  1. // (C) Copyright 2014 by  
  2. //
  3. using System;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using Autodesk.AutoCAD.DatabaseServices;
  7. using Autodesk.AutoCAD.Geometry;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Exception = System.Exception;


  10. [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in20.MyCommands))]

  11. namespace AutoCAD_CSharp_plug_in20
  12. {
  13.     public class MyCommands
  14.     {
  15.         
  16.         #region "send Move command by callback function"

  17.         private static bool MoveExit = false;

  18.         private static Line ln;

  19.         //declare the callback delegation
  20.         delegate void Del();

  21.         private static Del _actionCompletedDelegate;

  22.         // Exit function,check if Zoom command is esc\cancelled
  23.         static void MdiActiveDocument_CommandCancelled(object sender, CommandEventArgs e)
  24.         {
  25.             MoveExit = true;
  26.             try
  27.             {
  28.                 Transaction tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
  29.                 using (tr)
  30.                 {
  31.                     DBObject ent = (DBObject) tr.GetObject(ln.ObjectId, OpenMode.ForWrite);
  32.                     ent.Erase();
  33.                     tr.Commit();
  34.                 }
  35.             }
  36.             catch (Exception)
  37.             {
  38.                 throw;
  39.             }
  40.         }
  41.         static void MdiActiveDocument_CommandEnded(object sender, CommandEventArgs e)
  42.         {
  43.             MoveExit = true;
  44.         }
  45.         [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
  46.         public void MyCommand()
  47.         {

  48.             Document doc = Application.DocumentManager.MdiActiveDocument;
  49.             Editor ed;
  50.             if (doc != null)
  51.             {
  52.                 ed = doc.Editor;
  53.                 Database db = doc.Database;
  54.                 Transaction tr = db.TransactionManager.StartTransaction();
  55.                 using (tr)
  56.                 {
  57.                     try
  58.                     {
  59.                         Point3d p1 = new Point3d(0, 0, 0);
  60.                         Point3d p2 = new Point3d(100, 100, 0);
  61.                         ln = new Line();
  62.                         ln.StartPoint = p1;
  63.                         ln.EndPoint = p2;

  64.                         BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  65.                         btr.AppendEntity(ln);
  66.                         tr.AddNewlyCreatedDBObject(ln, true);
  67.                         tr.Commit();
  68.                         doc.CommandCancelled += MdiActiveDocument_CommandCancelled;
  69.                         doc.CommandEnded += MdiActiveDocument_CommandEnded;

  70.                         Editor.CommandResult cmdResult =
  71.                           ed.CommandAsync(new object[]{"_.Move", "L", "", p1, Editor.PauseToken});
  72.                         _actionCompletedDelegate = new Del(CreateMoveAsyncCallback);
  73.                         cmdResult.OnCompleted(new Action(_actionCompletedDelegate));
  74.                         MoveExit = false;
  75.                     }
  76.                     catch (Exception)
  77.                     {
  78.                         throw;
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         
  84.         // callback function
  85.         public static void CreateMoveAsyncCallback()
  86.         {
  87.             var ed = Application.DocumentManager.MdiActiveDocument.Editor;

  88.             //if Move command is running

  89.             if (!MoveExit)
  90.             {
  91.                 Point3d pt = new Point3d(0, 0, 0);
  92.                 // AutoCAD hands over to the callback function

  93.                 Editor.CommandResult cmdResult = ed.CommandAsync(new object[]{
  94.                     "_.Move", "L", "",pt, Editor.PauseToken});

  95.                 // delegate callback function, wait for interaction ends
  96.                 _actionCompletedDelegate = new Del(CreateMoveAsyncCallback);

  97.                 cmdResult.OnCompleted(new Action(_actionCompletedDelegate));
  98.             }
  99.             else
  100.             {
  101.                 ed.WriteMessage("Move Exit");
  102.                 return;
  103.             }
  104.         }
  105.         #endregion
  106.     }
  107. }
使用 Move 命令模拟 Jig,命令方式更简单写起来更快捷,Jig 繁琐强大,各有所长

使用了 Events (事件),Autolisp 中叫反应器(:vlr-commandended, :vlr-CommandCancelled)

仅 AutoCAD 2015 .Net 4.5 及以上支持

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

本版积分规则

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

GMT+8, 2024-5-22 12:17 , Processed in 0.238294 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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