NPort Event on Glued/Snapped Item Change


Author
Message
RLDP 2000
RLDP 2000
Forum Member (28 reputation)Forum Member (28 reputation)Forum Member (28 reputation)Forum Member (28 reputation)Forum Member (28 reputation)Forum Member (28 reputation)Forum Member (28 reputation)Forum Member (28 reputation)Forum Member (28 reputation)
Group: Forum Members
Posts: 11, Visits: 387
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.

Tags
Nevron Support
Nevron Support
Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)
Group: Administrators
Posts: 3.1K, Visits: 4.2K
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");
        }
    };
}



Best Regards,
Nevron Support Team


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic
1 active, 1 guest, 0 members, 0 anonymous
No members currently viewing this topic!

Login

Explore
Messages
Mentions
Search