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);
}
}
}
}