コード例 #1
0
        public void Init()
        {
            DefectInfos = new BindableCollection <DefectInfoViewModel>();
            DefectInfosCollectionView = DefectInfos.GetDefaultCollectionView();

            MeasurementInfos = new BindableCollection <MeasurementInfoViewModel>();
            MeasurementInfosCollectionView = MeasurementInfos.GetDefaultCollectionView();

            // Init SurfaceMonitors
            SurfaceMonitors = new List <SurfaceMonitorViewModel>();

            int surfaceCount = 0;

            surfaceCount = 2;

            for (int i = 0; i < surfaceCount; i++)
            {
                var sm = new SurfaceMonitorViewModel
                {
                    Index                      = i,
                    SurfaceTypeIndex           = i,
                    DisplayDefectInfo          = true,
                    DisplayAllDefectInfos      = false,
                    DisplayMeasurementInfo     = true,
                    DisplayAllMeasurementInfos = false,
                };
                SurfaceMonitors.Add(sm);
            }

            //
            SelectSurfaceMonitorCommand = new DelegateCommand <SurfaceMonitorViewModel>(
                o =>
            {
                if (SelectedSurfaceMonitor == o)
                {
                    return;
                }

                HideAll();

                SelectedSurfaceMonitor            = o;
                SelectedSurfaceMonitor.IsSelected = true;

                // Show DefectInfosCollectionView
                if (_displayAllDefectsOnImages)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllDefectInfos = false);
                    SelectedSurfaceMonitor.DisplayAllDefectInfos         = true;
                }

                DefectInfosCollectionView.Filter =
                    x =>
                {
                    var di = (DefectInfoViewModel)x;
                    return(di.SurfaceTypeIndex == SelectedSurfaceMonitor.SurfaceTypeIndex);
                };
                DefectInfosCollectionView.Refresh();

                // Show MeasurementInfosCollectionView
                if (_displayAllMeasurementInfosOnImages)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllMeasurementInfos = false);
                    SelectedSurfaceMonitor.DisplayAllMeasurementInfos         = true;
                }

                MeasurementInfosCollectionView.Filter =
                    x =>
                {
                    var di = (MeasurementInfoViewModel)x;
                    return(di.SurfaceTypeIndex == SelectedSurfaceMonitor.SurfaceTypeIndex);
                };
                MeasurementInfosCollectionView.Refresh();

                UpdateCommandStates();
            });

            SaveImageToFileCommand = new DelegateCommand(
                () =>
            {
                InspectionViewModelService.OpenSaveImageToFileDialog(SelectedSurfaceMonitor.BitmapSource,
                                                                     "SurfaceType_" + SelectedSurfaceMonitor.SurfaceTypeIndex);
            }, () => SelectedSurfaceMonitor != null);

            ShowAllMeasurementsCommand = new DelegateCommand(
                OnShowAllMeasurementsCommand);

            ShowGroupMeasurementsCommand = new DelegateCommand(
                OnShowGroupMeasurementsCommand);

            InspectImageFileCommand = new DelegateCommand(async() =>
            {
                int surfaceTypeIndex = SelectedSurfaceMonitor.SurfaceTypeIndex;

                var dialog = new OpenFileDialog()
                {
                    Title  = "Load image to surface " + surfaceTypeIndex,
                    Filter =
                        //                                     "BMP files (*.bmp)|*.bmp|" +
                        //                                              "TIFF files (*.tif)|*.tif|" +
                        //                                              "JPG files (*.jpg)|*.jpg|" +
                        "All files (*.*)|*.*",
                };
                var result = dialog.ShowDialog();

                if (!result.HasValue || !result.Value)
                {
                    return;
                }


                DefectInfos.Clear();
                MeasurementInfos.Clear();

                var fileName = dialog.FileName;
                await InspectService.InspectImageFileAsync(surfaceTypeIndex, fileName);
            }, () => SelectedSurfaceMonitor != null);

            StartCommand = new DelegateCommand(
                () => { InspectService.Start(); });

            StopCommand = new DelegateCommand(
                () => { InspectService.Reset(); });


            ZoomInCommand = new DelegateCommand(
                () =>
            {
                if (SelectedSurfaceMonitor != null)
                {
                    SelectedSurfaceMonitor.ZoomIn();
                }
                _isZoomFitDisplayAreaEnabled = false;
                _isZoomActualEnabled         = false;
            },
                () => SelectedSurfaceMonitor != null);

            ZoomOutCommand = new DelegateCommand(
                () =>
            {
                if (SelectedSurfaceMonitor != null)
                {
                    SelectedSurfaceMonitor.ZoomOut();
                }
                _isZoomFitDisplayAreaEnabled = false;
                _isZoomActualEnabled         = false;
            },
                () => SelectedSurfaceMonitor != null);

            ZoomFitCommand = new DelegateCommand(
                () =>
            {
                _isZoomFitDisplayAreaEnabled = !_isZoomFitDisplayAreaEnabled;
                if (_isZoomFitDisplayAreaEnabled)
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomFitDisplayArea();
                    }
                }
                else
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomFit();
                    }
                }
                _isZoomActualEnabled = false;
            },
                () => SelectedSurfaceMonitor != null);

            ZoomActualCommand = new DelegateCommand(
                () =>
            {
                _isZoomActualEnabled = !_isZoomActualEnabled;
                if (_isZoomActualEnabled)
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomActual();
                    }
                }
                else
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomFit();
                    }
                }

                _isZoomFitDisplayAreaEnabled = false;
            },
                () => SelectedSurfaceMonitor != null);

            // publish CommunicationInitializedEvent
            PrismEventAggregator
            .GetEvent <SplashFinishedEvent>()
            .Publish(new SplashFinishedEvent());

            InspectService.AcquisitionStartedEvent
            .ObserveOnDispatcher()
            .Subscribe(surfaceIndex =>
            {
                var surfaceMonitor              = SurfaceMonitors[surfaceIndex];
                surfaceMonitor.BitmapSource     = null;
                surfaceMonitor.DefectInfos      = null;
                surfaceMonitor.MeasurementInfos = null;
                surfaceMonitor.InspectState     = InspectState.Grabbing;
                Debug.WriteLine("InspectState.Grabbing");
                //                               surfaceMonitor.MeasurementInfos.Clear();
                //                               surfaceMonitor.DefectInfos.Clear();

                UpdateCommandStates();
            });

            InspectService.AcquisitionCompletedEvent
            .ObserveOnDispatcher()
            .Subscribe(imageInfo =>
            {
                var surfaceMonitor              = SurfaceMonitors[imageInfo.SurfaceTypeIndex];
                surfaceMonitor.BitmapSource     = null;
                surfaceMonitor.DefectInfos      = null;
                surfaceMonitor.MeasurementInfos = null;
                surfaceMonitor.InspectState     = InspectState.Grabbed;
                Debug.WriteLine("InspectState.Grabbed");

                UpdateCommandStates();
            });

            InspectService.CalibrationStartedEvent
            .ObserveOnDispatcher()
            .Subscribe(async surfaceIndex =>
            {
                var surfaceMonitor              = SurfaceMonitors[surfaceIndex];
                surfaceMonitor.BitmapSource     = null;
                surfaceMonitor.DefectInfos      = null;
                surfaceMonitor.MeasurementInfos = null;
                surfaceMonitor.InspectState     = InspectState.Calibrating;
                Debug.WriteLine("InspectState.Calibrating");

                UpdateCommandStates();
            });

            InspectService.CalibrationCompletedEvent
            .ObserveOnDispatcher()
            .Subscribe(async imageInfo =>
            {
                var surfaceMonitor          = SurfaceMonitors[imageInfo.SurfaceTypeIndex];
                surfaceMonitor.InspectState = InspectState.Calibrated;
                Debug.WriteLine("InspectState.Calibrated");

                var bs = await imageInfo.ToBitmapSourceAsync();
                surfaceMonitor.BitmapSource = bs;

                UpdateCommandStates();
            });

            InspectService.InspectionStartedEvent
            .ObserveOnDispatcher()
            .Subscribe(x =>
            {
                var surfaceMonitor          = SurfaceMonitors[x];
                surfaceMonitor.InspectState = InspectState.Inspecting;
                Debug.WriteLine("InspectState.Inspecting");

                UpdateCommandStates();
            });

            InspectService.InspectionCompletedEvent
            .ObserveOnDispatcher()
            .Subscribe(inspectInfo =>
            {
                DefectInfos.Clear();
                MeasurementInfos.Clear();

                var surfaceMonitor          = SurfaceMonitors[inspectInfo.SurfaceTypeIndex];
                surfaceMonitor.InspectState = inspectInfo.InspectInfo.DefectInfos.Count == 0
                                   ? InspectState.InspectedWithAccepted
                                   : InspectState.InspectedWithRejected;

                var iiVm = inspectInfo.InspectInfo.ToViewModel();

                surfaceMonitor.DefectInfos      = iiVm.DefectInfos.OrderBy(x => x.GroupName).ToList();
                surfaceMonitor.MeasurementInfos = iiVm.MeasurementInfos.OrderBy(x => x.GroupName).ToList();

                DefectInfos.AddRange(iiVm.DefectInfos);
                MeasurementInfos.AddRange(iiVm.MeasurementInfos);

                UpdateCommandStates();
            });

            SwitchDisplayAllDefectInfosCommand = new DelegateCommand(
                () =>
            {
                HideAll();

                DefectInfosCollectionView.Filter = null;
                DefectInfosCollectionView.Refresh();

                if (_displayAllDefectsOnImages)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllDefectInfos = true);
                }

                UpdateCommandStates();
            });

            SwitchDisplayAllDefectInfosOnImagesCommand = new DelegateCommand(
                () =>
            {
                _displayAllDefectsOnImages = !_displayAllDefectsOnImages;

                if (!_displayAllDefectsOnImages)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllDefectInfos = false);
                    return;
                }

                if (SelectedSurfaceMonitor == null)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllDefectInfos = true);
                }
                else
                {
                    SelectedSurfaceMonitor.DisplayAllDefectInfos = true;
                }

                UpdateCommandStates();
            });


            SwitchDisplayAllMeasurementInfosCommand = new DelegateCommand(
                () =>
            {
                HideAll();

                MeasurementInfosCollectionView.Filter = null;
                MeasurementInfosCollectionView.Refresh();

                if (_displayAllMeasurementInfosOnImages)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllMeasurementInfos = true);
                }

                UpdateCommandStates();
            });

            SwitchDisplayAllMeasurementInfosOnImagesCommand = new DelegateCommand(
                () =>
            {
                _displayAllMeasurementInfosOnImages = !_displayAllMeasurementInfosOnImages;

                if (!_displayAllMeasurementInfosOnImages)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllMeasurementInfos = false);
                    return;
                }

                if (SelectedSurfaceMonitor == null)
                {
                    SurfaceMonitors.ForEach(x => x.DisplayAllMeasurementInfos = true);
                }
                else
                {
                    SelectedSurfaceMonitor.DisplayAllMeasurementInfos = true;
                }

                UpdateCommandStates();
            });
        }
