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; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Runtime.CompilerServices; namespace PdmSwPlugin.PropertySetting.Panel.Model { public class BasePanelModel : 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 protected bool changed; public event PanelPropertyChanged UpdateProperty; protected ModelDoc2 doc; private Dictionary InitData = new Dictionary(); 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 virtual bool SetSettings(object settings) { return true; } public Dictionary GetAllProperties() { return NameConstant.GetAllProperties(this); } public bool ClearAllProperties(out string ErrMsg) { ErrMsg = null; return true; } public bool UpdateSingleProperty(string name, string value) { changed = NameConstant.UpdateSingleProperty(this, name, value); return changed; } public bool GetDocChanged() { return NameConstant.CompareWithInit(this, InitData); } public bool ResetProperty(out string errMsg) { throw new System.NotImplementedException(); } } }