SDC C-Project CF Review 프로그램
LYW
2021-09-23 c77236e1522caa3e2082dd7b5b32f6c30b125172
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
#include "pch.h"
#include "M_AutoDiagnosisManager/DiagnosisProcessor.h"
#include "M_AutoDiagnosisManager/DiagnosisManager.h"
//#include "CHReviewSetting/SystemInfo.h"
 
#define M_PI       3.14159265358979323846
#define ToRadian(degree)    ( (degree) * (M_PI/180.0) )
#define ToDegree(radian)    ( (radian) * (180.0/M_PI) )
 
enum MotorOriginDirection { MotorOriginDir_LeftTop = 0, MotorOriginDir_RightTop, MotorOriginDir_LeftBottom, MotorOriginDir_RightBottom, MotorOriginDir_Count };
 
CDiagnosisProcessor::CDiagnosisProcessor()
{
    m_pDP2P = NULL;
    m_bStartProcess = FALSE;
}
 
CDiagnosisProcessor::~CDiagnosisProcessor()
{
 
}
 
BOOL CDiagnosisProcessor::StartProcess(int nProcessMode/* = 0*/)
{
    BOOL bRet = FALSE;
 
    CDiagnosisProcessData* pThreadData = new CDiagnosisProcessData(this);
    if (pThreadData)
    {
        pThreadData->nProcessMode = nProcessMode;
        bRet = CreateWorkThread(pThreadData);
 
        m_bStartProcess = bRet;
    }
 
    return bRet;
}
 
void CDiagnosisProcessor::StopProcess()
{
    m_bStartProcess = FALSE;
 
    int nSelModuleIndex = m_pDP2P->IDP2P_GetSelectedModuleIndex();
    m_pDP2P->IDP2P_SetAFMTracking(nSelModuleIndex, FALSE);
    //m_pDP2P->IDP2P_DiagnosisSendCompleteSig();
    CloseWorkThread();
}
 
void CDiagnosisProcessor::WorkThreadProcess(PVOID pParameter)
{
    if (pParameter == NULL) return;
 
    CString strMessage = "";
 
    CDiagnosisProcessData* pProcessData = static_cast<CDiagnosisProcessData*>(pParameter);
    int nProcessMode = pProcessData->nProcessMode;
 
    switch (nProcessMode)
    {
    case DiagnosisMode_All:
        //taek ProcessAll 일때와 아닐때를 구분하자
        m_pManager->m_bProcessAll = true;
        ProcessAll();
        break;
        /*< LYW 20210719 : #3517 ADD Start >*/
    case DiagnosisMode_Review :
        strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisMode_Review START"));
        m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
 
        m_pManager->m_bProcessAll = false;
        if (IsCommandStop()) return;
        m_pDP2P->IDP2P_SetDialogTap(0);
        ProcessSquareness();
        Sleep(1000);
        if (IsCommandStop()) return;
        m_pDP2P->IDP2P_SetDialogTap(1);
        ProcessFlatness();
        Sleep(1000);
 
        if (ProcessDiagnosisJudge(nProcessMode) == FALSE) {
            strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisJudge FAIL"));
            m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
        }
 
        strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisMode_Review END"));
        m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
 
        break;
 
    case DiagnosisMode_Wsi:
        strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisMode_WSI START"));
        m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
 
        m_pManager->m_bProcessAll = false;
        if (IsCommandStop()) return;
        m_pDP2P->IDP2P_UpdateDialog(7);
        ProcessWsi();
        Sleep(1000);
 
        if (ProcessDiagnosisJudge(nProcessMode) == FALSE) {
            strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisJudge FAIL"));
            m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
        }
 
        strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisMode_WSI END"));
        m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
 
        break;
 
    case DiagnosisMode_Squareness:
        m_pManager->m_bProcessAll = false;
        ProcessSquareness();
        Sleep(1000);
        if (ProcessDiagnosisJudge(nProcessMode) == FALSE) {
            strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisJudge FAIL"));
            m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
        }
        break;
        /*< LYW 20210719 : #3517 ADD End >*/
    case DiagnosisMode_Flatness:
        m_pManager->m_bProcessAll = false;
        ProcessFlatness();
        Sleep(1000);
        if (ProcessDiagnosisJudge(nProcessMode) == FALSE) {
            strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisJudge FAIL"));
            m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
        }
        break;
 
    case DiagnosisMode_Flatness_Manual:
        m_pManager->m_bProcessAll = false;
    //    ProcessFlatness_Manual();
        break;
 
    case DiagnosisMode_Simulation:
        m_pManager->m_bProcessAll = false;
    //    ProcessSimulation();
        break;
 
    }
 
    //m_pManager->SaveDiagnosisFile();
}
 
