122 lines
3.5 KiB
C#
122 lines
3.5 KiB
C#
|
|
using Laservall.Solidworks.Common;
|
||
|
|
using Laservall.Solidworks.Extension;
|
||
|
|
using Laservall.Solidworks.Model;
|
||
|
|
using SolidWorks.Interop.sldworks;
|
||
|
|
using System.Diagnostics;
|
||
|
|
using System.Reflection;
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
|
||
|
|
public ICommand SaveCommand { get; }
|
||
|
|
public ICommand ResetCommand { get; }
|
||
|
|
|
||
|
|
public PartPropPaneViewModel()
|
||
|
|
{
|
||
|
|
VersionText = $"Ver.{Assembly.GetExecutingAssembly().GetName().Version}";
|
||
|
|
|
||
|
|
SaveCommand = new RelayCommand(ExecuteSave, CanSave);
|
||
|
|
ResetCommand = new RelayCommand(ExecuteReset, CanReset);
|
||
|
|
}
|
||
|
|
|
||
|
|
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 = "属性已重置";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|