NPort Event on Glued/Snapped Item Change


https://www.nevron.com/Forum/Topic14483.aspx
Print Topic | Close Window

By Jan Cruz - 4 Months Ago
Hi,

I would like to ask if there is an existing event under NPort object that triggers whenever the glued or snapped object changes?
I would like to use this feature to track connection changes on one of my NShape ports.  

Kindly advise if it is available on NOV library.
By Nevron Support - 4 Months Ago
You can use the following method to get the shapes glued to a port:


private static NList<NShape> GetGluedShapes(NPort port)
{
    NList<NShape> gluedShapes = new NList<NShape>();
    if (port.OwnerShape == null)
        return gluedShapes;

    int portIndex = port.GetAggregationInfo().Index;

    // Check incoming shapes
    NList<NShape> shapes = port.OwnerShape.GetIncomingShapes();
    for (int i = 0; i < shapes.Count; i++)
    {
        NShape shape = shapes[i];
        if (shape.EndPointGlue is NEndPointGlueToPort portGlue && portGlue.PortIndex == portIndex)
        {
            gluedShapes.Add(shape);
        }
    }

    // Check outgoing shapes
    shapes = port.OwnerShape.GetOutgoingShapes();
    for (int i = 0; i < shapes.Count; i++)
    {
        NShape shape = shapes[i];
        if (shape.BeginPointGlue is NEndPointGlueToPort portGlue && portGlue.PortIndex == portIndex)
        {
            gluedShapes.Add(shape);
        }
    }

    return gluedShapes;
}


We will add this method to the NPort class, so in the next version of NOV Diagram, you will be able to just use NPort.GetGluedShapes method.

Then subscribe to the Deactivated event of the NDragHandleTool of the drawing view. The following method is a complete code example that creates a diagram with 2 shapes, a connector and shows a message box when a new connector is glued to the right port of the red rectangle shape or you disconnect and then reconnect the connector:


private static void CreateSampleDiagram(NDrawingView drawingView)
{
    NPage page = drawingView.ActivePage;
    NBasicShapeFactory factory = new NBasicShapeFactory();

    // Create 2 shapes
    NShape shape1 = factory.CreateShape(ENBasicShape.Rectangle);
    shape1.Text = "Rectangle";
    shape1.SetBounds(100, 100, 150, 100);
    shape1.Geometry.Fill = new NColorFill(NColor.Red);
    page.Items.Add(shape1);

    NShape shape2 = factory.CreateShape(ENBasicShape.Circle);
    shape2.Text = "Circle";
    shape2.SetBounds(350, 300, 150, 100);
    shape2.Geometry.Fill = new NColorFill(NColor.Green);
    page.Items.Add(shape2);

    // Create a connector and connect the shapes
    NRoutableConnector connector = new NRoutableConnector();
    connector.UserClass = NDR.StyleSheetNameConnectors;
    page.Items.Add(connector);
    connector.GlueBeginToShape(shape1);
    connector.GlueEndToShape(shape2);

    // Get the right port of the rectangle shape
    NPort port = shape1.Ports.GetPortByName("Right");
    NShape portConnector;
    GetGluedShapes(port).TryGetFirst(null, out portConnector);

    // Subscribe to the deactivated event of the drag handle tool
    NDragHandleTool dragHandleTool = drawingView.Interactor.GetTool<NPointerTool>().GetTool<NDragHandleTool>();
    dragHandleTool.Deactivated += (NEventArgs arg) =>
    {
        NShape curPortConnector;
        GetGluedShapes(port).TryGetFirst(null, out curPortConnector);
        if (curPortConnector != null && curPortConnector != portConnector)
        {
            NMessageBox.Show("New connector connected to port.", "New Connection");
        }
    };
}