コード例 #2
0
        public void Init()
        {
            DefectInfosCollectionView = DefectInfos.GetDefaultCollectionView();

            // Init SurfaceMonitors
            SurfaceMonitors = new List <SurfaceMonitorViewModel>();

            int surfaceCount = 0;

            surfaceCount = 2;

            for (int i = 0; i < surfaceCount; i++)
            {
                var sm = new SurfaceMonitorViewModel
                {
                    Index                      = i,
                    SurfaceTypeIndex           = i,
                    DisplayDefectInfo          = true,
                    DisplayAllDefectInfos      = false,
                    DisplayMeasurementInfo     = true,
                    DisplayAllMeasurementInfos = false,
                };
                SurfaceMonitors.Add(sm);
            }

            //

            CreateWorkpieceInfoCommand = new DelegateCommand(
                () =>
            {
                var dialog = new OpenFileDialog()
                {
                    Multiselect = false,
                    Title       = "Select Image File",
                };

                var ret = dialog.ShowDialog();

                if (ret != true)
                {
                    return;
                }

                var fn = dialog.FileName;

                var di = ServiceLocator.GetInstance <WorkpieceInfo>();
                //di.StoreImage(fn);
                di.InspectDateTime = DateTime.Now;
                di.IsReject        = true;

                InspectionDomainService.AddWorkpieceInfo(di);
            });

            DeleteWorkpieceInfoCommand = new DelegateCommand <WorkpieceInfoEntryViewModel>(
                (di) =>
            {
                if (di == null)
                {
                    MessageBox.Show("Please select a WorkpieceInfo!");
                    return;
                }

                InspectionDomainService.DeleteWorkpieceInfo(di.Id);
            },
                (x) => { return(SelectedWorkpieceInfo != null); });

            SelectWorkpieceInfoCommand = new DelegateCommand <WorkpieceInfoEntryViewModel>(
                (workpieceInfoEntry) =>
            {
                //           var deferRefresh=  DefectInfosCollectionView.DeferRefresh();
                DefectInfosCollectionView.Filter = null;
                _defectInfos.Clear();

                foreach (var surfaceMonitor in SurfaceMonitors)
                {
                    surfaceMonitor.Reset();
                }

                HideAll();

                UpdateCommandStates();

                if (workpieceInfoEntry == null)
                {
                    return;
                }

                var id            = workpieceInfoEntry.Id;
                var workpieceInfo = InspectionDomainService.GetWorkpieceInfoById(id);
                var defVms        = workpieceInfo.DefectInfos.Select(x => x.ToViewModel());
                _defectInfos.AddRange(defVms);

                for (int i = 0; i < workpieceInfo.StoredImageInfo.Count; i++)
                {
                    var sii = workpieceInfo.StoredImageInfo[i];
                    var bs  = sii.LoadImage();

                    var surfaceMonitor          = SurfaceMonitors[sii.SurfaceTypeIndex];
                    surfaceMonitor.BitmapSource = bs;

                    var ds = _defectInfos.Where(x => x.SurfaceTypeIndex == surfaceMonitor.SurfaceTypeIndex).ToList();

                    if (ds.IsEmpty())
                    {
                        surfaceMonitor.DefectInfos  = null;
                        surfaceMonitor.InspectState = InspectState.InspectedWithAccepted;
                    }
                    else
                    {
                        surfaceMonitor.DefectInfos  = ds;
                        surfaceMonitor.InspectState = InspectState.InspectedWithRejected;
                    }
                }

                //            deferRefresh.Dispose();
                DefectInfosCollectionView.Filter = null;
                DefectInfosCollectionView.Refresh();

                UpdateCommandStates();
            });

            EventAggregator
            .GetEvents <WorkpieceInfoAddedDomainEvent>()
            .Subscribe(evt =>
            {
                var entryVm = evt.WorkpieceInfo.ToEntry().ToViewModel();
                WorkpieceInfoEntries.Add(entryVm);

                UpdateCommandStates();
            });

            EventAggregator
            .GetEvents <WorkpieceInfoRemovedDomainEvent>()
            .Subscribe(evt =>
            {
                var s = WorkpieceInfoEntries.SingleOrDefault(x => x.Id == evt.Id);
                if (s != null)
                {
                    WorkpieceInfoEntries.Remove(s);
                }

                UpdateCommandStates();
            });

            CreateDefectInfoCommand = new DelegateCommand(
                () =>
            {
                if (SelectedWorkpieceInfo == null)
                {
                }
                else
                {
                    var index      = DefectInfos.Count;
                    var defectInfo = new DefectInfo {
                        Width = 200, Height = 200, X = index * 100, Y = index * 100
                    };

                    InspectionDomainService.AddDefectInfo(SelectedWorkpieceInfo.Id, defectInfo);
                }
            },
                () => { return(SelectedWorkpieceInfo != null); });

            DeleteDefectInfoCommand = new DelegateCommand <DefectInfoViewModel>(
                (di) =>
            {
                if (SelectedWorkpieceInfo == null)
                {
                    return;
                }

                if (SelectedDefectInfo == null)
                {
                    return;
                }

                InspectionDomainService.DeleteDefectInfo(SelectedWorkpieceInfo.Id, SelectedDefectInfo.Id);
            },
                (x) => { return(SelectedDefectInfo != null); });



            CleanOldWorkpieceInfosCommand = new DelegateCommand(OnCleanOldWorkpieceInfosCommand);

            SelectDefectInfoCommand = new DelegateCommand <DefectInfoViewModel>(
                (di) =>
            {
                if (di != null)
                {
                    CroppedRegionRect = new Rect(di.X, di.Y, di.Width, di.Height);
                }

                UpdateCommandStates();
            });

            CreateMonithReportCommand = new DelegateCommand(
                () =>
            {
                var report = ReportingDomainService.GetMonthReport(SelectedMonthReportDateTime.Year,
                                                                   SelectedMonthReportDateTime.Month);

                if (report == null)
                {
                    MessageDialogService.Show("没有数据,无法生成报表");
                    return;
                }

                PreviewReportingDialogService
                .Show(report)
                .Subscribe(args =>
                {
                    if (args.IsCanceled)
                    {
                        return;
                    }

                    ReportingDomainService.ExportReport(args.Data);
                });
            });

            QueryMonthRecordsCommand = new DelegateCommand(
                () =>
            {
                var dis = ReportingDomainService.GetWorkpieceInfoEntriesByMonth(SelectedMonthReportDateTime.Year,
                                                                                SelectedMonthReportDateTime.Month);
                WorkpieceInfoEntries.Clear();
                WorkpieceInfoEntries.AddRange(dis.Select(x => x.ToViewModel()));
            });

            CreateDayReportCommand = new DelegateCommand(
                () =>
            {
                var report = ReportingDomainService.GetDayReport(SelectedDayReportDateTime.Year,
                                                                 SelectedDayReportDateTime.Month,
                                                                 SelectedDayReportDateTime.Day);

                if (report == null)
                {
                    MessageDialogService.Show("没有数据,无法生成报表");
                    return;
                }

                PreviewReportingDialogService
                .Show(report)
                .Subscribe(args =>
                {
                    if (args.IsCanceled)
                    {
                        return;
                    }

                    ReportingDomainService.ExportReport(args.Data);
                });
            });

            QueryDayRecordsCommand = new DelegateCommand(
                () =>
            {
                var dis = ReportingDomainService.GetWorkpieceInfoEntriesByDay(SelectedDayReportDateTime.Year,
                                                                              SelectedDayReportDateTime.Month,
                                                                              SelectedDayReportDateTime.Day);
                WorkpieceInfoEntries.Clear();
                WorkpieceInfoEntries.AddRange(dis.Select(x => x.ToViewModel()));
            });


            ZoomInCommand = new DelegateCommand(
                () =>
            {
                if (SelectedSurfaceMonitor != null)
                {
                    SelectedSurfaceMonitor.ZoomIn();
                }
                _isZoomFitDisplayAreaEnabled = false;
                _isZoomActualEnabled         = false;
            },
                () => SelectedSurfaceMonitor != null);

            ZoomOutCommand = new DelegateCommand(
                () =>
            {
                if (SelectedSurfaceMonitor != null)
                {
                    SelectedSurfaceMonitor.ZoomOut();
                }
                _isZoomFitDisplayAreaEnabled = false;
                _isZoomActualEnabled         = false;
            },
                () => SelectedSurfaceMonitor != null);

            ZoomFitCommand = new DelegateCommand(
                () =>
            {
                _isZoomFitDisplayAreaEnabled = !_isZoomFitDisplayAreaEnabled;
                if (_isZoomFitDisplayAreaEnabled)
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomFitDisplayArea();
                    }
                }
                else
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomFit();
                    }
                }
                _isZoomActualEnabled = false;
            },
                () => SelectedSurfaceMonitor != null);

            ZoomActualCommand = new DelegateCommand(
                () =>
            {
                _isZoomActualEnabled = !_isZoomActualEnabled;
                if (_isZoomActualEnabled)
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomActual();
                    }
                }
                else
                {
                    if (SelectedSurfaceMonitor != null)
                    {
                        SelectedSurfaceMonitor.ZoomFit();
                    }
                }

                _isZoomFitDisplayAreaEnabled = false;
            },
                () => SelectedSurfaceMonitor != null);



            SaveImageToFileCommand = new DelegateCommand(
                OnSaveImageToFileCommand,
                () => SelectedSurfaceMonitor != null);

            EventAggregator
            .GetEvents <DefectInfoAddedDomainEvent>()
            .Subscribe(evt =>
            {
                if (SelectedWorkpieceInfo == null)
                {
                    return;
                }

                if (SelectedWorkpieceInfo.Id != evt.WorkpieceInfoId)
                {
                    return;
                }

                _defectInfos.Add(evt.DefectInfo.ToViewModel());

                UpdateCommandStates();
            });


            EventAggregator
            .GetEvents <DefectInfoRemovedDomainEvent>()
            .Subscribe(evt =>
            {
                if (SelectedDefectInfo != null && SelectedDefectInfo.Id == evt.DefectInfoId)
                {
                    SelectedDefectInfo = null;
                }

                var di = _defectInfos.SingleOrDefault(x => x.Id == evt.DefectInfoId);
                if (di != null)
                {
                    _defectInfos.Remove(di);
                }

                UpdateCommandStates();
            });

            EventAggregator.GetEvents <ReportExportFailedEvent>()
            .Subscribe(evt => { MessageBox.Show("导出失败!" + "\n\n" + evt.Exception.Message); });

            EventAggregator.GetEvents <ReportExportSuccessfulEvent>()
            .Subscribe(evt => { MessageBox.Show("导出成功!" + "\n\n" + evt.FileName); });


            var thisMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            MonthReportDateTimes.Add(thisMonth);
            for (int i = 0; i < 5; i++)
            {
                MonthReportDateTimes.Add(thisMonth.AddMonths(-i - 1));
            }

            SelectedMonthReportDateTime        = thisMonth;
            SelectedDayReportYearMonthDateTime = thisMonth;
        }