using EPLAN.Harness.API; using EPLAN.Harness.API.ApiProperties; using EPLAN.Harness.API.Occurrences; using EPLAN.Harness.Common.Extensions; using EPLAN.Harness.Core; using EPLAN.Harness.Core.Controls; using EPLAN.Harness.Core.Props; using EPLAN.Harness.ProjectCore; using EPLAN.Harness.ProjectCore.Occurrences.Designer; using Sinvo.EplanHpD.Plugin.Service; using Sinvo.EplanHpD.Plugin.Service.Model; using Sinvo.EplanHpD.Plugin.WPFUI.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.Remoting.Contexts; using System.Security; using System.Text; using System.Threading.Tasks; namespace Sinvo.EplanHpD.Plugin.WPFUI.ViewModel { public class LayoutHelperViewModel : INotifyPropertyChanged { public FlexDesigner _currentFlexDesigner; private string _selectMotorModel; private MotorLectotypeService service = new(); private List _motors; private MotorModel _motor; private CableLectotypeViewModel cableLectotypeViewModel; private HpdApi api; public MotorModel Motor { get => _motor; set { _motor = value; OnPropertyChanged(); } } public LayoutHelperViewModel() { api = HpdApi.GetInstance(); api.Init(); } public LayoutHelperViewModel(FlexDesigner currentFlexDesigner, List motorFlags) { _currentFlexDesigner = currentFlexDesigner; _motors = motorFlags; api = HpdApi.GetInstance(); api.Init(); } public string SelectMotorModel { get { return _selectMotorModel; } set { _selectMotorModel = value; OnPropertyChanged(); } } private string _selectLineId; public string SelectLineName { get { try { if (_selectLineId == null) return ""; var part = _currentFlexDesigner.GetOccurrenceByID(_selectLineId); if (part != null) { return part.Parents?.FirstOrDefault()?.LibraryName; } else { return ""; } } catch (Exception) { return ""; } } set { } } /// /// 是否有下一个 /// public bool HasNext { get { if (_motors == null) return false; if (SelectedLines == null) return false; if (CurrentMotorSelectLineIndex < SelectedLines.Count - 1) { return true; } else if (CurrentMotorIndex < _motors.Count - 1) { return true; } else { return false; } } set { OnPropertyChanged(nameof(HasNext)); } } /// /// 是否有上一个 /// public bool HasPrev { get { if (CurrentMotorSelectLineIndex > 0) { return true; } else if (CurrentMotorIndex > 0) { return true; } else { return false; } } set { OnPropertyChanged(nameof(HasPrev)); } } private int CurrentMotorIndex = 0; private int CurrentMotorSelectLineIndex = 0; private LectotypeLineModel _selectedLine; public LectotypeLineModel SelectedLine { get => _selectedLine; set { _selectedLine = value; OnPropertyChanged(nameof(SelectedLine)); OnPropertyChanged(nameof(HasPrev)); OnPropertyChanged(nameof(HasNext)); } } private List _subLines; public List SubLines { get => _subLines; set { _subLines = value; OnPropertyChanged(nameof(SubLines)); } } public List SelectedLines; public void OnSelectChange(object sender) { if (sender is OccSelectSet selectSet) { var item = selectSet.SelectedItem; if (item is OccSubPart part) { Debug.WriteLine($"Select part -> {part.Name} / {part.ID}"); if (part.Parents != null) { SelectMotorModel = part.Parents?.First()?.Name ?? ""; Debug.WriteLine($"Select part parent -> {part.Parents?.First()?.Name} / {part.Parents?.First()?.ID}"); } } else if (item != null) { CheckSelLine(slient: true); _selectLineId = item.ID; OnPropertyChanged(nameof(SelectLineName)); } } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } internal void GetMotorCables() { if (_motors != null && _motors.Any()) { Task.Factory.StartNew(() => { var service = new MotorLectotypeService(); var data = service.GetMotorLectotypeData(_motors[CurrentMotorIndex]?.MotorUniqueFlag); if (data != null) { cableLectotypeViewModel = data; if (cableLectotypeViewModel?.SelectedLines?.Any() ?? false) { SelectedLines = cableLectotypeViewModel.SelectedLines.OrderBy(it => it.CableType).ThenBy(it => it.CurrentLine).ToList(); SelectedLine = SelectedLines[CurrentMotorSelectLineIndex]; ReSetSubLines(); Motor = _motors[CurrentMotorIndex]; return; } } CurrentMotorIndex++; if (CurrentMotorIndex < _motors.Count) { CurrentMotorSelectLineIndex = 0; GetMotorCables(); } }).Wait(); } } internal void ToPrevLine() { if (HasPrev) { if (CurrentMotorSelectLineIndex == 0) { ToPrevMotor(); } else { CurrentMotorSelectLineIndex--; SelectedLine = SelectedLines[CurrentMotorSelectLineIndex]; ReSetSubLines(); } } } internal void ToNextLine() { if (HasNext) { var canNext = CheckSelLine(); if (canNext) { if (CurrentMotorSelectLineIndex >= SelectedLines.Count - 1) { ToNextMotor(); } else { CurrentMotorSelectLineIndex++; SelectedLine = SelectedLines[CurrentMotorSelectLineIndex]; ReSetSubLines(); } } } } private bool CheckSelLine(bool slient = false) { try { var parts = _currentFlexDesigner.SelectSet; foreach (var part in parts) { if (part != null) { // 如果选中的是导线,则直接使用,如果选中的是绝缘层,则使用绝缘层,其他情况使用父级 var linePart = part is OccCableForked ? part as OccCableForked : part is OccCablesInsulatorGraph ? part : part.Parents?.FirstOrDefault() as OccCableForked; if (linePart != null) { var libName = linePart?.LibraryName; // 绝缘层选择上一层的导线来获取信息 if(part is OccCablesInsulatorGraph) { libName = (part.Parents?.First() as OccCableForked).LibraryName; //partLineModel = (part.Parents?.First() as OccCableForked).LibraryName.Split('&')[1]; } var partLineModel = (libName.Contains("&") ? libName.Split('&')[1] : libName).Trim(' '); if (SubLines.Any(it => it.CableModel == partLineModel)) { var subLine = SubLines.FirstOrDefault(it => it.CableModel == partLineModel); var imprint = ""; // 导线需要取绝缘层的印记信息,导线本身没有印记 if(part is OccCableForked) { var insulatorGraph = part.Children.FirstOrDefault(); imprint = insulatorGraph.Properties.FirstOrDefault(it => it.PropertyName == "Imprint")?.ValueHolder.StrVal; } else { imprint = (linePart as OccCablesInsulatorGraph).Imprint;//Properties.FirstOrDefault(it => it.PropertyName == "Imprint")?.ValueHolder.StrVal; } if (string.IsNullOrEmpty(imprint)) { #if DEBUG return true; #else if(!slient) FlexMessageBox.Warning($"当前选中的线 {libName} 印记未填写"); return false; #endif } if ($"{SelectedLine.AxisNo}-{SelectedLine.CurrentLine}" == imprint) { subLine.IsChecked = true; //cableLectotypeViewModel.SaveToDb(); SetSubLineAndSave(subLine.LectotypeLineId); return SubLines.All(it => it.IsChecked); } else { #if DEBUG return true; #else if(!slient) FlexMessageBox.Warning($"当前选中的线 {libName} 印记与推荐中的线印记不一致"); return false; #endif } } } } #if DEBUG return true; #else if(!slient) FlexMessageBox.Warning($"当前选中的线不是推荐中的线型号"); return false; #endif } } catch (Exception) { if(!slient) FlexMessageBox.Warning("抓取线信息异常"); return false; } if (!slient) FlexMessageBox.Warning("请选择正确的线"); return false; } internal void ToPrevMotor() { if (CurrentMotorIndex > 0) { CurrentMotorIndex--; CurrentMotorSelectLineIndex = 0; GetMotorCables(); } } internal void ToNextMotor() { if (CurrentMotorIndex < _motors.Count) { CurrentMotorIndex++; CurrentMotorSelectLineIndex = 0; GetMotorCables(); } } internal void ReSetSubLines() { SubLines = SelectedLine.SubLines.Distinct(it => it.CableModel).ToList(); } internal void SetSubLineAndSave(string lectotypeLineId) { var motorUniqueFlag = Motor.MotorUniqueFlag; var service = new MotorLectotypeService(); service.SetLineDone(motorUniqueFlag, lectotypeLineId); } } }