找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1524|回复: 0

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

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-7-30 21:26:30 来自手机 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 csharp 于 2014-7-31 08:13 编辑

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:

  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.EditorInput;

  3. using Autodesk.AutoCAD.Runtime;



  4. namespace InterceptCopyPaste

  5. {

  6.   public class Commands

  7.   {

  8.     // For our command to intercept PASTECLIP, we don't care

  9.     // about the pickfirst set: it just needs to be transparent



  10.     [CommandMethod("PCINT", CommandFlags.Transparent)]

  11.     static public void PasteClipIntercept()

  12.     {

  13.       var doc =

  14.         Application.DocumentManager.MdiActiveDocument;

  15.       var ed = doc.Editor;



  16.       // Get the contents of the clipboard



  17.       var obj =

  18.         (System.Windows.Forms.DataObject)

  19.           System.Windows.Forms.Clipboard.GetDataObject();



  20.       if (obj != null)

  21.       {

  22.         // Find out whether the clipboard contains AutoCAD data



  23.         var formats = obj.GetFormats();

  24.         bool foundDwg = false;

  25.         foreach (var name in formats)

  26.         {

  27.           if (name.Contains("AutoCAD.r"))

  28.           {

  29.             foundDwg = true;

  30.             break;

  31.           }

  32.         }

  33.         if (foundDwg)

  34.         {

  35.           // If so, start the PASTECIP command, cancelling any

  36.           // active commands (we may have been called transparently)



  37.           doc.SendStringToExecute(

  38.             "\x1B\x1B_.PASTECLIP ", true, false, true

  39.           );

  40.         }

  41.         else

  42.         {

  43.           // If not, try to get text data from the clipboard



  44.           var text = (string)obj.GetData("Text");

  45.           if (!string.IsNullOrEmpty(text))

  46.           {

  47.             // If there is some, send it to the command-line



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

  49.           }

  50.         }

  51.       }

  52.     }



  53.     // For the command that intercepts COPYCLIP, we need not only

  54.     // a transparent command, but one with pickfirst support



  55.     [CommandMethod(

  56.       "CCINT",

  57.       CommandFlags.Transparent |

  58.       CommandFlags.Redraw |

  59.       CommandFlags.UsePickSet

  60.       )]

  61.     static public void CopyClipIntercept()

  62.     {

  63.       var doc =

  64.         Application.DocumentManager.MdiActiveDocument;

  65.       var ed = doc.Editor;



  66.       // Start by getting the pickfirst set or object selection



  67.       var psr = ed.GetSelection();

  68.       if (psr.Status == PromptStatus.OK && psr.Value != null)

  69.       {

  70.         // In case the selection is not from the pickfirst set,

  71.         // we need to set it from the selection for COPYCLIP

  72.         // to pick up



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



  74.         // Report how many objects have been selected



  75.         ed.WriteMessage(

  76.           "\n{0} entities selected.\n", psr.Value.Count

  77.         );



  78.         // Pass the control on to COPYCLIP



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

  80.       }

  81.     }

  82.   }

  83. }



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豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-19 01:35 , Processed in 0.418976 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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