chr
2024-11-02 b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74
PdmSwPlugin.PDM/PdmControl.xaml.cs
@@ -39,6 +39,7 @@
using System.Windows.Input;
using NPOI.POIFS.Properties;
using NPOI.POIFS.Crypt;
using System.Threading.Tasks.Schedulers;
namespace PdmSwPlugin.PDM
{
@@ -144,11 +145,11 @@
                    this.Warning("请打开一张图纸");
                    return new ObservableCollection<PdmBom>();
                }
                if (7 == doc.GetBlockingState())
                {
                    this.Warning("请等待文档加载完毕");
                    return new ObservableCollection<PdmBom>();
                }
                //if (7 == doc.GetBlockingState())
                //{
                //    this.Warning("请等待文档加载完毕");
                //    return new ObservableCollection<PdmBom>();
                //}
                DateTime beforeDT = DateTime.Now;
@@ -896,7 +897,7 @@
        public Dictionary<string, DrawInfo> GetAllProperties(List<string> ErrMsgs)
        {
            Dictionary<string, DrawInfo> result = new Dictionary<string, DrawInfo>();
            ConcurrentDictionary<string, DrawInfo> result = new ConcurrentDictionary<string, DrawInfo>();
            PdmBom topBom = model.BomTree[0];
            CountdownEvent counter = new CountdownEvent(1);
            _ = ThreadPool.QueueUserWorkItem(
@@ -906,7 +907,65 @@
                );
            counter.Wait();
            counter.Dispose();
            return result;
            return new Dictionary<string, DrawInfo>(result);
        }
        /// <summary>
        /// 获取全部属性
        /// </summary>
        /// <param name="param"></param>
        public void GetAll(object param)
        {
            object[] datas = param as object[];
            PdmBom bom = (PdmBom)datas[0];
            ModelDoc2 sldDoc = bom.doc;
            Component2 component = bom.component;
            ConcurrentDictionary<string, DrawInfo> result = (ConcurrentDictionary<string, DrawInfo>)datas[1];
            CountdownEvent counter = (CountdownEvent)datas[2];
            List<string> ErrMsgs = (List<string>)datas[3];
            int level = (int)datas[4];
            try
            {
                counter.TryAddCount();
                string filepath = bom.partNo;
                if (result.ContainsKey(filepath))
                {
                    return;
                }
                if (!bom.skipCheck)
                {
                    result[filepath] = GetSingleDrawInfo(bom);
                }
                ObservableCollection<PdmBom> children = bom.modules;
                if (children != null)
                {
                    foreach (PdmBom child in children)
                    {
                        _ = ThreadPool.QueueUserWorkItem(
                              new WaitCallback(GetAll),
                              new object[] { child, result, counter, ErrMsgs, 1 });
                    }
                }
                MaskAdorner.ShowMessage(content, filepath);
            }
            catch (Exception e)
            {
                bom.drawInfo.checkRuleResult = 2;
                bom.drawInfo.errMessages = new List<string> { e.Message };
                Logger.Error($"V{PdmUser.LoginUser.pluginVersion},[{bom.filePath}] Read MASS Properies Failed.", e);
            }
            finally
            {
                counter.Signal();
                if (level == 0)
                {
                    counter.Signal();
                }
            }
        }
        /// <summary>
@@ -926,6 +985,7 @@
            Dictionary<string, string> allProperties = new Dictionary<string, string>();
            allProperties["物料型号"] = bom.partModel;
            DrawInfo drawInfo = bom._drawInfo;
            drawInfo.isHidden = bom.isHidden;
            drawInfo.allProperties = allProperties;
            allProperties["压缩"] = bom.suppressed.ToString();
            allProperties["轻量化"] = bom.lightWeight.ToString();
