CAD Forum - tips, tricks, discussion and utilities for AutoCAD, Inventor, Revit. Then paste the copied DWG texts e.g. To Word or other application using. You can download the CCtext (clipboard-copy text) application from.
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
{
publicclassCommands
{
// For our command to intercept PASTECLIP, we don't care
// about the pickfirst set: it just needs to be transparent
[CommandMethod('PCINT', CommandFlags.Transparent)]
staticpublicvoid 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(
'x1Bx1B_.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
)]
staticpublicvoid 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:
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.
Hello,I’m writing a script to copy a text to clipboard then paste it into an attribute of a block. This will repeat in hundreds plans.
The text will be in the same location so I use its coordination to catch it. The problem is that it always stops at the 3rd line (1351,22 coordinate of the text). I guess it doesn’t regonize selall (select all) or other reasons. I am a beginner of script and lisp. Any help would be appreciated.; copy text from old Mtext to clipboardmtextedit1351,22selallcopyclip(command);;paste it to attribute-atteditoFRATTR-.FEUILLET1320,320,301365,15V.PASTECLIPQsaveclose.
Wrote:Hello,I’m writing a script to copy a text to clipboard then paste it into an attribute of a block. This will repeat in hundreds plans. The text will be in the same location so I use its coordination to catch it. The problem is that it always stops at the 3rd line (1351,22 coordinate of the text). I guess it doesn’t regonize selall (select all) or other reasons.
I am a beginner of script and lisp. Any help would be appreciated.Is that whereabouts 1551,22 or exactly at that point?The value at clipboard will be assigned to 4 attribute tags?We can write a short code to run in conjunction with your script. Source MTEXT Attribute block on the same drawing.If that is the case then we wont be needing the clipboard when using lisp program with script. We can add more filter to select the MTEXT, like layers. Even knowing the height of the MTEXT would helpTry this code(defun c:demo (/ theMtextAtAGivenCoordinate theAttributeBlock i)(if (and(setq theMtextAtAGivenCoordinate(ssget 'X'((0. 'MTEXT') (10 1551.0 22.00 0.0))))(setq theMtextAtAGivenCoordinate(ssname theMtextAtAGivenCoordinate 0))(setq theMtextAtAGivenCoordinate(cdr (assoc 1 (entget theMtextAtAGivenCoordinate))))(setq theAttributeBlock(ssget 'X' '((0. 'FRATTR-XX')))))(repeat (setq i (sslength theAttributeBlock))(foreach atb(vlax-invoke(vlax-ename-vla-object(ssname theAttributeBlock(setq i (1- i))))'GetATtributes)(if (eq (vla-get-tagstring atb) 'FEUILLET')(vla-put-textstring atbtheMtextAtAGivenCoordinate))))))The theMtextAtAGivenCoordinate variable initial value if not nil is a selection set.There are a couple of ways to to a selection using a point, one is shown on the code above ('X'), another is using 'C' and a point list(setq theMtextAtAGivenCoordinate(ssget 'C' '( 1551.0 22.00 0.0) '( 1552.0 50.00 0.0)'((0.
'MTEXT') )))problem is o nly objects visible in the drawing area at the time of selection will be selected by this method.(nentselp ' (1551.00 22.00))but this will depend if the part of the object is at a given point, though one can increase the size of pickbox for a bigger range.AND back again to ssget 'X', you can add range filter to make 'X' behave like 'C' minus the restriction of being visible in the drawing area(setq ss (ssget 'X' '((0. 'MTEXT') (-4. '=,=,.' ) (10 1551.0 22.00.0)(-4. Wrote:Hello, pBe,Note. The block name has, T-i-t, the three letters at the end instead of xx, I assume the website has it changed automatically by sensitive word control.
The entity is Text instead of MtextThanks again.Replace(setq theMtextAtAGivenCoordinate(ssget 'X'((0. 'MTEXT') (10 1551.0 22.00 0.0))))With (setq theMtextAtAGivenCoordinate(ssget 'X' '((0.
'=,=,.' )(10 1338.0 18.0 0.0)(-4. It works!I know I can get help here.I list the entire routine below as a record. (defun c:demo (/ theMtextAtAGivenCoordinate theAttributeBlock i)(if (and(setq theMtextAtAGivenCoordinate(ssget 'X' '((0.
'=,=,.' )(10 1338.0 18.0 0.0)(-4. 'vla-object(ssname theAttributeBlock(setq i (1- i))))'GetATtributes)(if (eq (vla-get-tagstring atb) 'FEUILLET')(vla-put-textstring atbtheMtextAtAGivenCoordinate)))))). Wrote:I'm looking at the code and surely many things are beyond my knowledge; but one thing I dare to ask is what is the purpose of (-4. '=,=,.'
) and (-4. '=,=,.'
)(10 1338.0 18.0 0.0)(-4. '= 1338.0) = 18.0).