using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Runtime.CompilerServices; using System.Windows.Controls; using PdmSwPlugin.Common.CustomHandler; using PdmSwPlugin.Common.Util; using PdmSwPlugin.PropertySetting.Interface; using PdmSwPlugin.PropertySetting.Panel.Attr; using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; namespace PdmSwPlugin.PropertySetting.Panel { public partial class StandardModulePanel : UserControl, INotifyPropertyChanged, IPropertyOpt { #region 不能公用的东西,真有你的啊C# public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } public void RaiseAndSetIfChanged(ref T old, T @new, [CallerMemberName] string propertyName = null) { old = @new; if (propertyName != null) { RaisePropertyChanged(propertyName); /*var attr = this.GetType().GetProperty(propertyName)?.GetCustomAttribute(); if (attr == null) { return; } if (attr.NeedSave) { UpdateProperty?.Invoke(this, attr.Name, @new); }*/ } } #endregion public event PanelPropertyChanged UpdateProperty; private Dictionary InitData = new Dictionary(); private ModelDoc2 doc; #region UI属性 private string _materialCode; [PropertySettingAttr(Name = NameConstant.materialCode)] public string materialCode { get => _materialCode; set { RaiseAndSetIfChanged(ref _materialCode, value); } } private string _materialName; [PropertySettingAttr(Name = NameConstant.materialName)] public string materialName { get => _materialName; set { RaiseAndSetIfChanged(ref _materialName, value); } } private string _materialModel; [PropertySettingAttr(Name = NameConstant.materialModel)] public string materialModel { get => _materialModel; set { RaiseAndSetIfChanged(ref _materialModel, value); } } private string _brand; [PropertySettingAttr(Name = NameConstant.brand)] public string brand { get => _brand; set { RaiseAndSetIfChanged(ref _brand, value); } } private string _weight = ""; [PropertySettingAttr(Name = NameConstant.weight, NeedSave = false)] public string weight { get => CustomPropertyUtil.GetMass(doc); set { RaiseAndSetIfChanged(ref _weight, value); } } [PropertySettingAttr(Name = NameConstant.weight, NeedInit = false)] public string weightEval { get => doc == null ? null : $"\"SW-质量@{Path.GetFileName(doc.GetPathName())}\""; set { } } private string _designer = ""; [PropertySettingAttr(Name = NameConstant.designer)] public string designer { get => _designer; set { RaiseAndSetIfChanged(ref _designer, value); } } private string _version = ""; [PropertySettingAttr(Name = NameConstant.version)] public string version { get => _version; set { RaiseAndSetIfChanged(ref _version, value); } } private string _remark = ""; [PropertySettingAttr(Name = NameConstant.remark)] public string remark { get => _remark; set { RaiseAndSetIfChanged(ref _remark, value); } } #endregion public StandardModulePanel() { InitializeComponent(); DataContext = this; } /// /// 设置属性 /// /// /// /// public bool SetProperties(ModelDoc2 doc, object properties, bool docChange, out string errMsg) { this.doc = doc; var data = docChange ? InitData : null; return NameConstant.SetProperties(this, properties, data, out errMsg); } public bool SaveDoc(ref int err, ref int warn) { Dictionary props = GetAllProperties(); CustomPropertyUtil.SetCustomProperties(doc, props); if (doc.Save3((int)swSaveAsOptions_e.swSaveAsOptions_AvoidRebuildOnSave, ref err, ref warn)) { InitData = props; return true; } return false; } /// /// 获取属性 /// /// public Dictionary GetAllProperties() { return NameConstant.GetAllProperties(this); } public bool UpdateSingleProperty(string name, string value) { return NameConstant.UpdateSingleProperty(this, name, value); } public bool SetSettings(object settings) { return true; } public object GetPropertyValue(string propertyName) { return null; } public bool ClearAllProperties(out string ErrMsg) { ErrMsg = null; return true; } public bool InitProperties(object properties, out string errMsg) { errMsg = null; return true; } public bool GetDocChanged() { return NameConstant.CompareWithInit(this, InitData); } public bool ResetProperty(out string errMsg) { return SetProperties(doc, InitData, false, out errMsg); } } }