using log4net;
|
using PdmSwPlugin.Commmon.Control;
|
using PdmSwPlugin.Commmon.Util.UI;
|
using PdmSwPlugin.Common;
|
using PdmSwPlugin.Common.Entity.DrawAudit;
|
using PdmSwPlugin.Common.Entity.Pdm;
|
using PdmSwPlugin.Common.Entity.System;
|
using PdmSwPlugin.Common.Interface;
|
using PdmSwPlugin.Common.Setting;
|
using PdmSwPlugin.Common.Util;
|
using PdmSwPlugin.Common.Util.Http;
|
using SolidWorks.Interop.sldworks;
|
using SolidWorks.Interop.swconst;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
using System.IO;
|
using System.Linq;
|
using System.Net.Http;
|
using System.Runtime.CompilerServices;
|
using System.Threading.Tasks;
|
using System.Windows;
|
using System.Windows.Controls;
|
using System.Windows.Markup;
|
|
namespace PdmSwPlugin.DrawApprove
|
{
|
/// <summary>
|
/// UserControl1.xaml 的交互逻辑
|
/// </summary>
|
[PdmSwPlugin(Title = "送审记录")]
|
public partial class DrawAuditHisControl : UserControl, ISwAppSetter, INotifyPropertyChanged
|
{
|
#region ...
|
public virtual event PropertyChangedEventHandler PropertyChanged;
|
|
public virtual void RaisePropertyChanged(string name)
|
{
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
}
|
|
public void RaiseAndSetIfChanged<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
|
{
|
old = @new;
|
if (propertyName != null)
|
{
|
RaisePropertyChanged(propertyName);
|
}
|
}
|
#endregion
|
|
private static ILog Logger = LogManager.GetLogger("DrawAuditHis");
|
|
SldWorks SwApp;
|
|
SldWorks ISwAppSetter.SwApp => SwApp;
|
|
private readonly HttpClient Client;
|
private HttpClientCreator clientCreator { get; set; }
|
|
/// <summary>
|
/// 全部BOM树状结构
|
/// </summary>
|
private ObservableCollection<DrawAudit> _dataSource = new ObservableCollection<DrawAudit>();
|
public ObservableCollection<DrawAudit> dataSource
|
{
|
get => _dataSource;
|
set
|
{
|
RaiseAndSetIfChanged(ref _dataSource, value);
|
if (value == null)
|
{
|
StatusBarText = $"共 0 条";
|
}
|
else
|
{
|
StatusBarText = $"共 {value.Count} 条";
|
}
|
RefreshSelectedCount();
|
}
|
}
|
|
private string _SelectBomText = "已选中 0 条";
|
public string SelectBomText
|
{
|
get => _SelectBomText;
|
set => RaiseAndSetIfChanged(ref _SelectBomText, value);
|
}
|
|
private string _StatusBarText = "共 0 条";
|
|
public string StatusBarText
|
{
|
get { return _StatusBarText; }
|
set => RaiseAndSetIfChanged(ref _StatusBarText, value);
|
}
|
|
|
|
public DrawAuditHisControl() : this(null)
|
{
|
|
}
|
|
public DrawAuditHisControl(SldWorks swAddin)
|
{
|
SwApp = swAddin;
|
InitializeComponent();
|
clientCreator = new HttpClientCreator(new HttpConfig(PluginSetting.Instance.BaseAddress));
|
Client = clientCreator.GetClient();
|
DataContext = this;
|
}
|
|
public void SetSwApp(SldWorks SwApp)
|
{
|
this.SwApp = SwApp;
|
}
|
|
/// <summary>
|
/// 全选事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void GlobalCheckBox_Checked(object sender, RoutedEventArgs e)
|
{
|
CheckBox cb = sender as CheckBox;
|
List<DrawAudit> datas = dataSource.ToList();
|
foreach (DrawAudit data in datas)
|
{
|
data.selected = cb.IsChecked.Value;
|
}
|
int count = cb.IsChecked.Value ? (dataSource == null ? 0 : dataSource.Count) : 0;
|
SelectBomText = $"已选中 {count} 条";
|
}
|
|
private void RefreshSelectedCount()
|
{
|
if (dataSource == null || dataSource.Count <= 0)
|
{
|
SelectBomText = "已选中 0 条";
|
return;
|
}
|
int count = dataSource.Where(e => e.selected).Count();
|
SelectBomText = $"已选中 {count} 条";
|
}
|
|
/// <summary>
|
/// 单个点选事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void SingleCheckBox_Event(object sender, RoutedEventArgs e)
|
{
|
RefreshSelectedCount();
|
}
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
{
|
MaskAdorner.ShowMask(content, "请求中,请稍后...");
|
Task.Run(() =>
|
{
|
try
|
{
|
List<DrawAudit> datas = RefreshList();
|
Dispatcher.Invoke(() =>
|
{
|
dataSource = new ObservableCollection<DrawAudit>(datas);
|
});
|
}
|
catch (Exception ex)
|
{
|
Logger.Error("UI update failed.", ex);
|
this.Error($"UI更新列表失败!异常:{ex}");
|
dataSource = new ObservableCollection<DrawAudit>();
|
}
|
finally
|
{
|
MaskAdorner.HideMask(content);
|
}
|
});
|
}
|
|
private void OpenDocMayError(string filePath, int docType, int option, string config)
|
{
|
int err = 0, warn = 0;
|
SwApp.OpenDoc6(filePath, docType, option, config, ref err, ref warn);
|
if (err == (int)swFileLoadError_e.swAddinInteruptError)
|
{
|
this.Error("打开失败!用户尝试打开文件,然后中断打开文件例程以打开其他文件");
|
}
|
else if (err == (int)swFileLoadError_e.swApplicationBusy)
|
{
|
this.Error("打开失败!Solidworks繁忙");
|
}
|
else if (err == (int)swFileLoadError_e.swFileCriticalDataRepairError)
|
{
|
this.Error("打开失败!文档存在严重数据损坏");
|
}
|
else if (err == (int)swFileLoadError_e.swFileNotFoundError)
|
{
|
this.Error("打开失败!无法找到文件;未加载文件或禁止显示引用的文件(即组件)");
|
}
|
else if (err == (int)swFileLoadError_e.swFileRequiresRepairError)
|
{
|
this.Error("打开失败!文档具有非关键自定义属性数据损坏");
|
}
|
else if (err == (int)swFileLoadError_e.swFutureVersion)
|
{
|
this.Error("打开失败!文档已保存在 SOLIDWORKS 的未来版本中");
|
}
|
else if (err == (int)swFileLoadError_e.swInvalidFileTypeError)
|
{
|
this.Error("打开失败!文件类型参数无效");
|
}
|
else if (err == (int)swFileLoadError_e.swLiquidMachineDoc)
|
{
|
this.Error("打开失败!由 Liquid Machines 加密的文件");
|
}
|
else if (err == (int)swFileLoadError_e.swLowResourcesError)
|
{
|
this.Error("打开失败!文件被打开并被阻止,因为系统内存不足,或者 GDI 句柄数已超过允许的最大值");
|
}
|
else if (err == (int)swFileLoadError_e.swNoDisplayData)
|
{
|
this.Error("打开失败!文件不包含显示数据");
|
}
|
else if (err == (int)swFileLoadError_e.swFileWithSameTitleAlreadyOpen)
|
{
|
this.Error("打开失败!具有相同名称的文档已打开");
|
//SwApp.ActivateDoc3(filePath, false, 0, ref err);
|
//if (err == 2)
|
//{
|
// this.Error("需要重新生成已激活的文档");
|
//}
|
//else if (err == 1)
|
//{
|
// this.Error("遇到不明错误,并且未激活文档");
|
//}
|
}
|
else if (err != 0)
|
{
|
Logger.Error($"OpenDoc6 Failed,Error:{err}.See Solidworks Doc.");
|
}
|
else if (warn == 128)
|
{
|
SwApp.ActivateDoc3(filePath, false, 0, ref err);
|
if (err == 2)
|
{
|
this.Error("需要重新生成已激活的文档");
|
}
|
else if (err == 1)
|
{
|
this.Error("遇到不明错误,并且未激活文档");
|
}
|
}
|
}
|
|
private void Detail_Click(object sender, RoutedEventArgs e)
|
{
|
Button btn = sender as Button;
|
DrawAudit da = btn.DataContext as DrawAudit;
|
GetAuditHisDetailAndOpen(da);
|
}
|
|
private void Reback_Click(object sender, RoutedEventArgs e)
|
{
|
Button btn = sender as Button;
|
DrawAudit da = btn.DataContext as DrawAudit;
|
MessageBoxResult mr = MessageBox.Show($"确定撤销物料【{da.materialCode}】的图纸审批吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
|
if (mr == MessageBoxResult.OK)
|
{
|
MaskAdorner.ShowMask(content, "请求中,请稍后...");
|
Task.Run(() =>
|
{
|
try
|
{
|
Result<List<DrawAuditHis>> res = Client.PostSyncAction<List<DrawAuditHis>>(new DrawAudit
|
{
|
id = da.id,
|
requestUserId = PdmUser.LoginUser.id
|
}, "drawAudit/back");
|
res.HandleResult();
|
Dispatcher.Invoke(() =>
|
{
|
dataSource = new ObservableCollection<DrawAudit>(RefreshList());
|
});
|
}
|
catch (Exception ex)
|
{
|
Logger.Error("Reback Draw Audit Failed.", ex);
|
this.Error($"撤销申请失败!{ex.Message}");
|
}
|
finally
|
{
|
MaskAdorner.HideMask(content);
|
}
|
});
|
}
|
}
|
|
private string GetRealFilePath(string dbPath)
|
{
|
string fileName = Path.GetFileName(dbPath);
|
string filePath = Path.Combine(PluginSetting.Instance.SwFilePath, fileName);
|
return filePath;
|
}
|
|
private void OpenDoc_Click(object sender, RoutedEventArgs e)
|
{
|
Button btn = sender as Button;
|
DrawAudit da = btn.DataContext as DrawAudit;
|
string filePath = GetRealFilePath(da.d3RelativePath);
|
if (!File.Exists(filePath))
|
{
|
this.Error($"服务器中未找到图纸文件,路径:{filePath}");
|
return;
|
}
|
OpenDocMayError(filePath, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_ReadOnly,
|
"");
|
}
|
|
public List<DrawAudit> RefreshList()
|
{
|
try
|
{
|
Result<List<DrawAudit>> res = Client.GetSyncAction<List<DrawAudit>>("drawAudit/listQuery", new DrawAudit
|
{
|
requestUserId = PdmUser.LoginUser.id
|
});
|
return res.HandleResult();
|
}
|
catch (Exception ex)
|
{
|
Logger.Error("Get draw audit data list failed.", ex);
|
this.Error($"刷新列表失败!异常:{ex.Message}");
|
return new List<DrawAudit>();
|
}
|
}
|
|
public void CloseAuditedDoc(DrawAudit da)
|
{
|
try
|
{
|
string modelPath = Path.Combine(PluginSetting.Instance.SwFilePath, Path.GetFileName(da.d3RelativePath));
|
string drawingPath = Path.Combine(PluginSetting.Instance.SwFilePath, Path.GetFileName(da.d2RelativePath));
|
SwApp.CloseDoc(modelPath);
|
SwApp.CloseDoc(drawingPath);
|
}
|
catch (Exception exx)
|
{
|
Logger.Error("Auto close open doc failed.", exx);
|
}
|
}
|
|
/// <summary>
|
/// 查询送审图纸的审核详情并在新窗口打开
|
/// </summary>
|
/// <param name="da"></param>
|
private void GetAuditHisDetailAndOpen(DrawAudit da)
|
{
|
MaskAdorner.ShowMask(content, "请求中,请稍后...");
|
Task.Run(() =>
|
{
|
try
|
{
|
Result<List<DrawAuditHis>> res = Client.GetSyncAction<List<DrawAuditHis>>("drawAudit/listTaskHis", new DrawAudit
|
{
|
id = da.id
|
});
|
var datas = res.HandleResult();
|
Dispatcher.Invoke(() =>
|
{
|
//DrawAuditHisWindow window = new DrawAuditHisWindow(this, $"【{bom.partModel}】审核详情", datas);
|
RichHisWindow window = new RichHisWindow(this, $"【{da.materialCode}】审核详情", datas);
|
|
window.ShowDialog();
|
});
|
}
|
catch (Exception ex)
|
{
|
Logger.Error("Get draw audit history failed.", ex);
|
this.Error($"获取审核历史失败!{ex.Message}");
|
}
|
finally
|
{
|
MaskAdorner.HideMask(content);
|
}
|
});
|
}
|
|
private void TextBlock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
|
{
|
// 检查是否是双击
|
if (e.ClickCount == 2)
|
{
|
TextBlock textBlock = sender as TextBlock;
|
DrawAudit data = textBlock.DataContext as DrawAudit;
|
GetAuditHisDetailAndOpen(data);
|
}
|
}
|
}
|
}
|