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.WPFUI.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; 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 _motorIds; private MotorModel _motor; private CableLectotypeViewModel cableLectotypeViewModel; public MotorModel Motor { get => _motor; set { _motor = value; OnPropertyChanged(); } } public LayoutHelperViewModel() { } public LayoutHelperViewModel(FlexDesigner currentFlexDesigner, List motorIds) { _currentFlexDesigner = currentFlexDesigner; _motorIds = motorIds; } public string SelectMotorModel { get { return _selectMotorModel; } set { _selectMotorModel = value; OnPropertyChanged(); } } private string _selectLineId; public string SelectLineName { get { if (_selectLineId == null) return ""; var part = _currentFlexDesigner.GetOccurrenceByID(_selectLineId); if (part != null) { return part.Parents?.First()?.LibraryName; } else { return ""; } } set { } } /// /// 是否有下一个 /// public bool HasNext { get { if (_motorIds == null) return false; if (SelectedLines == null) return false; if (CurrentMotorSelectLineIndex < SelectedLines.Count - 1) { return true; } else if (CurrentMotorIndex < _motorIds.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) { _selectLineId = item.ID; OnPropertyChanged(nameof(SelectLineName)); } } } private bool IsCable(BaseOccurrence occ) { return occ is OccCableForked; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } internal void GetMotorCables() { if (_motorIds != null && _motorIds.Any()) { Task.Factory.StartNew(() => { var service = new MotorLectotypeService(); var data = service.GetMotorLectotypeData(_motorIds[CurrentMotorIndex]); if (!string.IsNullOrEmpty(data)) { cableLectotypeViewModel = Newtonsoft.Json.JsonConvert.DeserializeObject(data); if (cableLectotypeViewModel?.SelectedLines?.Any() ?? false) { SelectedLines = cableLectotypeViewModel.SelectedLines; SelectedLine = SelectedLines[CurrentMotorSelectLineIndex]; ReSetSubLines(); Motor = cableLectotypeViewModel.Motor; return; } } { CurrentMotorIndex++; if (CurrentMotorIndex < _motorIds.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) { if (CheckAndSetSelLine()) { if (CurrentMotorSelectLineIndex >= SelectedLines.Count - 1) { ToNextMotor(); } else { CurrentMotorSelectLineIndex++; SelectedLine = SelectedLines[CurrentMotorSelectLineIndex]; ReSetSubLines(); } } } } private bool CheckAndSetSelLine() { var part = _currentFlexDesigner.GetOccurrenceByID(_selectLineId); if (part != null) { if (part.Properties.Any(it => it.ValueHolder.StrVal == "轴号")) return true; var prop = new EPLAN.Harness.Core.Props.Property(new PropDef { Name = "轴号" }); prop.Set(SelectedLine.AxisNo); part.Properties.Add(prop); return true; } else { FlexMessageBox.Warning("当前选中的线不是推荐中的线"); return false; } } internal void ToPrevMotor() { if (CurrentMotorIndex > 0) { CurrentMotorIndex--; CurrentMotorSelectLineIndex = 0; GetMotorCables(); } } internal void ToNextMotor() { if (CurrentMotorIndex < _motorIds.Count) { CurrentMotorIndex++; CurrentMotorSelectLineIndex = 0; GetMotorCables(); } } internal void ReSetSubLines() { SubLines = SelectedLine.SubLines.Distinct(it => it.CableModel).ToList(); } } }