int CDiagnosisProcessor::ProcessAll()
{
    if (IsCommandStop()) return 1;
 
    CString strMessage = "";
    
    m_pDP2P->IDP2P_UpdateDialog(7);//WSI Result 칸 초기화 
    //if (m_pDP2P == NULL) return 0;
 
    m_pDP2P->IDP2P_SetDialogTap(0);
    ProcessSquareness();
 
    Sleep(1000);
    if (IsCommandStop()) return 1;
 
    if ( m_pManager->GetDiagnosisInfo()->GetPanelCount_Wsi() > 0)
    {
        m_pDP2P->IDP2P_SetDialogTap(2);
    ProcessWsi();
        Sleep(1000);
        if (IsCommandStop()) return 1;
    }
    m_pDP2P->IDP2P_SetDialogTap(1);
    ProcessFlatness();
 
    /*< LYW 20210719 : #3517 ADD Start >*/
    //Sleep(2000);
    Sleep(1000);
 
    if (ProcessDiagnosisJudge(DiagnosisMode_All) == FALSE) {
        strMessage.Format(_T("[CDiagnosisProcessor] DiagnosisJudge FAIL"));
        m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
    }
    /*< LYW 20210719 : #3517 ADD Start >*/
 
    m_pManager->m_bProcessAll = false;
 
    return 1;
}
 
