Profile Picture

Paste at location of right mouse

Posted By Douglas Earl 12 Years Ago
Author
Message
Douglas Earl
Posted 12 Years Ago
View Quick Profile
Junior Member

Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)

Group: Forum Members
Last Active: 10 Years Ago
Posts: 15, Visits: 1

Is there any way to programatically paste at a specific location in the diagram?  When pasting from the context menu (we implement our own context menu) we would like to paste shapes at the location where the user clicked the mouse.  The default paste method in NDrawingView pastes at a point that is offset slightly from the original shape. Thanks.



Nevron Support
Posted 12 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009
Hi Douglas,

The following code performs paste at location for a view of your choice.

public NTransactionResult Paste(NDrawingView view, NPointF location)
{
NTransactionResult result = new NTransactionResult(NDR.TransactionPaste);
if (view.CanPaste() == false)
{
result.Status = TransactionResultStatus.FailedNotExecuted;
return result;
}

view.Document.StartTransaction(NDR.TransactionPaste);
try
{
// get data object from the clipboard and adapt it to drawing data object
// using the document data object adaptors
IDataObject dataObject = Clipboard.GetDataObject();
NDataObjectAdaptor adapter = view.Document.DataObjectAdaptors.GetAdaptorForDataObject(dataObject);
NDrawingDataObject ddo = (adapter.Adapt(dataObject) as NDrawingDataObject);

// calculate the translation needed to position the elements so that their bounds center
// is mapped to the location argument.
NNodeList elements = new NNodeList(ddo.Elements);
NBatchTranslate batch = new NBatchTranslate(null, elements);

NRectangleF batchBounds = batch.Bounds;
float dx = batchBounds.Center.X - location.X;
float dy = batchBounds.Center.Y - location.Y;

// translate data object elements and add them in the document active layer.
result.Combine(batch.Translate(dx, dy, true, false), false);
result.Combine(ddo.AddInComposite(view.Document, view.Document.ActiveLayer), false);

// select the pasted elements
view.Selection.SingleSelect(elements);
}
catch (Exception ex)
{
view.Document.Rollback();

result.LogError(ex);
result.Status = TransactionResultStatus.FailedRolledback;
return result;
}

view.Document.Commit();
return result;
}



Best Regards,
Nevron Support Team



Douglas Earl
Posted 12 Years Ago
View Quick Profile
Junior Member

Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)Junior Member (15 reputation)

Group: Forum Members
Last Active: 10 Years Ago
Posts: 15, Visits: 1

Thanks for that.  For anyone else who might want to use this, I had to reverse the calculations of dx and dy (instead of batchBounds.Center.X - location.X, it should be location.X - batchBounds.Center.X).

Also, to get the correct coordinates of where to paste based on the location of the right mouse click I used NNodeMouseEventArgs.ScenePoint when handling the mouse click event.  This takes into account zooming and scrolling of the diagram.

Pretty advanced method - I'm sure I wouldn't have been able to figure it out on my own. Maybe add it as a supported method on NDrawingView in a future release? Seems a pretty standard way to paste (Visio for one does this).



Nevron Support
Posted 12 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009
Hi,

We will add this method as an overload of the current paste method (takes no arguments and determines the location automatically based on the PastePositionMode, PasteOffsetX and PasteOffsetY properties of the NDrawingDocument.Settings object). The method should appear in the next service pack of the .NET Vision.

Best Regards,
Nevron Support Team





Similar Topics


Reading This Topic