private static Dictionary <int, DeviceGroup> GetGroupById(IEnumerable <int> connectedDeviceIds, Dictionary <int, DeviceSymbol> deviceSymbolById, Dictionary <int, CableInfo> cableInfoById)
        {
            Dictionary <int, DeviceGroup> groupById = new Dictionary <int, DeviceGroup>();

            foreach (int deviceId in connectedDeviceIds)
            {
                DeviceSymbol deviceSymbol = deviceSymbolById[deviceId];
                bool         isAdded      = false;
                foreach (DeviceGroup group in groupById.Values)
                {
                    if (group.TryAddSymbol(deviceSymbol))
                    {
                        isAdded = true;
                        break;
                    }
                }
                if (!isAdded)
                {
                    int count = groupById.Count;
                    if (!groupById.ContainsKey(count))
                    {
                        groupById.Add(count, new DeviceGroup(count));
                    }
                    groupById[count].TryAddSymbol(deviceSymbol);
                }
            }
            SetLoopsInGroupAndCableInfo(cableInfoById, groupById.Values);
            foreach (DeviceGroup group in groupById.Values)
            {
                group.SetPosition();
            }
            return(groupById);
        }
 public bool TryAddSymbol(DeviceSymbol deviceSymbol)
 {
     if (deviceSymbols.Count == 0)
     {
         deviceSymbols.Add(deviceSymbol);
         assignment = deviceSymbol.Assignment;
         cableIds   = deviceSymbol.CableIds;
         return(true);
     }
     if (!assignment.Equals(deviceSymbol.Assignment))
     {
         return(false);
     }
     if (!cableIds.Intersect <int>(deviceSymbol.CableIds).Any <int>())
     {
         return(false);
     }
     if (IsConnectionBetweenSymbolsExist(deviceSymbol))
     {
         return(false);
     }
     deviceSymbols.Add(deviceSymbol);
     foreach (int cableId in deviceSymbol.CableIds)
     {
         if (!cableIds.Contains(cableId))
         {
             cableIds.Add(cableId);
         }
     }
     return(true);
 }
 private bool IsConnectionBetweenSymbolsExist(DeviceSymbol deviceSymbol)
 {
     foreach (DeviceSymbol thisDeviceSymbol in deviceSymbols)
     {
         if (thisDeviceSymbol.ConnectionIds.Intersect <int>(deviceSymbol.ConnectionIds).Any <int>())
         {
             return(true);
         }
     }
     return(false);
 }