int CDiagnosisProcessor::ProcessSquareness()
{
    m_pDP2P->IDP2P_UpdateDialog(3);
    if (m_pDP2P == NULL || m_pManager == NULL) return 0;
    int nResultCount = 0;
    for(int i=0; i<2;i++)
    { 
 
 
     CDiagnosisInfo* pSettingInfo = m_pManager->GetDiagnosisInfo();
     if (pSettingInfo == NULL) { 
         m_pDP2P->IDP2P_UpdateDialog(4);
         return 0; }
 
    //int nSelModuleIndex = m_pDP2P->IDP2P_GetSelectedModuleIndex();
    int nSelModuleIndex = i;
 
    int nPanelCount = pSettingInfo->GetPanelCount_Squareness();
     CDiagnosisResult* pSettingResult = pSettingInfo->GetSettingResult_Squareness(nSelModuleIndex);
     if (pSettingResult == NULL) {
         m_pDP2P->IDP2P_UpdateDialog(4);
         return 0;
     }
 
    CDiagnosisResult* pProcessResult = m_pManager->GetDiagnosisResult_Squareness(nSelModuleIndex);
    if (pProcessResult == NULL) {
        m_pDP2P->IDP2P_UpdateDialog(4);
        return 0;
    }
 
    DWORD dwTime = GetTickCount(), dwTimeMotionEnd = 0;
    CString strMessage = _T("");
 
    if (nSelModuleIndex == 0)
    {
        m_pDP2P->IDP2P_MotorGo(1, 300, 400);
 
        if (m_pDP2P->IDP2P_GetMotionEnd(1) == FALSE)
        {
            m_pDP2P->IDP2P_UpdateDialog(4);
            return FALSE;
        }
    }
    else
    {
        m_pDP2P->IDP2P_MotorGo(0, 300, 400);
 
        if (m_pDP2P->IDP2P_GetMotionEnd(0) == FALSE)
        {
            m_pDP2P->IDP2P_UpdateDialog(4);
            return FALSE;
        }
    }
 
    pProcessResult->SetPanelCount_Squareness(nPanelCount);
    double dPosX = 0, dPosY = 0;
    int nIndex = 0;
    for (int nIdx=0; nIdx<nPanelCount; nIdx++)
    {
        if (IsCommandStop())
        {
            m_pDP2P->IDP2P_UpdateDialog(4);
            return 1;
        }
 
        SPositionData* pSettingPoint = pSettingResult->GetPositionData_Squareness(nIdx);
        if (pSettingPoint == NULL) continue;
 
        if (nSelModuleIndex == 0)
        {
            if (nIdx == 0)
            {
                pSettingPoint->dPosX = pSettingInfo->GetReviewPanelPosX();//1039.367;
                pSettingPoint->dPosY = pSettingInfo->GetReviewPanelPosY();// 258.97;
                }
            else if (nIdx == 1)
            {
                    pSettingPoint->dPosX = pSettingInfo->GetReviewPanelPosX3(); // 1039.383;
                    pSettingPoint->dPosY = pSettingInfo->GetReviewPanelPosY3(); // 2562.295;
            }
            else if (nIdx == 2)
            {
                    pSettingPoint->dPosX = pSettingInfo->GetReviewPanelPosX5(); // 1039.367 + a;
                    pSettingPoint->dPosY = pSettingInfo->GetReviewPanelPosY5(); // 258.97;
            }
        }
        else
        {
            if (nIdx == 0)
            {
                pSettingPoint->dPosX = pSettingInfo->GetReviewPanelPosX2();// 1704.449;
                pSettingPoint->dPosY = pSettingInfo->GetReviewPanelPosY2();// 59.723;
            }
                else if (nIdx == 1)
                {
                    pSettingPoint->dPosX = pSettingInfo->GetReviewPanelPosX4(); // 1704.407;
                    pSettingPoint->dPosY = pSettingInfo->GetReviewPanelPosY4(); // 2363.057;
                }
                else if (nIdx == 2)
            {
                    pSettingPoint->dPosX = pSettingInfo->GetReviewPanelPosX6(); // 1704.449 + a;
                    pSettingPoint->dPosY = pSettingInfo->GetReviewPanelPosY6(); // 59.723;
            }
        
        }
    
        // move panel
        m_pDP2P->IDP2P_MotorGo(nSelModuleIndex, pSettingPoint->dPosX, pSettingPoint->dPosY);
 
        if (m_pDP2P->IDP2P_GetMotionEnd(nSelModuleIndex) == FALSE) continue;
 
        
       
        
 
        int stTime = GetTickCount();
        while (1)
        {
            /*< LYW 20210719 : #3517 ADD Start >*/
            //if (11000 <= GetTickCount() - stTime)
            /*< LYW 20210719 : #3517 ADD End >*/
            if (1500 <= GetTickCount() - stTime)
            {
                break;
            }
 
 
        }
        m_pDP2P->IDP2P_SetAFMTracking(nSelModuleIndex, TRUE);
        /*< LYW 20210719 : #3517 ADD Start >*/
        //Sleep(1000);
        Sleep(1000);
        /*< LYW 20210719 : #3517 ADD End >*/
 
        CString strimagename;
        strimagename.Format(_T("%s_Module[%d]Index[%d].bmp"), m_pManager->m_strResultImageFileName, nSelModuleIndex, nIdx);
 
 
        // find mark at panel
        double dFindPixX = 0., dFindPixY = 0.;
        if (m_pDP2P->IDP2P_FindPanelCenter(nSelModuleIndex, dFindPixX, dFindPixY, strimagename) == FALSE) {
            strMessage.Format(_T("[CDiagnosisProcessor] find mark at panel FAIL"));
            m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
            continue;
        }
 
        m_pDP2P->IDP2P_SetAFMTracking(nSelModuleIndex, FALSE);
 
        stTime = GetTickCount();
        while (1)
        {
            if (1000 <= GetTickCount() - stTime)
            {
                break;
            }
        }
 
 
 
        if (nSelModuleIndex == 1)
        {
            nResultCount = nIdx + 3;
        }
        else
        {
            nResultCount = nIdx;
        }
 
        // set result
        SPositionData* pResultPoint = pProcessResult->GetPositionData_Squareness(nResultCount);
        if (pResultPoint == NULL) continue;
 
        pResultPoint->nIndex = nResultCount;
        pResultPoint->nPosMode = nIdx;
        pResultPoint->nModuleIdx = nSelModuleIndex;
        pResultPoint->bProcsss = TRUE;
        pResultPoint->dPixelX = dFindPixX;
        pResultPoint->dPixelY = dFindPixY;
        pResultPoint->dPosX = pSettingPoint->dPosX;
        pResultPoint->dPosY = pSettingPoint->dPosY;
 
        nResultCount += 1;
 
        m_pDP2P->IDP2P_UpdateDialog(0);        // update calc theta
    }
 
    // calc gantry squareness
    CalculateSquareness(nSelModuleIndex);
    //if (CalculateSquareness() == FALSE) return FALSE;
 
    strMessage.Format(_T("[AutoDiagnosisProcessor] ProcessSquareness Time [%d]ms"), GetTickCount() - dwTime);
    m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
 
    // make history data
    m_pManager->SaveDiangosisHistory_Squareness(); //taek 측정 히스토리 남긴다
 
    m_pManager->SaveDiagnosisFile_Squareness();
 
    m_pDP2P->IDP2P_UpdateDialog(0);        // update calc theta
    
    if (nSelModuleIndex == 1)
    {
        m_pDP2P->IDP2P_MotorGo(1, 300, 400);
 
        if (m_pDP2P->IDP2P_GetMotionEnd(1) == FALSE)
        {
            m_pDP2P->IDP2P_UpdateDialog(4);
            return FALSE;
        }
    }
 
    }
    m_pDP2P->IDP2P_UpdateDialog(4);
    return TRUE;
}
 