@@ -947,7 +1007,7 @@
            // 工程图一致性检查
            allProperties["工程图是否存在"] = drawExists.ToString();
            allProperties["md5"] = bom.localD3Md5;
            if (bom.lost || sldDoc == null)
            if (bom.lost || bom.BomInfo.isHidden || sldDoc == null)
            {
                allProperties["图纸是否存在"] = "false";
                return drawInfo;
@@ -1005,17 +1065,28 @@
            return drawInfo;
        }
        /// <summary>
        /// 获取全部属性
        /// </summary>
        /// <param name="param"></param>
        public void GetAll(object param)
        public Dictionary<string, DrawInfo> GetAllPropertiesByTask(List<string> ErrMsgs)
        {
            ConcurrentDictionary<string, DrawInfo> result = new ConcurrentDictionary<string, DrawInfo>();
            PdmBom topBom = model.BomTree[0];
            CountdownEvent counter = new CountdownEvent(1);
            Task.Run(() =>
            {
                GetAllByTask(new object[] {
                topBom, result, counter, ErrMsgs, 0 });
            });
            counter.Wait();
            counter.Dispose();
            return new Dictionary<string, DrawInfo>(result);
        }
        public void GetAllByTask(object param)
        {
            object[] datas = param as object[];
            PdmBom bom = (PdmBom)datas[0];
            ModelDoc2 sldDoc = bom.doc;
            Component2 component = bom.component;
            Dictionary<string, DrawInfo> result = (Dictionary<string, DrawInfo>)datas[1];
            ConcurrentDictionary<string, DrawInfo> result = (ConcurrentDictionary<string, DrawInfo>)datas[1];
            CountdownEvent counter = (CountdownEvent)datas[2];
            List<string> ErrMsgs = (List<string>)datas[3];
            int level = (int)datas[4];
@@ -1023,24 +1094,34 @@
            {
                counter.TryAddCount();
                string filepath = bom.partNo;
                if (result.ContainsKey(filepath))
                lock (result)
                {
                    return;
                    if (result.ContainsKey(filepath))
                    {
                        if (bom.isHidden)
                        {
                            result[filepath].isHidden = true;
                        }
                        return;
                    }
                    if (!bom.skipCheck)
                    {
                        result[filepath] = GetSingleDrawInfo(bom);
                    }
                }
                if (!bom.skipCheck)
                {
                    result[filepath] = GetSingleDrawInfo(bom);
                }
                ObservableCollection<PdmBom> children = bom.modules;
                if (children != null)
                {
                    foreach (PdmBom child in children)
                    {
                        _ = ThreadPool.QueueUserWorkItem(
                              new WaitCallback(GetAll),
                              new object[] { child, result, counter, ErrMsgs, 1 });
                        Task.Run(() =>
                        {
                            GetAllByTask(new object[] { child, result, counter, ErrMsgs, 1 });
                        });
                    }
                }
                MaskAdorner.ShowMessage(content, filepath);
@@ -1054,7 +1135,10 @@
            finally
            {
                counter.Signal();
                if (level == 0) counter.Signal();
                if (level == 0)
                {
                    counter.Signal();
                }
            }
        }
@@ -1096,78 +1180,127 @@
            return allSuccess;
        }
        public void sb(dynamic[] datas) {
            /*Dictionary<string, DrawInfo> properties = datas[0];
            string key = datas[1];
            KeyValuePair<string, DrawInfo> property = datas[2];
            List<string> temp = datas[3];
            CountdownEvent counter = datas[4];
            try
            {
                int iii, ref bool allSuccess
                Logger.Info($"{key} checking rule...");
                iii++;
                Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
                DrawInfo checkResult = result.HandleResult();
                properties[key].checkRuleResult = checkResult.checkRuleResult;
                properties[key].errMessages = checkResult.errMessages;
                var msgs = checkResult.errMessages;
                if (!properties[key].HistoryData && !checkResult.checkRuleOk)
                {
                    allSuccess = false;
                }
                if (!properties[key].HistoryData && msgs != null && msgs.Count > 0)
                {
                    lock (temp)
                    {
                        foreach (string msg in msgs)
                        {
                            temp.Add($"物料【{key}】未通过规则检查!{msg}");
                        }
                    }
                }
                MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
            }
            catch (Exception ex)
            {
                properties[key].checkRuleResult = 2;
                properties[key].errMessages = new List<string> { "发起检查失败,请重试" };
                lock (temp)
                {
                    temp.Add($"物料【{key}】发起检查失败,请重试");
                }
                allSuccess = false;
                MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
            }
            finally
            {
                counter.Signal();
            }*/
        }
        public bool DoCheckRuleAsync(out List<string> messages)
        {
            var temp = new List<string>();
            bool allSuccess = true;
            Dictionary<string, DrawInfo> properties = GetAllProperties(temp);
            Dictionary<string, DrawInfo> properties = GetAllPropertiesByTask(temp);
            MaskAdorner.ShowMessage(content, "正在请求数据...");
            var factory = LimitedConcurrencyLevelTaskScheduler.Factory;
           // var factory = LimitedConcurrencyLevelTaskScheduler.Factory;
            CountdownEvent counter = new CountdownEvent(properties.Count);
            int iii = 1;
            int iii = 0;
            foreach (KeyValuePair<string, DrawInfo> property in properties)
            {
                string key = property.Key;
                _ = ThreadPool.QueueUserWorkItem(
                (WaitCallback)delegate
                /// 这个方法最快,但不好管控,因为不知道属性字典有多大
                Task.Run(() =>
                {
                    string key = property.Key;
                    iii++;
                    try
                    {
                        Logger.Error($"{key} checking rule...");
                        iii++;
                        Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
                        DrawInfo checkResult = result.HandleResult();
                        properties[key].checkRuleResult = checkResult.checkRuleResult;
                        properties[key].errMessages = checkResult.errMessages;
                        var msgs = checkResult.errMessages;
                        if (!properties[key].HistoryData && (!checkResult.checkRuleOk||property.Value.isHidden))
                        {
                            allSuccess = false;
                        }
                        if (!properties[key].HistoryData)
                        {
                            lock (temp)
                            {
                                if (property.Value.isHidden)
                                {
                                    temp.Add($"物料【{key}】被设置为隐藏!");
                                }
                                if (msgs != null && msgs.Count > 0)
                                {
                                    foreach (string msg in msgs)
                                    {
                                        temp.Add($"物料【{key}】未通过规则检查!{msg}");
                                    }
                                }
                            }
                        }
                        MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
                    }
                    catch (Exception ex)
                    {
                        properties[key].checkRuleResult = 2;
                        properties[key].errMessages = new List<string> { "发起检查失败,请重试" };
                        lock (temp)
                        {
                            temp.Add($"物料【{key}】发起检查失败,请重试");
                        }
                        allSuccess = false;
                        MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
                    }
                    finally
                    {
                        counter.Signal();
                    }
                });
                //LimitedConcurrencyLevelTaskScheduler scheduler = new LimitedConcurrencyLevelTaskScheduler(int.MaxValue);
                //Task.Factory.StartNew(() => {
                //    string key = property.Key;
                //    iii++;
                //    try
                //    {
                //        Logger.Error($"{key} checking rule...");
                //        Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
                //        DrawInfo checkResult = result.HandleResult();
                //        properties[key].checkRuleResult = checkResult.checkRuleResult;
                //        properties[key].errMessages = checkResult.errMessages;
                //        var msgs = checkResult.errMessages;
                //        if (!properties[key].HistoryData && !checkResult.checkRuleOk)
                //        {
                //            allSuccess = false;
                //        }
                //        if (!properties[key].HistoryData && msgs != null && msgs.Count > 0)
                //        {
                //            lock (temp)
                //            {
                //                foreach (string msg in msgs)
                //                {
                //                    temp.Add($"物料【{key}】未通过规则检查!{msg}");
                //                }
                //            }
                //        }
                //        MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
                //    }
                //    catch (Exception ex)
                //    {
                //        properties[key].checkRuleResult = 2;
                //        properties[key].errMessages = new List<string> { "发起检查失败,请重试" };
                //        lock (temp)
                //        {
                //            temp.Add($"物料【{key}】发起检查失败,请重试");
                //        }
                //        allSuccess = false;
                //        MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
                //    }
                //    finally
                //    {
                //        counter.Signal();
                //    }
                //}, CancellationToken.None, TaskCreationOptions.None, scheduler);
                /// 下面两个都在管控Task数量,但还是很慢
                /*string key = property.Key;
                _ = ThreadPool.QueueUserWorkItem(
                (WaitCallback)delegate
                {
                    iii++;
                    try
                    {
                        Logger.Error($"{key} checking rule...");
                        Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
                        DrawInfo checkResult = result.HandleResult();
                        properties[key].checkRuleResult = checkResult.checkRuleResult;
@@ -1205,12 +1338,53 @@
                    {
                        counter.Signal();
                    }
                });
                });*/
                /* factory.StartNew(() =>
                 {
                //factory.StartNew(() =>
                //{
                //    string key = property.Key;
                //    iii++;
                //    try
                //    {
                //        Logger.Error($"{key} checking rule...");
                //        Result<DrawInfo> result = Client.PostSyncAction<DrawInfo>(property.Value, "wpf/bom/openApi/checkRuleSingle");
                //        DrawInfo checkResult = result.HandleResult();
                //        properties[key].checkRuleResult = checkResult.checkRuleResult;
                //        properties[key].errMessages = checkResult.errMessages;
                //        var msgs = checkResult.errMessages;
                //        if (!properties[key].HistoryData && !checkResult.checkRuleOk)
                //        {
                //            allSuccess = false;
                //        }
                 });*/
                //        if (!properties[key].HistoryData && msgs != null && msgs.Count > 0)
                //        {
                //            lock (temp)
                //            {
                //                foreach (string msg in msgs)
                //                {
                //                    temp.Add($"物料【{key}】未通过规则检查!{msg}");
                //                }
                //            }
                //        }
                //        MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
                //    }
                //    catch (Exception ex)
                //    {
                //        properties[key].checkRuleResult = 2;
                //        properties[key].errMessages = new List<string> { "发起检查失败,请重试" };
                //        lock (temp)
                //        {
                //            temp.Add($"物料【{key}】发起检查失败,请重试");
                //        }
                //        allSuccess = false;
                //        MaskAdorner.ShowMessage(content, $"物料【{key}】检查完成");
                //    }
                //    finally
                //    {
                //        counter.Signal();
                //    }
                //});
            }
            counter.Wait();
            counter.Dispose();
@@ -1245,7 +1419,7 @@
                try
                {
                    RefreshBomList(SwApp.IActiveDoc2);
                    if (!DoCheckRule(out List<string> msgs))
                    if (!DoCheckRuleAsync(out List<string> msgs))
                    {
                        Dispatcher.Invoke(() =>
                        {
@@ -1615,13 +1789,19 @@
                    return;
                }
                if (bom.checkRuleOk)
                if (bom.checkRuleOk && !bom.isHidden)
                {
                    this.Show("规则检查通过");
                }
                else
                {
                    MultiExWindow exWin = new MultiExWindow(this, "检查结果", bom.drawInfo.errMessages);
                    List<string> errs = new List<string>();
                    if (bom.isHidden)
                    {
                        errs.Add("文档被设置为隐藏");
                    }
                    errs.AddRange(bom.drawInfo.errMessages);
                    MultiExWindow exWin = new MultiExWindow(this, "检查结果", errs);
                    exWin.ShowDialog();
                }
            }
@@ -1817,7 +1997,7 @@
                        {
                            throw new CantCheckInException(new List<string> { "请打开一张图纸" });
                        }
                        DoCheckRule(out _);
                        DoCheckRuleAsync(out _);
                        // 筛选所有在物料明细表中,不跳过检查并且检查未通过的,类型是加工件的未通过审核的,加工件审核通过的但图纸变更的
                        List<string> errs = model.bomTreeReader.CacheList.Select(b => b.NeedStopCheckIn())
                        .Where(s => s != null).ToList();