using log4net;
using Microsoft.Win32;
using NPOI.SS.Formula.PTG;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using PdmSwPlugin.Commmon.Control;
using PdmSwPlugin.Commmon.Util.UI;
using PdmSwPlugin.Common;
using PdmSwPlugin.Common.Constants;
using PdmSwPlugin.Common.Control.TreeGrid;
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 PdmSwPlugin.Common.Util.UI;
using PdmSwPlugin.PDM.Constant;
using PdmSwPlugin.PDM.Model;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interop;
using static PdmSwPlugin.PDM.Event.PdmEvent;
namespace PdmSwPlugin.PDM
{
///
/// MainWindow.xaml 的交互逻辑
///
[PdmSwPlugin(Title = "PDM")]
public partial class PdmControl : UserControl, IActiveDocChangeHandler
{
private static ILog Logger = LogManager.GetLogger("PDM");
SldWorks SwApp;
private Model.PdmControlModel model;
private readonly HttpClient Client;
private string CurrentUserName;
private readonly int MaxThreads;
private ModelDoc2 OpenDoc { get; set; }
private HttpClientCreator clientCreator { get; set; }
private volatile PdmStatus workStatus;
private TaskFactory taskFactory = LimitedConcurrencyLevelTaskSchedulerMsn.Factory;
SldWorks ISwAppSetter.SwApp => SwApp;
public PdmControl() : this(null)
{
}
public PdmControl(SldWorks swAddin)
{
SwApp = swAddin;
InitializeComponent();
// 滚动事件
// RaiseWheelEvent();
model = new Model.PdmControlModel();
model.bomTreeReader.SingleCompleted += BomTreeReader_SingleCompleted;
DataContext = model;
clientCreator = new HttpClientCreator(new HttpConfig(
PluginSetting.Instance.TimeOut,
PluginSetting.Instance.BaseAddress));
Client = clientCreator.GetClient();
CurrentUserName = System.Environment.UserName;
PdmBom.InBomChanged += PdmBom_InBomChanged;
InitDemonThread();
}
private void PdmBom_InBomChanged(object sender, bool e)
{
//if (e)
//{
// MaskAdorner.ShowMask(content, "请稍后...");
//}
//else {
// MaskAdorner.HideMask(content);
//}
MaskAdorner.ShowMask(content, "请稍后...");
Task.Run(() =>
{
try
{
workStatus = PdmStatus.READING;
PdmBom bom = sender as PdmBom;
bom.UpdateInBom(e);
RefreshBomTree();
}
finally
{
MaskAdorner.HideMask(content);
workStatus = PdmStatus.FREE;
}
});
}
private void RefreshBomTree()
{
var cache = model.BomTree;
model.BomTree = new ObservableCollection();
model.BomTree = cache;
}
///
/// 加载BOM结构时,单个BOM完成事件
///
///
///
private void BomTreeReader_SingleCompleted(object sender, PdmBom e)
{
MaskAdorner.ShowMessage(content, e.d3FilePath);
}
///
/// 刷新BOM列表
/// 内部消化异常,不往外发
///
public ObservableCollection RefreshBomList(ModelDoc2 doc)
{
OpenDoc = doc;
try
{
if (doc == null)
{
this.Warning("请打开一张图纸");
return new ObservableCollection();
}
//if (7 == doc.GetBlockingState())
//{
// this.Warning("请等待文档加载完毕");
// return new ObservableCollection();
//}
DateTime beforeDT = DateTime.Now;
model.DoRefresh(Client, SwApp, doc);
DateTime now = DateTime.Now;
double times = DateTime.Now.Subtract(beforeDT).TotalMilliseconds;
return model.BomTree;
}
catch (Exception e)
{
// FillBomInfo(boms, bomInfos, drawInfos);
model.BomTree = new ObservableCollection();
Logger.Error($"V{PdmUser.LoginUser.pluginVersion}, Refresh Bom List Failed.", e);
this.Error($"V{PdmUser.LoginUser.pluginVersion},刷新失败:{e.Message}");
return new ObservableCollection();
}
}
public void AutoSaveAlert(List msgs)
{
//Dispatcher.Invoke(() =>
//{
// MultiExWindow exWin = new MultiExWindow(this, "以下文档因删除特定配置而被保存", msgs, "文件路径");
// ShowExWindow(exWin);
//});
}
///
/// 刷新按钮
///
///
///
private async void Refresh_Click(object sender, RoutedEventArgs e)
{
MaskAdorner.ShowMask(content, "请求中,请稍后...");
try
{
workStatus = PdmStatus.READING;
Logger.Debug("PDM插件,刷新列表...");
// !!! Task.Run中不能抛出异常,不然会中断程序
// 这里一定要用Task.Run,不然会阻塞UI线程,导致遮罩层显示不出来
// 垃圾WPF
await Task.Run(() =>
{
RefreshBomList(SwApp.IActiveDoc2);
if (model.bomTreeReader.needSaveList.Count > 0)
{
List msgs = new List(model.bomTreeReader.needSaveList);
AutoSaveAlert(msgs);
}
});
Logger.Debug("PDM插件,列表刷新完成!");
}
finally
{
workStatus = PdmStatus.FREE;
MaskAdorner.HideMask(content);
}
}
public void ValidateBoms(out string error)
{
error = null;
ObservableCollection boms = model.BomTree;
if (boms == null || boms.Count <= 0)
{
error = string.Empty;
}
List lostBoms = model.CacheList.Where(b => b.lost).Select(b => b.partModel).ToList();
if (lostBoms != null && lostBoms.Count > 0)
{
error = "[" + string.Join(",", lostBoms) + "]文件不存在!请检查";
}
}
///
/// 检出操作,同步方法,不关注校验,校验放外边
///
private object DoCheckOut()
{
ObservableCollection boms = model.BomTree;
string bomId = boms[0].id;
PdmBomParam param = new PdmBomParam
{
id = bomId,
checkUserName = CurrentUserName
};
Result