int CDiagnosisProcessor::ProcessWsi()
{
    m_pDP2P->IDP2P_UpdateDialog(5); //버튼 끄기
    if (m_pDP2P == NULL || m_pManager == NULL) return 0;
 
    const CDiagnosisInfo* pSettingInfo = m_pManager->GetDiagnosisInfo();
    if (pSettingInfo == NULL) {
        m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
        return 0;
    }
    int nSelModuleIndex = -1;
 
    int nPanelCount = pSettingInfo->GetPanelCount_Wsi();
    double dXpos = 0;
    double dYpos = 0;
 
    for (int i = 0; i < nPanelCount; i++)
    {
        nSelModuleIndex  = i;
        m_pManager->setWSIVLSIEnd(FALSE);
        if (nSelModuleIndex == -1)
        {
            m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
            return FALSE; 
        }
 
        if (nSelModuleIndex == 0)
        {
            m_pDP2P->IDP2P_MotorGo(1, 300, 400);
 
            if (m_pDP2P->IDP2P_IsGantryMotionEnd(1) == FALSE)
            {
                m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
                return FALSE;
            }
 
            dXpos = pSettingInfo->GetPanelPosX_Wsi();
            dYpos = pSettingInfo->GetPanelPosY_Wsi();
 
        }
        else
        {
            m_pDP2P->IDP2P_MotorGo(0, 300, 400);
 
            if (m_pDP2P->IDP2P_IsGantryMotionEnd(0) == FALSE)
            {
                m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
                return FALSE;
            }
            dXpos = pSettingInfo->GetPanelPosX2_Wsi();
            dYpos = pSettingInfo->GetPanelPosY2_Wsi();
        }
 
        DWORD dwTime = GetTickCount(), dwTimeMotionEnd = 0;
        CString strMessage = _T("");
 
 
        if (IsCommandStop())
        {
            m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
            return 1;
        }
        m_pDP2P->IDP2P_SetWsiManualMeasure(nSelModuleIndex, dXpos, dYpos);
        // move panel
        /*< LYW 20210719 : #3517 ADD Start >*/
        //Sleep(2000);
        Sleep(1000);
        /*< LYW 20210719 : #3517 ADD End >*/
        m_pDP2P->IDP2P_MotorGo(nSelModuleIndex, dXpos, dYpos);
 
        if (m_pDP2P->IDP2P_IsGantryMotionEnd(nSelModuleIndex) == FALSE)
        {
            m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
            return FALSE;
        }
 
        int stTime = GetTickCount();
        while (1)
        {
            /*< LYW 20210719 : #3517 ADD Start >*/
            //if (10000 <= GetTickCount() - stTime)
            if (11000 <= GetTickCount() - stTime)
            /*< LYW 20210719 : #3517 ADD End >*/
            {
                break;
            }
 
 
        }
 
        m_pDP2P->IDP2P_WsiMotorMotionEnd(nSelModuleIndex, dXpos, dYpos);
 
        int tickcount = GetTickCount();
        while (1)
        {
            Sleep(50);
        
 
            if (m_pManager->getWSIVLSIEnd() == true)
            {
                strMessage.Format(_T("[AutoDiagnosisProcessor] WSI VLSI END"));
 
                if (nSelModuleIndex == 1)
                {
                    m_pDP2P->IDP2P_MotorGo(1, 300, 400);
 
                    if (m_pDP2P->IDP2P_IsGantryMotionEnd(1) == FALSE)
                    {
                        m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
                        return FALSE;
                    }
                }
 
                // make history data
                m_pManager->SaveDiangosisHistory_Wsi(); //taek 측정 히스토리 남긴다
 
                //taek 종료 되기 전에 save
                m_pManager->SaveDiagnosisFile_Wsi();
 
                break;
            }
 
            if (30000 <= (GetTickCount() - tickcount))
            {
 
                strMessage.Format(_T("[AutoDiagnosisProcessor] WSI VLSI OVER TIME"));
                m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
                m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
                return FALSE;
            }
        }
        //}
    }
 
    
    m_pDP2P->IDP2P_UpdateDialog(6); //버튼 켜기
    return TRUE;
}
 
