找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1395|回复: 2

[分享] Hooking into AutoCAD copy & paste via Ctrl-C and -V

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2017-5-21 15:34:44 | 显示全部楼层 |阅读模式

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

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

×
http://through-the-interface.typepad.com/through_the_interface/2012/12/hooking-into-autocad-copy-paste-via-ctrl-c-and-v-using-net.html

Hooking into AutoCAD copy & paste via Ctrl-C and -V                                                        This was a fun question that came in from James Meading. I genuinely didn’t think I’d manage to look into it before the break, but it tweaked my interest during my trip back from the UK:
I thought this might be a topic you would be interested in. I do not use ctrl-v for pasting entities, only text to command line.
I already tried removing the keyboard shortcuts to ctrl-v via the cui, and that just makes ctrl-v not do anything when command line does not have focus.
So I think I need to write a transparent function, make a command in the CUI that runs the function, and assign ctrl-v to it. Its the transparent part I have never done.
I would want the “enhanced paste” routine to be able to run inside any other command gracefully. Maybe this is just running a little function that runs before paste.
This really sounded like an interesting little problem but also a very useful bit of functionality: basically you could have AutoCAD change its “paste” behaviour based on the type of data in the clipboard. For instance, if the clipboard contains drawing data, let the PASTECLIP command have at it. If it contains text, send it to the command-line.
I decided to generalise the request (and ultimately the solution) to encompass copy operations, too. At  a basic level, we can just hook into Ctrl-C and then look at the selection set chosen by the user: in our case we’re just going to write a message to the command-line mentioning the number of objects selected, but we might choose only to call COPYCLIP under certain circumstances. We might also use this opportunity to add certain objects into the operation (although there are other ways this might be achieved inside AutoCAD).
Here’s the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace InterceptCopyPaste
{
  public class Commands
  {
    // For our command to intercept PASTECLIP, we don't care
    // about the pickfirst set: it just needs to be transparent

    [CommandMethod("PCINT", CommandFlags.Transparent)]
    static public void PasteClipIntercept()
    {
      var doc =
        Application.DocumentManager.MdiActiveDocument;
      var ed = doc.Editor;

      // Get the contents of the clipboard

      var obj =
        (System.Windows.Forms.DataObject)
          System.Windows.Forms.Clipboard.GetDataObject();

      if (obj != null)
      {
        // Find out whether the clipboard contains AutoCAD data

        var formats = obj.GetFormats();
        bool foundDwg = false;
        foreach (var name in formats)
        {
          if (name.Contains("AutoCAD.r"))
          {
            foundDwg = true;
            break;
          }
        }
        if (foundDwg)
        {
          // If so, start the PASTECIP command, cancelling any
          // active commands (we may have been called transparently)

          doc.SendStringToExecute(
            "\x1B\x1B_.PASTECLIP ", true, false, true
          );
        }
        else
        {
          // If not, try to get text data from the clipboard

          var text = (string)obj.GetData("Text");
          if (!string.IsNullOrEmpty(text))
          {
            // If there is some, send it to the command-line

            doc.SendStringToExecute(text, true, false, true);
          }
        }
      }
    }

    // For the command that intercepts COPYCLIP, we need not only
    // a transparent command, but one with pickfirst support

    [CommandMethod(
      "CCINT",
      CommandFlags.Transparent |
      CommandFlags.Redraw |
      CommandFlags.UsePickSet
    static public void CopyClipIntercept()
    {
      var doc =
        Application.DocumentManager.MdiActiveDocument;
      var ed = doc.Editor;

      // Start by getting the pickfirst set or object selection

      var psr = ed.GetSelection();
      if (psr.Status == PromptStatus.OK && psr.Value != null)
      {
        // In case the selection is not from the pickfirst set,
        // we need to set it from the selection for COPYCLIP
        // to pick up

        ed.SetImpliedSelection(psr.Value.GetObjectIds());

        // Report how many objects have been selected

        ed.WriteMessage(
          "\n{0} entities selected.\n", psr.Value.Count
        );

        // Pass the control on to COPYCLIP

        doc.SendStringToExecute("_.COPYCLIP ", true, false, true);
      }
    }
  }
}

To really make this work properly from Ctrl-C and -V, we need to use the CUI command to reassign the macro for Copy and Paste to “^C^C_ccint” and “’_pcint”, respectively:
0.jpg
1.jpg
We’ve removed the "”^C^C” prefix from the Ctrl-V call – replacing it with an apostrophe – because we want our new command to be transparently callable, something that is really only useful if we’re sending text data from the clipboard to the command-line. If the normal PASTECLIP command is to be called, we anyway prefix the command-string with escape characters to cancel any active commands.
Now when we use Ctrl-C and -V to copy or paste in AutoCAD, we have our custom behaviour kick in. You may find these commands more verbose on the command-line, as we’re selecting once before passing through to COPYCLIP, for instance, but we could streamline then somewhat to not echo the command-strings, at least. That’s really left as an exercise for the reader, depending on the specific needs.
Right then... that's me nearly done for the year. In the next few days I'll be heading up to the mountains for Autodesk's annual "week of rest" closure, but I'll almost certainly have the odd tidbit to share while I'm there.
In the meantime, I wish you all the very best for this festive season, whether you celebrate Christmas and the New Year or not.
               

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

已领礼包: 774个

财富等级: 财运亨通

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

使用道具 举报

已领礼包: 6434个

财富等级: 富甲天下

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-21 20:32 , Processed in 0.429061 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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