2023-01-26 18:27:17 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Wpf.DiagramDesigner.Algorithms
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class LinksReconnectionAlgorithms
|
|
|
|
|
|
{
|
|
|
|
|
|
public static void ReconnectLinksToClosestPorts(this IDiagramViewModel diagram)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Only refresh ports once
|
|
|
|
|
|
//var portsToRefresh = new HashSet<FullyCreatedConnectorInfo>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var link in diagram.Items.OfType<ConnectionViewModel>())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (link.IsFullConnection == false)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2023-05-03 09:59:46 +08:00
|
|
|
|
var sourcePorts = link.SourceConnectorInfoFully.DataItem.Connectors;
|
2023-01-26 18:27:17 +08:00
|
|
|
|
var targetPorts = link.SinkConnectorInfoFully.DataItem.Connectors;
|
|
|
|
|
|
|
|
|
|
|
|
// Find the ports with minimal distance
|
|
|
|
|
|
var minDistance = double.MaxValue;
|
2023-05-03 09:59:46 +08:00
|
|
|
|
var minSourcePort = link.SourceConnectorInfoFully;
|
2023-01-26 18:27:17 +08:00
|
|
|
|
var minTargetPort = link.SinkConnectorInfoFully;
|
|
|
|
|
|
foreach (var sourcePort in sourcePorts)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var targetPort in targetPorts)
|
|
|
|
|
|
{
|
|
|
|
|
|
var distance = sourcePort.Position.DistanceTo(targetPort.Position);
|
|
|
|
|
|
if (distance < minDistance)
|
|
|
|
|
|
{
|
|
|
|
|
|
minDistance = distance;
|
|
|
|
|
|
minSourcePort = sourcePort;
|
|
|
|
|
|
minTargetPort = targetPort;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reconnect
|
|
|
|
|
|
if (link.SourceConnectorInfo != minSourcePort)
|
|
|
|
|
|
{
|
2023-05-03 09:59:46 +08:00
|
|
|
|
//portsToRefresh.Add(link.SourceConnectorInfoFully);
|
2023-01-26 18:27:17 +08:00
|
|
|
|
//portsToRefresh.Add(minSourcePort);
|
2023-05-03 09:59:46 +08:00
|
|
|
|
link.SourceConnectorInfo = minSourcePort;
|
2023-01-26 18:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (link.SinkConnectorInfo != minTargetPort)
|
|
|
|
|
|
{
|
|
|
|
|
|
//portsToRefresh.Add(link.SinkConnectorInfoFully);
|
|
|
|
|
|
//portsToRefresh.Add(minTargetPort);
|
2023-05-03 09:59:46 +08:00
|
|
|
|
link.SinkConnectorInfo = minTargetPort;
|
2023-01-26 18:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//foreach (var port in portsToRefresh)
|
|
|
|
|
|
// port.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|