int CDiagnosisProcessor::ProcessFlatness()
{
    if (m_pDP2P == NULL || m_pManager == NULL) return 0;
 
    CString strMessage = "";
 
    CDiagnosisInfo * pSettingInfo = m_pManager->GetDiagnosisInfo();
    if (pSettingInfo == NULL) return 0;
    /*< LYW 20210719 : #3517 ADD Start >*/
    if (pSettingInfo->GetFlastnessSkipMode() == TRUE) {
        strMessage.Format(_T("[CDiagnosisProcessor] FlastnessSkipMode!"));
        m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
        return 0;
    }
 
    /*< LYW 20210719 : #3517 ADD End >*/
 
    int nSelModuleIndex = m_pDP2P->IDP2P_GetSelectedModuleIndex();
    int nFlatnessMode = pSettingInfo->GetFlatnessMode();
    int nTotalCount = pSettingInfo->GetRangeCount_Flatness();
    
    CDiagnosisResult * pProcessResult = m_pManager->GetDiagnosisResult_Flatness();
    if (pProcessResult == NULL) return 0;
    
    pProcessResult->SetRangeCount_Flatness(nTotalCount);
    pProcessResult->SetFlatnessMode(nFlatnessMode);
    pProcessResult->SetPositionData_Flatness(nTotalCount);
 
    SPositionData * pResultPoint = pProcessResult->GetPositionData_Flatness(0);
    if (pResultPoint == NULL) return 0;
 
    int nGlassSizeX = pSettingInfo->GetGlassSizeX_Flatness();
    int nGlassSizeY = pSettingInfo->GetGlassSizeY_Flatness();
 
    int nAFDelay = pSettingInfo->GetAFDelay();
 
    pProcessResult->PathScheduling_Flatness(pResultPoint, nGlassSizeX, nGlassSizeY);
 
    m_pDP2P->IDP2P_MotorGo(1, 0, 0);
 
    if (m_pDP2P->IDP2P_IsGantryMotionEnd(1) == FALSE)
    {
        return FALSE;
    }
 
    for (int nIdx = 0; nIdx < nTotalCount; nIdx++)
    {
        if (IsCommandStop()) return 1;
        double dZpos = 0.;
 
 
 
        if (nIdx == 0)    // if 1st point, afm tracking on
        {
            // AF Home
            m_pDP2P->IDP2P_AFMHomePosition(0);
            /*< LYW 20210719 : #3517 ADD Start >*/
            //Sleep(5000);
            Sleep(1000);
            /*< LYW 20210719 : #3517 ADD End >*/
            m_pDP2P->IDP2P_SetAFMTracking(0, TRUE);
            Sleep(500);
        }
 
//         if (nIdx == 0)
//         {
//             pResultPoint[nIdx].dPosX = 657.425
//                 ;
//             pResultPoint[nIdx].dPosY = 540.14
//                 ;
//         }
//         else if (nIdx == 1)
//         {
//             pResultPoint[nIdx].dPosX = 657.425
//                 ;
//             pResultPoint[nIdx].dPosY = 2278.636
//                 ;
//         }
//         else if (nIdx == 2)
//         {
//             pResultPoint[nIdx].dPosX = 2096.944
//                 ;
//             pResultPoint[nIdx].dPosY = 540.141
//                 ;
//         }
//         else if (nIdx == 3)
//         {
//             pResultPoint[nIdx].dPosX = 2096.944
//                 ;
//             pResultPoint[nIdx].dPosY = 2278.636;
//         }
 
 
        //1. 모터를 이동
        m_pDP2P->IDP2P_MotorGo(0, pResultPoint[nIdx].dPosX, pResultPoint[nIdx].dPosY);
 
        if (m_pDP2P->IDP2P_GetMotionEnd(0) == FALSE) continue;
 
 
        //2. AF Delay
        Sleep(nAFDelay);
 
        //3. Z축 값 얻어오기
        m_pDP2P->IDP2P_GetAFMZPosition(0, dZpos);
        
        // set result
        pResultPoint[nIdx].dPosZ = dZpos/1000.0;
        m_pDP2P->IDP2P_SetDlgFlatness(nIdx);
    }
 
    //AFM Tracking OFF
    m_pDP2P->IDP2P_SetAFMTracking(0, FALSE);
 
    // make 3d data file
    m_pManager->SaveDiagnosis3DImageFile_Flatness();
 
    // make result file
    m_pManager->SaveDiagnosisFile_Flatness(nFlatnessMode);
 
    m_pDP2P->IDP2P_UpdateDialog(2);
 
    return TRUE;
}
 
int CDiagnosisProcessor::ProcessFlatness_Manual()
{
    if (m_pDP2P == NULL) return 0;
 
    int nSelModuleIndex = m_pDP2P->IDP2P_GetSelectedModuleIndex();
    double dPosZ = 0.;
 
    // AF Home
    m_pDP2P->IDP2P_AFMHomePosition(nSelModuleIndex);
    Sleep(5000);
 
    // AF Tracking ON
    m_pDP2P->IDP2P_SetAFMTracking(nSelModuleIndex, TRUE);
    Sleep(500);
 
    // get Z Value
    m_pDP2P->IDP2P_GetAFMZPosition(nSelModuleIndex, dPosZ);
 
    //AFM Tracking OFF
    m_pDP2P->IDP2P_SetAFMTracking(nSelModuleIndex, FALSE);
    
    CString strPosZ;
    strPosZ.Format(_T("Position Z : %6lf"), dPosZ);
    AfxMessageBox(strPosZ);
 
    return TRUE;
}
 
