void OnCreateNew() { Configuration = new Configuration(); Configuration.Lines.Add(new Line()); Initialize(); }
public SpecificationViewModel(Configuration configuration, Action initializeConfiguration, List<DeviceSpecificationItem> deviceSpecificationItems = null, List<CableSpecificationItem> cableSpecificationItems = null) { Title = "Спецификация устройств и кабелей"; Configuration = configuration; InitializeConfiguration = initializeConfiguration; AddDeviceCommand = new RelayCommand(OnAddDevice); RemoveDeviceCommand = new RelayCommand(OnRemoveDevice, CanRemoveDevice); AddCableCommand = new RelayCommand(OnAddCable); RemoveCableCommand = new RelayCommand(OnRemoveCable, CanRemoveCable); GenerateFromSpecificationCommand = new RelayCommand(OnGenerateFromSpecification); DeviceSpecificationItems = new ObservableCollection<DeviceSpecificationItemViewModel>( (deviceSpecificationItems == null ? Configuration.DeviceSpecificationItems : deviceSpecificationItems).Select(x => new DeviceSpecificationItemViewModel(x))); CableSpecificationItems = new ObservableCollection<CableSpecificationItemViewModel>( (cableSpecificationItems == null ? Configuration.CableSpecificationItems : cableSpecificationItems).Select(x => new CableSpecificationItemViewModel(x))); }
public static IEnumerable<CableSpecificationItem> GenerateFromSpecification(Configuration configuration, IList<DeviceSpecificationItem> deviceSpecificationItems, IList<CableSpecificationItem> cableSpecificationItems) { const int maxAdress = 240; configuration.Lines = new List<Line>(); var totalDevicesCount = deviceSpecificationItems.Sum(x => x.Count * x.Driver.Mult); for (int i = 0; i <= totalDevicesCount / maxAdress; i++) { configuration.Lines.Add(new Line()); } var expandedDeviceSpecificationItems = new List<DeviceSpecificationItem>(); foreach (var deviceSpecificationItem in deviceSpecificationItems) for (int i = 0; i < deviceSpecificationItem.Count; i++) expandedDeviceSpecificationItems.Add(new DeviceSpecificationItem() { DriverType = deviceSpecificationItem.DriverType, Count = 1 } ); var sortedDeviceSpecificationItems = expandedDeviceSpecificationItems.OrderByDescending(x=>x.Driver.DeviceType).ThenBy(x => x.Driver.I); bool needAnotherLine = false; for (int i = 0; i < configuration.Lines.Count; i++) { uint sum = 0; for (int j = i; j < expandedDeviceSpecificationItems.Count; j+=configuration.Lines.Count) sum += expandedDeviceSpecificationItems[j].Driver.Mult; if (sum > maxAdress) { needAnotherLine = true; break; } } if (needAnotherLine) configuration.Lines.Add(new Line()); var lineNo = 0; foreach (var deviceSpecificationItem in sortedDeviceSpecificationItems) { var line = configuration.Lines[lineNo]; lineNo++; if (lineNo >= configuration.Lines.Count) lineNo = 0; var device = new Device(); device.DriverType = deviceSpecificationItem.DriverType; device.Cable.Length = 0; device.Cable.Resistivity = 0; line.Devices.Add(device); } //Cables var cableRemains = cableSpecificationItems.OrderBy(x => x.Resistivity) .Select(x => new CableSpecificationItem() { CableType = x.CableType, Length = x.Length, Resistivity = x.Resistivity }) .ToList(); var cablePieces = new List<CableSpecificationItem>(); do { bool goAgain = false; var totalCableLength = cableRemains.Sum(x => x.Length); var averageCableLength = totalCableLength / (expandedDeviceSpecificationItems.Count - cablePieces.Count); if (averageCableLength > 1000) averageCableLength = 1000; for (int i = cableRemains.Count - 1; i >= 0 && cablePieces.Count < expandedDeviceSpecificationItems.Count; i--) { if (cableRemains[i].Length < averageCableLength) { goAgain = true; cablePieces.Add(cableRemains[i]); cableRemains.RemoveAt(i); if (i < cableRemains.Count) i++; } } if (cablePieces.Count >= expandedDeviceSpecificationItems.Count) break; if (goAgain) continue; int piecesCount = 0; int selectedIndex = 0; double maxPieceLength = -1; for (int i = cableRemains.Count - 1; i >= 0; i--) { int pc; if (cableRemains.Count == 1) pc = expandedDeviceSpecificationItems.Count - cablePieces.Count; else pc = cableRemains[i].Length % averageCableLength == 0 ? (int)(cableRemains[i].Length / averageCableLength) : (int)(cableRemains[i].Length / averageCableLength) + 1; double pieceLength = cableRemains[i].Length / pc; if (pieceLength > 1000) pieceLength = 1000; if (pieceLength > maxPieceLength) { maxPieceLength = pieceLength; selectedIndex = i; piecesCount = pc; } } for (int i = 0; i < piecesCount; i++) cablePieces.Add(new CableSpecificationItem() { Length = maxPieceLength, Resistivity = cableRemains[selectedIndex].Resistivity, CableType = cableRemains[selectedIndex].CableType }); if (selectedIndex < cableRemains.Count) { cableRemains[selectedIndex].Length -= maxPieceLength * piecesCount; if (cableRemains[selectedIndex].Length <= 0) cableRemains.RemoveAt(selectedIndex); } } while (cablePieces.Count < expandedDeviceSpecificationItems.Count); var sortedCablePieces = cablePieces.OrderByDescending(x => x.Resistivity).ThenBy(x => x.Length).ToList(); lineNo = 0; var deviceNo = 0; foreach (var cablePiece in cablePieces) { var line = configuration.Lines[lineNo]; if (deviceNo < line.Devices.Count) { line.Devices[deviceNo].Cable.Length = cablePiece.Length; line.Devices[deviceNo].Cable.Resistivity = cablePiece.Resistivity; line.Devices[deviceNo].Cable.CableType = cablePiece.CableType; } if (++lineNo >= configuration.Lines.Count) { lineNo = 0; deviceNo++; } } return cableRemains; }
void OnLoadFromFile() { var openFileDialog = new OpenFileDialog() { Filter = "Файл конфигурации АЛС (*.ALS)|*.ALS" }; if (openFileDialog.ShowDialog().Value) { try { SelectedLine = null; var xmlSerializer = new XmlSerializer(typeof(Configuration)); using (var streamReader = new StreamReader(openFileDialog.FileName)) { var configuration = (Configuration)xmlSerializer.Deserialize(streamReader); Configuration = configuration; } } catch (Exception ex) { MessageBoxService.ShowError("Ошибка загрузки:\n" + ex.Message); } } Initialize(); }
public static IEnumerable<CableSpecificationItem> GenerateFromSpecification(Configuration configuration) { return GenerateFromSpecification(configuration, configuration.DeviceSpecificationItems, configuration.CableSpecificationItems); }