using PdmSwPlugin.Common.CustomHandler;
using PdmSwPlugin.Common.Util;
using PdmSwPlugin.PropertySetting.Entity;
using PdmSwPlugin.PropertySetting.Interface;
using PdmSwPlugin.PropertySetting.Panel.Attr;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
namespace PdmSwPlugin.PropertySetting.Tab
{
///
/// JgjPartTab.xaml 的交互逻辑
///
public partial class JgjPartTab : 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 void ccc(ref T old, T @new, [CallerMemberName] string propertyName = null)
{
old = @new;
if (propertyName != null)
{
RaisePropertyChanged(propertyName);
}
}
public event PanelPropertyChanged UpdateProperty;
///
/// 初始数据
///
private Dictionary InitData = new Dictionary();
///
/// 实际数据
///
private Dictionary SaveData = new Dictionary();
private Dictionary datas;
private Dictionary> stuffItemCache;
private Dictionary> surfaceItemCache;
private Dictionary> heatItemCache;
private ModelDoc2 doc;
#region 下拉框数据源
private ObservableCollection _jgjTypeItems;
public ObservableCollection jgjTypeItems
{
get => _jgjTypeItems;
set => RaiseAndSetIfChanged(ref _jgjTypeItems, value);
}
private ObservableCollection _stuffItems;
public ObservableCollection stuffItems
{
get => _stuffItems;
set => RaiseAndSetIfChanged(ref _stuffItems, value);
}
private ObservableCollection _surfaceItems;
public ObservableCollection surfaceItems
{
get => _surfaceItems;
set => RaiseAndSetIfChanged(ref _surfaceItems, value);
}
private ObservableCollection _heatItems;
public ObservableCollection heatItems
{
get => _heatItems;
set => RaiseAndSetIfChanged(ref _heatItems, value);
}
private ObservableCollection _materialTypeItems = NameConstant.MaterialTypes;
public ObservableCollection materialTypeItems
{
get => _materialTypeItems;
set => RaiseAndSetIfChanged(ref _materialTypeItems, value);
}
private ObservableCollection _docTypeItems = NameConstant.DocTypes;
public ObservableCollection docTypeItems
{
get => _docTypeItems;
set => RaiseAndSetIfChanged(ref _docTypeItems, value);
}
#endregion
private bool _docTypeEnabled = true;
[PropertySettingAttr(NeedInit = false, NeedSave = false)]
public bool DocTypeEnabled
{
get => _docTypeEnabled;
set => RaiseAndSetIfChanged(ref _docTypeEnabled, value);
}
#region UI属性
private string _materialCode;
[PropertySettingAttr(Name = NameConstant.materialCode, defaultValue = "$PRP:\"SW-File Name\"")]
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 _jgjType;
[PropertySettingAttr(Name = NameConstant.jgjType)]
public string jgjType
{
get => _jgjType;
set
{
string temp = value;// jgjTypeItems.Contains(value) ? value : "无";
var datas = stuffItemCache.Get(temp, new ObservableCollection { "无" });
RaiseAndSetIfChanged(ref _stuffItems, datas, "stuffItems");
RaiseAndSetIfChanged(ref _jgjType, temp);
UpdateField("jgjType", temp);
}
}
private string _stuff;
[PropertySettingAttr(Name = NameConstant.stuff)]
public string stuff
{
get => _stuff;
set
{
string temp = value;// stuffItems.Contains(value) ? value : "无";
var sDatas = surfaceItemCache.Get($"{jgjType}_{temp}", new ObservableCollection { "无" });
var hDatas = heatItemCache.Get($"{jgjType}_{temp}", new ObservableCollection { "无" });
ccc(ref _surfaceItems, sDatas, "surfaceItems");
ccc(ref _heatItems, hDatas, "heatItems");
ccc(ref _stuff, temp);
}
}
private string _surface;
[PropertySettingAttr(Name = NameConstant.surface)]
public string surface
{
get => _surface;
set
{
string temp = value;// surfaceItems.Contains(value) ? value : "无";
ccc(ref _surface, temp);
}
}
private string _heat;
[PropertySettingAttr(Name = NameConstant.heat)]
public string heat
{
get => _heat;
set
{
string temp = value;// heatItems.Contains(value) ? value : "无";
ccc(ref _heat, temp);
}
}
private string _weight = "";
[PropertySettingAttr(Name = NameConstant.weight, NeedSave = false)]
public string weight
{
get => CustomPropertyUtil.GetMass(doc);
set
{
ccc(ref _weight, value);
}
}
[PropertySettingAttr(Name = NameConstant.weight, NeedInit = false)]
public string weightEval
{
get => doc == null ? null : $"\"SW-质量@{Path.GetFileName(doc.GetPathName())}\"";
set { }
}
private string _materialType = "";
[PropertySettingAttr(Name = NameConstant.materialType, defaultValue = "普通件")]
public string materialType
{
get => _materialType;
set
{
RaiseAndSetIfChanged(ref _materialType, value);
}
}
private string _designer = "";
[PropertySettingAttr(Name = NameConstant.designer)]
public string designer
{
get => _designer;
set
{
RaiseAndSetIfChanged(ref _designer, value);
}
}
private string _remark = "";
[PropertySettingAttr(Name = NameConstant.remark)]
public string remark
{
get => _remark;
set
{
RaiseAndSetIfChanged(ref _remark, value);
}
}
private string _docType = "";
[PropertySettingAttr(Name = NameConstant.docType, defaultValue = "加工件")]
public string docType
{
get => _docType;
set
{
string temp = value;
if (NameConstant.SpecialJgjType.Contains(jgjType))
{
temp = "加工件";
}
ccc(ref _docType, temp);
}
}
private string _versionChange = "0";
[PropertySettingAttr(Name = NameConstant.versionChange, defaultValue = "0")]
public string versionChange
{
get => _versionChange;
set
{
RaiseAndSetIfChanged(ref _versionChange, value);
RaiseAndSetIfChanged(ref _versionChangeBind, value == "1", "versionChangeBind");
}
}
private bool _versionChangeBind = false;
[PropertySettingAttr(Name = NameConstant.versionChange, NeedSave = false, NeedInit = false)]
public bool versionChangeBind
{
get => _versionChangeBind;
set
{
RaiseAndSetIfChanged(ref _versionChangeBind, value);
RaiseAndSetIfChanged(ref _versionChange, value ? "1" : "0", "versionChange");
}
}
#endregion
public JgjPartTab()
{
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 void RefreshItem()
{
if (datas == null)
{
return;
}
var finalStuffs = stuffItemCache.Get(jgjType, new ObservableCollection { "无" });
string key = $"{jgjType}_{stuff}";
var finalSurfaces = surfaceItemCache.Get(key, new ObservableCollection { "无" });
var finalHeats = heatItemCache.Get(key, new ObservableCollection { "无" });
Dispatcher.Invoke(() =>
{
stuffItems = finalStuffs;
surfaceItems = finalSurfaces;
heatItems = finalHeats;
});
}
public bool SetSettings(object settings)
{
Dictionary datas = settings as Dictionary;
this.datas = datas;
stuffItemCache = new Dictionary>();
surfaceItemCache = new Dictionary>();
heatItemCache = new Dictionary>();
ObservableCollection jgjTypes = new ObservableCollection { "无" };
foreach (string key in datas.Keys)
{
jgjTypes.Add(key);
JgjType type = datas[key];
ObservableCollection stuffs = new ObservableCollection { "无" };
Dictionary stuffDatas = type.stuffMap;
foreach (string stuff in stuffDatas.Keys)
{
if (stuff != "无")
{
stuffs.Add(stuff);
}
StuffType stuffType = stuffDatas[stuff];
string key2 = key + "_" + stuff;
ObservableCollection surfaces = new ObservableCollection { "无" };
ObservableCollection heats = new ObservableCollection { "无" };
foreach (string surface in stuffType.surfaceParam)
{
if (surface != "无") surfaces.Add(surface);
}
foreach (string heat in stuffType.heatParam)
{
if (heat != "无") heats.Add(heat);
}
surfaceItemCache.Add(key2, surfaces);
heatItemCache.Add(key2, heats);
}
stuffItemCache.Add(key, stuffs);
if (!stuffItemCache.ContainsKey("无"))
{
stuffItemCache.Add("无", new ObservableCollection { "无" });
}
}
Dispatcher.Invoke(() =>
{
jgjTypeItems = jgjTypes;
});
return true;
}
public bool ClearAllProperties(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);
}
private void stuffInput_DropDownOpened(object sender, System.EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox == null) return;
if (comboBox.Name == "stuffInput")
{
stuffItems = stuffItemCache.Get(jgjType, new ObservableCollection { "无" });
}
else if (comboBox.Name == "surfaceInput")
{
string key = $"{jgjType}_{stuff}";
surfaceItems = surfaceItemCache.Get(key, new ObservableCollection { "无" });
}
else if (comboBox.Name == "heatInput")
{
string key = $"{jgjType}_{stuff}";
heatItems = heatItemCache.Get(key, new ObservableCollection { "无" });
}
}
private void UpdateField(string field, string value)
{
if (field == "jgjType")
{
if (NameConstant.SpecialJgjType.Contains(value))
{
docType = "加工件";
DocTypeEnabled = false;
}
else
{
DocTypeEnabled = true;
}
return;
}
}
}
}