int CDiagnosisProcessor::ProcessSimulation()
{
 
    if (m_pDP2P == NULL || m_pManager == NULL) return 0;
 
    CDiagnosisInfo * pSettingInfo = m_pManager->GetDiagnosisInfo();
    if (pSettingInfo == NULL) return 0;
 
    int nSelModuleIndex = m_pDP2P->IDP2P_GetSelectedModuleIndex();
    int nFlatnessMode = pSettingInfo->GetFlatnessMode();
    int nTotalCount = pSettingInfo->GetRangeCount_Flatness();
 
    CDiagnosisResult * pProcessResult = m_pManager->GetDiagnosisResult_Flatness();
    if (pProcessResult == NULL) return 0;
 
    pProcessResult->SetRangeCount_Flatness(nTotalCount);
    pProcessResult->SetFlatnessMode(nFlatnessMode);
    pProcessResult->SetPositionData_Flatness(nTotalCount);
 
    SPositionData * pResultPoint = pProcessResult->GetPositionData_Flatness(nSelModuleIndex);
    if (pResultPoint == NULL) return 0;
 
    int nGlassSizeX = pSettingInfo->GetGlassSizeX_Flatness();
    int nGlassSizeY = pSettingInfo->GetGlassSizeY_Flatness();
 
    int nAFDelay = pSettingInfo->GetAFDelay();
 
    pProcessResult->PathScheduling_Flatness(pResultPoint, nGlassSizeX, nGlassSizeY);
 
    double dTempPosZ = 0.;
    for (int nIdx = 0; nIdx < nTotalCount; nIdx++)
    {
        pResultPoint[nIdx].dPosZ = dTempPosZ++;
        Sleep(50);
        m_pDP2P->IDP2P_SetDlgFlatness(nIdx);
 
    }
    // make 3d data file
    m_pManager->SaveDiagnosis3DImageFile_Flatness();
 
    // make result file
    m_pManager->SaveDiagnosisFile_Flatness(nFlatnessMode);
 
    return TRUE;
}
 
int CDiagnosisProcessor::ProcessDiagnosisJudge(int nMode)
{
    for (int i = 0; i < 2; i++)
    {
        int nSelModuleIndex = i;
 
        CDiagnosisInfo* pSettingInfo = m_pManager->GetDiagnosisInfo();
        if (pSettingInfo == NULL) {
            m_pDP2P->IDP2P_UpdateDialog(4);
            return 0;
        }
 
        CDiagnosisResult* pProcessResult = m_pManager->GetDiagnosisResult_Squareness(nSelModuleIndex); // 0412 kyh 직각도 측정 모듈에 대한 결과정보
        if (pProcessResult == NULL) {
            m_pDP2P->IDP2P_UpdateDialog(4);
            return 0;
        }
 
        CDiagnosisResult* pProcessWSIResult = m_pManager->GetDiagnosisResult_Wsi(nSelModuleIndex); // 0412 kyh WSI 측정 모듈에 대한 결과 정보
        if (pProcessWSIResult == NULL) {
            m_pDP2P->IDP2P_UpdateDialog(6);
            return 0;
        }
 
        CString strMessage = _T("");
        if (nMode == DiagnosisMode_All || nMode == DiagnosisMode_Review || nMode == DiagnosisMode_Squareness)
        {
            double dDate = (90 - pProcessResult->GetGantryDegree());
            if (abs(dDate) >= abs(pSettingInfo->GetJudgeData_Degree())) // 0412 kyh 직각도에서 나온 Dgree 값이 설정한 Judge 값보다 크거나 같을경우
            {
 
                m_pDP2P->IDP2P_DiagnosisSendJudgeAlarm(); // 알람 발생
                strMessage.Format(_T("[Judge Data] [Gantry %d]Diagnosis Error ! GantryDegree[%lf] Judge[%lf]"), i, dDate, pSettingInfo->GetJudgeData_Degree());
                m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
            }
        }
 
        if (nMode == DiagnosisMode_All || nMode == DiagnosisMode_Wsi)
        {
            if (pProcessWSIResult->getWSI_RTB() >= pSettingInfo->GetJudgeData_3Sigma() || pProcessWSIResult->getWSI_RTB() <= 0) // 0412 kyh WSI 표준편차 비교
            {
                m_pDP2P->IDP2P_DiagnosisSendJudgeAlarm(); // 알람 발생
                strMessage.Format(_T("[Judge Data] [WSI %d]Diagnosis Error ! WSI RTB[%lf] Judge[%lf]"), i, pProcessWSIResult->getWSI_RTB(), pSettingInfo->GetJudgeData_3Sigma());
                m_pDP2P->IDP2P_DisplayMessage(0, strMessage);
            }
        }
 
    }
    //#3407 KYH 자가진단 Judge 판정 및 알람 발생 기능 ADD END
 
    //컴플릿트 신호 보내기
    m_pDP2P->IDP2P_DiagnosisSendCompleteSig();
    return 1;
}
 
