Profile Picture

Performing a HitTest to find shapes under a given shape

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 am wanting to find the first shape/group underneath a specified shape/group.

I am wanting to grab a shape/group and move it on the drawing onto another shape/group and then connect the two.

Appearently this is not considered drapdrop as the shapes we already on the drawing and the dragdrop event doesn't fire in this case.

The approach I have been taking is to catch the transformchanged event and that gives me the moving shape/group.  I was trying to use that to do a hit test in the document, but I have not figured out how to do that properly.

 

Is that my best approach?  Is there a better way? Can you point me to an example?  I have not been able find much information on that.  I had the most trouble with the HitTestContext.

 

Thanks in advance,

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,

What I got from your description is that you want to move one shape over another and when the shape is dropped the 2 shapes to automatically connect but how ? Through a line connecting them or the 2 shapes should be directly connected using an outward port ? Please, explain in more details what are you trying to do.



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

Say I have a drawing with 2 shapes A and B each with a port in the center of the shape. 

I want to be able to grab shape A and move it over on top of shape B.  At that point I want to detect that happened, connect the two shapes with a line and then programmatically move shape A back to its original position, but now Shape A and B are now connected with a line.

I first went down the path of tying into drag and drop, but it seems when 2 shapes are already on the drawing that action is not considered drag/drop but only transformChanged. (Note: I’ve tried setting  moveTool.Mode = MoveToolMode.MoveAndDragDrop” but the View NodeDragDrop event never fires when the 2 shapes where already on the drawing.)

So next, I have been trying to catch the NodeTransformChanged event and using a HitTest with args.Node to see if the was a shape underneath where it had been moved to know if I need to connect to it. 

 

So my questions are:

 

1.   Am I persuing the best approach?  Is there an easier way?

2.  If yes, then can you give me some tips on doing the HitTest?  Especially on the HitTestContext parameter.

If I can catch the NodeTransformChanged event and know what shape was moved, and what shape is underneath it, then I can write the code to connect them.

 

Thanks for your time.

 

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,

Yes, there is an easier way – just subscribe to the NodeTransformChanging event of the document’s event sink service. In the event handler you can use the following code:

 

private void EventSinkService_NodeTransformChanging(NNodeTransformCancelEventArgs args)

{

      NShape shape = args.Node as NShape;

      if(shape == null)

            return;

 

      // Get the rect bounds in device coordinates

      NMatrix2DF newTransform = args.NewTransform;

      newTransform.Multiply(shape.ParentSceneTransform);

      NRectangleF rect = newTransform.TransformBounds(shape.ModelBounds);

      rect = view.SceneToDevice.TransformRect(rect);

 

      // Get the nodes under the rect

      NNodeList nodes = view.HitTestDocument(rect, NFilters.Shape2D);

      if (nodes.Count != 1)

            return;

 

      // Get the hit shape

      NShape hitShape = (NShape)nodes[0];

 

      // The moved shape is the hit shape, so do nothing

      if (hitShape == shape)

            return;

 

      // Create a line connecting the 2 shapes

      NDrawingDocument doc = (NDrawingDocument)shape.Document;

      NLineShape line = new NLineShape();

      doc.ActiveLayer.AddChild(line);

      line.FromShape = shape;

      line.ToShape = hitShape;

 

      // Cancel the movement of the shape

      args.Cancel = true;

}

 



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
Thats very helpful. That would have taken me forever to work out.

Thanks,
Jason





Similar Topics


Reading This Topic