Profile Picture

Need Info When a New Shape is Dropped on the Drawing

Posted By Jason Irby 14 Years Ago
Author
Message
Jason Irby
Posted 14 Years Ago
View Quick Profile
Forum Member

Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)

Group: Forum Members
Last Active: 3 Years Ago
Posts: 59, Visits: 77

I'll give that a try.

 

Thanks,

Jason



Nevron Support
Posted 14 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,

In general the diagram will raise the document.EventSinkService.NodeInserted event whenever a node (be it a shape, primitive, port etc.) is added to the document. However the event does not give information of whether the node was inserted because of drag & drop or other operation.

So we can only suggest that you try to override the default drag and drop target tool, to perform the tasks that you need to to. Take a look at the following example:

Drawing View - Custom Drag and Drop

The NMyDragDropTargetTool class overrides ProcessDragDrop to make a hit test that determines the shape that the user has dropped on. So you can do the following:


1. Handle the NodeInserted event.
2. Make a flag which indicates whether your are currently in drag and drop.
3. Override the ProcessDragDrop.

Attached is the code of the default NDragDropTargetTool for reference purposes.
Following is the sample code that we created for your case:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Nevron.Dom;
using Nevron.GraphicsCore;
using Nevron.Diagram;
using Nevron.Diagram.Shapes;
using Nevron.Diagram.Filters;
using Nevron.Diagram.WinForm;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool m_bDroping = false;
NNodeList m_DroppedShapes = null;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// create the views
NDrawingView drawingView = new NDrawingView();
drawingView.Dock = DockStyle.Fill;
Controls.Add(drawingView);

NLibraryView libraryView = new NLibraryView();
libraryView.Dock = DockStyle.Left;
Controls.Add(libraryView);

// create the documents
NDrawingDocument drawing = new NDrawingDocument();
drawingView.Document = drawing;

NBasicShapesFactory basicShapes = new NBasicShapesFactory(NGraphicsUnit.Pixel, 96.0f);
basicShapes.DefaultSize = new NSizeF(40, 30);
libraryView.Document = basicShapes.CreateLibrary();

// override the default drag & drop on drawings
// replace the default drag drop target tool with your own one
// to extend the drop capabilities of the view
NTool tool = drawingView.Controller.Tools.GetToolByName(NDWFR.ToolDragDropTarget);
int index = drawingView.Controller.Tools.IndexOf(tool);
drawingView.Controller.Tools.Remove(tool);

tool = new NMyDragDropTargetTool(this);
tool.Enabled = true;
drawingView.Controller.Tools.Insert(index, tool);

// subscribe for node inserted
drawing.EventSinkService.NodeInserted += new ChildNodeEventHandler(EventSinkService_NodeInserted);
}
private void EventSinkService_NodeInserted(NChildNodeEventArgs args)
{
if (m_bDroping)
{
// accumulate the shapes that are inserted in a drag and drop operation
NShape shape = args.Child as NShape;
if (shape!=null)
{
m_DroppedShapes.Add(shape);
}
}
}
private void OnDropped(NNodeList droppedShapes, NShape dropShape)
{
string message = "Dropped shapes count: " + droppedShapes.Count;
if (dropShape == null)
{
message += " in active layer";
}
else
{
message += " in shape: " + dropShape.ToString();
}

MessageBox.Show(message);
}

[Serializable]
public class NMyDragDropTargetTool : NDragDropTargetTool
{
///
/// Default constructor
///

public NMyDragDropTargetTool(Form1 form)
{
m_Form = form;
}
///
/// Processes the drag drop event
///

///
/// Overriden to the drag drop data object in the document active layer
///

///
/// true if the event was processed, otherwise false
public override bool ProcessDragDrop(DragEventArgs e)
{
// check whether a group is hit, and if not use base implementation
// which adds shapes to the active layer
m_Form.m_bDroping = true;
m_Form.m_DroppedShapes = new NNodeList();
NShape dropShape = null;

try
{
NPointF mouseInDevice = View.GetMousePositionInDevice();
dropShape = View.LastActiveDocumentContentHit(mouseInDevice, -1, NFilters.TypeNShape) as NShape;
return base.ProcessDragDrop(e);
}
finally
{
m_Form.OnDropped(m_Form.m_DroppedShapes, dropShape);
m_Form.m_DroppedShapes = null;
m_Form.m_bDroping = false;
}
}
Form1 m_Form;
}
}
}

Best Regards,
Nevron Support Team



Attachments
NDragDropTargetTool.cs (72 views, 6.00 KB)
Jason Irby
Posted 14 Years Ago
View Quick Profile
Forum Member

Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)

Group: Forum Members
Last Active: 3 Years Ago
Posts: 59, Visits: 77

Well, they are not connected yet.

If I drop a shape on an empty space on the document, I want to attach a certain piece of custom data to the new shapes tag and do no connections.

If I drop a shape onto of another shape, I will attach a certain piece of custom data to the new shapes tag and then connect the new shape with the shape dropped on in certain ways depending on their shape type.

So: I need to know when a shape is dropped, a ref to the new shape being added to the drawing, a ref to the underlying shape being dropped-on if any.

Regards,
Jason


Nevron Support
Posted 14 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,

Have you tried the Connecting event of the document's event sink service ? You can use code similar to:

private void Nodes_Connecting(NConnectionCancelEventArgs args)
{
NDynamicPort port1 = (NDynamicPort)document.GetElementFromUniqueId(args.UniqueId1);
NDynamicPort port2 = (NDynamicPort)document.GetElementFromUniqueId(args.UniqueId2);
...
}

Thus you get the ports that are being connecting and you can cancel the connection if you want by setting the Cancel property of the args to true. Please, take a look at the Pipeline Editor that comes wit our Diagram Designer. If that's what you want to acomplish we can send you the source of this example. Just send an email to support@nevron.com if you are interested in the source code of the example and we'll send it to you.



Best Regards,
Nevron Support Team



Jason Irby
Posted 14 Years Ago
View Quick Profile
Forum Member

Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)Forum Member (41 reputation)

Group: Forum Members
Last Active: 3 Years Ago
Posts: 59, Visits: 77

 

Hi,

I need a little help.  I have an app where I drag a shape from a Library Browser  onto a drawing.

What I need is:

·         To be able to get an event when that happens.

·         To get a reference to the new NDiagramElement created so that, based on its Name property, attach specific data to its Tag property.

·         To be able to know if it’s being dropped on an empty space on the document or on top of another NDiagramElement.  If the latter I need a reference to that underneath object to do some auto connecting logic (again depending on the Name and/or Tags of the shapes involved).

What I was initially looking for was something like a DrawingDocument_ShapeAdded event handler, but couldn’t find anything that obvious. 

I’ve tried the DrawingDocument_DrapDrop event handler with partial success.  I can cast args.HitNode to a Document or NDiagramElement to get what was dropped on.  However I don’t have a reference to the newly created shape.  At best, I’ve been able to get to args.Data.GetData("Library Data Object").Masters[0].Name.  But I need to get to the new object itself to attach custom data to the tag and do special connection logic.

Is there an easier approach I am missing?

 

Thanks in Advance,

Jason Irby

 

 

 

 

 

 

 

 





Similar Topics


Reading This Topic