BOOL CDiagnosisProcessor::CalculateSquareness(int nIndex)
{
    if (m_pDP2P == NULL || m_pManager == NULL) return 0;
 
    int nSelModuleIndex = nIndex;
 
    // setting info
    const CDiagnosisInfo* pSettingInfo = m_pManager->GetDiagnosisInfo();
    if (pSettingInfo == NULL) return 0;
    const CDiagnosisResult* pSettingResult = pSettingInfo->GetSettingResult_Squareness(nSelModuleIndex);
    if (pSettingResult == NULL) return 0;
 
    // find result
    CDiagnosisResult* pProcessResult = m_pManager->GetDiagnosisResult_Squareness(nSelModuleIndex);
    if (pProcessResult == NULL) return 0;
 
    int nPanelCount = pProcessResult->GetPanelCount_Squareness();
    double dFrameWidth = pSettingInfo->GetCameraFrameWidth();
    double dFrameHeight = pSettingInfo->GetCameraFrameHeight();
    double dResol = pSettingInfo->GetCameraResolution();
 
    // calc image direction
    //enum MotorOriginDirection        { MotorOriginDir_LeftTop=0, MotorOriginDir_RightTop, MotorOriginDir_LeftBottom, MotorOriginDir_RightBottom, MotorOriginDir_Count };
    int nDirX[2] = { 0, }, nDirY[2] = { 0, };
    int nMotorDir = m_pDP2P->IDP2P_GetOrigionMotorDir() + nSelModuleIndex;
    switch (nMotorDir)
    {
    case MotorOriginDir_LeftTop:
        nDirX[nSelModuleIndex] = 1;
        nDirY[nSelModuleIndex] = 1;
        break;
    case MotorOriginDir_LeftBottom:
        nDirX[nSelModuleIndex] = 1;
        nDirY[nSelModuleIndex] = -1;
        break;
    case MotorOriginDir_RightTop:
        nDirX[nSelModuleIndex] = -1;
        nDirY[nSelModuleIndex] = 1;
        break;
    case MotorOriginDir_RightBottom:
        nDirX[nSelModuleIndex] = -1;
        nDirY[nSelModuleIndex] = -1;
        break;
    }
 
    switch (nPanelCount)
    {
    case 2:
    {
        double dMotorPosX[2] = { 0, };
        double dMotorPosY[2] = { 0, };
        double dPixelPosX[2] = { 0, };
        double dPixelPosY[2] = { 0, };
 
        for (int nIdx = 0; nIdx < nPanelCount; nIdx++)
        {
            const SPositionData* pSettingPos = pSettingResult->GetPositionData_Squareness(nIdx);
            const SPositionData* pDataPos = pProcessResult->GetPositionData_Squareness(nIdx);
            if (pSettingPos == NULL || pDataPos == NULL) continue;
 
            dMotorPosX[nIdx] = pSettingPos->dPosX;
            dMotorPosY[nIdx] = pSettingPos->dPosY;
 
            dPixelPosX[nIdx] = pDataPos->dPixelX;
            dPixelPosY[nIdx] = pDataPos->dPixelY;
        }
 
        // calc degree
        double dCenter2MarkX[2] = { 0, };
        double dCenter2MarkY[2] = { 0, };
 
        dCenter2MarkX[0] = (dPixelPosX[0] - dFrameWidth / 2) * dResol;
        dCenter2MarkX[1] = (dPixelPosX[1] - dFrameWidth / 2) * dResol;
        dCenter2MarkY[0] = (dPixelPosY[0] - dFrameHeight / 2) * dResol;
        dCenter2MarkY[1] = (dPixelPosY[1] - dFrameHeight / 2) * dResol;
 
        double dx = abs((dMotorPosX[0] + dCenter2MarkX[0] * nDirX[nSelModuleIndex]) - (dMotorPosX[1] + dCenter2MarkX[1] * nDirX[nSelModuleIndex]));
        double dy = abs((dMotorPosY[0] + dCenter2MarkY[0] * nDirY[nSelModuleIndex]) - (dMotorPosY[1] + dCenter2MarkY[1] * nDirY[nSelModuleIndex]));
        double dRadian = -1.0 * atan(dx / dy);
        double dDegree = ToDegree(dRadian);
 
        // trans degree
        dDegree = 90 - dDegree;
 
        pProcessResult->SetGantryDegree(dDegree);
 
        break;
    }
 
    case 3:
    {
        double dMotorPosX[3] = { 0, };
        double dMotorPosY[3] = { 0, };
        double dPixelPosX[3] = { 0, };
        double dPixelPosY[3] = { 0, };
 
        for (int nIdx = 0; nIdx < nPanelCount; nIdx++)
        {
            //LYW 210811
            const SPositionData* pSettingPos;
            const SPositionData* pDataPos;
 
            if (nSelModuleIndex == 1)
            {
                pSettingPos = pSettingResult->GetPositionData_Squareness(nIdx);
                pDataPos = pProcessResult->GetPositionData_Squareness(nIdx + 3);
            }
            else
            {
                pSettingPos = pSettingResult->GetPositionData_Squareness(nIdx);
                pDataPos = pProcessResult->GetPositionData_Squareness(nIdx);
            }
            //LYW 210811
            if (pSettingPos == NULL || pDataPos == NULL) continue;
 
            dMotorPosX[nIdx] = pSettingPos->dPosX;
            dMotorPosY[nIdx] = pSettingPos->dPosY;
 
            dPixelPosX[nIdx] = pDataPos->dPixelX;
            dPixelPosY[nIdx] = pDataPos->dPixelY;
        }
 
        // calc gantry degree
        double dCenter2MarkX[3] = { 0, };
        double dCenter2MarkY[3] = { 0, };
 
        dCenter2MarkX[0] = (dPixelPosX[0] - dFrameWidth / 2) * dResol;
        dCenter2MarkY[0] = (dPixelPosY[0] - dFrameHeight / 2) * dResol;
        dCenter2MarkX[1] = (dPixelPosX[1] - dFrameWidth / 2) * dResol;
        dCenter2MarkY[1] = (dPixelPosY[1] - dFrameHeight / 2) * dResol;
 
        double dx = abs((dMotorPosX[0] + dCenter2MarkX[0] * nDirX[nSelModuleIndex]) - (dMotorPosX[1] + dCenter2MarkX[1] * nDirX[nSelModuleIndex]));
        double dy = abs((dMotorPosY[0] + dCenter2MarkY[0] * nDirY[nSelModuleIndex]) - (dMotorPosY[1] + dCenter2MarkY[1] * nDirY[nSelModuleIndex]));
        double dRadian = -1.0 * atan(dx / dy);
        double dGantryDegree = ToDegree(dRadian);
 
        // calc x scale degree
        dCenter2MarkX[2] = (dPixelPosX[2] - dFrameWidth / 2) * dResol;
        dCenter2MarkY[2] = (dPixelPosY[2] - dFrameHeight / 2) * dResol;
 
        dx = abs((dMotorPosX[0] + dCenter2MarkX[0] * nDirX[nSelModuleIndex]) - (dMotorPosX[2] + dCenter2MarkX[2] * nDirX[nSelModuleIndex]));
        dy = abs((dMotorPosY[0] + dCenter2MarkY[0] * nDirY[nSelModuleIndex]) - (dMotorPosY[2] + dCenter2MarkY[2] * nDirY[nSelModuleIndex]));
        dRadian = -1.0 * atan(dy / dx);
        double dScaleDegree = ToDegree(dRadian);
 
        // trans degree - select
        //double dDegree = 90 - dGantryDegree;
        double dDegree = 90 - dGantryDegree - dScaleDegree;
 
        // calc virtual x panel position
        double dv = 0., dx2 = 0., dy2 = 0.;
        if (dy > dx)
        {
            dx2 = dy;
            dv = dx2 / cos(dRadian);
            dy2 = dx2 * tan(dRadian);
 
            SPositionData* dataCalc = pProcessResult->GetPositionData_CalcResult();
            if (dataCalc)
            {
                dataCalc->dPosAssemX = dMotorPosX[0] + dv;
                dataCalc->dPosAssemY = dMotorPosY[0] + dy2;
            }
        } //taek 210105
 
        pProcessResult->SetGantryDegree(dDegree);
        pProcessResult->SetXScaleDegree(dScaleDegree);
 
        break;
    }    
 
    default:
        break;
    }
 
    
 
    return TRUE;
}