找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2651|回复: 5

[分享] Editor.Command In AutoCAD 2015

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-9 00:51:18 | 显示全部楼层 |阅读模式

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

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

×
Migration in .NET
I would say the most exciting new API in Longbow is the introduction of .NET versions of acedCommand ((command) in LISP). This hasn’t been possible before because of AutoCAD’s use of Fibers – and because the .NET Framework doesn’t support Fibers. So these new APIs – synchronous and asynchronous versions of acedCommand – are now possible for the first time.
acedCommand is probably the most powerful API in ObjectARX. It allows you to embed entire AutoCAD commands within your own commands; and even to invoke partial commands, and then programmatically respond to how the user interacts with those commands. This saves ObjectARX and LISP developers a huge amount of work where otherwise they’d have to reproduce all the native command behavior in their own code – and now .NET developers get the same benefit.
The synchronous version of Command is Editor.Command, the equivalent of the subroutine version of acedCommand. This is how you invoke a complete command from your code. As you can see, the code is very similar to the ObjectARX version – you pass a full set of command tokens to the AutoCAD commandline (the editor), AutoCAD completes the command and immediately returns control back to your code.
//simple use of Editor.Command
//most popular case - invoke a whole command with no user interaction
[CommandMethod(“MyCommand")]
publicstaticvoid MyMethod()
{
    Editor ed =  
Application.DocumentManager.MdiActiveDocument.Editor;
ed.Command(newObject[] {
"_.LINE", "0,0,0", "10,10,0", "" });
}
There is also the asynchronous version of Command Editor.CommandAsync. This is the equivalent of the coroutine version of acedCommand. You’ll recall that acedCommandC required you to pass in a reference to a callback function. The .NET version is a little simpler. Because we’re using .NET Framework 4.5, we can make use of the async and await keywords to greatly simplify our asyncronous code. We mark our .NET commandmethod with the async keyword, and we can then use the await keyword inside that method. The await keyword tells AutoCAD where in your code to return control to after it has finished gathering user input. In this example, we’re simply starting the circle comamnd, using a PAUSE to let the user specify the circle center, and then specifying the circle radius in our code.

//Invoke a partial command with user interaction
[CommandMethod("DotNetType2")]
publicstaticasyncvoid DotNetType2()
{
    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    //create a circle of radius 2.0 at chosen location.
    await ed.CommandAsync("_.CIRCLE");
    //wait for user input
    await ed.CommandAsync(Editor.PauseToken);
    //provide radius input
    await ed.CommandAsync(2.0);
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-6-10 02:13:17 | 显示全部楼层
本帖最后由 csharp 于 2014-6-10 02:20 编辑

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace CommandCalling
{
  public class Commands
  {
    [CommandMethod("IBS")]
    public void InsertBlockSync()
    {
      var doc =  Application.DocumentManager.MdiActiveDocument;
      var ed = doc.Editor;
      // Our insertion point is hardcoded at 10,10
      ed.Command("_.INSERT", "TEST", "10,10", 1, 1, 0);
      ed.WriteMessage("\nWe have inserted our block.");
    }
    [CommandMethod("IBA")]
    async public void InsertBlockAsync()
    {
      var doc =  Application.DocumentManager.MdiActiveDocument;
      var ed = doc.Editor;
      // Let's ask the user to select the insertion point
      await ed.CommandAsync("_.INSERT", "TEST", Editor.PauseToken, 1, 1, 0 );
      ed.WriteMessage("\nWe have inserted our block.");
    }
  }
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-6-10 02:20:48 | 显示全部楼层
#region "send Zoom command by callback function"
private static bool ZoomExit = false;
//declare the callback delegation
delegate void Del();
private static Del _actionCompletedDelegate;
// Exit function,check if Zoom command is esc\cancelled
static void
MdiActiveDocument_CommandCancelled(object sender,CommandEventArgs e)
{
    ZoomExit = true;
}
[CommandMethod("TestZoom")]
public static void TestZoom()
{
   var ed = Application.DocumentManager.MdiActiveDocument.Editor;
   var doc = Application.DocumentManager.MdiActiveDocument;
   //esc event
   doc.CommandCancelled += MdiActiveDocument_CommandCancelled;
   // start Zoom command
   Editor.CommandResult cmdResult1 =
   ed.CommandAsync(new object[]{
   "_.ZOOM", Editor.PauseToken, Editor.PauseToken});
   // delegate callback function, wait for interaction ends
   _actionCompletedDelegate = new Del(CreateZoomAsyncCallback);
   cmdResult1.OnCompleted(new Action(_actionCompletedDelegate));
   ZoomExit = false;
}
// callback function
public static void CreateZoomAsyncCallback()
{
    var ed = Application.DocumentManager.MdiActiveDocument.Editor;
    //if Zoom command is running
    if (!ZoomExit)
    {
        // AutoCAD hands over to the callback function
        Editor.CommandResult cmdResult1 =
        ed.CommandAsync(new object[]{
        "_.ZOOM", Editor.PauseToken, Editor.PauseToken});
        // delegate callback function, wait for interaction ends
        _actionCompletedDelegate = new Del(CreateZoomAsyncCallback);
       cmdResult1.OnCompleted(new Action(_actionCompletedDelegate));
    }
    else
    {
        ed.WriteMessage("Zoom Exit");
        return;
    }
}
#endregion
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2014-9-11 18:00:38 来自手机 | 显示全部楼层
editor.command 为什么不能用在windows 窗体按钮命令下面呢?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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