using Laservall.Solidworks.Common; using Laservall.Solidworks.Extension; using Laservall.Solidworks.Model; using SolidWorks.Interop.sldworks; using System.Diagnostics; using System.Reflection; using System.Threading.Tasks; using System.Windows.Input; using Xarial.XCad; using Xarial.XCad.Documents; using Xarial.XCad.Features; using Xarial.XCad.SolidWorks.Documents; namespace Laservall.Solidworks.Pane { public class PartPropPaneViewModel : ViewModelBase { private IModelDoc2 _selectedDoc; private PartPropModel _model = new PartPropModel(); public PartPropModel Model { get => _model; set => Set(ref _model, value); } private string _statusText; public string StatusText { get => _statusText; set => Set(ref _statusText, value); } private string _versionText; public string VersionText { get => _versionText; set => Set(ref _versionText, value); } private bool _isLoadingPartTypes; public bool IsLoadingPartTypes { get => _isLoadingPartTypes; set => Set(ref _isLoadingPartTypes, value); } public ICommand SaveCommand { get; } public ICommand ResetCommand { get; } public ICommand RefreshPartTypesCommand { get; } public PartPropPaneViewModel() { VersionText = $"Ver.{Assembly.GetExecutingAssembly().GetName().Version}"; SaveCommand = new RelayCommand(ExecuteSave, CanSave); ResetCommand = new RelayCommand(ExecuteReset, CanReset); RefreshPartTypesCommand = new RelayCommand(async _ => await LoadPartTypesAsync()); // 异步加载零件类型数据 Task.Run(async () => await LoadPartTypesAsync()); } /// /// 从PLM API加载零件类型数据 /// private async Task LoadPartTypesAsync() { try { IsLoadingPartTypes = true; StatusText = "正在加载零件类型..."; // 调用PLM API获取零件类型 var partTypes = await PartTypeService.GetAllAsync(); if (partTypes.Count > 0) { // 注册到可搜索ComboBox编辑器 SearchableComboBoxPropertyEditor.RegisterPartTypes("零件类型", partTypes); StatusText = $"已加载 {partTypes.Count} 个零件类型"; } else { StatusText = "未找到零件类型数据"; } } catch (System.Exception ex) { Debug.WriteLine($"加载零件类型失败: {ex.Message}"); StatusText = "加载零件类型失败"; } finally { IsLoadingPartTypes = false; } } public void OnSelectionChanged(IXDocument doc, IXSelObject selObject) { IModelDoc2 resolvedDoc = null; if (selObject.OwnerDocument is IXDocument) { if (selObject is ISwPartComponent partComponent) { resolvedDoc = (IModelDoc2)partComponent.Component.GetModelDoc(); } else if (selObject is IXFeature feature) { if (feature.Component is ISwComponent swPartComponent) { resolvedDoc = swPartComponent.Component.GetModelDoc() as IModelDoc2; } else if (feature.OwnerDocument != null) { resolvedDoc = (feature.OwnerDocument as ISwDocument).Model; } } } if (selObject.OwnerDocument is ISwPart) { resolvedDoc = null; } _selectedDoc = resolvedDoc; if (_selectedDoc != null) { SWDocReader.ReadDocProperties(_selectedDoc, Model); StatusText = $"已加载: {_selectedDoc.GetTitle()}"; } else { StatusText = ""; } ((RelayCommand)SaveCommand).RaiseCanExecuteChanged(); ((RelayCommand)ResetCommand).RaiseCanExecuteChanged(); Debug.WriteLine($"OnSelectionChanged: {selObject.GetType()}"); } private bool CanSave() { return _selectedDoc != null; } private void ExecuteSave() { if (_selectedDoc == null) return; SWDocWriter.WriteDocProperties(_selectedDoc, Model); StatusText = "属性已保存"; } private bool CanReset() { return _selectedDoc != null; } private void ExecuteReset() { if (_selectedDoc == null) return; SWDocReader.ReadDocProperties(_selectedDoc, Model); StatusText = "属性已重置"; } } }