qqq
C3031
2025-12-31 cbfa25a6e8df52ff785be28f29d0b1ae1f39cbb3
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
using HalconDotNet;
using Microsoft.Extensions.Logging.Abstractions;
using LB_VisionProcesses.Alogrithms.Halcon;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using OpenCvSharp;
using OpenCvSharp.XFeatures2D;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using ZXing;
using ZXing.Common;
using ZXing.Windows.Compatibility;
using static System.Collections.Specialized.BitVector32;
using static System.Net.Mime.MediaTypeNames;
using static System.Resources.ResXFileRef;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Extensions = OpenCvSharp.Extensions;
using Point = OpenCvSharp.Point;
using LB_VisionControl;
 
namespace LB_VisionProcesses.Alogrithms
{
    public class TAlgorithm : IProcess
    {
        /// <summary>
        /// 读写锁
        /// </summary>
        public static readonly object lockObj = new object();
 
        /// <summary>
        /// 裁切图片为hoDomainImage(保留原坐标系,其余填充为空)
        /// </summary>
        /// <returns></returns>
        public virtual bool ReduceDomainImage(object InputImage, ref object image)
        {
            image = null;
            if (InputImage == null)
            {
                image = null;
                Msg = "输入图片为空";
                Result = false;
                return false;
            }
 
            lock (InputImage)
            {
                try
                {
                    if (InputImage is Bitmap)
                    {
                        TAlgorithm.Bitmap2Mat((Bitmap)InputImage, out Mat src);
 
                        if (Params.Fixture == null)
                            Params.Fixture = new Fixture();
                        else if (dicFixtures.ContainsKey(Params.Fixture.strName))
                            Params.Fixture = dicFixtures[Params.Fixture.strName];
 
                        switch (Params.ROI?.GetType().Name)
                        {
                            case "HRectangle2":
                                // 1. 定义旋转矩形(中心点、大小、角度)
                                RotatedRect rotatedRect = new RotatedRect(
                                    new Point2f(Convert.ToSingle(Params.ROI.X + Params.Fixture.X)
                                    , Convert.ToSingle(Params.ROI.Y + Params.Fixture.Y)), // 中心坐标
                                    new Size2f(Convert.ToSingle(((HRectangle2)Params.ROI).Width)
                                    , Convert.ToSingle(((HRectangle2)Params.ROI).Height)),  // 宽度和高度
                                     Convert.ToSingle(((HRectangle2)Params.ROI).Angle + Params.Fixture.Angle)                   // 旋转角度(度)
                                );
 
                                // 2. 获取旋转矩形的4个顶点
                                Point2f[] vertices = rotatedRect.Points();
                                // 3. 修正超出边界的点坐标(关键步骤)
                                Point2f[] clampedPoints = vertices.Select(p =>
                                {
                                    float x = Clamp(p.X, 0, src.Width - 1);
                                    float y = Clamp(p.Y, 0, src.Height - 1);
                                    return new Point2f(x, y);
                                }).ToArray();
                                Rect boundingRect = Cv2.BoundingRect(clampedPoints);
 
                                // 4. 创建与原图大小相同的Mat,并初始化无效值
                                image = new Mat(src.Size(), src.Type());
                                ((Mat)image).SetTo(TAlgorithm.GetInvalidValueForMat(src)); // 全部初始化为无效值
 
                                // 5. 创建遮罩:在result上绘制旋转矩形(白色填充)
                                using (Mat cropped = new Mat(src, boundingRect))
                                {
                                    cropped.CopyTo(new Mat(((Mat)image), boundingRect));
                                }
 
                                break;
                            case "HCircle":
                                // 1. 定义圆形(圆心、半径)
                                Point center = new Point(Params.ROI.X + Params.Fixture.X
                                    , Params.ROI.Y + Params.Fixture.Y);
                                int radius = Convert.ToInt16(((HCircle)Params.ROI).Radius);
 
                                // 2. 创建与原图大小相同的Mat,并初始化无效值
                                image = new Mat(src.Size(), src.Type());
                                ((Mat)image).SetTo(TAlgorithm.GetInvalidValueForMat(src)); // 全部初始化为无效值
 
                                // 3. 创建圆形遮罩
                                using (Mat mask = Mat.Zeros(src.Rows, src.Cols, MatType.CV_8UC1))
                                {
                                    Cv2.Circle(mask, center, radius, Scalar.White, -1);
                                    src.CopyTo(((Mat)image), mask);
                                }
                                break;
                            case "ROI":
                            default:
                                image = ((Bitmap)InputImage)?.Clone();
                                return true;
                        }
                        TAlgorithm.Mat2Bitmap((Mat)image, out Bitmap bmp);
                        image = bmp;
                        return true;
                    }
                    else if (InputImage is HObject ho_image)
                    {
                        if (!ho_image.IsInitialized())
                            return false;
 
                        if (Params.Fixture == null)
                            Params.Fixture = new Fixture();
 
                        HObject hoDomainImage = null;
 
                        switch (Params.ROI?.GetType().Name)
                        {
                            case "HRectangle2":
                                HOperatorSet.GenRectangle2(out HObject hRectangle2, (HTuple)(Params.ROI.Row + Params.Fixture.Row), (HTuple)(Params.ROI.Column + Params.Fixture.Column)
                                    , (HTuple)(Params.ROI.Phi + Params.Fixture.Phi), (HTuple)((HRectangle2)Params.ROI).SemiLength1, (HTuple)((HRectangle2)Params.ROI).SemiLength2);
                                HOperatorSet.ReduceDomain(ho_image, hRectangle2, out hoDomainImage);
                                break;
                            case "HCircle":
                                HOperatorSet.GenCircle(out HObject hCircle, (HTuple)(Params.ROI.Row + Params.Fixture.Row), (HTuple)(Params.ROI.Column + Params.Fixture.Column)
                                    , (HTuple)((HCircle)Params.ROI).Radius);
                                HOperatorSet.ReduceDomain(ho_image, hCircle, out hoDomainImage);
                                break;
                            case "ROI":
                            default:
                                image = ho_image.CopyObj(1, -1);
                                return true;
                        }
 
                        image = hoDomainImage;
                        return true;
                    }
                    else if (InputImage is Mat)
                    {
                        Mat src = ((Mat)InputImage);
 
                        if (Params.Fixture == null)
                            Params.Fixture = new Fixture();
                        switch (Params.ROI?.GetType().Name)
                        {
                            case "HRectangle2":
                                // 1. 定义旋转矩形(中心点、大小、角度)
                                RotatedRect rotatedRect = new RotatedRect(
                                    new Point2f(Convert.ToSingle(Params.ROI.X + Params.Fixture.X)
                                    , Convert.ToSingle(Params.ROI.Y + Params.Fixture.Y)), // 中心坐标
                                    new Size2f(Convert.ToSingle(((HRectangle2)Params.ROI).Width)
                                    , Convert.ToSingle(((HRectangle2)Params.ROI).Height)),  // 宽度和高度
                                     Convert.ToSingle(((HRectangle2)Params.ROI).Angle + Params.Fixture.Angle)                   // 旋转角度(度)
                                );
 
                                // 2. 获取旋转矩形的4个顶点
                                Point2f[] vertices = rotatedRect.Points();
                                // 3. 修正超出边界的点坐标(关键步骤)
                                Point2f[] clampedPoints = vertices.Select(p =>
                                {
                                    float x = Clamp(p.X, 0, src.Width - 1);
                                    float y = Clamp(p.Y, 0, src.Height - 1);
                                    return new Point2f(x, y);
                                }).ToArray();
                                Rect boundingRect = Cv2.BoundingRect(clampedPoints);
 
                                // 4. 创建与原图大小相同的Mat,并初始化无效值
                                image = new Mat(src.Size(), src.Type());
                                ((Mat)image).SetTo(TAlgorithm.GetInvalidValueForMat(src)); // 全部初始化为无效值
 
                                // 5. 创建遮罩:在result上绘制旋转矩形(白色填充)
                                using (Mat cropped = new Mat(src, boundingRect))
                                {
                                    cropped.CopyTo(new Mat(((Mat)image), boundingRect));
                                }
 
                                break;
                            case "HCircle":
                                // 1. 定义圆形(圆心、半径)
                                Point center = new Point(Params.ROI.X + Params.Fixture.X
                                    , Params.ROI.Y + Params.Fixture.Y);
                                int radius = Convert.ToInt16(((HCircle)Params.ROI).Radius);
 
                                // 2. 创建与原图大小相同的Mat,并初始化无效值
                                image = new Mat(src.Size(), src.Type());
                                ((Mat)image).SetTo(TAlgorithm.GetInvalidValueForMat(src)); // 全部初始化为无效值
 
                                // 3. 创建圆形遮罩
                                using (Mat mask = Mat.Zeros(src.Rows, src.Cols, MatType.CV_8UC1))
                                {
                                    Cv2.Circle(mask, center, radius, Scalar.White, -1);
                                    src.CopyTo(((Mat)image), mask);
                                }
                                break;
                            case "ROI":
                            default:
                                image = ((Mat)InputImage)?.Clone();
                                return true;
                        }
                        return true;
                    }
                    else
                    {
                        image = null;
                        Msg = $"输入格式不正确{InputImage.GetType()}";
                        Result = false;
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    image = null;
                    Msg = $"裁剪区域失败,原因是:{ex.ToString()}";
                    Result = false;
                    return false;
                }
            }
        }
 
        public override void Dispose()
        {
            if (InputImage != null)
            {
                if (InputImage is HObject)
                    ((HObject)InputImage).Dispose();
                else if (InputImage is Mat)
                    ((Mat)InputImage).Dispose();
                else if (InputImage is Bitmap)
                    ((Bitmap)InputImage).Dispose();
 
                InputImage = null;
            }
 
            if (OutputImage != null)
            {
                if (OutputImage is HObject)
                    ((HObject)OutputImage).Dispose();
                else if (OutputImage is Mat)
                    ((Mat)OutputImage).Dispose();
                else if (OutputImage is Bitmap)
                    ((Bitmap)OutputImage).Dispose();
 
                OutputImage = null;
            }
 
            if (Record != null)
            {
                Record.Dispose();
                Record = null;
            }
        }
 
        public override object Clone()
        {
            try
            {
                var obj = (TAlgorithm)MemberwiseClone(); // 浅拷贝
 
                // 手动处理引用类型
                if (InputImage != null)
                {
                    if (InputImage is HObject ho_image && ho_image.IsInitialized())
                        obj.InputImage = ho_image.CopyObj(1, -1);
                    else if (InputImage is Mat mat && !mat.Empty())
                        obj.InputImage = mat.Clone();
                    else if (InputImage is Bitmap bitmap)
                        obj.InputImage = bitmap.Clone();
                }
 
                return obj;
            }
            catch { return (TAlgorithm)MemberwiseClone(); }
        }
 
        public override void InitRunParams()
        {
            Result = true;
            bCompleted = false;
            Msg = "";
            if (OutputImage != null)
            {
                if (OutputImage is HObject)
                    ((HObject)OutputImage).Dispose();
                else if (OutputImage is Mat)
                    ((Mat)OutputImage).Dispose();
                else if (OutputImage is Bitmap)
                    ((Bitmap)OutputImage).Dispose();
                OutputImage = null;
            }
 
            if (Record != null)
                Record.Dispose();
        }
 
        public override bool Run()
        {
            DateTime StartTime = DateTime.Now;
 
            InitRunParams();
            HOperatorSet.GenEmptyObj(out HObject EmptyObj);
            OutputImage = EmptyObj;
 
            // 创建并启动任务
            Task.Factory.StartNew(() => { TAlgorithmMain(); });
 
            while ((DateTime.Now - StartTime).TotalMilliseconds <= MaxTimeOut)
            {
                if (bCompleted)
                {
                    RunTime = (DateTime.Now - StartTime).TotalMilliseconds;
                    return Result;
                }
 
                Thread.Sleep(30);
            }
 
            Msg = "运行超时";
            Result = false;
            RunTime = (DateTime.Now - StartTime).TotalMilliseconds;
            return false;
        }
 
        /// <summary>
        /// 算子逻辑
        /// </summary>
        public virtual void TAlgorithmMain()
        {
            bCompleted = true;
            Debug.WriteLine("任务完成");
        }
 
        /// <summary>
        /// 加载算法
        /// </summary>
        /// <param name="fullPath">完整路径带.json</param>
        /// <returns></returns>
        public override bool Load(string fullPath = "")
        {
            try
            {
                if (string.IsNullOrEmpty(fullPath))
                    return false;
 
                if (!fullPath.Contains(".json"))
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
                if (string.IsNullOrEmpty(fullPath) || fullPath.Trim() == "")
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
 
                // 获取不带文件名的目录路径
                string directoryPath = Path.GetDirectoryName(fullPath);
                strProcessName = Path.GetFileNameWithoutExtension(fullPath);
 
                if (!File.Exists(fullPath))
                {
                    Debug.WriteLine("文件不存在创建空文件");
                    Save(directoryPath);
                    return true;
                }
 
                string strJson = string.Empty;
                using (StreamReader streamReader = new StreamReader(fullPath, Encoding.UTF8))
                {
                    strJson = streamReader.ReadToEnd();
                    streamReader.Close();
                }
                Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
                if (Params == null)
                    return false;
 
                Params.FixDeserializedData();
                return true;
            }
            catch { return false; }
        }
 
        /// <summary>
        /// 保存算法
        /// </summary>
        /// <param name="filePath">不带.json</param>
        /// <returns></returns>
        public override bool Save(string filePath = "")
        {
            try
            {
                if (string.IsNullOrEmpty(filePath) || filePath.Trim() == "")
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
 
                string strJson = string.Empty;
                var settings = new JsonSerializerSettings
                {
                    Formatting = Newtonsoft.Json.Formatting.Indented,
                    // 自定义缩进(4空格)
                    ContractResolver = new DefaultContractResolver
                    {
                        NamingStrategy = new CamelCaseNamingStrategy()
                    }
                };
                strJson = JsonConvert.SerializeObject(Params, settings);
 
                Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
                //判断文件夹是否存在,防呆输入为文件名称
                if (!Directory.Exists(filePath))
                {
                    try
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    catch (Exception)
                    { }
                }
                File.WriteAllText(filePath + "//" + strProcessName + ".json", strJson, Encoding.UTF8);
                return true;
            }
            catch { return false; }
        }
 
        #region 自定义的算法
        // 通用版本的 Clamp 方法
        public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
        {
            if (value.CompareTo(min) < 0)
                return min;
            else if (value.CompareTo(max) > 0)
                return max;
            else
                return value;
        }
 
        public static void Bitmap2HObject(Bitmap bmp, out HObject image)
        {
            BitmapData srcBmpData;
 
            try
            {
                if (bmp == null || bmp.Width == 0 || bmp.Height == 0)
                {
                    image = null;
                    return;
                }
 
                lock (bmp)
                {
                    switch (bmp.PixelFormat)
                    {
                        case PixelFormat.Format24bppRgb:
                            srcBmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
 
                            int width = bmp.Width;
                            int height = bmp.Height;
                            int stride = srcBmpData.Stride;
 
                            if (stride == width * 3)
                                HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
                            else
                                image = HandleStrideAlignmentBest(srcBmpData.Scan0, width, height, stride);
 
                            bmp.UnlockBits(srcBmpData);
                            break;
                        default:
                            srcBmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                            HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
                            bmp.UnlockBits(srcBmpData);
                            break;
                    }
                }
                return;
            }
            catch
            {
                image = null;
                return;
            }
        }
 
        private static HObject HandleStrideAlignmentBest(IntPtr scan0, int width, int height, int stride)
        {
            int expectedStride = width * 3;
            byte[] alignedData = new byte[width * height * 3];
 
            unsafe
            {
                byte* srcPtr = (byte*)scan0;
                fixed (byte* dstPtr = alignedData)
                {
                    // 使用最快速的复制方法
 
                    // .NET Core 3.0+ 使用 Buffer.MemoryCopy
                    for (int y = 0; y < height; y++)
                    {
                        Buffer.MemoryCopy(
                            srcPtr + y * stride,
                            dstPtr + y * expectedStride,
                            expectedStride,
                            expectedStride
                        );
                    }
                    HOperatorSet.GenImageInterleaved(out HObject ho_img, new IntPtr(dstPtr), "bgr", width, height, 0, "byte", width, height, 0, 0, -1, 0);
                    return ho_img;
                }
            }
        }
 
        public static unsafe void HObject2Bitmap(HObject hObject, out Bitmap bmp)
        {
            try
            {
                if (hObject == null || !hObject.IsInitialized())
                {
                    bmp = null;
                    return;
                }
 
                // 获取图像信息
                HOperatorSet.GetImageSize(hObject, out HTuple width, out HTuple height);
                HOperatorSet.CountChannels(hObject, out HTuple channels);
 
                if (channels.I == 1)
                {
                    // 灰度图处理
                    HTuple ptr, type;
                    HOperatorSet.GetImagePointer1(hObject, out ptr, out type, out width, out height);
 
                    bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
 
                    // 设置灰度调色板
                    ColorPalette palette = bmp.Palette;
                    for (int i = 0; i < 256; i++)
                        palette.Entries[i] = Color.FromArgb(i, i, i);
                    bmp.Palette = palette;
 
                    // 直接内存拷贝
                    BitmapData bmpData = bmp.LockBits(
                        new Rectangle(0, 0, width, height),
                        ImageLockMode.WriteOnly,
                        PixelFormat.Format8bppIndexed);
 
                    Buffer.MemoryCopy(
                      (void*)ptr.L,
                      (void*)bmpData.Scan0,
                      width * height,
                      width * height);
 
                    bmp.UnlockBits(bmpData);
                    return;
                }
                else
                {
                    // 彩色图处理(BGR顺序)
                    HTuple ptrR, ptrG, ptrB, type;
                    HOperatorSet.GetImagePointer3(hObject, out ptrR, out ptrG, out ptrB, out type, out width, out height);
 
                    bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
                    BitmapData bmpData = bmp.LockBits(
                        new Rectangle(0, 0, width, height),
                        ImageLockMode.WriteOnly,
                        PixelFormat.Format24bppRgb);
 
                    byte* dstPtr = (byte*)bmpData.Scan0;
                    byte* srcR = (byte*)ptrR.L;
                    byte* srcG = (byte*)ptrG.L;
                    byte* srcB = (byte*)ptrB.L;
 
                    Parallel.For(0, height.I, y =>
                    {
                        int srcOffset = y * width;
                        int dstOffset = y * bmpData.Stride;
 
                        for (int x = 0; x < width; x++)
                        {
                            dstPtr[dstOffset + x * 3] = srcB[srcOffset + x];     // B
                            dstPtr[dstOffset + x * 3 + 1] = srcG[srcOffset + x]; // G
                            dstPtr[dstOffset + x * 3 + 2] = srcR[srcOffset + x]; // R
                        }
                    });
 
                    bmp.UnlockBits(bmpData);
                    return;
                }
            }
            catch
            {
                bmp = null;
                return;
            }
        }
 
        public static void Mat2HObject(Mat mat, out HObject image)
        {
            try
            {
                if (mat == null || mat.Empty())
                {
                    image = null;
                    return;
                }
 
                if (mat.Type() == MatType.CV_8UC3) // 彩色图像 (BGR)
                {
                    HOperatorSet.GenImageInterleaved(
                        out image,
                        mat.Data,
                        "bgr",
                        mat.Width,
                        mat.Height,
                        0,
                        "byte",
                        0, 0, 0, 0, -1, 0);
                }
                else if (mat.Type() == MatType.CV_8UC1) // 灰度图像
                {
                    HOperatorSet.GenImage1(
                        out image,
                        "byte",
                        mat.Width,
                        mat.Height,
                        mat.Data);
                }
                else
                {
                    throw new ArgumentException($"Mat2HObject不支持的图像格式:{mat.Type()}");
                    return;
                }
                return;
            }
            catch
            {
                image = null;
                return;
            }
        }
 
        public static void HObject2Mat(HObject image, out Mat mat)
        {
            try
            {
                if (image == null || !image.IsInitialized())
                {
                    mat = null;
                    return;
                }
 
                // 获取图像类型信息
                HOperatorSet.CountChannels(image, out HTuple channels);
 
                // 根据通道数选择不同的处理方式
                if (channels.I == 1)
                {
                    mat = ConvertGrayImage(image);
                }
                else if (channels.I == 3)
                {
                    mat = ConvertColorImage(image);
                }
                else
                {
                    mat = null;
                    return;
                }
            }
            catch
            {
                mat = null;
                return;
            }
        }
 
        public static Mat ConvertColorImage(HObject colorimage)
        {
            // 获取Halcon图像指针
            HOperatorSet.GetImagePointer3(colorimage,
                out HTuple ptrR, out HTuple ptrG, out HTuple ptrB,
                out HTuple type, out HTuple width, out HTuple height);
 
            // 创建OpenCV Mat(注意BGR顺序)
            Mat mat = new Mat(height.I, width.I, MatType.CV_8UC3);
 
            unsafe
            {
                byte* matPtr = (byte*)mat.Data;
                byte[] r = new byte[width * height];
                byte[] g = new byte[width * height];
                byte[] b = new byte[width * height];
 
                // 从Halcon拷贝数据
                Marshal.Copy(ptrR.IP, r, 0, r.Length);
                Marshal.Copy(ptrG.IP, g, 0, g.Length);
                Marshal.Copy(ptrB.IP, b, 0, b.Length);
 
                // 填充Mat(Halcon的RGB → OpenCV的BGR)
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        int idx = i * width + j;
                        matPtr[i * mat.Step() + j * 3] = b[idx];     // B
                        matPtr[i * mat.Step() + j * 3 + 1] = g[idx]; // G
                        matPtr[i * mat.Step() + j * 3 + 2] = r[idx]; // R
                    }
                }
            }
            return mat;
        }
 
        public static Mat ConvertGrayImage(HObject grayImage)
        {
            // 获取单通道图像指针
            HOperatorSet.GetImagePointer1(grayImage, out HTuple ptrGray,
                out HTuple type, out HTuple width, out HTuple height);
 
            // 创建OpenCV灰度Mat
            Mat mat = new Mat(height.I, width.I, MatType.CV_8UC1);
 
            //获取图像数据总字节数
            int totalBytes = width.I * height.I;
            // 使用非托管内存复制
            unsafe
            {
                Buffer.MemoryCopy(
                    (void*)ptrGray.IP,     // 源指针 (Halcon)
                    (void*)mat.Data,       // 目标指针 (OpenCV)
                    totalBytes,            // 目标缓冲区大小
                    totalBytes             // 要复制的字节数
                );
            }
            // 拷贝数据
            //Marshal.Copy(ptrGray.IP, mat.Data, 0, width.I * height.I);
 
            return mat;
        }
 
        public static void Bitmap2Mat(Bitmap bmp, out Mat image)
        {
            try
            {
                if (bmp == null)
                {
                    image = null;
                    return;
                }
 
                image = Extensions.BitmapConverter.ToMat(bmp);
                return;
            }
            catch
            {
                image = null;
                return;
            }
        }
 
        public static void Mat2Bitmap(Mat mat, out Bitmap image)
        {
            try
            {
                if (mat == null)
                {
                    image = null;
                    return;
                }
 
                image = Extensions.BitmapConverter.ToBitmap(mat);
            }
            catch
            {
                image = null;
                return;
            }
        }
 
        public static void Bitmap2BinaryBitmap(Bitmap bitmap, out BinaryBitmap binarybitmap)
        {
            BinaryBitmap bbmp;
            try
            {
                if (bitmap != null)
                {
                    LuminanceSource source = new BitmapLuminanceSource(bitmap);
                    //var binarizer = new GlobalHistogramBinarizer(source);
                    var binarizer = new HybridBinarizer(source); // 或者用这个
                    bbmp = new BinaryBitmap(binarizer);
                    binarybitmap = bbmp;
                }
                else
                {
                    binarybitmap = null;
                }
            }
            catch
            {
                binarybitmap = null;
            }
        }
 
        public static void BinaryBitmap2Bitmap(BinaryBitmap binarybitmap, out Bitmap bitmap)
        {
            try
            {
                if (binarybitmap != null)
                {
                    BitMatrix bitmatrix = binarybitmap.BlackMatrix;
                    bitmap = new Bitmap(bitmatrix.Width, bitmatrix.Height);
                    for (int i = 0; i < bitmatrix.Width; i++)
                    {
                        for (int j = 0; j < bitmatrix.Height; j++)
                            bitmap.SetPixel(i, j, bitmatrix[i, j] ? Color.Black : Color.White);
                    }
                }
                else
                {
                    bitmap = null;
                }
            }
            catch
            {
                bitmap = null;
            }
        }
 
        public static void DeepImage2PointsCloud(HObject image, out HTuple pointsCloud)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
 
            HObject ho_ImageZ, ho_ImageX, ho_ImageY, ho_Region;
            HObject ho_ImageReduced, ho_Z, ho_ImageScaled;
 
            // Local control variables 
 
            HTuple hv_xResolution = new HTuple(), hv_yResolution = new HTuple();
            HTuple hv_zResolution = new HTuple(), hv_ScaleFactor = new HTuple();
            HTuple hv_WindowHandle = new HTuple(), hv_Width = new HTuple();
            HTuple hv_Height = new HTuple(), hv_Min = new HTuple();
            HTuple hv_Max = new HTuple(), hv_Range = new HTuple();
            HTuple hv_factor = new HTuple(), hv_ScaleZ = new HTuple();
            HTuple hv_zScale = new HTuple();
            HTuple hv_Instructions = new HTuple(), hv_PoseOut = new HTuple();
            pointsCloud = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ImageZ);
            HOperatorSet.GenEmptyObj(out ho_ImageX);
            HOperatorSet.GenEmptyObj(out ho_ImageY);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Z);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            try
            {
                //读取深度图
                ho_ImageZ.Dispose();
                ho_ImageZ = image;
 
                //确定xyz的显示分辨率,即确定缩放因子
                hv_xResolution.Dispose();
                hv_xResolution = 1;
                hv_yResolution.Dispose();
                hv_yResolution = 1;
                hv_zResolution.Dispose();
                hv_zResolution = 1;
                //确定缩放因子
                hv_ScaleFactor.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_ScaleFactor = new HTuple();
                    hv_ScaleFactor = hv_ScaleFactor.TupleConcat(hv_xResolution, hv_yResolution, hv_zResolution);
                }
                //获取窗口句柄
                if (HDevWindowStack.IsOpen())
                {
                    hv_WindowHandle = HDevWindowStack.GetActive();
                }
                //获取图像大小
                hv_Width.Dispose(); hv_Height.Dispose();
                HOperatorSet.GetImageSize(ho_ImageZ, out hv_Width, out hv_Height);
 
 
                //生成X曲面图像
                ho_ImageX.Dispose();
                HOperatorSet.GenImageSurfaceFirstOrder(out ho_ImageX, "real", 0, 1, 0, 0, 0,
                    hv_Width, hv_Height);
                //生成Y曲面图像
                ho_ImageY.Dispose();
                HOperatorSet.GenImageSurfaceFirstOrder(out ho_ImageY, "real", 1, 0, 0, 0, 0,
                    hv_Width, hv_Height);
 
 
                //对深度图进行处理滤波处理
 
                //获取深度图最小值 Min 和最大值 Max,以及范围 Range
                hv_Min.Dispose(); hv_Max.Dispose(); hv_Range.Dispose();
                HOperatorSet.MinMaxGray(ho_ImageZ, ho_ImageZ, 0, out hv_Min, out hv_Max, out hv_Range);
 
                //如果最小值小于0,则图像乘1,加-min,即保持最低值为0
                if ((int)(new HTuple(hv_Min.TupleLess(0.0))) != 0)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.ScaleImage(ho_ImageZ, out ExpTmpOutVar_0, 1.0, -hv_Min);
                        ho_ImageZ.Dispose();
                        ho_ImageZ = ExpTmpOutVar_0;
                    }
                }
                //获取新深度图的最小值 Min 和最大值 Max,以及范围 Range
                hv_Min.Dispose(); hv_Max.Dispose(); hv_Range.Dispose();
                HOperatorSet.MinMaxGray(ho_ImageZ, ho_ImageZ, 0, out hv_Min, out hv_Max, out hv_Range);
                // 检查Min和Max的值
                Debug.WriteLine($"Min: {hv_Min.D}, Max: {hv_Max.D}");
                //阈值提取(核心)
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageZ, out ho_Region
                    , hv_Max.D < 1.0 ? 0 : 1, hv_Max);
                //获取区域图像
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_ImageZ, ho_Region, out ho_ImageReduced);
                //转实数图
                ho_Z.Dispose();
                HOperatorSet.ConvertImageType(ho_ImageReduced, out ho_Z, "real");
                //获取新深度图的最小值 Min 和最大值 Max,以及范围 Range
                hv_Min.Dispose(); hv_Max.Dispose(); hv_Range.Dispose();
                HOperatorSet.MinMaxGray(ho_Z, ho_Z, 0, out hv_Min, out hv_Max, out hv_Range);
                //计算缩放因子 factor = xResolution / zResolution,然后计算 Z 方向上的缩放比例 ScaleZ = 1 / factor
                hv_factor.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_factor = hv_xResolution / hv_zResolution;
                }
                hv_ScaleZ.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_ScaleZ = 1 / hv_factor;
                }
                //输出
                hv_zScale.Dispose();
                hv_zScale = new HTuple(hv_ScaleZ);
                //将 Z 按照缩放比例 ScaleZ 进行缩放,并存储在 ImageScaled 中
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_ImageScaled.Dispose();
                    HOperatorSet.ScaleImage(ho_Z, out ho_ImageScaled, hv_ScaleZ, (-hv_ScaleZ) * hv_Min);
                }
 
                //把X,Y,Z图像拟合为3D点云模型
                pointsCloud.Dispose();
                HOperatorSet.XyzToObjectModel3d(ho_ImageX, ho_ImageY, ho_ImageScaled, out pointsCloud);
                //HOperatorSet.WriteObjectModel3d(hv_ObjectModel3D, "om3", "test", new HTuple(),
                //    new HTuple());
                if (hv_Instructions == null)
                    hv_Instructions = new HTuple();
                hv_Instructions[0] = "";
 
                //objectModel3D = hv_ObjectModel3D.Clone();
                //// 持久化生成的3D模型
                //HOperatorSet.WriteObjectModel3d(objectModel3D, "om3", "pcl.om3", new HTuple(), new HTuple());
                //// 提示生成3D模型成功
                //MessageBox.Show("生成3D模型OK");
 
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_ImageZ.Dispose();
                ho_ImageX.Dispose();
                ho_ImageY.Dispose();
                ho_Region.Dispose();
                ho_ImageReduced.Dispose();
                ho_Z.Dispose();
                ho_ImageScaled.Dispose();
 
                hv_xResolution.Dispose();
                hv_yResolution.Dispose();
                hv_zResolution.Dispose();
                hv_ScaleFactor.Dispose();
                hv_WindowHandle.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_Min.Dispose();
                hv_Max.Dispose();
                hv_Range.Dispose();
                hv_factor.Dispose();
                hv_ScaleZ.Dispose();
                hv_zScale.Dispose();
                hv_Instructions.Dispose();
                hv_PoseOut.Dispose();
 
                throw HDevExpDefaultException;
            }
            finally
            {
                ho_ImageZ.Dispose();
                ho_ImageX.Dispose();
                ho_ImageY.Dispose();
                ho_Region.Dispose();
                ho_ImageReduced.Dispose();
                ho_Z.Dispose();
                ho_ImageScaled.Dispose();
 
                hv_xResolution.Dispose();
                hv_yResolution.Dispose();
                hv_zResolution.Dispose();
                hv_ScaleFactor.Dispose();
                hv_WindowHandle.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_Min.Dispose();
                hv_Max.Dispose();
                hv_Range.Dispose();
                hv_factor.Dispose();
                hv_ScaleZ.Dispose();
                hv_zScale.Dispose();
                hv_Instructions.Dispose();
                hv_PoseOut.Dispose();
            }
        }
 
        public static void DeepImage2PointsCloud(HObject image, out HTuple pointsCloud
            , double XScale = 1, double YScale = 1, double ZScale = 1, int ZOffset = 0
            , int InvalidValue = 0, int minColor = 0, int maxColor = 180, double grayPercent = 0.8)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
            HObject ho_ImageH;
            HObject ho_ImageZ, ho_ImageX, ho_ImageY, ho_Region;
            HObject ho_ImageReduced, ho_Z, ho_ImageScaled;
 
            // Local control variables 
 
            HTuple hv_xResolution = new HTuple(), hv_yResolution = new HTuple();
            HTuple hv_zResolution = new HTuple(), hv_ScaleFactor = new HTuple();
            HTuple hv_WindowHandle = new HTuple(), hv_Width = new HTuple();
            HTuple hv_Height = new HTuple(), hv_Min = new HTuple();
            HTuple hv_Max = new HTuple(), hv_Range = new HTuple();
            HTuple hv_factor = new HTuple(), hv_ScaleZ = new HTuple();
            HTuple hv_zScale = new HTuple();
            HTuple hv_Instructions = new HTuple(), hv_PoseOut = new HTuple();
            pointsCloud = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ImageH);
            HOperatorSet.GenEmptyObj(out ho_ImageZ);
            HOperatorSet.GenEmptyObj(out ho_ImageX);
            HOperatorSet.GenEmptyObj(out ho_ImageY);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Z);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            try
            {
                //获取原图像
                ho_ImageH.Dispose();
                HOperatorSet.CopyImage(image, out ho_ImageH);
 
                hv_Width.Dispose(); hv_Height.Dispose();
                HOperatorSet.GetImageSize(image, out hv_Width, out hv_Height);
 
                //读取深度图
                ho_ImageZ.Dispose();
                HOperatorSet.GetDomain(ho_ImageH, out HObject ho_ImageDomain);
 
                //获取深度图最小值 Min 和最大值 Max,以及范围 Range
                hv_Min.Dispose(); hv_Max.Dispose(); hv_Range.Dispose();
                HOperatorSet.MinMaxGray(ho_ImageDomain, ho_ImageH, 0, out hv_Min, out hv_Max, out hv_Range);
 
                // 检查Min和Max的值
                Debug.WriteLine($"Min: {hv_Min.D}, Max: {hv_Max.D}");
                //如果最小值小于0,则图像乘1,加-min,即保持最低值为0
                //if ((int)(new HTuple(hv_Min.TupleLess(0.0))) != 0)
                //{
                //    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                //    {
                //        HObject ExpTmpOutVar_0;
                //        HOperatorSet.ScaleImage(ho_ImageZ, out ExpTmpOutVar_0, 1.0, -hv_Min);
                //        ho_ImageZ.Dispose();
                //        ho_ImageZ = ExpTmpOutVar_0;
                //    }
                //}
 
                //阈值提取(核心)
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageH, out ho_Region
                    , hv_Min.D > 0 && InvalidValue > 0 ? InvalidValue : 0, hv_Max);
 
                //转实数图
                HOperatorSet.ConvertImageType(ho_ImageH, out ho_ImageH, "real");
 
                //将 Z 按照缩放比例 ScaleZ 进行缩放,并存储在 ImageScaled 中
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_ImageScaled.Dispose();
                    HOperatorSet.ScaleImage(ho_ImageH, out ho_ImageScaled, ZScale, ZScale * ZOffset);
                }
 
                //获取新深度图的最小值 Min 和最大值 Max,以及范围 Range
                hv_Min.Dispose(); hv_Max.Dispose(); hv_Range.Dispose();
                HOperatorSet.MinMaxGray(ho_Region, ho_ImageScaled, 0, out hv_Min, out hv_Max, out hv_Range);
 
                //将高度图转为HSV格式的图像
                double k = (maxColor - minColor) * 1.0 / (hv_Max.D - hv_Min.D);
                double b = maxColor - k * hv_Min.D * 1.0;
                HOperatorSet.ReduceDomain(ho_ImageScaled, ho_Region, out HObject ho_ImageZOut);
                //
                HOperatorSet.ScaleImage(ho_ImageZOut, out HObject ho_ImageZOutScaled, k, b);
                HOperatorSet.ConvertImageType(ho_ImageZOutScaled, out HObject ho_ImageZByte, "byte");
                HOperatorSet.GenImageProto(ho_ImageZByte, out HObject ho_ImageZProto, 255);
                HOperatorSet.TransToRgb(ho_ImageZByte, ho_ImageZProto, ho_ImageZProto
                    , out HObject H, out HObject S, out HObject V, "hsv");
                HOperatorSet.Compose3(H, S, V, out HObject HSV);
 
                //生成X曲面图像
                ho_ImageX.Dispose();
                HOperatorSet.GenImageSurfaceFirstOrder(out ho_ImageX, "real", 0, XScale, 0, 0, 0,
                    hv_Width, hv_Height);
                //生成Y曲面图像
                ho_ImageY.Dispose();
                HOperatorSet.GenImageSurfaceFirstOrder(out ho_ImageY, "real", YScale, 0, 0, 0, 0,
                    hv_Width, hv_Height);
 
                //把X,Y,Z图像拟合为3D点云模型
                pointsCloud.Dispose();
                HOperatorSet.XyzToObjectModel3d(ho_ImageX, ho_ImageY, ho_ImageZOut, out pointsCloud);
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_ImageZ.Dispose();
                ho_ImageX.Dispose();
                ho_ImageY.Dispose();
                ho_Region.Dispose();
                ho_ImageReduced.Dispose();
                ho_Z.Dispose();
                ho_ImageScaled.Dispose();
 
                hv_xResolution.Dispose();
                hv_yResolution.Dispose();
                hv_zResolution.Dispose();
                hv_ScaleFactor.Dispose();
                hv_WindowHandle.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_Min.Dispose();
                hv_Max.Dispose();
                hv_Range.Dispose();
                hv_factor.Dispose();
                hv_ScaleZ.Dispose();
                hv_zScale.Dispose();
                hv_Instructions.Dispose();
                hv_PoseOut.Dispose();
 
                throw HDevExpDefaultException;
            }
            finally
            {
                ho_ImageH.Dispose();
                ho_ImageZ.Dispose();
                ho_ImageX.Dispose();
                ho_ImageY.Dispose();
                ho_Region.Dispose();
                ho_ImageReduced.Dispose();
                ho_Z.Dispose();
                ho_ImageScaled.Dispose();
 
                hv_xResolution.Dispose();
                hv_yResolution.Dispose();
                hv_zResolution.Dispose();
                hv_ScaleFactor.Dispose();
                hv_WindowHandle.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_Min.Dispose();
                hv_Max.Dispose();
                hv_Range.Dispose();
                hv_factor.Dispose();
                hv_ScaleZ.Dispose();
                hv_zScale.Dispose();
                hv_Instructions.Dispose();
                hv_PoseOut.Dispose();
            }
        }
 
        public static void PointsCloud2DeepImage(HTuple pointsCloud, out HObject image, double Resolution = 1.0)
        {
            HOperatorSet.GenEmptyObj(out image);
            image = null;
            try
            {
                HOperatorSet.GetObjectModel3dParams(pointsCloud, "point_coord_z", out HTuple hv_z);
                HOperatorSet.GetObjectModel3dParams(pointsCloud, "point_coord_y", out HTuple hv_y);
                HOperatorSet.GetObjectModel3dParams(pointsCloud, "point_coord_x", out HTuple hv_x);
 
                HOperatorSet.TupleMin(hv_x, out HTuple hv_Min);
                HTuple hv_xTemp = hv_x - hv_Min;
                hv_x = hv_xTemp;
                hv_xTemp = ((hv_x / Resolution)).TupleInt();
                hv_x.Dispose();
                hv_x = hv_xTemp;
 
                HOperatorSet.TupleMin(hv_y, out hv_Min);
                HTuple hv_yTemp = hv_y - hv_Min;
                hv_y = hv_yTemp;
                hv_yTemp = ((hv_y / Resolution)).TupleInt();
                hv_y.Dispose();
                hv_y = hv_yTemp;
 
                HOperatorSet.TupleMin(hv_z, out hv_Min);
                HTuple hv_zTemp = hv_z - hv_Min;
                hv_z.Dispose();
                hv_z = hv_zTemp;
 
                HOperatorSet.TupleMax(hv_x, out HTuple hv_xMax);
                HOperatorSet.TupleMax(hv_y, out HTuple hv_yMax);
                HOperatorSet.GenImageConst(out image, "real", hv_xMax + 1, hv_yMax + 1);
                HOperatorSet.SetGrayval(image, hv_y, hv_x, hv_z);
 
                //HOperatorSet.GetImageSize(ho_Image, out HTuple hv_Width, out HTuple hv_Height);
                //HOperatorSet.GenRectangle1(out HObject ho_Rectangle, 0, 0, hv_Height - 1, hv_Width - 1);
                //HOperatorSet.GetRegionPoints(ho_Rectangle, out HTuple hv_Rows, out HTuple hv_Columns);
                //HOperatorSet.GetGrayval(ho_Image, hv_Rows, hv_Columns, out HTuple hv_Z);
            }
            catch { image = null; }
        }
 
        /// <summary>
        /// 计算两点的距离
        /// </summary>
        /// <param name="startPoint"></param>
        /// <param name="endPoint"></param>
        /// <returns></returns>
        public static double GetDistanceP2P(HPoint startPoint, HPoint endPoint)
        {
            try
            {
                return Math.Sqrt(Math.Pow(startPoint.X - endPoint.X, 2) + Math.Pow(startPoint.Y - endPoint.Y, 2));
            }
            catch { return 9994; }
        }
 
        public static double GetDistanceP2P(Point startPoint, Point endPoint)
        {
            return GetDistanceP2P(new HPoint(startPoint), new HPoint(endPoint));
        }
 
        public static double DistanceP2P(double startX, double startY, double endX, double endY)
        {
            return GetDistanceP2P(new HPoint(startX, startY), new HPoint(endX, endY));
        }
 
        /// <summary>
        /// 获取两点的中点
        /// </summary>
        /// <param name="startPoint"></param>
        /// <param name="endPoint"></param>
        /// <returns></returns>
        public static HPoint GetMidPoint(HPoint startPoint, HPoint endPoint)
        {
            return new HPoint((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
        }
 
        public static Point GetMidPoint(Point startPoint, Point endPoint)
        {
            return new Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
        }
 
        public static System.Drawing.Point GetMidPoint(System.Drawing.Point startPoint, System.Drawing.Point endPoint)
        {
            return new System.Drawing.Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
        }
 
        /// <summary>
        /// 判断点是否在线段上
        /// </summary>
        /// <param name="point"></param>
        /// <param name="segment"></param>
        /// <returns></returns>
        public static bool IsPointOnSegment(HPoint pt, HSegment segment, double tolerance = 1e-3)
        {
            // 计算直线方程的系数
            double A = segment.EndY - segment.StartX;
            double B = segment.StartX - segment.EndX;
            double C = segment.EndX * segment.StartY - segment.StartX * segment.EndY;
 
            // 计算点到直线的距离
            double distance = Math.Abs(A * pt.X + B * pt.Y + C) / Math.Sqrt(A * A + B * B);
 
            // 允许一个很小的误差
            if (distance < tolerance)
            {
                // 判断点是否在直线段范围内
                if (Math.Min(segment.StartX, segment.EndX) <= pt.X && pt.X <= Math.Max(segment.StartX, segment.EndX)
                 && Math.Min(segment.StartY, segment.EndY) <= pt.Y && pt.Y <= Math.Max(segment.StartY, segment.EndY))
                {
                    return true;
                }
            }
            return false;
        }
 
        public static bool IsPointOnSegment(double px, double py, double x1, double y1, double x2, double y2)
        {
            return IsPointOnSegment(new HPoint(px, py), new HSegment(x1, y1, x2, y2));
        }
 
        /// <summary>
        /// 判断点是否在矩形边附近
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="rect"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        static bool IsPointNearRectangleSilde(System.Drawing.Point pt, Rectangle rect, double tolerance = 100)
        {
            try
            {
                // 如果点的 X 坐标等于矩形的左边 (rect.Left) 或右边 (rect.Right),并且 Y 坐标在矩形的上下边界之间,那么点在矩形的垂直边界上。
                // 如果点的 Y 坐标等于矩形的上边(rect.Top) 或下边(rect.Bottom),并且 X 坐标在矩形的左右边界之间,那么点在矩形的水平边界上。
                return (Math.Abs(pt.X - rect.Left) <= tolerance || Math.Abs(pt.X - rect.Right) <= tolerance) && pt.Y >= rect.Top + tolerance && pt.Y <= rect.Bottom - tolerance
                    || (Math.Abs(pt.Y - rect.Top) <= tolerance || Math.Abs(pt.Y - rect.Bottom) <= tolerance) && pt.X >= rect.Left - tolerance && pt.X <= rect.Right + tolerance;
 
            }
            catch { return false; }
        }
 
        public static bool IsPointNearRectangleSilde(HPoint pt, HRectangle2 rect, double tolerance = 100)
        {
            return IsPointNearRectangleSilde(new System.Drawing.Point((int)pt.X, (int)pt.Y), new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height), tolerance);
        }
 
        /// <summary>
        /// 判断点是否在点附近
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="pt2"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        public static bool IsPointNearPoint(HPoint pt1, HPoint pt2, double tolerance = 100)
        {
            if (GetDistanceP2P(pt1, pt2) <= tolerance)
                return true;
            return false;
        }
 
        public static bool IsPointNearPoint(Point pt1, Point pt2, double tolerance = 100)
        {
            if (GetDistanceP2P(pt1, pt2) <= tolerance)
                return true;
            return false;
        }
 
        public static bool IsPointNearPoint(double x1, double y1, double x2, double y2, int tolerance = 100)
        {
            return IsPointNearPoint(new HPoint(x1, y1), new HPoint(x2, y2), tolerance);
        }
 
        /// <summary>
        /// 判断点是否在矩形角上
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="rect"></param>
        /// <param name="corner"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        public static bool IsPointNearRectangleCorner(Point pt, Rectangle rect, out string corner, double tolerance = 10)
        {
            try
            {
                //按顺时针去匹配角位
                Point LeftTopPoint = new Point(rect.Left, rect.Top);
                if (IsPointNearPoint(LeftTopPoint, pt, tolerance))
                {
                    corner = "LeftTop";
                    return true;
                }
 
                Point RightTopPoint = new Point(rect.Right, rect.Top);
                if (IsPointNearPoint(RightTopPoint, pt, tolerance))
                {
                    corner = "RightTop";
                    return true;
                }
 
                Point RightBtmPoint = new Point(rect.Right, rect.Bottom);
                if (IsPointNearPoint(RightBtmPoint, pt, tolerance))
                {
                    corner = "RightBtm";
                    return true;
                }
 
                Point LeftBtmPoint = new Point(rect.Left, rect.Bottom);
                if (IsPointNearPoint(LeftBtmPoint, pt, tolerance))
                {
                    corner = "LeftBtm";
                    return true;
                }
 
                corner = "";
                return false;
            }
            catch { corner = ""; return false; }
        }
 
        public static bool IsPointNearRectangleCorner(HPoint pt, HRectangle2 rect, out string corner, double tolerance = 10)
        {
            try
            {
                //按顺时针去匹配角位
                var rectCorners = rect.Corners;
                HPoint LeftTopPoint = rectCorners[0];
                if (IsPointNearPoint(LeftTopPoint, pt, tolerance))
                {
                    corner = "LeftTop";
                    return true;
                }
 
                HPoint RightTopPoint = rectCorners[1];
                if (IsPointNearPoint(RightTopPoint, pt, tolerance))
                {
                    corner = "RightTop";
                    return true;
                }
 
                HPoint RightBtmPoint = rectCorners[2];
                if (IsPointNearPoint(RightBtmPoint, pt, tolerance))
                {
                    corner = "RightBtm";
                    return true;
                }
 
                HPoint LeftBtmPoint = rectCorners[3];
                if (IsPointNearPoint(LeftBtmPoint, pt, tolerance))
                {
                    corner = "LeftBtm";
                    return true;
                }
 
                corner = "";
                return false;
            }
            catch { corner = ""; return false; }
        }
 
        /// <summary>
        /// 计算两线的交点
        /// </summary>
        /// <param name="p1">线1起始点</param>
        /// <param name="p2"></param>
        /// <param name="p3">线2起始点</param>
        /// <param name="p4"></param>
        /// <returns></returns>
        public static Point2d? GetLineIntersection(Point2d p1, Point2d p2, Point2d p3, Point2d p4, bool bOnSegment = false)
        {
            // 直线1的向量
            double x1 = p1.X, y1 = p1.Y;
            double x2 = p2.X, y2 = p2.Y;
 
            // 直线2的向量
            double x3 = p3.X, y3 = p3.Y;
            double x4 = p4.X, y4 = p4.Y;
 
            // 计算分母
            double denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
 
            // 如果分母为0,说明两线平行或重合
            if (denominator == 0)
                return null;
 
            // 计算ua和ub
            double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator;
            double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator;
 
            //如果ua和ub在0和1之间,说明交点在两条线段上
            if (bOnSegment && !(ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1))
                return null; // 交点不在线段上
 
 
            // 计算交点坐标
            double x = x1 + ua * (x2 - x1);
            double y = y1 + ua * (y2 - y1);
 
            return new Point2d(x, y);
        }
        #endregion
 
        #region Halcon
        // Chapter: Graphics / Output
        // Short Description: Display 3D object models 
        public static void visualize_object_model_3d(HTuple hv_WindowHandle, HTuple hv_ObjectModel3D,
            HTuple hv_CamParam, HTuple hv_PoseIn, HTuple hv_GenParamName, HTuple hv_GenParamValue,
            HTuple hv_Title, HTuple hv_Label, HTuple hv_Information, out HTuple hv_PoseOut)
        {
            // Local iconic variables 
 
            HObject ho_Image = null, ho_ImageDump = null;
 
            // Local control variables 
 
            HTuple ExpTmpLocalVar_gDispObjOffset = new HTuple();
            HTuple ExpTmpLocalVar_gLabelsDecor = new HTuple(), ExpTmpLocalVar_gInfoDecor = new HTuple();
            HTuple ExpTmpLocalVar_gInfoPos = new HTuple(), ExpTmpLocalVar_gTitlePos = new HTuple();
            HTuple ExpTmpLocalVar_gTitleDecor = new HTuple(), ExpTmpLocalVar_gTerminationButtonLabel = new HTuple();
            HTuple ExpTmpLocalVar_gAlphaDeselected = new HTuple();
            HTuple ExpTmpLocalVar_gIsSinglePose = new HTuple(), ExpTmpLocalVar_gUsesOpenGL = new HTuple();
            HTuple hv_Scene3DTest = new HTuple(), hv_Scene3D = new HTuple();
            HTuple hv_WindowHandleBuffer = new HTuple(), hv_TrackballSize = new HTuple();
            HTuple hv_VirtualTrackball = new HTuple(), hv_MouseMapping = new HTuple();
            HTuple hv_WaitForButtonRelease = new HTuple(), hv_MaxNumModels = new HTuple();
            HTuple hv_WindowCenteredRotation = new HTuple(), hv_NumModels = new HTuple();
            HTuple hv_SelectedObject = new HTuple(), hv_ClipRegion = new HTuple();
            HTuple hv_CPLength = new HTuple(), hv_RowNotUsed = new HTuple();
            HTuple hv_ColumnNotUsed = new HTuple(), hv_Width = new HTuple();
            HTuple hv_Height = new HTuple(), hv_WPRow1 = new HTuple();
            HTuple hv_WPColumn1 = new HTuple(), hv_WPRow2 = new HTuple();
            HTuple hv_WPColumn2 = new HTuple(), hv_CamParamValue = new HTuple();
            HTuple hv_CamWidth = new HTuple(), hv_CamHeight = new HTuple();
            HTuple hv_Scale = new HTuple(), hv_Indices = new HTuple();
            HTuple hv_DispBackground = new HTuple(), hv_Mask = new HTuple();
            HTuple hv_Center = new HTuple(), hv_PoseEstimated = new HTuple();
            HTuple hv_Poses = new HTuple(), hv_HomMat3Ds = new HTuple();
            HTuple hv_Sequence = new HTuple(), hv_Font = new HTuple();
            HTuple hv_Exception = new HTuple(), hv_OpenGLInfo = new HTuple();
            HTuple hv_DummyObjectModel3D = new HTuple(), hv_CameraIndexTest = new HTuple();
            HTuple hv_PoseTest = new HTuple(), hv_InstanceIndexTest = new HTuple();
            HTuple hv_MinImageSize = new HTuple(), hv_TrackballRadiusPixel = new HTuple();
            HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
            HTuple hv_TextWidth = new HTuple(), hv_TextHeight = new HTuple();
            HTuple hv_NumChannels = new HTuple(), hv_ColorImage = new HTuple();
            HTuple hv_CameraIndex = new HTuple(), hv_AllInstances = new HTuple();
            HTuple hv_SetLight = new HTuple(), hv_LightParam = new HTuple();
            HTuple hv_LightPosition = new HTuple(), hv_LightKind = new HTuple();
            HTuple hv_LightIndex = new HTuple(), hv_PersistenceParamName = new HTuple();
            HTuple hv_PersistenceParamValue = new HTuple(), hv_AlphaOrig = new HTuple();
            HTuple hv_I = new HTuple(), hv_ParamName = new HTuple();
            HTuple hv_ParamValue = new HTuple(), hv_ParamNameTrunk = new HTuple();
            HTuple hv_Instance = new HTuple(), hv_HomMat3D = new HTuple();
            HTuple hv_Qx = new HTuple(), hv_Qy = new HTuple(), hv_Qz = new HTuple();
            HTuple hv_TBCenter = new HTuple(), hv_TBSize = new HTuple();
            HTuple hv_ButtonHold = new HTuple(), hv_VisualizeTB = new HTuple();
            HTuple hv_MaxIndex = new HTuple(), hv_TrackballCenterRow = new HTuple();
            HTuple hv_TrackballCenterCol = new HTuple(), hv_GraphEvent = new HTuple();
            HTuple hv_Exit = new HTuple(), hv_GraphButtonRow = new HTuple();
            HTuple hv_GraphButtonColumn = new HTuple(), hv_GraphButton = new HTuple();
            HTuple hv_ButtonReleased = new HTuple(), hv_e = new HTuple();
            HTuple hv_CamParam_COPY_INP_TMP = new HTuple(hv_CamParam);
            HTuple hv_GenParamName_COPY_INP_TMP = new HTuple(hv_GenParamName);
            HTuple hv_GenParamValue_COPY_INP_TMP = new HTuple(hv_GenParamValue);
            HTuple hv_Label_COPY_INP_TMP = new HTuple(hv_Label);
            HTuple hv_PoseIn_COPY_INP_TMP = new HTuple(hv_PoseIn);
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_ImageDump);
            hv_PoseOut = new HTuple();
            try
            {
                //The procedure visualize_object_model_3d can be used to display
                //one or more 3d object models and to interactively modify
                //the object poses by using the mouse.
                //
                //The pose can be modified by moving the mouse while
                //pressing a mouse button. The default settings are:
                //
                // Rotate: Left mouse button
                // Zoom: Shift + Left mouse button (or Center mouse button)
                // Pan: Ctrl + Left mouse button
                //
                //Furthermore, it is possible to select and deselect objects,
                //to decrease the mouse sensitivity, and to toggle the
                //inspection mode (see the description of the generic parameter
                //'inspection_mode' below):
                //
                // (De-)select object(s): Right mouse button
                // Low mouse sensitivity: Alt + mouse button
                // Toggle inspection mode: Ctrl + Alt + Left mouse button
                //
                //In GenParamName and GenParamValue all generic Parameters
                //of disp_object_model_3d are supported.
                //
                //**********************************************************
                //Define global variables
                //**********************************************************
                //
                //global def tuple gDispObjOffset
                //global def tuple gLabelsDecor
                //global def tuple gInfoDecor
                //global def tuple gInfoPos
                //global def tuple gTitlePos
                //global def tuple gTitleDecor
                //global def tuple gTerminationButtonLabel
                //global def tuple gAlphaDeselected
                //global def tuple gIsSinglePose
                //global def tuple gUsesOpenGL
                //
                //**********************************************************
                //Initialize Handles to enable correct handling in error case
                //**********************************************************
                hv_Scene3DTest.Dispose();
                hv_Scene3DTest = new HTuple();
                hv_Scene3D.Dispose();
                hv_Scene3D = new HTuple();
                hv_WindowHandleBuffer.Dispose();
                hv_WindowHandleBuffer = new HTuple();
 
                //**********************************************************
                //Some user defines that may be adapted if desired
                //**********************************************************
                //
                //TrackballSize defines the diameter of the trackball in
                //the image with respect to the smaller image dimension.
                hv_TrackballSize.Dispose();
                hv_TrackballSize = 0.8;
                //
                //VirtualTrackball defines the type of virtual trackball that
                //shall be used ('shoemake' or 'bell').
                hv_VirtualTrackball.Dispose();
                hv_VirtualTrackball = "shoemake";
                //VirtualTrackball := 'bell'
                //
                //Functionality of mouse buttons
                //    1: Left mouse button
                //    2: Middle mouse button
                //    4: Right mouse button
                //    5: Left+Right mouse button
                //  8+x: Shift + mouse button
                // 16+x: Ctrl + mouse button
                // 48+x: Ctrl + Alt + mouse button
                //in the order [Translate, Rotate, Scale, ScaleAlternative1, ScaleAlternative2, SelectObjects, ToggleSelectionMode]
                hv_MouseMapping.Dispose();
                hv_MouseMapping = new HTuple();
                hv_MouseMapping[0] = 17;
                hv_MouseMapping[1] = 1;
                hv_MouseMapping[2] = 2;
                hv_MouseMapping[3] = 5;
                hv_MouseMapping[4] = 9;
                hv_MouseMapping[5] = 4;
                hv_MouseMapping[6] = 49;
                //
                //The labels of the objects appear next to their projected
                //center. With gDispObjOffset a fixed offset is added
                //                  R,  C
                ExpTmpLocalVar_gDispObjOffset = new HTuple();
                ExpTmpLocalVar_gDispObjOffset[0] = -30;
                ExpTmpLocalVar_gDispObjOffset[1] = 0;
                HDevelopExport.ExpSetGlobalVar_gDispObjOffset(ExpTmpLocalVar_gDispObjOffset);
                //
                //Customize the decoration of the different text elements
                //              Color,   Box
                ExpTmpLocalVar_gInfoDecor = new HTuple();
                ExpTmpLocalVar_gInfoDecor[0] = "white";
                ExpTmpLocalVar_gInfoDecor[1] = "false";
                HDevelopExport.ExpSetGlobalVar_gInfoDecor(ExpTmpLocalVar_gInfoDecor);
                ExpTmpLocalVar_gLabelsDecor = new HTuple();
                ExpTmpLocalVar_gLabelsDecor[0] = "white";
                ExpTmpLocalVar_gLabelsDecor[1] = "false";
                HDevelopExport.ExpSetGlobalVar_gLabelsDecor(ExpTmpLocalVar_gLabelsDecor);
                ExpTmpLocalVar_gTitleDecor = new HTuple();
                ExpTmpLocalVar_gTitleDecor[0] = "black";
                ExpTmpLocalVar_gTitleDecor[1] = "true";
                HDevelopExport.ExpSetGlobalVar_gTitleDecor(ExpTmpLocalVar_gTitleDecor);
                //
                //Customize the position of some text elements
                //  gInfoPos has one of the values
                //  {'UpperLeft', 'LowerLeft', 'UpperRight'}
                ExpTmpLocalVar_gInfoPos = "LowerLeft";
                HDevelopExport.ExpSetGlobalVar_gInfoPos(ExpTmpLocalVar_gInfoPos);
                //  gTitlePos has one of the values
                //  {'UpperLeft', 'UpperCenter', 'UpperRight'}
                ExpTmpLocalVar_gTitlePos = "UpperLeft";
                HDevelopExport.ExpSetGlobalVar_gTitlePos(ExpTmpLocalVar_gTitlePos);
                //Alpha value (=1-transparency) that is used for visualizing
                //the objects that are not selected
                ExpTmpLocalVar_gAlphaDeselected = 0.3;
                HDevelopExport.ExpSetGlobalVar_gAlphaDeselected(ExpTmpLocalVar_gAlphaDeselected);
                //Customize the label of the continue button
                ExpTmpLocalVar_gTerminationButtonLabel = " Continue ";
                HDevelopExport.ExpSetGlobalVar_gTerminationButtonLabel(ExpTmpLocalVar_gTerminationButtonLabel);
                //Define if the continue button responds to a single click event or
                //if it responds only if the mouse button is released while being placed
                //over the continue button.
                //'true':  Wait until the continue button has been released.
                //         This should be used to avoid unwanted continuations of
                //         subsequent calls of visualize_object_model_3d, which can
                //         otherwise occur if the mouse button remains pressed while the
                //         next visualization is active.
                //'false': Continue the execution already if the continue button is
                //         pressed. This option allows a fast forwarding through
                //         subsequent calls of visualize_object_model_3d.
                hv_WaitForButtonRelease.Dispose();
                hv_WaitForButtonRelease = "true";
                //Number of 3D Object models that can be selected and handled individually.
                //If there are more models passed then this number, some calculations
                //are performed differently and the individual selection and handling
                //of models is not supported anymore. Note that the value of MaxNumModels
                //can be overwritten with the generic parameter max_num_selectable_models.
                hv_MaxNumModels.Dispose();
                hv_MaxNumModels = 1000;
                //Defines the default for the initial state of the rotation center:
                //(1) The rotation center is fixed in the center of the image and lies
                //    on the surface of the object.
                //(2) The rotation center lies in the center of the object.
                hv_WindowCenteredRotation.Dispose();
                hv_WindowCenteredRotation = 2;
                //
                //**********************************************************
                //
                //Initialize some values
                hv_NumModels.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_NumModels = new HTuple(hv_ObjectModel3D.TupleLength()
                        );
                }
                hv_SelectedObject.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_SelectedObject = HTuple.TupleGenConst(
                        hv_NumModels, 1);
                }
                //
                //Apply some system settings
                // dev_get_preferences(...); only in hdevelop
                // dev_set_preferences(...); only in hdevelop
                // dev_get_preferences(...); only in hdevelop
                // dev_set_preferences(...); only in hdevelop
                hv_ClipRegion.Dispose();
                HOperatorSet.GetSystem("clip_region", out hv_ClipRegion);
                HOperatorSet.SetSystem("clip_region", "false");
                HDevelopExport.dev_update_off();
                //
                //Check if GenParamName matches GenParamValue
                if ((int)new HTuple(new HTuple(hv_GenParamName_COPY_INP_TMP.TupleLength()
                    ).TupleNotEqual(new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleLength()
                    ))) != 0)
                {
                    throw new HalconException("Number of generic parameters does not match number of generic parameter values");
                }
                //
                try
                {
                    //
                    //Refactor camera parameters to fit to window size
                    //
                    hv_CPLength.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_CPLength = new HTuple(hv_CamParam_COPY_INP_TMP.TupleLength()
                            );
                    }
                    hv_RowNotUsed.Dispose(); hv_ColumnNotUsed.Dispose(); hv_Width.Dispose(); hv_Height.Dispose();
                    HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowNotUsed, out hv_ColumnNotUsed,
                        out hv_Width, out hv_Height);
                    hv_WPRow1.Dispose(); hv_WPColumn1.Dispose(); hv_WPRow2.Dispose(); hv_WPColumn2.Dispose();
                    HOperatorSet.GetPart(hv_WindowHandle, out hv_WPRow1, out hv_WPColumn1, out hv_WPRow2,
                        out hv_WPColumn2);
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height - 1, hv_Width - 1);
                    }
                    if ((int)new HTuple(hv_CPLength.TupleEqual(0)) != 0)
                    {
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_CamParam_COPY_INP_TMP.Dispose();
                            HDevelopExport.gen_cam_par_area_scan_division(0.06, 0, 8.5e-6, 8.5e-6, hv_Width / 2, hv_Height / 2,
                                hv_Width, hv_Height, out hv_CamParam_COPY_INP_TMP);
                        }
                    }
                    else
                    {
                        hv_CamParamValue.Dispose();
                        HDevelopExport.get_cam_par_data(hv_CamParam_COPY_INP_TMP, new HTuple("sx").TupleConcat(
                            "sy").TupleConcat("cx").TupleConcat("cy").TupleConcat("image_width").TupleConcat(
                            "image_height"), out hv_CamParamValue);
                        hv_CamWidth.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_CamWidth = hv_CamParamValue.TupleSelect(
                                4).TupleReal();
                        }
                        hv_CamHeight.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_CamHeight = hv_CamParamValue.TupleSelect(
                                5).TupleReal();
                        }
                        hv_Scale.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Scale = (hv_Width / hv_CamWidth).TupleConcat(
                                hv_Height / hv_CamHeight).TupleMin();
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HTuple ExpTmpOutVar_0;
                            set_cam_par_data(hv_CamParam_COPY_INP_TMP, "sx", hv_CamParamValue.TupleSelect(
                                0) / hv_Scale, out ExpTmpOutVar_0);
                            hv_CamParam_COPY_INP_TMP.Dispose();
                            hv_CamParam_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HTuple ExpTmpOutVar_0;
                            set_cam_par_data(hv_CamParam_COPY_INP_TMP, "sy", hv_CamParamValue.TupleSelect(
                                1) / hv_Scale, out ExpTmpOutVar_0);
                            hv_CamParam_COPY_INP_TMP.Dispose();
                            hv_CamParam_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HTuple ExpTmpOutVar_0;
                            set_cam_par_data(hv_CamParam_COPY_INP_TMP, "cx", hv_CamParamValue.TupleSelect(
                                2) * hv_Scale, out ExpTmpOutVar_0);
                            hv_CamParam_COPY_INP_TMP.Dispose();
                            hv_CamParam_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HTuple ExpTmpOutVar_0;
                            set_cam_par_data(hv_CamParam_COPY_INP_TMP, "cy", hv_CamParamValue.TupleSelect(
                                3) * hv_Scale, out ExpTmpOutVar_0);
                            hv_CamParam_COPY_INP_TMP.Dispose();
                            hv_CamParam_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HTuple ExpTmpOutVar_0;
                            set_cam_par_data(hv_CamParam_COPY_INP_TMP, "image_width", (hv_CamParamValue.TupleSelect(
                                4) * hv_Scale).TupleInt(), out ExpTmpOutVar_0);
                            hv_CamParam_COPY_INP_TMP.Dispose();
                            hv_CamParam_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HTuple ExpTmpOutVar_0;
                            set_cam_par_data(hv_CamParam_COPY_INP_TMP, "image_height", (hv_CamParamValue.TupleSelect(
                                5) * hv_Scale).TupleInt(), out ExpTmpOutVar_0);
                            hv_CamParam_COPY_INP_TMP.Dispose();
                            hv_CamParam_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                    }
                    //
                    //Check the generic parameters for max_num_selectable_models
                    //(Note that the default is set above to MaxNumModels := 1000)
                    hv_Indices.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                            "max_num_selectable_models");
                    }
                    if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                        new HTuple()))) != 0)
                    {
                        if ((int)hv_GenParamValue_COPY_INP_TMP.TupleSelect(hv_Indices.TupleSelect(
                            0)).TupleIsNumber() != 0)
                        {
                            if ((int)new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                hv_Indices.TupleSelect(0)).TupleNumber().TupleInt().TupleLess(
                                1)) != 0)
                            {
                                //Wrong parameter value: Only integer values greater than 0 are allowed
                                throw new HalconException("Wrong value for parameter 'max_num_selectable_models' (must be an integer value greater than 0)");
                            }
                        }
                        else
                        {
                            //Wrong parameter value: Only integer values greater than 0 are allowed
                            throw new HalconException("Wrong value for parameter 'max_num_selectable_models' (must be an integer value greater than 0)");
                        }
                        hv_MaxNumModels.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_MaxNumModels = hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                hv_Indices.TupleSelect(0)).TupleNumber().TupleInt();
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamName_COPY_INP_TMP.Dispose();
                                hv_GenParamName_COPY_INP_TMP = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamValue_COPY_INP_TMP.Dispose();
                                hv_GenParamValue_COPY_INP_TMP = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                    }
                    //
                    //Check the generic parameters for window_centered_rotation
                    //(Note that the default is set above to WindowCenteredRotation := 2)
                    hv_Indices.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                            "inspection_mode");
                    }
                    if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                        new HTuple()))) != 0)
                    {
                        if ((int)new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleSelect(hv_Indices.TupleSelect(
                            0)).TupleEqual("surface")) != 0)
                        {
                            hv_WindowCenteredRotation.Dispose();
                            hv_WindowCenteredRotation = 1;
                        }
                        else if ((int)new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                            hv_Indices.TupleSelect(0)).TupleEqual("standard")) != 0)
                        {
                            hv_WindowCenteredRotation.Dispose();
                            hv_WindowCenteredRotation = 2;
                        }
                        else
                        {
                            //Wrong parameter value, use default value
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamName_COPY_INP_TMP.Dispose();
                                hv_GenParamName_COPY_INP_TMP = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamValue_COPY_INP_TMP.Dispose();
                                hv_GenParamValue_COPY_INP_TMP = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                    }
                    //
                    //Check the generic parameters for disp_background
                    //(The former parameter name 'use_background' is still supported
                    // for compatibility reasons)
                    hv_DispBackground.Dispose();
                    hv_DispBackground = "false";
                    if ((int)new HTuple(new HTuple(hv_GenParamName_COPY_INP_TMP.TupleLength()
                        ).TupleGreater(0)) != 0)
                    {
                        hv_Mask.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Mask = hv_GenParamName_COPY_INP_TMP.TupleEqualElem(
                                "disp_background").TupleOr(hv_GenParamName_COPY_INP_TMP.TupleEqualElem(
                                "use_background"));
                        }
                        hv_Indices.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Indices = hv_Mask.TupleFind(
                                1);
                        }
                    }
                    else
                    {
                        hv_Indices.Dispose();
                        hv_Indices = -1;
                    }
                    if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                        new HTuple()))) != 0)
                    {
                        hv_DispBackground.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_DispBackground = hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                hv_Indices.TupleSelect(0));
                        }
                        if ((int)new HTuple(hv_DispBackground.TupleNotEqual("true")).TupleAnd(
                            new HTuple(hv_DispBackground.TupleNotEqual("false"))) != 0)
                        {
                            //Wrong parameter value: Only 'true' and 'false' are allowed
                            throw new HalconException("Wrong value for parameter 'disp_background' (must be either 'true' or 'false')");
                        }
                        //Note that the background is handled explicitly in this procedure
                        //and therefore, the parameter is removed from the list of
                        //parameters and disp_background is always set to true (see below)
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamName_COPY_INP_TMP.Dispose();
                                hv_GenParamName_COPY_INP_TMP = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamValue_COPY_INP_TMP.Dispose();
                                hv_GenParamValue_COPY_INP_TMP = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                    }
                    //
                    //Read and check the parameter Label for each object
                    if ((int)new HTuple(new HTuple(hv_Label_COPY_INP_TMP.TupleLength()).TupleEqual(
                        0)) != 0)
                    {
                        //no labels set -> leave as []
                    }
                    else if ((int)new HTuple(new HTuple(hv_Label_COPY_INP_TMP.TupleLength()
                        ).TupleEqual(1)) != 0)
                    {
                        //a single label set for all models
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_Label = HTuple.TupleGenConst(
                                    hv_NumModels, hv_Label_COPY_INP_TMP);
                                hv_Label_COPY_INP_TMP.Dispose();
                                hv_Label_COPY_INP_TMP = ExpTmpLocalVar_Label;
                            }
                        }
                    }
                    else
                    {
                        if ((int)new HTuple(new HTuple(hv_Label_COPY_INP_TMP.TupleLength()).TupleNotEqual(
                            hv_NumModels)) != 0)
                        {
                            //Number of elements in Label does not match
                            //the number of object models.
                            throw new HalconException(new HTuple(new HTuple("Number of elements in Label (") + new HTuple(hv_Label_COPY_INP_TMP.TupleLength()
                                )) + ") does not match the number of object models(" + hv_NumModels + ").");
                        }
                    }
                    //Convert labels into strings
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                              ExpTmpLocalVar_Label = "" + hv_Label_COPY_INP_TMP;
                            hv_Label_COPY_INP_TMP.Dispose();
                            hv_Label_COPY_INP_TMP = ExpTmpLocalVar_Label;
                        }
                    }
                    //
                    //Read and check the parameter PoseIn for each object
                    hv_Center.Dispose();
                    HDevelopExport.get_object_models_center(hv_ObjectModel3D, out hv_Center);
                    if ((int)new HTuple(hv_Center.TupleEqual(new HTuple())) != 0)
                    {
                        hv_Center.Dispose();
                        hv_Center = new HTuple();
                        hv_Center[0] = 0;
                        hv_Center[1] = 0;
                        hv_Center[2] = 0;
                    }
                    if ((int)new HTuple(new HTuple(hv_PoseIn_COPY_INP_TMP.TupleLength()).TupleEqual(
                        0)) != 0)
                    {
                        //If no pose was specified by the caller, automatically calculate
                        //a pose that is appropriate for the visualization.
                        //Set the initial model reference pose. The orientation is parallel
                        //to the object coordinate system, the position is at the center
                        //of gravity of all models.
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_PoseIn_COPY_INP_TMP.Dispose();
                            HOperatorSet.CreatePose(-hv_Center.TupleSelect(0), -hv_Center.TupleSelect(
                                1), -hv_Center.TupleSelect(2), 0, 0, 0, "Rp+T", "gba", "point",
                                out hv_PoseIn_COPY_INP_TMP);
                        }
                        hv_PoseEstimated.Dispose();
                        HDevelopExport.determine_optimum_pose_distance(hv_ObjectModel3D, hv_CamParam_COPY_INP_TMP,
                            0.9, hv_PoseIn_COPY_INP_TMP, out hv_PoseEstimated);
                        hv_Poses.Dispose();
                        hv_Poses = new HTuple();
                        hv_HomMat3Ds.Dispose();
                        hv_HomMat3Ds = new HTuple();
                        hv_Sequence.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Sequence = HTuple.TupleGenSequence(
                                0, hv_NumModels * 7 - 1, 1);
                        }
                        hv_Poses.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Poses = hv_PoseEstimated.TupleSelect(
                                hv_Sequence % 7);
                        }
                        ExpTmpLocalVar_gIsSinglePose = 1;
                        HDevelopExport.ExpSetGlobalVar_gIsSinglePose(ExpTmpLocalVar_gIsSinglePose);
                    }
                    else if ((int)new HTuple(new HTuple(hv_PoseIn_COPY_INP_TMP.TupleLength()
                        ).TupleEqual(7)) != 0)
                    {
                        hv_Poses.Dispose();
                        hv_Poses = new HTuple();
                        hv_HomMat3Ds.Dispose();
                        hv_HomMat3Ds = new HTuple();
                        hv_Sequence.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Sequence = HTuple.TupleGenSequence(
                                0, hv_NumModels * 7 - 1, 1);
                        }
                        hv_Poses.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Poses = hv_PoseIn_COPY_INP_TMP.TupleSelect(
                                hv_Sequence % 7);
                        }
                        ExpTmpLocalVar_gIsSinglePose = 1;
                        HDevelopExport.ExpSetGlobalVar_gIsSinglePose(ExpTmpLocalVar_gIsSinglePose);
                    }
                    else
                    {
                        if ((int)new HTuple(new HTuple(hv_PoseIn_COPY_INP_TMP.TupleLength()).TupleNotEqual(
                            new HTuple(hv_ObjectModel3D.TupleLength()) * 7)) != 0)
                        {
                            //Wrong number of values of input control parameter 'PoseIn'
                            throw new HalconException("Wrong number of values of input control parameter 'PoseIn'.");
                        }
                        else
                        {
                            hv_Poses.Dispose();
                            hv_Poses = new HTuple(hv_PoseIn_COPY_INP_TMP);
                        }
                        ExpTmpLocalVar_gIsSinglePose = 0;
                        HDevelopExport.ExpSetGlobalVar_gIsSinglePose(ExpTmpLocalVar_gIsSinglePose);
                    }
                    //
                    //Open (invisible) buffer window to avoid flickering
                    hv_WindowHandleBuffer.Dispose();
                    HOperatorSet.OpenWindow(0, 0, hv_Width, hv_Height, 0, "buffer", "", out hv_WindowHandleBuffer);
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HOperatorSet.SetPart(hv_WindowHandleBuffer, 0, 0, hv_Height - 1, hv_Width - 1);
                    }
                    hv_Font.Dispose();
                    HOperatorSet.GetFont(hv_WindowHandle, out hv_Font);
                    try
                    {
                        HOperatorSet.SetFont(hv_WindowHandleBuffer, hv_Font);
                    }
                    // catch (Exception) 
                    catch (HalconException HDevExpDefaultException2)
                    {
                        HDevExpDefaultException2.ToHTuple(out hv_Exception);
                    }
                    //
                    // Is OpenGL available and should it be used?
                    ExpTmpLocalVar_gUsesOpenGL = "true";
                    HDevelopExport.ExpSetGlobalVar_gUsesOpenGL(ExpTmpLocalVar_gUsesOpenGL);
                    hv_Indices.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                            "opengl");
                    }
                    if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                        new HTuple()))) != 0)
                    {
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ExpTmpLocalVar_gUsesOpenGL = hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                hv_Indices.TupleSelect(0));
                        }
                        HDevelopExport.ExpSetGlobalVar_gUsesOpenGL(ExpTmpLocalVar_gUsesOpenGL);
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamName_COPY_INP_TMP.Dispose();
                                hv_GenParamName_COPY_INP_TMP = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue_COPY_INP_TMP.TupleRemove(
                                    hv_Indices);
                                hv_GenParamValue_COPY_INP_TMP.Dispose();
                                hv_GenParamValue_COPY_INP_TMP = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                        if ((int)new HTuple(HDevelopExport.ExpGetGlobalVar_gUsesOpenGL().TupleNotEqual("true")).TupleAnd(
                            new HTuple(HDevelopExport.ExpGetGlobalVar_gUsesOpenGL().TupleNotEqual("false"))) != 0)
                        {
                            //Wrong parameter value: Only 'true' and 'false' are allowed
                            throw new HalconException("Wrong value for parameter 'opengl' (must be either 'true' or 'false')");
                        }
                    }
                    if ((int)new HTuple(HDevelopExport.ExpGetGlobalVar_gUsesOpenGL().TupleEqual("true")) != 0)
                    {
                        hv_OpenGLInfo.Dispose();
                        HOperatorSet.GetSystem("opengl_info", out hv_OpenGLInfo);
                        if ((int)new HTuple(hv_OpenGLInfo.TupleEqual("No OpenGL support included.")) != 0)
                        {
                            ExpTmpLocalVar_gUsesOpenGL = "false";
                            HDevelopExport.ExpSetGlobalVar_gUsesOpenGL(ExpTmpLocalVar_gUsesOpenGL);
                        }
                        else
                        {
                            hv_DummyObjectModel3D.Dispose();
                            HOperatorSet.GenObjectModel3dFromPoints(0, 0, 0, out hv_DummyObjectModel3D);
                            hv_Scene3DTest.Dispose();
                            HOperatorSet.CreateScene3d(out hv_Scene3DTest);
                            hv_CameraIndexTest.Dispose();
                            HOperatorSet.AddScene3dCamera(hv_Scene3DTest, hv_CamParam_COPY_INP_TMP,
                                out hv_CameraIndexTest);
                            hv_PoseTest.Dispose();
                            HDevelopExport.determine_optimum_pose_distance(hv_DummyObjectModel3D, hv_CamParam_COPY_INP_TMP,
                                0.9, new HTuple(0).TupleConcat(0).TupleConcat(0).TupleConcat(
                                0).TupleConcat(0).TupleConcat(0).TupleConcat(0), out hv_PoseTest);
                            hv_InstanceIndexTest.Dispose();
                            HOperatorSet.AddScene3dInstance(hv_Scene3DTest, hv_DummyObjectModel3D,
                                hv_PoseTest, out hv_InstanceIndexTest);
                            try
                            {
                                HOperatorSet.DisplayScene3d(hv_WindowHandleBuffer, hv_Scene3DTest,
                                    hv_InstanceIndexTest);
                            }
                            // catch (Exception) 
                            catch (HalconException HDevExpDefaultException2)
                            {
                                HDevExpDefaultException2.ToHTuple(out hv_Exception);
                                ExpTmpLocalVar_gUsesOpenGL = "false";
                                HDevelopExport.ExpSetGlobalVar_gUsesOpenGL(ExpTmpLocalVar_gUsesOpenGL);
                            }
                            HOperatorSet.ClearScene3d(hv_Scene3DTest);
                            hv_Scene3DTest.Dispose();
                            hv_Scene3DTest = new HTuple();
                            HOperatorSet.ClearObjectModel3d(hv_DummyObjectModel3D);
                        }
                    }
                    //
                    //Compute the trackball
                    hv_MinImageSize.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_MinImageSize = hv_Width.TupleConcat(
                            hv_Height).TupleMin();
                    }
                    hv_TrackballRadiusPixel.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_TrackballRadiusPixel = hv_TrackballSize * hv_MinImageSize / 2.0;
                    }
                    //
                    //Measure the text extents for the continue button in the
                    //graphics window
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Ascent.Dispose(); hv_Descent.Dispose(); hv_TextWidth.Dispose(); hv_TextHeight.Dispose();
                        HOperatorSet.GetStringExtents(hv_WindowHandleBuffer, HDevelopExport.ExpGetGlobalVar_gTerminationButtonLabel() + "  ",
                            out hv_Ascent, out hv_Descent, out hv_TextWidth, out hv_TextHeight);
                    }
                    //
                    //Store background image
                    if ((int)new HTuple(hv_DispBackground.TupleEqual("false")) != 0)
                    {
                        HOperatorSet.ClearWindow(hv_WindowHandle);
                    }
                    ho_Image.Dispose();
                    HOperatorSet.DumpWindowImage(out ho_Image, hv_WindowHandle);
                    //Special treatment for color background images necessary
                    hv_NumChannels.Dispose();
                    HOperatorSet.CountChannels(ho_Image, out hv_NumChannels);
                    hv_ColorImage.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_ColorImage = new HTuple(hv_NumChannels.TupleEqual(
                            3));
                    }
                    //
                    hv_Scene3D.Dispose();
                    HOperatorSet.CreateScene3d(out hv_Scene3D);
                    hv_CameraIndex.Dispose();
                    HOperatorSet.AddScene3dCamera(hv_Scene3D, hv_CamParam_COPY_INP_TMP, out hv_CameraIndex);
                    hv_AllInstances.Dispose();
                    HOperatorSet.AddScene3dInstance(hv_Scene3D, hv_ObjectModel3D, hv_Poses, out hv_AllInstances);
                    //Always set 'disp_background' to true,  because it is handled explicitly
                    //in this procedure (see above)
                    HOperatorSet.SetScene3dParam(hv_Scene3D, "disp_background", "true");
                    //Check if we have to set light specific parameters
                    hv_SetLight.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_SetLight = new HTuple(hv_GenParamName_COPY_INP_TMP.TupleRegexpTest(
                            "light_"));
                    }
                    if ((int)hv_SetLight != 0)
                    {
                        //set position of light source
                        hv_Indices.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                                "light_position");
                        }
                        if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                            new HTuple()))) != 0)
                        {
                            //If multiple light positions are given, use the last one
                            hv_LightParam.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_LightParam = hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                    hv_Indices.TupleSelect(new HTuple(hv_Indices.TupleLength()) - 1)).TupleSplit(
                                    new HTuple(", ")).TupleNumber();
                            }
                            if ((int)new HTuple(new HTuple(hv_LightParam.TupleLength()).TupleNotEqual(
                                4)) != 0)
                            {
                                throw new HalconException("light_position must be given as a string that contains four space separated floating point numbers");
                            }
                            hv_LightPosition.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_LightPosition = hv_LightParam.TupleSelectRange(
                                    0, 2);
                            }
                            hv_LightKind.Dispose();
                            hv_LightKind = "point_light";
                            if ((int)new HTuple(hv_LightParam.TupleSelect(3).TupleEqual(0)) != 0)
                            {
                                hv_LightKind.Dispose();
                                hv_LightKind = "directional_light";
                            }
                            //Currently, only one light source is supported
                            HOperatorSet.RemoveScene3dLight(hv_Scene3D, 0);
                            hv_LightIndex.Dispose();
                            HOperatorSet.AddScene3dLight(hv_Scene3D, hv_LightPosition, hv_LightKind,
                                out hv_LightIndex);
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRemove(hv_GenParamName_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                                hv_GenParamName_COPY_INP_TMP.Dispose();
                                hv_GenParamName_COPY_INP_TMP = ExpTmpOutVar_0;
                            }
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRemove(hv_GenParamValue_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                                hv_GenParamValue_COPY_INP_TMP.Dispose();
                                hv_GenParamValue_COPY_INP_TMP = ExpTmpOutVar_0;
                            }
                        }
                        //set ambient part of light source
                        hv_Indices.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                                "light_ambient");
                        }
                        if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                            new HTuple()))) != 0)
                        {
                            //If the ambient part is set multiple times, use the last setting
                            hv_LightParam.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_LightParam = hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                    hv_Indices.TupleSelect(new HTuple(hv_Indices.TupleLength()) - 1)).TupleSplit(
                                    new HTuple(", ")).TupleNumber();
                            }
                            if ((int)new HTuple(new HTuple(hv_LightParam.TupleLength()).TupleLess(
                                3)) != 0)
                            {
                                throw new HalconException("light_ambient must be given as a string that contains three space separated floating point numbers");
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                HOperatorSet.SetScene3dLightParam(hv_Scene3D, 0, "ambient", hv_LightParam.TupleSelectRange(
                                    0, 2));
                            }
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRemove(hv_GenParamName_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                                hv_GenParamName_COPY_INP_TMP.Dispose();
                                hv_GenParamName_COPY_INP_TMP = ExpTmpOutVar_0;
                            }
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRemove(hv_GenParamValue_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                                hv_GenParamValue_COPY_INP_TMP.Dispose();
                                hv_GenParamValue_COPY_INP_TMP = ExpTmpOutVar_0;
                            }
                        }
                        //Set diffuse part of light source
                        hv_Indices.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                                "light_diffuse");
                        }
                        if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                            new HTuple()))) != 0)
                        {
                            //If the diffuse part is set multiple times, use the last setting
                            hv_LightParam.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_LightParam = hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                    hv_Indices.TupleSelect(new HTuple(hv_Indices.TupleLength()) - 1)).TupleSplit(
                                    new HTuple(", ")).TupleNumber();
                            }
                            if ((int)new HTuple(new HTuple(hv_LightParam.TupleLength()).TupleLess(
                                3)) != 0)
                            {
                                throw new HalconException("light_diffuse must be given as a string that contains three space separated floating point numbers");
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                HOperatorSet.SetScene3dLightParam(hv_Scene3D, 0, "diffuse", hv_LightParam.TupleSelectRange(
                                    0, 2));
                            }
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRemove(hv_GenParamName_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                                hv_GenParamName_COPY_INP_TMP.Dispose();
                                hv_GenParamName_COPY_INP_TMP = ExpTmpOutVar_0;
                            }
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleRemove(hv_GenParamValue_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                                hv_GenParamValue_COPY_INP_TMP.Dispose();
                                hv_GenParamValue_COPY_INP_TMP = ExpTmpOutVar_0;
                            }
                        }
                    }
                    //
                    //Handle persistence parameters separately because persistence will
                    //only be activated immediately before leaving the visualization
                    //procedure
                    hv_PersistenceParamName.Dispose();
                    hv_PersistenceParamName = new HTuple();
                    hv_PersistenceParamValue.Dispose();
                    hv_PersistenceParamValue = new HTuple();
                    //Set position of light source
                    hv_Indices.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                            "object_index_persistence");
                    }
                    if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                        new HTuple()))) != 0)
                    {
                        if ((int)new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleSelect(hv_Indices.TupleSelect(
                            new HTuple(hv_Indices.TupleLength()) - 1)).TupleEqual("true")) != 0)
                        {
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                      ExpTmpLocalVar_PersistenceParamName = hv_PersistenceParamName.TupleConcat(
                                        "object_index_persistence");
                                    hv_PersistenceParamName.Dispose();
                                    hv_PersistenceParamName = ExpTmpLocalVar_PersistenceParamName;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                      ExpTmpLocalVar_PersistenceParamValue = hv_PersistenceParamValue.TupleConcat(
                                        "true");
                                    hv_PersistenceParamValue.Dispose();
                                    hv_PersistenceParamValue = ExpTmpLocalVar_PersistenceParamValue;
                                }
                            }
                        }
                        else if ((int)new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                            hv_Indices.TupleSelect(new HTuple(hv_Indices.TupleLength()) - 1)).TupleEqual(
                            "false")) != 0)
                        {
                        }
                        else
                        {
                            throw new HalconException("Wrong value for parameter 'object_index_persistence' (must be either 'true' or 'false')");
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleRemove(hv_GenParamName_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                            hv_GenParamName_COPY_INP_TMP.Dispose();
                            hv_GenParamName_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleRemove(hv_GenParamValue_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                            hv_GenParamValue_COPY_INP_TMP.Dispose();
                            hv_GenParamValue_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                    }
                    hv_Indices.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Indices = hv_GenParamName_COPY_INP_TMP.TupleFind(
                            "depth_persistence");
                    }
                    if ((int)new HTuple(hv_Indices.TupleNotEqual(-1)).TupleAnd(new HTuple(hv_Indices.TupleNotEqual(
                        new HTuple()))) != 0)
                    {
                        if ((int)new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleSelect(hv_Indices.TupleSelect(
                            new HTuple(hv_Indices.TupleLength()) - 1)).TupleEqual("true")) != 0)
                        {
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                      ExpTmpLocalVar_PersistenceParamName = hv_PersistenceParamName.TupleConcat(
                                        "depth_persistence");
                                    hv_PersistenceParamName.Dispose();
                                    hv_PersistenceParamName = ExpTmpLocalVar_PersistenceParamName;
                                }
                            }
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                {
                                    HTuple
                                      ExpTmpLocalVar_PersistenceParamValue = hv_PersistenceParamValue.TupleConcat(
                                        "true");
                                    hv_PersistenceParamValue.Dispose();
                                    hv_PersistenceParamValue = ExpTmpLocalVar_PersistenceParamValue;
                                }
                            }
                        }
                        else if ((int)new HTuple(hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                            hv_Indices.TupleSelect(new HTuple(hv_Indices.TupleLength()) - 1)).TupleEqual(
                            "false")) != 0)
                        {
                        }
                        else
                        {
                            throw new HalconException("Wrong value for parameter 'depth_persistence' (must be either 'true' or 'false')");
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleRemove(hv_GenParamName_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                            hv_GenParamName_COPY_INP_TMP.Dispose();
                            hv_GenParamName_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleRemove(hv_GenParamValue_COPY_INP_TMP, hv_Indices, out ExpTmpOutVar_0);
                            hv_GenParamValue_COPY_INP_TMP.Dispose();
                            hv_GenParamValue_COPY_INP_TMP = ExpTmpOutVar_0;
                        }
                    }
                    //
                    //Parse the generic parameters
                    //- First, all parameters that are understood by set_scene_3d_instance_param
                    hv_AlphaOrig.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_AlphaOrig = HTuple.TupleGenConst(
                            hv_NumModels, 1);
                    }
                    for (hv_I = 0; (int)hv_I <= (int)(new HTuple(hv_GenParamName_COPY_INP_TMP.TupleLength()
                        ) - 1); hv_I = (int)hv_I + 1)
                    {
                        hv_ParamName.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ParamName = hv_GenParamName_COPY_INP_TMP.TupleSelect(
                                hv_I);
                        }
                        hv_ParamValue.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ParamValue = hv_GenParamValue_COPY_INP_TMP.TupleSelect(
                                hv_I);
                        }
                        //Check if this parameter is understood by set_scene_3d_param
                        if ((int)new HTuple(hv_ParamName.TupleEqual("alpha")) != 0)
                        {
                            hv_AlphaOrig.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_AlphaOrig = HTuple.TupleGenConst(
                                    hv_NumModels, hv_ParamValue);
                            }
                        }
                        try
                        {
                            HOperatorSet.SetScene3dParam(hv_Scene3D, hv_ParamName, hv_ParamValue);
                            continue;
                        }
                        // catch (Exception) 
                        catch (HalconException HDevExpDefaultException2)
                        {
                            HDevExpDefaultException2.ToHTuple(out hv_Exception);
                            if ((int)new HTuple(hv_Exception.TupleSelect(0).TupleEqual(1203)).TupleOr(
                                new HTuple(hv_Exception.TupleSelect(0).TupleEqual(1303))) != 0)
                            {
                                if ((int)new HTuple(new HTuple(new HTuple(hv_ParamName.TupleEqual(
                                    "color_attrib")).TupleOr(new HTuple(hv_ParamName.TupleEqual("red_channel_attrib")))).TupleOr(
                                    new HTuple(hv_ParamName.TupleEqual("green_channel_attrib")))).TupleOr(
                                    new HTuple(hv_ParamName.TupleEqual("blue_channel_attrib"))) != 0)
                                {
                                    throw new HalconException("Wrong type or value for parameter " + hv_ParamName + ": " + hv_ParamValue + ". " + hv_ParamValue + " may not be attached to the points of the 3D object model. Compare the parameter AttachExtAttribTo of set_object_model_3d_attrib.");
                                }
                                else
                                {
                                    throw new HalconException("Wrong type or value for parameter " + hv_ParamName + ": " + hv_ParamValue);
                                }
                            }
                        }
                        //Check if it is a parameter that is valid for only one instance
                        //and therefore can be set only with set_scene_3d_instance_param
                        hv_ParamNameTrunk.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ParamNameTrunk = hv_ParamName.TupleRegexpReplace(
                                "_\\d+$", "");
                        }
                        if ((int)new HTuple(hv_ParamName.TupleEqual(hv_ParamNameTrunk)) != 0)
                        {
                            hv_Instance.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_Instance = HTuple.TupleGenSequence(
                                    0, hv_NumModels - 1, 1);
                            }
                        }
                        else
                        {
                            hv_Instance.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_Instance = hv_ParamName.TupleRegexpReplace(
                                    "^" + hv_ParamNameTrunk + "_(\\d+)$", "$1").TupleNumber();
                            }
                            if ((int)new HTuple(hv_Instance.TupleLess(0)).TupleOr(new HTuple(hv_Instance.TupleGreater(
                                hv_NumModels - 1))) != 0)
                            {
                                throw new HalconException("Parameter " + hv_ParamName + " refers to a non existing 3D object model");
                            }
                        }
                        try
                        {
                            HOperatorSet.SetScene3dInstanceParam(hv_Scene3D, hv_Instance, hv_ParamNameTrunk,
                                hv_ParamValue);
                        }
                        // catch (Exception) 
                        catch (HalconException HDevExpDefaultException2)
                        {
                            HDevExpDefaultException2.ToHTuple(out hv_Exception);
                            if ((int)new HTuple(hv_Exception.TupleSelect(0).TupleEqual(1204)).TupleOr(
                                new HTuple(hv_Exception.TupleSelect(0).TupleEqual(1304))) != 0)
                            {
                                if ((int)new HTuple(new HTuple(new HTuple(hv_ParamNameTrunk.TupleEqual(
                                    "color_attrib")).TupleOr(new HTuple(hv_ParamNameTrunk.TupleEqual(
                                    "red_channel_attrib")))).TupleOr(new HTuple(hv_ParamNameTrunk.TupleEqual(
                                    "green_channel_attrib")))).TupleOr(new HTuple(hv_ParamNameTrunk.TupleEqual(
                                    "blue_channel_attrib"))) != 0)
                                {
                                    throw new HalconException("Wrong type or value for parameter " + hv_ParamName + ": " + hv_ParamValue + ". " + hv_ParamValue + " may not be attached to the points of the 3D object model. Compare the parameter AttachExtAttribTo of set_object_model_3d_attrib.");
                                }
                                else
                                {
                                    throw new HalconException("Wrong type or value for parameter " + hv_ParamName + ": " + hv_ParamValue);
                                }
                            }
                            else if ((int)new HTuple(hv_Exception.TupleSelect(0).TupleEqual(
                                1203)).TupleOr(new HTuple(hv_Exception.TupleSelect(0).TupleEqual(
                                1303))) != 0)
                            {
                                throw new HalconException("Wrong parameter name " + hv_ParamName);
                            }
                            else
                            {
                                throw new HalconException(hv_Exception);
                            }
                        }
                        if ((int)new HTuple(hv_ParamNameTrunk.TupleEqual("alpha")) != 0)
                        {
                            if (hv_AlphaOrig == null)
                                hv_AlphaOrig = new HTuple();
                            hv_AlphaOrig[hv_Instance] = hv_ParamValue;
                        }
                    }
                    //
                    //Start the visualization loop
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_HomMat3D.Dispose();
                        HOperatorSet.PoseToHomMat3d(hv_Poses.TupleSelectRange(0, 6), out hv_HomMat3D);
                    }
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Qx.Dispose(); hv_Qy.Dispose(); hv_Qz.Dispose();
                        HOperatorSet.AffineTransPoint3d(hv_HomMat3D, hv_Center.TupleSelect(0), hv_Center.TupleSelect(
                            1), hv_Center.TupleSelect(2), out hv_Qx, out hv_Qy, out hv_Qz);
                    }
                    hv_TBCenter.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_TBCenter = new HTuple();
                        hv_TBCenter = hv_TBCenter.TupleConcat(hv_Qx, hv_Qy, hv_Qz);
                    }
                    hv_TBSize.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_TBSize = (0.5 + 0.5 * hv_SelectedObject.TupleSum()
                             / hv_NumModels) * hv_TrackballRadiusPixel;
                    }
                    hv_ButtonHold.Dispose();
                    hv_ButtonHold = 0;
                    while (1 != 0)
                    {
                        // 设置在死循环不卡主线程
 
                        hv_VisualizeTB.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_VisualizeTB = new HTuple(hv_SelectedObject.TupleMax()
                                .TupleNotEqual(0));
                        }
                        hv_MaxIndex.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_MaxIndex = new HTuple(hv_ObjectModel3D.TupleLength()
                                ).TupleConcat(hv_MaxNumModels).TupleMin() - 1;
                        }
                        //Set trackball fixed in the center of the window
                        hv_TrackballCenterRow.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_TrackballCenterRow = hv_Height / 2;
                        }
                        hv_TrackballCenterCol.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_TrackballCenterCol = hv_Width / 2;
                        }
                        if ((int)new HTuple(hv_WindowCenteredRotation.TupleEqual(1)) != 0)
                        {
                            try
                            {
                                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                                {
                                    hv_TBCenter.Dispose(); hv_TBSize.Dispose();
                                    HDevelopExport.get_trackball_center_fixed(hv_SelectedObject.TupleSelectRange(0, hv_MaxIndex),
                                        hv_TrackballCenterRow, hv_TrackballCenterCol, hv_TrackballRadiusPixel,
                                        hv_Scene3D, hv_ObjectModel3D.TupleSelectRange(0, hv_MaxIndex), hv_Poses.TupleSelectRange(
                                        0, (hv_MaxIndex + 1) * 7 - 1), hv_WindowHandleBuffer, hv_CamParam_COPY_INP_TMP,
                                        hv_GenParamName_COPY_INP_TMP, hv_GenParamValue_COPY_INP_TMP, out hv_TBCenter,
                                        out hv_TBSize);
                                }
                            }
                            // catch (Exception) 
                            catch (HalconException HDevExpDefaultException2)
                            {
                                HDevExpDefaultException2.ToHTuple(out hv_Exception);
                                disp_message(hv_WindowHandle, "Surface inspection mode is not available.",
                                    "image", 5, 20, "red", "true");
                                hv_WindowCenteredRotation.Dispose();
                                hv_WindowCenteredRotation = 2;
                                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                                {
                                    hv_TBCenter.Dispose(); hv_TBSize.Dispose();
                                    HDevelopExport.get_trackball_center(hv_SelectedObject.TupleSelectRange(0, hv_MaxIndex),
                                        hv_TrackballRadiusPixel, hv_ObjectModel3D.TupleSelectRange(0, hv_MaxIndex),
                                        hv_Poses.TupleSelectRange(0, (hv_MaxIndex + 1) * 7 - 1), out hv_TBCenter,
                                        out hv_TBSize);
                                }
                                HOperatorSet.WaitSeconds(1);
                            }
                        }
                        else
                        {
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_TBCenter.Dispose(); hv_TBSize.Dispose();
                                HDevelopExport.get_trackball_center(hv_SelectedObject.TupleSelectRange(0, hv_MaxIndex),
                                    hv_TrackballRadiusPixel, hv_ObjectModel3D.TupleSelectRange(0, hv_MaxIndex),
                                    hv_Poses.TupleSelectRange(0, (hv_MaxIndex + 1) * 7 - 1), out hv_TBCenter,
                                    out hv_TBSize);
                            }
                        }
                        HDevelopExport.dump_image_output(ho_Image, hv_WindowHandleBuffer, hv_Scene3D, hv_AlphaOrig,
                            hv_ObjectModel3D, hv_GenParamName_COPY_INP_TMP, hv_GenParamValue_COPY_INP_TMP,
                            hv_CamParam_COPY_INP_TMP, hv_Poses, hv_ColorImage, hv_Title, hv_Information,
                            hv_Label_COPY_INP_TMP, hv_VisualizeTB, "true", hv_TrackballCenterRow,
                            hv_TrackballCenterCol, hv_TBSize, hv_SelectedObject, hv_WindowCenteredRotation,
                            hv_TBCenter);
                        ho_ImageDump.Dispose();
                        HOperatorSet.DumpWindowImage(out ho_ImageDump, hv_WindowHandleBuffer);
                        HDevWindowStack.SetActive(hv_WindowHandle);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.DispObj(ho_ImageDump, HDevWindowStack.GetActive());
                        }
                        //
                        //Check for mouse events
                        hv_GraphEvent.Dispose();
                        hv_GraphEvent = 0;
                        hv_Exit.Dispose();
                        hv_Exit = 0;
                        while (1 != 0)
                        {
                            // 设置在死循环不卡主线程
                            //
                            //Check graphic event
                            try
                            {
                                hv_GraphButtonRow.Dispose(); hv_GraphButtonColumn.Dispose(); hv_GraphButton.Dispose();
                                HOperatorSet.GetMpositionSubPix(hv_WindowHandle, out hv_GraphButtonRow,
                                    out hv_GraphButtonColumn, out hv_GraphButton);
                                if ((int)new HTuple(hv_GraphButton.TupleNotEqual(0)) != 0)
                                {
                                    if ((int)new HTuple(new HTuple(new HTuple(hv_GraphButtonRow.TupleGreater(
                                        hv_Height - hv_TextHeight - 25)).TupleAnd(new HTuple(hv_GraphButtonRow.TupleLess(
                                        hv_Height)))).TupleAnd(new HTuple(hv_GraphButtonColumn.TupleGreater(
                                        hv_Width - hv_TextWidth - 15)))).TupleAnd(new HTuple(hv_GraphButtonColumn.TupleLess(
                                        hv_Width))) != 0)
                                    {
                                        //Wait until the continue button has been released
                                        if ((int)new HTuple(hv_WaitForButtonRelease.TupleEqual("true")) != 0)
                                        {
                                            while (1 != 0)
                                            {
                                                hv_GraphButtonRow.Dispose(); hv_GraphButtonColumn.Dispose(); hv_GraphButton.Dispose();
                                                HOperatorSet.GetMpositionSubPix(hv_WindowHandle, out hv_GraphButtonRow,
                                                    out hv_GraphButtonColumn, out hv_GraphButton);
                                                if ((int)new HTuple(hv_GraphButton.TupleEqual(0)).TupleOr(
                                                    new HTuple(hv_GraphButton.TupleEqual(new HTuple()))) != 0)
                                                {
                                                    if ((int)new HTuple(new HTuple(new HTuple(hv_GraphButtonRow.TupleGreater(
                                                        hv_Height - hv_TextHeight - 25)).TupleAnd(new HTuple(hv_GraphButtonRow.TupleLess(
                                                        hv_Height)))).TupleAnd(new HTuple(hv_GraphButtonColumn.TupleGreater(
                                                        hv_Width - hv_TextWidth - 15)))).TupleAnd(new HTuple(hv_GraphButtonColumn.TupleLess(
                                                        hv_Width))) != 0)
                                                    {
                                                        hv_ButtonReleased.Dispose();
                                                        hv_ButtonReleased = 1;
                                                    }
                                                    else
                                                    {
                                                        hv_ButtonReleased.Dispose();
                                                        hv_ButtonReleased = 0;
                                                    }
                                                    //
                                                    break;
                                                }
                                                //Keep waiting until mouse button is released or moved out of the window
                                            }
                                        }
                                        else
                                        {
                                            hv_ButtonReleased.Dispose();
                                            hv_ButtonReleased = 1;
                                        }
                                        //Exit the visualization loop
                                        if ((int)hv_ButtonReleased != 0)
                                        {
                                            hv_Exit.Dispose();
                                            hv_Exit = 1;
                                            break;
                                        }
                                    }
                                    hv_GraphEvent.Dispose();
                                    hv_GraphEvent = 1;
                                    break;
                                }
                                else
                                {
                                    hv_ButtonHold.Dispose();
                                    hv_ButtonHold = 0;
                                }
                            }
                            // catch (Exception) 
                            catch (HalconException HDevExpDefaultException2)
                            {
                                HDevExpDefaultException2.ToHTuple(out hv_Exception);
                                //Keep waiting
                            }
                        }
                        if ((int)hv_GraphEvent != 0)
                        {
                            {
                                HTuple ExpTmpOutVar_0; HTuple ExpTmpOutVar_1; HTuple ExpTmpOutVar_2; HTuple ExpTmpOutVar_3;
                                HDevelopExport.analyze_graph_event(ho_Image, hv_MouseMapping, hv_GraphButton, hv_GraphButtonRow,
                                    hv_GraphButtonColumn, hv_WindowHandle, hv_WindowHandleBuffer, hv_VirtualTrackball,
                                    hv_TrackballSize, hv_SelectedObject, hv_Scene3D, hv_AlphaOrig, hv_ObjectModel3D,
                                    hv_CamParam_COPY_INP_TMP, hv_Label_COPY_INP_TMP, hv_Title, hv_Information,
                                    hv_GenParamName_COPY_INP_TMP, hv_GenParamValue_COPY_INP_TMP, hv_Poses,
                                    hv_ButtonHold, hv_TBCenter, hv_TBSize, hv_WindowCenteredRotation,
                                    hv_MaxNumModels, out ExpTmpOutVar_0, out ExpTmpOutVar_1, out ExpTmpOutVar_2,
                                    out ExpTmpOutVar_3);
                                hv_Poses.Dispose();
                                hv_Poses = ExpTmpOutVar_0;
                                hv_SelectedObject.Dispose();
                                hv_SelectedObject = ExpTmpOutVar_1;
                                hv_ButtonHold.Dispose();
                                hv_ButtonHold = ExpTmpOutVar_2;
                                hv_WindowCenteredRotation.Dispose();
                                hv_WindowCenteredRotation = ExpTmpOutVar_3;
                            }
                        }
                        if ((int)hv_Exit != 0)
                        {
                            break;
                        }
                    }
                    //
                    //Display final state with persistence, if requested
                    //Note that disp_object_model_3d must be used instead of the 3D scene
                    if ((int)new HTuple(new HTuple(hv_PersistenceParamName.TupleLength()).TupleGreater(
                        0)) != 0)
                    {
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            HOperatorSet.DispObjectModel3d(hv_WindowHandle, hv_ObjectModel3D, hv_CamParam_COPY_INP_TMP,
                                hv_Poses, new HTuple("disp_background").TupleConcat("alpha").TupleConcat(
                                hv_PersistenceParamName), new HTuple("true").TupleConcat(0.0).TupleConcat(
                                hv_PersistenceParamValue));
                        }
                    }
                    //
                    //Compute the output pose
                    if ((int)HDevelopExport.ExpGetGlobalVar_gIsSinglePose() != 0)
                    {
                        hv_PoseOut.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_PoseOut = hv_Poses.TupleSelectRange(
                                0, 6);
                        }
                    }
                    else
                    {
                        hv_PoseOut.Dispose();
                        hv_PoseOut = new HTuple(hv_Poses);
                    }
                    //
                    //Clean up.
                    HOperatorSet.SetSystem("clip_region", hv_ClipRegion);
                    // dev_set_preferences(...); only in hdevelop
                    // dev_set_preferences(...); only in hdevelop
                    HDevelopExport.dump_image_output(ho_Image, hv_WindowHandleBuffer, hv_Scene3D, hv_AlphaOrig,
                        hv_ObjectModel3D, hv_GenParamName_COPY_INP_TMP, hv_GenParamValue_COPY_INP_TMP,
                        hv_CamParam_COPY_INP_TMP, hv_Poses, hv_ColorImage, hv_Title, new HTuple(),
                        hv_Label_COPY_INP_TMP, 0, "false", hv_TrackballCenterRow, hv_TrackballCenterCol,
                        hv_TBSize, hv_SelectedObject, hv_WindowCenteredRotation, hv_TBCenter);
                    ho_ImageDump.Dispose();
                    HOperatorSet.DumpWindowImage(out ho_ImageDump, hv_WindowHandleBuffer);
                    HDevWindowStack.SetActive(hv_WindowHandle);
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_ImageDump, HDevWindowStack.GetActive());
                    }
                    HOperatorSet.CloseWindow(hv_WindowHandleBuffer);
                    HOperatorSet.SetPart(hv_WindowHandle, hv_WPRow1, hv_WPColumn1, hv_WPRow2,
                        hv_WPColumn2);
                    HOperatorSet.ClearScene3d(hv_Scene3D);
                    hv_Scene3D.Dispose();
                    hv_Scene3D = new HTuple();
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    try
                    {
                        //Try to clean up as much as possible.
                        if ((int)new HTuple(new HTuple(0).TupleLess(new HTuple(hv_Scene3DTest.TupleLength()
                            ))) != 0)
                        {
                            HOperatorSet.ClearScene3d(hv_Scene3DTest);
                            hv_Scene3DTest.Dispose();
                            hv_Scene3DTest = new HTuple();
                        }
                        if ((int)new HTuple(new HTuple(0).TupleLess(new HTuple(hv_Scene3D.TupleLength()
                            ))) != 0)
                        {
                            HOperatorSet.ClearScene3d(hv_Scene3D);
                            hv_Scene3D.Dispose();
                            hv_Scene3D = new HTuple();
                        }
                        if ((int)new HTuple(new HTuple(0).TupleLess(new HTuple(hv_WindowHandleBuffer.TupleLength()
                            ))) != 0)
                        {
                            HOperatorSet.CloseWindow(hv_WindowHandleBuffer);
                            hv_WindowHandleBuffer.Dispose();
                            hv_WindowHandleBuffer = new HTuple();
                        }
                    }
                    // catch (e) 
                    catch (HalconException HDevExpDefaultException2)
                    {
                        HDevExpDefaultException2.ToHTuple(out hv_e);
                        //Suppress all further exceptions to return the original exception.
                    }
                    try
                    {
                        //Restore system settings.
                        HOperatorSet.SetSystem("clip_region", hv_ClipRegion);
                        // dev_set_preferences(...); only in hdevelop
                        // dev_set_preferences(...); only in hdevelop
                    }
                    // catch (e) 
                    catch (HalconException HDevExpDefaultException2)
                    {
                        HDevExpDefaultException2.ToHTuple(out hv_e);
                        //Suppress all further exceptions to return the original exception.
                    }
                    //
                    throw new HalconException(hv_Exception);
                }
                ho_Image.Dispose();
                ho_ImageDump.Dispose();
 
                hv_CamParam_COPY_INP_TMP.Dispose();
                hv_GenParamName_COPY_INP_TMP.Dispose();
                hv_GenParamValue_COPY_INP_TMP.Dispose();
                hv_Label_COPY_INP_TMP.Dispose();
                hv_PoseIn_COPY_INP_TMP.Dispose();
                hv_Scene3DTest.Dispose();
                hv_Scene3D.Dispose();
                hv_WindowHandleBuffer.Dispose();
                hv_TrackballSize.Dispose();
                hv_VirtualTrackball.Dispose();
                hv_MouseMapping.Dispose();
                hv_WaitForButtonRelease.Dispose();
                hv_MaxNumModels.Dispose();
                hv_WindowCenteredRotation.Dispose();
                hv_NumModels.Dispose();
                hv_SelectedObject.Dispose();
                hv_ClipRegion.Dispose();
                hv_CPLength.Dispose();
                hv_RowNotUsed.Dispose();
                hv_ColumnNotUsed.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_WPRow1.Dispose();
                hv_WPColumn1.Dispose();
                hv_WPRow2.Dispose();
                hv_WPColumn2.Dispose();
                hv_CamParamValue.Dispose();
                hv_CamWidth.Dispose();
                hv_CamHeight.Dispose();
                hv_Scale.Dispose();
                hv_Indices.Dispose();
                hv_DispBackground.Dispose();
                hv_Mask.Dispose();
                hv_Center.Dispose();
                hv_PoseEstimated.Dispose();
                hv_Poses.Dispose();
                hv_HomMat3Ds.Dispose();
                hv_Sequence.Dispose();
                hv_Font.Dispose();
                hv_Exception.Dispose();
                hv_OpenGLInfo.Dispose();
                hv_DummyObjectModel3D.Dispose();
                hv_CameraIndexTest.Dispose();
                hv_PoseTest.Dispose();
                hv_InstanceIndexTest.Dispose();
                hv_MinImageSize.Dispose();
                hv_TrackballRadiusPixel.Dispose();
                hv_Ascent.Dispose();
                hv_Descent.Dispose();
                hv_TextWidth.Dispose();
                hv_TextHeight.Dispose();
                hv_NumChannels.Dispose();
                hv_ColorImage.Dispose();
                hv_CameraIndex.Dispose();
                hv_AllInstances.Dispose();
                hv_SetLight.Dispose();
                hv_LightParam.Dispose();
                hv_LightPosition.Dispose();
                hv_LightKind.Dispose();
                hv_LightIndex.Dispose();
                hv_PersistenceParamName.Dispose();
                hv_PersistenceParamValue.Dispose();
                hv_AlphaOrig.Dispose();
                hv_I.Dispose();
                hv_ParamName.Dispose();
                hv_ParamValue.Dispose();
                hv_ParamNameTrunk.Dispose();
                hv_Instance.Dispose();
                hv_HomMat3D.Dispose();
                hv_Qx.Dispose();
                hv_Qy.Dispose();
                hv_Qz.Dispose();
                hv_TBCenter.Dispose();
                hv_TBSize.Dispose();
                hv_ButtonHold.Dispose();
                hv_VisualizeTB.Dispose();
                hv_MaxIndex.Dispose();
                hv_TrackballCenterRow.Dispose();
                hv_TrackballCenterCol.Dispose();
                hv_GraphEvent.Dispose();
                hv_Exit.Dispose();
                hv_GraphButtonRow.Dispose();
                hv_GraphButtonColumn.Dispose();
                hv_GraphButton.Dispose();
                hv_ButtonReleased.Dispose();
                hv_e.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Image.Dispose();
                ho_ImageDump.Dispose();
 
                hv_CamParam_COPY_INP_TMP.Dispose();
                hv_GenParamName_COPY_INP_TMP.Dispose();
                hv_GenParamValue_COPY_INP_TMP.Dispose();
                hv_Label_COPY_INP_TMP.Dispose();
                hv_PoseIn_COPY_INP_TMP.Dispose();
                hv_Scene3DTest.Dispose();
                hv_Scene3D.Dispose();
                hv_WindowHandleBuffer.Dispose();
                hv_TrackballSize.Dispose();
                hv_VirtualTrackball.Dispose();
                hv_MouseMapping.Dispose();
                hv_WaitForButtonRelease.Dispose();
                hv_MaxNumModels.Dispose();
                hv_WindowCenteredRotation.Dispose();
                hv_NumModels.Dispose();
                hv_SelectedObject.Dispose();
                hv_ClipRegion.Dispose();
                hv_CPLength.Dispose();
                hv_RowNotUsed.Dispose();
                hv_ColumnNotUsed.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_WPRow1.Dispose();
                hv_WPColumn1.Dispose();
                hv_WPRow2.Dispose();
                hv_WPColumn2.Dispose();
                hv_CamParamValue.Dispose();
                hv_CamWidth.Dispose();
                hv_CamHeight.Dispose();
                hv_Scale.Dispose();
                hv_Indices.Dispose();
                hv_DispBackground.Dispose();
                hv_Mask.Dispose();
                hv_Center.Dispose();
                hv_PoseEstimated.Dispose();
                hv_Poses.Dispose();
                hv_HomMat3Ds.Dispose();
                hv_Sequence.Dispose();
                hv_Font.Dispose();
                hv_Exception.Dispose();
                hv_OpenGLInfo.Dispose();
                hv_DummyObjectModel3D.Dispose();
                hv_CameraIndexTest.Dispose();
                hv_PoseTest.Dispose();
                hv_InstanceIndexTest.Dispose();
                hv_MinImageSize.Dispose();
                hv_TrackballRadiusPixel.Dispose();
                hv_Ascent.Dispose();
                hv_Descent.Dispose();
                hv_TextWidth.Dispose();
                hv_TextHeight.Dispose();
                hv_NumChannels.Dispose();
                hv_ColorImage.Dispose();
                hv_CameraIndex.Dispose();
                hv_AllInstances.Dispose();
                hv_SetLight.Dispose();
                hv_LightParam.Dispose();
                hv_LightPosition.Dispose();
                hv_LightKind.Dispose();
                hv_LightIndex.Dispose();
                hv_PersistenceParamName.Dispose();
                hv_PersistenceParamValue.Dispose();
                hv_AlphaOrig.Dispose();
                hv_I.Dispose();
                hv_ParamName.Dispose();
                hv_ParamValue.Dispose();
                hv_ParamNameTrunk.Dispose();
                hv_Instance.Dispose();
                hv_HomMat3D.Dispose();
                hv_Qx.Dispose();
                hv_Qy.Dispose();
                hv_Qz.Dispose();
                hv_TBCenter.Dispose();
                hv_TBSize.Dispose();
                hv_ButtonHold.Dispose();
                hv_VisualizeTB.Dispose();
                hv_MaxIndex.Dispose();
                hv_TrackballCenterRow.Dispose();
                hv_TrackballCenterCol.Dispose();
                hv_GraphEvent.Dispose();
                hv_Exit.Dispose();
                hv_GraphButtonRow.Dispose();
                hv_GraphButtonColumn.Dispose();
                hv_GraphButton.Dispose();
                hv_ButtonReleased.Dispose();
                hv_e.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        // Chapter: Calibration / Camera Parameters
        // Short Description: Set the value of a specified camera parameter in the camera parameter tuple. 
        public static void set_cam_par_data(HTuple hv_CameraParamIn, HTuple hv_ParamName, HTuple hv_ParamValue,
            out HTuple hv_CameraParamOut)
        {
            // Local iconic variables 
 
            // Local control variables 
 
            HTuple hv_CameraType = new HTuple(), hv_CameraParamNames = new HTuple();
            HTuple hv_Index = new HTuple(), hv_ParamNameInd = new HTuple();
            HTuple hv_I = new HTuple(), hv_IsTelecentric = new HTuple();
            // Initialize local and output iconic variables 
            hv_CameraParamOut = new HTuple();
            try
            {
                //set_cam_par_data sets the value of the parameter that
                //is given in ParamName in the tuple of camera parameters
                //given in CameraParamIn. The modified camera parameters
                //are returned in CameraParamOut.
                //
                //Check for consistent length of input parameters
                if ((int)new HTuple(new HTuple(hv_ParamName.TupleLength()).TupleNotEqual(
                    new HTuple(hv_ParamValue.TupleLength()))) != 0)
                {
                    throw new HalconException("Different number of values in ParamName and ParamValue");
                }
                //First, get the parameter names that correspond to the
                //elements in the input camera parameter tuple.
                hv_CameraType.Dispose(); hv_CameraParamNames.Dispose();
                HDevelopExport.get_cam_par_names(hv_CameraParamIn, out hv_CameraType, out hv_CameraParamNames);
                //
                //Find the index of the requested camera data and return
                //the corresponding value.
                hv_CameraParamOut.Dispose();
                hv_CameraParamOut = new HTuple(hv_CameraParamIn);
                for (hv_Index = 0; (int)hv_Index <= (int)(new HTuple(hv_ParamName.TupleLength()
                    ) - 1); hv_Index = (int)hv_Index + 1)
                {
                    hv_ParamNameInd.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_ParamNameInd = hv_ParamName.TupleSelect(
                            hv_Index);
                    }
                    hv_I.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_I = hv_CameraParamNames.TupleFind(
                            hv_ParamNameInd);
                    }
                    if ((int)new HTuple(hv_I.TupleNotEqual(-1)) != 0)
                    {
                        if (hv_CameraParamOut == null)
                            hv_CameraParamOut = new HTuple();
                        hv_CameraParamOut[hv_I] = hv_ParamValue.TupleSelect(hv_Index);
                    }
                    else
                    {
                        throw new HalconException("Wrong ParamName " + hv_ParamNameInd);
                    }
                    //Check the consistency of focus and telecentricity
                    if ((int)new HTuple(hv_ParamNameInd.TupleEqual("focus")) != 0)
                    {
                        hv_IsTelecentric.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_IsTelecentric = new HTuple(hv_CameraType.TupleStrstr(
                                "telecentric").TupleNotEqual(-1)).TupleAnd(new HTuple(hv_CameraType.TupleStrstr(
                                "image_side_telecentric").TupleEqual(-1)));
                        }
                        if ((int)hv_IsTelecentric != 0)
                        {
                            throw new HalconException(new HTuple("Focus for telecentric lenses is always 0, and hence, cannot be changed."));
                        }
                        if ((int)new HTuple(hv_IsTelecentric.TupleNot()).TupleAnd(new HTuple(hv_ParamValue.TupleSelect(
                            hv_Index).TupleEqual(0.0))) != 0)
                        {
                            throw new HalconException("Focus for non-telecentric lenses must not be 0.");
                        }
                    }
                }
 
                hv_CameraType.Dispose();
                hv_CameraParamNames.Dispose();
                hv_Index.Dispose();
                hv_ParamNameInd.Dispose();
                hv_I.Dispose();
                hv_IsTelecentric.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
 
                hv_CameraType.Dispose();
                hv_CameraParamNames.Dispose();
                hv_Index.Dispose();
                hv_ParamNameInd.Dispose();
                hv_I.Dispose();
                hv_IsTelecentric.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        public static void gen_arrow_contour_xld(out HObject ho_Arrow, HTuple hv_Row1, HTuple hv_Column1,
            HTuple hv_Row2, HTuple hv_Column2, HTuple hv_HeadLength, HTuple hv_HeadWidth)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
 
            HObject ho_TempArrow = null;
 
            // Local control variables 
 
            HTuple hv_Length = new HTuple(), hv_ZeroLengthIndices = new HTuple();
            HTuple hv_DR = new HTuple(), hv_DC = new HTuple(), hv_HalfHeadWidth = new HTuple();
            HTuple hv_RowP1 = new HTuple(), hv_ColP1 = new HTuple();
            HTuple hv_RowP2 = new HTuple(), hv_ColP2 = new HTuple();
            HTuple hv_Index = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Arrow);
            HOperatorSet.GenEmptyObj(out ho_TempArrow);
            try
            {
                //This procedure generates arrow shaped XLD contours,
                //pointing from (Row1, Column1) to (Row2, Column2).
                //If starting and end point are identical, a contour consisting
                //of a single point is returned.
                //
                //input parameteres:
                //Row1, Column1: Coordinates of the arrows' starting points
                //Row2, Column2: Coordinates of the arrows' end points
                //HeadLength, HeadWidth: Size of the arrow heads in pixels
                //
                //output parameter:
                //Arrow: The resulting XLD contour
                //
                //The input tuples Row1, Column1, Row2, and Column2 have to be of
                //the same length.
                //HeadLength and HeadWidth either have to be of the same length as
                //Row1, Column1, Row2, and Column2 or have to be a single element.
                //If one of the above restrictions is violated, an error will occur.
                //
                //
                //Init
                ho_Arrow.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Arrow);
                //
                //Calculate the arrow length
                hv_Length.Dispose();
                HOperatorSet.DistancePp(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out hv_Length);
                //
                //Mark arrows with identical start and end point
                //(set Length to -1 to avoid division-by-zero exception)
                hv_ZeroLengthIndices.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_ZeroLengthIndices = hv_Length.TupleFind(
                        0);
                }
                if ((int)(new HTuple(hv_ZeroLengthIndices.TupleNotEqual(-1))) != 0)
                {
                    if (hv_Length == null)
                        hv_Length = new HTuple();
                    hv_Length[hv_ZeroLengthIndices] = -1;
                }
                //
                //Calculate auxiliary variables.
                hv_DR.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_DR = (1.0 * (hv_Row2 - hv_Row1)) / hv_Length;
                }
                hv_DC.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_DC = (1.0 * (hv_Column2 - hv_Column1)) / hv_Length;
                }
                hv_HalfHeadWidth.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_HalfHeadWidth = hv_HeadWidth / 2.0;
                }
                //
                //Calculate end points of the arrow head.
                hv_RowP1.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_RowP1 = (hv_Row1 + ((hv_Length - hv_HeadLength) * hv_DR)) + (hv_HalfHeadWidth * hv_DC);
                }
                hv_ColP1.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_ColP1 = (hv_Column1 + ((hv_Length - hv_HeadLength) * hv_DC)) - (hv_HalfHeadWidth * hv_DR);
                }
                hv_RowP2.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_RowP2 = (hv_Row1 + ((hv_Length - hv_HeadLength) * hv_DR)) - (hv_HalfHeadWidth * hv_DC);
                }
                hv_ColP2.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_ColP2 = (hv_Column1 + ((hv_Length - hv_HeadLength) * hv_DC)) + (hv_HalfHeadWidth * hv_DR);
                }
                //
                //Finally create output XLD contour for each input point pair
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_Length.TupleLength())) - 1); hv_Index = (int)hv_Index + 1)
                {
                    if ((int)(new HTuple(((hv_Length.TupleSelect(hv_Index))).TupleEqual(-1))) != 0)
                    {
                        //Create_ single points for arrows with identical start and end point
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ho_TempArrow.Dispose();
                            HOperatorSet.GenContourPolygonXld(out ho_TempArrow, hv_Row1.TupleSelect(
                                hv_Index), hv_Column1.TupleSelect(hv_Index));
                        }
                    }
                    else
                    {
                        //Create arrow contour
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ho_TempArrow.Dispose();
                            HOperatorSet.GenContourPolygonXld(out ho_TempArrow, ((((((((((hv_Row1.TupleSelect(
                                hv_Index))).TupleConcat(hv_Row2.TupleSelect(hv_Index)))).TupleConcat(
                                hv_RowP1.TupleSelect(hv_Index)))).TupleConcat(hv_Row2.TupleSelect(hv_Index)))).TupleConcat(
                                hv_RowP2.TupleSelect(hv_Index)))).TupleConcat(hv_Row2.TupleSelect(hv_Index)),
                                ((((((((((hv_Column1.TupleSelect(hv_Index))).TupleConcat(hv_Column2.TupleSelect(
                                hv_Index)))).TupleConcat(hv_ColP1.TupleSelect(hv_Index)))).TupleConcat(
                                hv_Column2.TupleSelect(hv_Index)))).TupleConcat(hv_ColP2.TupleSelect(
                                hv_Index)))).TupleConcat(hv_Column2.TupleSelect(hv_Index)));
                        }
                    }
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.ConcatObj(ho_Arrow, ho_TempArrow, out ExpTmpOutVar_0);
                        ho_Arrow.Dispose();
                        ho_Arrow = ExpTmpOutVar_0;
                    }
                }
                ho_TempArrow.Dispose();
 
                hv_Length.Dispose();
                hv_ZeroLengthIndices.Dispose();
                hv_DR.Dispose();
                hv_DC.Dispose();
                hv_HalfHeadWidth.Dispose();
                hv_RowP1.Dispose();
                hv_ColP1.Dispose();
                hv_RowP2.Dispose();
                hv_ColP2.Dispose();
                hv_Index.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_TempArrow.Dispose();
 
                hv_Length.Dispose();
                hv_ZeroLengthIndices.Dispose();
                hv_DR.Dispose();
                hv_DC.Dispose();
                hv_HalfHeadWidth.Dispose();
                hv_RowP1.Dispose();
                hv_ColP1.Dispose();
                hv_RowP2.Dispose();
                hv_ColP2.Dispose();
                hv_Index.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        public static void get_rect2_vertex(HTuple hv_Row, HTuple hv_Column, HTuple hv_Phi, HTuple hv_Length1,
            HTuple hv_Length2, out HTuple hv_TopLeft_Row, out HTuple hv_TopLeft_Col, out HTuple hv_TopRight_Row,
            out HTuple hv_TopRight_Col, out HTuple hv_LowLeft_Row, out HTuple hv_LowLeft_Col,
            out HTuple hv_LowRight_Row, out HTuple hv_LowRight_Col)
        {
            // Local iconic variables 
 
            // Local control variables 
 
            HTuple hv_Tmp = new HTuple(), hv_Sin = new HTuple();
            HTuple hv_Cos = new HTuple(), hv_TopLeft_X = new HTuple();
            HTuple hv_TopLeft_Y = new HTuple(), hv_TopRight_X = new HTuple();
            HTuple hv_TopRight_Y = new HTuple(), hv_LowerRight_X = new HTuple();
            HTuple hv_LowerRight_Y = new HTuple(), hv_LowerLeft_X = new HTuple();
            HTuple hv_LowerLeft_Y = new HTuple();
            HTuple hv_Length1_COPY_INP_TMP = new HTuple(hv_Length1);
            HTuple hv_Length2_COPY_INP_TMP = new HTuple(hv_Length2);
            HTuple hv_Phi_COPY_INP_TMP = new HTuple(hv_Phi);
 
            // Initialize local and output iconic variables 
            hv_TopLeft_Row = new HTuple();
            hv_TopLeft_Col = new HTuple();
            hv_TopRight_Row = new HTuple();
            hv_TopRight_Col = new HTuple();
            hv_LowLeft_Row = new HTuple();
            hv_LowLeft_Col = new HTuple();
            hv_LowRight_Row = new HTuple();
            hv_LowRight_Col = new HTuple();
            try
            {
                if ((int)(new HTuple(((((hv_Phi_COPY_INP_TMP.TupleDeg())).TupleAbs())).TupleGreater(
                    45))) != 0)
                {
 
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                              ExpTmpLocalVar_Phi = (((hv_Phi_COPY_INP_TMP.TupleDeg()
                                ) - (90 * (hv_Phi_COPY_INP_TMP / (hv_Phi_COPY_INP_TMP.TupleAbs()))))).TupleRad()
                                ;
                            hv_Phi_COPY_INP_TMP.Dispose();
                            hv_Phi_COPY_INP_TMP = ExpTmpLocalVar_Phi;
                        }
                    }
 
                    hv_Tmp.Dispose();
                    hv_Tmp = new HTuple(hv_Length1_COPY_INP_TMP);
 
                    hv_Length1_COPY_INP_TMP.Dispose();
                    hv_Length1_COPY_INP_TMP = new HTuple(hv_Length2_COPY_INP_TMP);
 
                    hv_Length2_COPY_INP_TMP.Dispose();
                    hv_Length2_COPY_INP_TMP = new HTuple(hv_Tmp);
 
                }
 
                hv_Sin.Dispose();
                HOperatorSet.TupleSin(hv_Phi_COPY_INP_TMP, out hv_Sin);
 
                hv_Cos.Dispose();
                HOperatorSet.TupleCos(hv_Phi_COPY_INP_TMP, out hv_Cos);
 
                //左上角
 
                hv_TopLeft_X.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopLeft_X = ((-hv_Length1_COPY_INP_TMP) * hv_Cos) - (hv_Length2_COPY_INP_TMP * hv_Sin);
                }
 
                hv_TopLeft_Y.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopLeft_Y = ((-hv_Length1_COPY_INP_TMP) * hv_Sin) + (hv_Length2_COPY_INP_TMP * hv_Cos);
                }
 
                hv_TopLeft_Row.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopLeft_Row = hv_Row - hv_TopLeft_Y;
                }
 
                hv_TopLeft_Col.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopLeft_Col = hv_Column + hv_TopLeft_X;
                }
 
                //右上角
 
                hv_TopRight_X.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopRight_X = (hv_Length1_COPY_INP_TMP * hv_Cos) - (hv_Length2_COPY_INP_TMP * hv_Sin);
                }
 
                hv_TopRight_Y.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopRight_Y = (hv_Length1_COPY_INP_TMP * hv_Sin) + (hv_Length2_COPY_INP_TMP * hv_Cos);
                }
 
                hv_TopRight_Row.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopRight_Row = hv_Row - hv_TopRight_Y;
                }
 
                hv_TopRight_Col.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_TopRight_Col = hv_Column + hv_TopRight_X;
                }
 
                //右下角
 
                hv_LowerRight_X.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowerRight_X = (hv_Length1_COPY_INP_TMP * hv_Cos) + (hv_Length2_COPY_INP_TMP * hv_Sin);
                }
 
                hv_LowerRight_Y.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowerRight_Y = (hv_Length1_COPY_INP_TMP * hv_Sin) - (hv_Length2_COPY_INP_TMP * hv_Cos);
                }
 
                hv_LowRight_Row.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowRight_Row = hv_Row - hv_LowerRight_Y;
                }
 
                hv_LowRight_Col.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowRight_Col = hv_Column + hv_LowerRight_X;
                }
 
                //左下角
 
                hv_LowerLeft_X.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowerLeft_X = ((-hv_Length1_COPY_INP_TMP) * hv_Cos) + (hv_Length2_COPY_INP_TMP * hv_Sin);
                }
 
                hv_LowerLeft_Y.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowerLeft_Y = ((-hv_Length1_COPY_INP_TMP) * hv_Sin) - (hv_Length2_COPY_INP_TMP * hv_Cos);
                }
 
                hv_LowLeft_Row.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowLeft_Row = hv_Row - hv_LowerLeft_Y;
                }
 
                hv_LowLeft_Col.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_LowLeft_Col = hv_Column + hv_LowerLeft_X;
                }
 
 
                hv_Length1_COPY_INP_TMP.Dispose();
                hv_Length2_COPY_INP_TMP.Dispose();
                hv_Phi_COPY_INP_TMP.Dispose();
                hv_Tmp.Dispose();
                hv_Sin.Dispose();
                hv_Cos.Dispose();
                hv_TopLeft_X.Dispose();
                hv_TopLeft_Y.Dispose();
                hv_TopRight_X.Dispose();
                hv_TopRight_Y.Dispose();
                hv_LowerRight_X.Dispose();
                hv_LowerRight_Y.Dispose();
                hv_LowerLeft_X.Dispose();
                hv_LowerLeft_Y.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
 
                hv_Length1_COPY_INP_TMP.Dispose();
                hv_Length2_COPY_INP_TMP.Dispose();
                hv_Phi_COPY_INP_TMP.Dispose();
                hv_Tmp.Dispose();
                hv_Sin.Dispose();
                hv_Cos.Dispose();
                hv_TopLeft_X.Dispose();
                hv_TopLeft_Y.Dispose();
                hv_TopRight_X.Dispose();
                hv_TopRight_Y.Dispose();
                hv_LowerRight_X.Dispose();
                hv_LowerRight_Y.Dispose();
                hv_LowerLeft_X.Dispose();
                hv_LowerLeft_Y.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        public static void pts_to_best_line(out HObject ho_LineXld, HTuple hv_Rows, HTuple hv_Columns,
            HTuple hv_IgnoreNum, out HTuple hv_Row1, out HTuple hv_Column1, out HTuple hv_Row2,
            out HTuple hv_Column2)
        {
            // Local iconic variables 
 
            HObject ho_Contour = null;
 
            // Local control variables 
 
            HTuple hv_Length = new HTuple(), hv_Nr = new HTuple();
            HTuple hv_Nc = new HTuple(), hv_Dist = new HTuple(), hv_Length1 = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_LineXld);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            hv_Row1 = new HTuple();
            hv_Column1 = new HTuple();
            hv_Row2 = new HTuple();
            hv_Column2 = new HTuple();
            try
            {
                //初始化
                hv_Row1.Dispose();
                hv_Row1 = 0;
                hv_Column1.Dispose();
                hv_Column1 = 0;
                hv_Row2.Dispose();
                hv_Row2 = 0;
                hv_Column2.Dispose();
                hv_Column2 = 0;
                //产生一个空的直线对象,用于保存拟合后的直线
                ho_LineXld.Dispose();
                HOperatorSet.GenEmptyObj(out ho_LineXld);
                //计算边缘数量
                hv_Length.Dispose();
                HOperatorSet.TupleLength(hv_Columns, out hv_Length);
                //当边缘数量不小于有效点数时进行拟合
                if ((int)((new HTuple(hv_Length.TupleGreaterEqual(2))).TupleAnd(new HTuple(hv_IgnoreNum.TupleLess(
                    hv_Length - 2)))) != 0)
                {
                    //halcon的拟合是基于xld的,需要把边缘连接成xld
                    ho_Contour.Dispose();
                    HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_Rows, hv_Columns);
                    //拟合直线。使用的算法是'tukey',其他算法参考fit_line_contour_xld的描述
                    hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Row2.Dispose(); hv_Column2.Dispose(); hv_Nr.Dispose(); hv_Nc.Dispose(); hv_Dist.Dispose();
                    HOperatorSet.FitLineContourXld(ho_Contour, "tukey", -1, hv_IgnoreNum, 5,
                        2, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2, out hv_Nr,
                        out hv_Nc, out hv_Dist);
                    //判断拟合结果是否有效:如果拟合成功,数组中元素的数量大于0
                    hv_Length1.Dispose();
                    HOperatorSet.TupleLength(hv_Dist, out hv_Length1);
                    if ((int)(new HTuple(hv_Length1.TupleLess(1))) != 0)
                    {
                        ho_Contour.Dispose();
 
                        hv_Length.Dispose();
                        hv_Nr.Dispose();
                        hv_Nc.Dispose();
                        hv_Dist.Dispose();
                        hv_Length1.Dispose();
 
                        return;
                    }
                    //根据拟合结果,产生直线xld
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        ho_LineXld.Dispose();
                        HOperatorSet.GenContourPolygonXld(out ho_LineXld, hv_Row1.TupleConcat(hv_Row2),
                            hv_Column1.TupleConcat(hv_Column2));
                    }
                }
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_Contour.Dispose();
 
                hv_Length.Dispose();
                hv_Nr.Dispose();
                hv_Nc.Dispose();
                hv_Dist.Dispose();
                hv_Length1.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        public static (double, double) CalculateTailValues(HTuple minValue, HTuple maxValue, HTuple mean, HTuple deviation, double tailPercentage = 0.3)
        {
            // 计算低尾灰度值
            double lowTailValue = minValue.TupleReal() + (mean.TupleReal() - minValue.TupleReal()) * tailPercentage;
            if (lowTailValue == 0)
                lowTailValue = mean.TupleReal();
 
            // 计算高尾灰度值
            double highTailValue = maxValue.TupleReal() - (maxValue.TupleReal() - mean.TupleReal()) * tailPercentage;
            if (highTailValue == 0)
                highTailValue = mean.TupleReal();
 
            return (highTailValue, lowTailValue);
        }
 
        /// <summary>
        /// 卡尺算法
        /// </summary>
        /// <param name="ho_Image">待测图片</param>
        /// <param name="ho_Regions">测量卡尺区域显示</param>
        /// <param name="hv_Elements">卡尺个数</param>
        /// <param name="hv_DetectHeight">卡尺长度</param>
        /// <param name="hv_DetectWidth">卡尺宽度</param>
        /// <param name="hv_Sigma">过滤一半像素(推荐值:2)</param>
        /// <param name="hv_Threshold">对比度阈值(推荐值:5)</param>
        /// <param name="hv_Transition">极性(negative/positive/ignore)</param>
        /// <param name="hv_Select">边缘位置(first/last/best)</param>
        /// <param name="hv_Row1">起始纵坐标</param>
        /// <param name="hv_Column1">起始横坐标</param>
        /// <param name="hv_Row2">结束纵坐标</param>
        /// <param name="hv_Column2">结束横坐标</param>
        /// <param name="hv_ResultRow">结果点集合纵坐标</param>
        /// <param name="hv_ResultColumn">结果点集合横坐标</param>
        public static void Rake(HObject ho_Image, out HObject ho_Regions, HTuple hv_Elements,
            HTuple hv_DetectHeight, HTuple hv_DetectWidth, HTuple hv_Sigma, HTuple hv_Threshold,
            HTuple hv_Transition, HTuple hv_Select, HTuple hv_Row1, HTuple hv_Column1, HTuple hv_Row2,
            HTuple hv_Column2, out HTuple hv_ResultRow, out HTuple hv_ResultColumn)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
 
            HObject ho_RegionLines = null, ho_Rectangle = null;
            HObject ho_Arrow1 = null;
            HObject ExpTmpOutVar_0 = null;
 
            // Local control variables 
 
            HTuple hv_SelectOut = new HTuple(), hv_TransitionOut = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_ATan = new HTuple(), hv_i = new HTuple(), hv_RowC = new HTuple();
            HTuple hv_ColC = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_RowL2 = new HTuple(), hv_RowL1 = new HTuple();
            HTuple hv_ColL2 = new HTuple(), hv_ColL1 = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
            HTuple hv_ColEdge = new HTuple(), hv_Amplitude = new HTuple();
            HTuple hv_tRow = new HTuple(), hv_tCol = new HTuple();
            HTuple hv_t = new HTuple(), hv_Number = new HTuple(), hv_j = new HTuple();
            HTuple hv_DetectWidth_COPY_INP_TMP = new HTuple(hv_DetectWidth);
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Arrow1);
            HOperatorSet.GenEmptyObj(out ExpTmpOutVar_0);
            hv_ResultRow = new HTuple();
            hv_ResultColumn = new HTuple();
            try
            {
                hv_SelectOut.Dispose();
                hv_SelectOut = new HTuple(hv_Select);
 
                hv_TransitionOut.Dispose();
                hv_TransitionOut = new HTuple(hv_Transition);
 
                //获取图像尺寸
                hv_Width.Dispose(); hv_Height.Dispose();
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
 
                //产生一个空显示对象,用于显示
                ho_Regions.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Regions);
 
                //初始化边缘坐标数组
                hv_ResultRow.Dispose();
                hv_ResultRow = new HTuple();
 
                hv_ResultColumn.Dispose();
                hv_ResultColumn = new HTuple();
 
                //产生直线xld
                //using (HDevDisposeHelper dh = new HDevDisposeHelper())
                //{
                //    ho_RegionLines.Dispose();
                //    HOperatorSet.GenContourPolygonXld(out ho_RegionLines, hv_Row1.TupleConcat(hv_Row2),
                //        hv_Column1.TupleConcat(hv_Column2));
                //}
 
                //存储到显示对象
                HOperatorSet.ConcatObj(ho_Regions, ho_RegionLines, out ExpTmpOutVar_0);
                ho_Regions.Dispose();
                ho_Regions = ExpTmpOutVar_0;
 
                //计算直线与x轴的夹角,逆时针方向为正向
                hv_ATan.Dispose();
                HOperatorSet.AngleLx(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out hv_ATan);
 
                //边缘检测方向垂直由于检测直线:直线方向正向旋转90°为边缘检测方向
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    {
                        HTuple
                          ExpTmpLocalVar_ATan = hv_ATan + ((new HTuple(90)).TupleRad()
                            );
                        hv_ATan.Dispose();
                        hv_ATan = ExpTmpLocalVar_ATan;
                    }
                }
 
                //根据检测直线按顺序产生测量区域矩形,并存储到显示对象
                HTuple end_val36 = hv_Elements;
                HTuple step_val36 = 1;
                for (hv_i = 1; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36))
                {
                    //如果只有一个测量矩形,作为卡尺工具,宽度为检测直线的长度
                    if ((int)(new HTuple(hv_Elements.TupleEqual(1))) != 0)
                    {
 
                        hv_RowC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_RowC = (hv_Row1 + hv_Row2) * 0.5;
                        }
 
                        hv_ColC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ColC = (hv_Column1 + hv_Column2) * 0.5;
                        }
 
                        //判断是否超出图像,超出不检测边缘
                        if ((int)((new HTuple((new HTuple((new HTuple(hv_RowC.TupleGreater(hv_Height - 1))).TupleOr(
                            new HTuple(hv_RowC.TupleLess(0))))).TupleOr(new HTuple(hv_ColC.TupleGreater(
                            hv_Width - 1))))).TupleOr(new HTuple(hv_ColC.TupleLess(0)))) != 0)
                            continue;
 
                        hv_Distance.Dispose();
                        HOperatorSet.DistancePp(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out hv_Distance);
 
                        hv_DetectWidth_COPY_INP_TMP.Dispose();
                        hv_DetectWidth_COPY_INP_TMP = new HTuple(hv_Distance);
 
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ho_Rectangle.Dispose();
                            HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_RowC, hv_ColC,
                                hv_ATan, hv_DetectHeight / 2, hv_Distance / 2);
                        }
 
                    }
                    else
                    {
                        //如果有多个测量矩形,产生该测量矩形xld
                        hv_RowC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_RowC = hv_Row1 + (((hv_Row2 - hv_Row1) * (hv_i - 1)) / (hv_Elements - 1));
                        }
 
                        hv_ColC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ColC = hv_Column1 + (((hv_Column2 - hv_Column1) * (hv_i - 1)) / (hv_Elements - 1));
                        }
 
                        //判断是否超出图像,超出不检测边缘
                        if ((int)((new HTuple((new HTuple((new HTuple(hv_RowC.TupleGreater(hv_Height - 1))).TupleOr(
                            new HTuple(hv_RowC.TupleLess(0))))).TupleOr(new HTuple(hv_ColC.TupleGreater(
                            hv_Width - 1))))).TupleOr(new HTuple(hv_ColC.TupleLess(0)))) != 0)
                            continue;
 
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ho_Rectangle.Dispose();
                            HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_RowC, hv_ColC,
                                hv_ATan, hv_DetectHeight / 2, hv_DetectWidth_COPY_INP_TMP / 2);
                        }
 
                    }
 
                    //把测量矩形xld存储到显示对象
                    HOperatorSet.ConcatObj(ho_Regions, ho_Rectangle, out ExpTmpOutVar_0);
                    ho_Regions.Dispose();
                    ho_Regions = ExpTmpOutVar_0;
 
                    if ((int)(new HTuple(hv_i.TupleEqual(1))) != 0)
                    {
 
                        //在第一个测量矩形绘制一个箭头xld,用于指示边缘检测方向
                        hv_RowL2.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_RowL2 = hv_RowC + ((hv_DetectHeight / 2) * (((-hv_ATan)).TupleSin()
                                ));
                        }
 
                        hv_RowL1.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_RowL1 = hv_RowC - ((hv_DetectHeight / 2) * (((-hv_ATan)).TupleSin()
                                ));
                        }
 
                        hv_ColL2.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ColL2 = hv_ColC + ((hv_DetectHeight / 2) * (((-hv_ATan)).TupleCos()
                                ));
                        }
 
                        hv_ColL1.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ColL1 = hv_ColC - ((hv_DetectHeight / 2) * (((-hv_ATan)).TupleCos()
                                ));
                        }
 
                        //ho_Arrow1.Dispose();
                        //gen_arrow_contour_xld(out ho_Arrow1, hv_RowL1, hv_ColL1, hv_RowL2, hv_ColL2,
                        //    25, 25);
 
                        ////把xld存储到显示对象
                        //HOperatorSet.ConcatObj(ho_Regions, ho_Arrow1, out ExpTmpOutVar_0);
                        //ho_Regions.Dispose();
                        //ho_Regions = ExpTmpOutVar_0;
                    }
 
                    //产生测量对象句柄
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_MeasureHandle.Dispose();
                        HOperatorSet.GenMeasureRectangle2(hv_RowC, hv_ColC, hv_ATan, hv_DetectHeight / 2,
                            hv_DetectWidth_COPY_INP_TMP / 2, hv_Width, hv_Height, "nearest_neighbor",
                            out hv_MeasureHandle);
                    }
 
                    //设置极性
                    if ((int)(new HTuple(hv_TransitionOut.TupleEqual("negative"))) != 0)
                    {
                        hv_TransitionOut.Dispose();
                        hv_TransitionOut = "negative";
                    }
                    else if ((int)(new HTuple(hv_TransitionOut.TupleEqual("positive"))) != 0)
                    {
                        hv_TransitionOut.Dispose();
                        hv_TransitionOut = "positive";
                    }
                    else
                    {
                        hv_TransitionOut.Dispose();
                        hv_TransitionOut = "all";
                    }
 
                    //设置边缘位置,最强点是从所有边缘中选择幅度绝对值对大点,需要设置为'all'
                    if ((int)(new HTuple(hv_SelectOut.TupleEqual("first"))) != 0)
                    {
                        hv_SelectOut.Dispose();
                        hv_SelectOut = "first";
                    }
                    else if ((int)(new HTuple(hv_SelectOut.TupleEqual("last"))) != 0)
                    {
                        hv_SelectOut.Dispose();
                        hv_SelectOut = "last";
                    }
                    else
                    {
                        hv_SelectOut.Dispose();
                        hv_SelectOut = "all";
                    }
 
                    //检测边缘
                    hv_RowEdge.Dispose(); hv_ColEdge.Dispose(); hv_Amplitude.Dispose(); hv_Distance.Dispose();
                    HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, hv_Sigma, hv_Threshold,
                        hv_TransitionOut, hv_SelectOut, out hv_RowEdge, out hv_ColEdge, out hv_Amplitude,
                        out hv_Distance);
 
                    //清除测量对象句柄
                    HOperatorSet.CloseMeasure(hv_MeasureHandle);
 
                    //临时变量初始化
 
                    //保存找到指定边缘的坐标
                    hv_tRow.Dispose();
                    hv_tRow = 0;
 
                    hv_tCol.Dispose();
                    hv_tCol = 0;
 
                    //保存边缘的幅度绝对值
                    hv_t.Dispose();
                    hv_t = 0;
 
                    //找到的边缘必须至少为1个
                    hv_Number.Dispose();
                    HOperatorSet.TupleLength(hv_RowEdge, out hv_Number);
 
                    if ((int)(new HTuple(hv_Number.TupleLess(1))) != 0)
                        continue;
 
 
                    //有多个边缘时,选择幅度绝对之后最大的边缘
                    HTuple end_val178 = hv_Number - 1;
                    HTuple step_val178 = 1;
                    for (hv_j = 0; hv_j.Continue(end_val178, step_val178); hv_j = hv_j.TupleAdd(step_val178))
                    {
 
                        if ((int)(new HTuple(((((hv_Amplitude.TupleSelect(hv_j))).TupleAbs())).TupleGreater(
                            hv_t))) != 0)
                        {
 
                            hv_tRow.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_tRow = hv_RowEdge.TupleSelect(
                                    hv_j);
                            }
 
                            hv_tCol.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_tCol = hv_ColEdge.TupleSelect(
                                    hv_j);
                            }
 
                            hv_t.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_t = hv_Amplitude.TupleAbs()
                                    ;
                            }
 
                        }
 
                    }
 
                    //把找到的边缘保存在输出数组
 
                    if ((int)(new HTuple(hv_t.TupleGreater(0))) != 0)
                    {
 
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_ResultRow = hv_ResultRow.TupleConcat(
                                    hv_tRow);
                                hv_ResultRow.Dispose();
                                hv_ResultRow = ExpTmpLocalVar_ResultRow;
                            }
                        }
 
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_ResultColumn = hv_ResultColumn.TupleConcat(
                                    hv_tCol);
                                hv_ResultColumn.Dispose();
                                hv_ResultColumn = ExpTmpLocalVar_ResultColumn;
                            }
                        }
 
                    }
 
                }
 
                ho_RegionLines.Dispose();
                ho_Rectangle.Dispose();
                ho_Arrow1.Dispose();
 
                hv_DetectWidth_COPY_INP_TMP.Dispose();
                hv_SelectOut.Dispose();
                hv_TransitionOut.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_ATan.Dispose();
                hv_i.Dispose();
                hv_RowC.Dispose();
                hv_ColC.Dispose();
                hv_Distance.Dispose();
                hv_RowL2.Dispose();
                hv_RowL1.Dispose();
                hv_ColL2.Dispose();
                hv_ColL1.Dispose();
                hv_MeasureHandle.Dispose();
                hv_RowEdge.Dispose();
                hv_ColEdge.Dispose();
                hv_Amplitude.Dispose();
                hv_tRow.Dispose();
                hv_tCol.Dispose();
                hv_t.Dispose();
                hv_Number.Dispose();
                hv_j.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_RegionLines.Dispose();
                ho_Rectangle.Dispose();
                ho_Arrow1.Dispose();
 
                hv_DetectWidth_COPY_INP_TMP.Dispose();
                hv_SelectOut.Dispose();
                hv_TransitionOut.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_ATan.Dispose();
                hv_i.Dispose();
                hv_RowC.Dispose();
                hv_ColC.Dispose();
                hv_Distance.Dispose();
                hv_RowL2.Dispose();
                hv_RowL1.Dispose();
                hv_ColL2.Dispose();
                hv_ColL1.Dispose();
                hv_MeasureHandle.Dispose();
                hv_RowEdge.Dispose();
                hv_ColEdge.Dispose();
                hv_Amplitude.Dispose();
                hv_tRow.Dispose();
                hv_tCol.Dispose();
                hv_t.Dispose();
                hv_Number.Dispose();
                hv_j.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        public static void GetRakeRegions(HObject ho_Image, out HObject ho_Regions, HTuple hv_Elements,
            HTuple hv_DetectHeight, HTuple hv_DetectWidth, HTuple hv_Sigma, HTuple hv_Threshold,
            HTuple hv_Transition, HTuple hv_Select, HTuple hv_Row1, HTuple hv_Column1, HTuple hv_Row2,
            HTuple hv_Column2, out HTuple hv_ResultRow, out HTuple hv_ResultColumn)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];
 
            // Local iconic variables 
 
            HObject ho_RegionLines = null, ho_Rectangle = null;
            HObject ho_Arrow1 = null;
            HObject ExpTmpOutVar_0 = null;
 
            // Local control variables 
 
            HTuple hv_SelectOut = new HTuple(), hv_TransitionOut = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_ATan = new HTuple(), hv_i = new HTuple(), hv_RowC = new HTuple();
            HTuple hv_ColC = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_RowL2 = new HTuple(), hv_RowL1 = new HTuple();
            HTuple hv_ColL2 = new HTuple(), hv_ColL1 = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
            HTuple hv_ColEdge = new HTuple(), hv_Amplitude = new HTuple();
            HTuple hv_tRow = new HTuple(), hv_tCol = new HTuple();
            HTuple hv_t = new HTuple(), hv_Number = new HTuple(), hv_j = new HTuple();
            HTuple hv_DetectWidth_COPY_INP_TMP = new HTuple(hv_DetectWidth);
 
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Arrow1);
            HOperatorSet.GenEmptyObj(out ExpTmpOutVar_0);
            hv_ResultRow = new HTuple();
            hv_ResultColumn = new HTuple();
            try
            {
                hv_SelectOut.Dispose();
                hv_SelectOut = new HTuple(hv_Select);
 
                hv_TransitionOut.Dispose();
                hv_TransitionOut = new HTuple(hv_Transition);
 
                //获取图像尺寸
                hv_Width.Dispose(); hv_Height.Dispose();
                //HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
 
                //产生一个空显示对象,用于显示
                ho_Regions.Dispose();
                HOperatorSet.GenEmptyObj(out ho_Regions);
 
                //初始化边缘坐标数组
                hv_ResultRow.Dispose();
                hv_ResultRow = new HTuple();
 
                hv_ResultColumn.Dispose();
                hv_ResultColumn = new HTuple();
 
                //产生直线xld
                //using (HDevDisposeHelper dh = new HDevDisposeHelper())
                //{
                //    ho_RegionLines.Dispose();
                //    HOperatorSet.GenContourPolygonXld(out ho_RegionLines, hv_Row1.TupleConcat(hv_Row2),
                //        hv_Column1.TupleConcat(hv_Column2));
                //}
 
                //存储到显示对象
                HOperatorSet.ConcatObj(ho_Regions, ho_RegionLines, out ExpTmpOutVar_0);
                ho_Regions.Dispose();
                ho_Regions = ExpTmpOutVar_0;
 
                //计算直线与x轴的夹角,逆时针方向为正向
                hv_ATan.Dispose();
                HOperatorSet.AngleLx(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out hv_ATan);
 
                //边缘检测方向垂直由于检测直线:直线方向正向旋转90°为边缘检测方向
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    {
                        HTuple
                          ExpTmpLocalVar_ATan = hv_ATan + ((new HTuple(90)).TupleRad()
                            );
                        hv_ATan.Dispose();
                        hv_ATan = ExpTmpLocalVar_ATan;
                    }
                }
 
                //根据检测直线按顺序产生测量区域矩形,并存储到显示对象
                HTuple end_val36 = hv_Elements;
                HTuple step_val36 = 1;
                for (hv_i = 1; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36))
                {
                    //如果只有一个测量矩形,作为卡尺工具,宽度为检测直线的长度
                    if ((int)(new HTuple(hv_Elements.TupleEqual(1))) != 0)
                    {
 
                        hv_RowC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_RowC = (hv_Row1 + hv_Row2) * 0.5;
                        }
 
                        hv_ColC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ColC = (hv_Column1 + hv_Column2) * 0.5;
                        }
 
                        ////判断是否超出图像,超出不检测边缘
                        //if ((int)((new HTuple((new HTuple((new HTuple(hv_RowC.TupleGreater(hv_Height - 1))).TupleOr(
                        //    new HTuple(hv_RowC.TupleLess(0))))).TupleOr(new HTuple(hv_ColC.TupleGreater(
                        //    hv_Width - 1))))).TupleOr(new HTuple(hv_ColC.TupleLess(0)))) != 0)
                        //    continue;
 
                        hv_Distance.Dispose();
                        HOperatorSet.DistancePp(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out hv_Distance);
 
                        hv_DetectWidth_COPY_INP_TMP.Dispose();
                        hv_DetectWidth_COPY_INP_TMP = new HTuple(hv_Distance);
 
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ho_Rectangle.Dispose();
                            HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_RowC, hv_ColC,
                                hv_ATan, hv_DetectHeight / 2, hv_Distance / 2);
                        }
                    }
                    else
                    {
                        //如果有多个测量矩形,产生该测量矩形xld
                        hv_RowC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_RowC = hv_Row1 + (((hv_Row2 - hv_Row1) * (hv_i - 1)) / (hv_Elements - 1));
                        }
 
                        hv_ColC.Dispose();
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_ColC = hv_Column1 + (((hv_Column2 - hv_Column1) * (hv_i - 1)) / (hv_Elements - 1));
                        }
 
                        ////判断是否超出图像,超出不检测边缘
                        //if ((int)((new HTuple((new HTuple((new HTuple(hv_RowC.TupleGreater(hv_Height - 1))).TupleOr(
                        //    new HTuple(hv_RowC.TupleLess(0))))).TupleOr(new HTuple(hv_ColC.TupleGreater(
                        //    hv_Width - 1))))).TupleOr(new HTuple(hv_ColC.TupleLess(0)))) != 0)
                        //    continue;
 
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ho_Rectangle.Dispose();
                            HOperatorSet.GenRectangle2ContourXld(out ho_Rectangle, hv_RowC, hv_ColC,
                                hv_ATan, hv_DetectHeight / 2, hv_DetectWidth_COPY_INP_TMP / 2);
                        }
 
                    }
 
                    //把测量矩形xld存储到显示对象
                    HOperatorSet.ConcatObj(ho_Regions, ho_Rectangle, out ExpTmpOutVar_0);
                    ho_Regions.Dispose();
                    ho_Regions = ExpTmpOutVar_0;
 
                    //保存找到指定边缘的坐标
                    hv_tRow.Dispose();
                    hv_tRow = 0;
 
                    hv_tCol.Dispose();
                    hv_tCol = 0;
 
                    //保存边缘的幅度绝对值
                    hv_t.Dispose();
                    hv_t = 0;
                }
 
                ho_RegionLines.Dispose();
                ho_Rectangle.Dispose();
                ho_Arrow1.Dispose();
 
                hv_DetectWidth_COPY_INP_TMP.Dispose();
                hv_SelectOut.Dispose();
                hv_TransitionOut.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_ATan.Dispose();
                hv_i.Dispose();
                hv_RowC.Dispose();
                hv_ColC.Dispose();
                hv_Distance.Dispose();
                hv_RowL2.Dispose();
                hv_RowL1.Dispose();
                hv_ColL2.Dispose();
                hv_ColL1.Dispose();
                hv_MeasureHandle.Dispose();
                hv_RowEdge.Dispose();
                hv_ColEdge.Dispose();
                hv_Amplitude.Dispose();
                hv_tRow.Dispose();
                hv_tCol.Dispose();
                hv_t.Dispose();
                hv_Number.Dispose();
                hv_j.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
                ho_RegionLines.Dispose();
                ho_Rectangle.Dispose();
                ho_Arrow1.Dispose();
 
                hv_DetectWidth_COPY_INP_TMP.Dispose();
                hv_SelectOut.Dispose();
                hv_TransitionOut.Dispose();
                hv_Width.Dispose();
                hv_Height.Dispose();
                hv_ATan.Dispose();
                hv_i.Dispose();
                hv_RowC.Dispose();
                hv_ColC.Dispose();
                hv_Distance.Dispose();
                hv_RowL2.Dispose();
                hv_RowL1.Dispose();
                hv_ColL2.Dispose();
                hv_ColL1.Dispose();
                hv_MeasureHandle.Dispose();
                hv_RowEdge.Dispose();
                hv_ColEdge.Dispose();
                hv_Amplitude.Dispose();
                hv_tRow.Dispose();
                hv_tCol.Dispose();
                hv_t.Dispose();
                hv_Number.Dispose();
                hv_j.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        /// <summary>
        /// 显示符号
        /// </summary>
        /// <param name="hv_WindowHandle"></param>
        /// <param name="hv_String"></param>
        /// <param name="hv_CoordSystem"></param>
        /// <param name="hv_Row"></param>
        /// <param name="hv_Column"></param>
        /// <param name="hv_Color"></param>
        /// <param name="hv_Box"></param>
        static void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem, HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {
            // Local iconic variables 
 
            // Local control variables 
 
            HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
            HTuple hv_Color_COPY_INP_TMP = new HTuple(hv_Color);
            HTuple hv_Column_COPY_INP_TMP = new HTuple(hv_Column);
            HTuple hv_CoordSystem_COPY_INP_TMP = new HTuple(hv_CoordSystem);
            HTuple hv_Row_COPY_INP_TMP = new HTuple(hv_Row);
 
            try
            {
                if ((int)new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(new HTuple())).TupleOr(
                    new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
                {
 
                    hv_Color_COPY_INP_TMP.Dispose();
                    hv_Column_COPY_INP_TMP.Dispose();
                    hv_CoordSystem_COPY_INP_TMP.Dispose();
                    hv_Row_COPY_INP_TMP.Dispose();
                    hv_GenParamName.Dispose();
                    hv_GenParamValue.Dispose();
 
                    return;
                }
                if ((int)new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1)) != 0)
                {
                    hv_Row_COPY_INP_TMP.Dispose();
                    hv_Row_COPY_INP_TMP = 12;
                }
                if ((int)new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1)) != 0)
                {
                    hv_Column_COPY_INP_TMP.Dispose();
                    hv_Column_COPY_INP_TMP = 12;
                }
                //
                //Convert the parameter Box to generic parameters.
                hv_GenParamName.Dispose();
                hv_GenParamName = new HTuple();
                hv_GenParamValue.Dispose();
                hv_GenParamValue = new HTuple();
                if ((int)new HTuple(new HTuple(hv_Box.TupleLength()).TupleGreater(0)) != 0)
                {
                    if ((int)new HTuple(hv_Box.TupleSelect(0).TupleEqual("false")) != 0)
                    {
                        //Display no box
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
                                    "box");
                                hv_GenParamName.Dispose();
                                hv_GenParamName = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
                                    "false");
                                hv_GenParamValue.Dispose();
                                hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                    }
                    else if ((int)new HTuple(hv_Box.TupleSelect(0).TupleNotEqual(
                        "true")) != 0)
                    {
                        //Set a color other than the default.
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
                                    "box_color");
                                hv_GenParamName.Dispose();
                                hv_GenParamName = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
                                    hv_Box.TupleSelect(0));
                                hv_GenParamValue.Dispose();
                                hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                    }
                }
                if ((int)new HTuple(new HTuple(hv_Box.TupleLength()).TupleGreater(1)) != 0)
                {
                    if ((int)new HTuple(hv_Box.TupleSelect(1).TupleEqual("false")) != 0)
                    {
                        //Display no shadow.
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
                                    "shadow");
                                hv_GenParamName.Dispose();
                                hv_GenParamName = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
                                    "false");
                                hv_GenParamValue.Dispose();
                                hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                    }
                    else if ((int)new HTuple(hv_Box.TupleSelect(1).TupleNotEqual(
                        "true")) != 0)
                    {
                        //Set a shadow color other than the default.
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
                                    "shadow_color");
                                hv_GenParamName.Dispose();
                                hv_GenParamName = ExpTmpLocalVar_GenParamName;
                            }
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            {
                                HTuple
                                  ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
                                    hv_Box.TupleSelect(1));
                                hv_GenParamValue.Dispose();
                                hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
                            }
                        }
                    }
                }
                //Restore default CoordSystem behavior.
                if ((int)new HTuple(hv_CoordSystem_COPY_INP_TMP.TupleNotEqual("window")) != 0)
                {
                    hv_CoordSystem_COPY_INP_TMP.Dispose();
                    hv_CoordSystem_COPY_INP_TMP = "image";
                }
                //
                if ((int)new HTuple(hv_Color_COPY_INP_TMP.TupleEqual("")) != 0)
                {
                    //disp_text does not accept an empty string for Color.
                    hv_Color_COPY_INP_TMP.Dispose();
                    hv_Color_COPY_INP_TMP = new HTuple();
                }
                //
                //HOperatorSet.DispText(hv_WindowHandle, hv_String, hv_CoordSystem_COPY_INP_TMP,
                //    hv_Row_COPY_INP_TMP, hv_Column_COPY_INP_TMP, hv_Color_COPY_INP_TMP, hv_GenParamName,hv_GenParamValue);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_Column_COPY_INP_TMP);
                HOperatorSet.WriteString(hv_WindowHandle, hv_String);
                hv_Color_COPY_INP_TMP.Dispose();
                hv_Column_COPY_INP_TMP.Dispose();
                hv_CoordSystem_COPY_INP_TMP.Dispose();
                hv_Row_COPY_INP_TMP.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
 
                hv_Color_COPY_INP_TMP.Dispose();
                hv_Column_COPY_INP_TMP.Dispose();
                hv_CoordSystem_COPY_INP_TMP.Dispose();
                hv_Row_COPY_INP_TMP.Dispose();
                hv_GenParamName.Dispose();
                hv_GenParamValue.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        /// <summary>
        /// 设置符号大小
        /// </summary>
        /// <param name="hv_WindowHandle"></param>
        /// <param name="hv_Size"></param>
        /// <param name="hv_Font"></param>
        /// <param name="hv_Bold"></param>
        /// <param name="hv_Slant"></param>
        public static void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, string hv_Font = "mono", string hv_Bold = "true", string hv_Slant = "false")
        {
            // Local iconic variables 
 
            // Local control variables 
 
            HTuple hv_OS = new HTuple(), hv_Fonts = new HTuple();
            HTuple hv_Style = new HTuple(), hv_Exception = new HTuple();
            HTuple hv_AvailableFonts = new HTuple(), hv_Fdx = new HTuple();
            HTuple hv_Indices = new HTuple();
            HTuple hv_Font_COPY_INP_TMP = new HTuple(hv_Font);
            HTuple hv_Size_COPY_INP_TMP = new HTuple(hv_Size);
 
            // Initialize local and output iconic variables 
            try
            {
                //This procedure sets the text font of the current window with
                //the specified attributes.
                //
                //Input parameters:
                //WindowHandle: The graphics window for which the font will be set
                //Size: The font size. If Size=-1, the default of 16 is used.
                //Bold: If set to 'true', a bold font is used
                //Slant: If set to 'true', a slanted font is used
                //
                hv_OS.Dispose();
                HOperatorSet.GetSystem("operating_system", out hv_OS);
                if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
                    new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
                {
                    hv_Size_COPY_INP_TMP.Dispose();
                    hv_Size_COPY_INP_TMP = 16;
                }
                if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
                {
                    //Restore previous behaviour
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                              ExpTmpLocalVar_Size = ((1.13677 * hv_Size_COPY_INP_TMP)).TupleInt()
                                ;
                            hv_Size_COPY_INP_TMP.Dispose();
                            hv_Size_COPY_INP_TMP = ExpTmpLocalVar_Size;
                        }
                    }
                }
                else
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                              ExpTmpLocalVar_Size = hv_Size_COPY_INP_TMP.TupleInt()
                                ;
                            hv_Size_COPY_INP_TMP.Dispose();
                            hv_Size_COPY_INP_TMP = ExpTmpLocalVar_Size;
                        }
                    }
                }
                if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))) != 0)
                {
                    hv_Fonts.Dispose();
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "Courier";
                    hv_Fonts[1] = "Courier 10 Pitch";
                    hv_Fonts[2] = "Courier New";
                    hv_Fonts[3] = "CourierNew";
                    hv_Fonts[4] = "Liberation Mono";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
                {
                    hv_Fonts.Dispose();
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "Consolas";
                    hv_Fonts[1] = "Menlo";
                    hv_Fonts[2] = "Courier";
                    hv_Fonts[3] = "Courier 10 Pitch";
                    hv_Fonts[4] = "FreeMono";
                    hv_Fonts[5] = "Liberation Mono";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
                {
                    hv_Fonts.Dispose();
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "Luxi Sans";
                    hv_Fonts[1] = "DejaVu Sans";
                    hv_Fonts[2] = "FreeSans";
                    hv_Fonts[3] = "Arial";
                    hv_Fonts[4] = "Liberation Sans";
                }
                else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
                {
                    hv_Fonts.Dispose();
                    hv_Fonts = new HTuple();
                    hv_Fonts[0] = "Times New Roman";
                    hv_Fonts[1] = "Luxi Serif";
                    hv_Fonts[2] = "DejaVu Serif";
                    hv_Fonts[3] = "FreeSerif";
                    hv_Fonts[4] = "Utopia";
                    hv_Fonts[5] = "Liberation Serif";
                }
                else
                {
                    hv_Fonts.Dispose();
                    hv_Fonts = new HTuple(hv_Font_COPY_INP_TMP);
                }
                hv_Style.Dispose();
                hv_Style = "";
                if ((int)(new HTuple(((HTuple)hv_Bold).TupleEqual("true"))) != 0)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                              ExpTmpLocalVar_Style = hv_Style + "Bold";
                            hv_Style.Dispose();
                            hv_Style = ExpTmpLocalVar_Style;
                        }
                    }
                }
                else if ((int)(new HTuple(((HTuple)hv_Bold).TupleNotEqual("false"))) != 0)
                {
                    hv_Exception.Dispose();
                    hv_Exception = "Wrong value of control parameter Bold";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(((HTuple)hv_Slant).TupleEqual("true"))) != 0)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        {
                            HTuple
                              ExpTmpLocalVar_Style = hv_Style + "Italic";
                            hv_Style.Dispose();
                            hv_Style = ExpTmpLocalVar_Style;
                        }
                    }
                }
                else if ((int)(new HTuple(((HTuple)hv_Slant).TupleNotEqual("false"))) != 0)
                {
                    hv_Exception.Dispose();
                    hv_Exception = "Wrong value of control parameter Slant";
                    throw new HalconException(hv_Exception);
                }
                if ((int)(new HTuple(hv_Style.TupleEqual(""))) != 0)
                {
                    hv_Style.Dispose();
                    hv_Style = "Normal";
                }
                hv_AvailableFonts.Dispose();
                HOperatorSet.QueryFont(hv_WindowHandle, out hv_AvailableFonts);
                hv_Font_COPY_INP_TMP.Dispose();
                hv_Font_COPY_INP_TMP = "";
                for (hv_Fdx = 0; (int)hv_Fdx <= (int)((new HTuple(hv_Fonts.TupleLength())) - 1); hv_Fdx = (int)hv_Fdx + 1)
                {
                    hv_Indices.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_Indices = hv_AvailableFonts.TupleFind(
                            hv_Fonts.TupleSelect(hv_Fdx));
                    }
                    if ((int)(new HTuple((new HTuple(hv_Indices.TupleLength())).TupleGreater(
                        0))) != 0)
                    {
                        if ((int)(new HTuple(((hv_Indices.TupleSelect(0))).TupleGreaterEqual(0))) != 0)
                        {
                            hv_Font_COPY_INP_TMP.Dispose();
                            using (HDevDisposeHelper dh = new HDevDisposeHelper())
                            {
                                hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(
                                    hv_Fdx);
                            }
                            break;
                        }
                    }
                }
                if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(""))) != 0)
                {
                    throw new HalconException("Wrong value of control parameter Font");
                }
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    {
                        HTuple
                          ExpTmpLocalVar_Font = (((hv_Font_COPY_INP_TMP + "-") + hv_Style) + "-") + hv_Size_COPY_INP_TMP;
                        hv_Font_COPY_INP_TMP.Dispose();
                        hv_Font_COPY_INP_TMP = ExpTmpLocalVar_Font;
                    }
                }
                HOperatorSet.SetFont(hv_WindowHandle, hv_Font_COPY_INP_TMP);
 
                hv_Font_COPY_INP_TMP.Dispose();
                hv_Size_COPY_INP_TMP.Dispose();
                hv_OS.Dispose();
                hv_Fonts.Dispose();
                hv_Style.Dispose();
                hv_Exception.Dispose();
                hv_AvailableFonts.Dispose();
                hv_Fdx.Dispose();
                hv_Indices.Dispose();
 
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
 
                hv_Font_COPY_INP_TMP.Dispose();
                hv_Size_COPY_INP_TMP.Dispose();
                hv_OS.Dispose();
                hv_Fonts.Dispose();
                hv_Style.Dispose();
                hv_Exception.Dispose();
                hv_AvailableFonts.Dispose();
                hv_Fdx.Dispose();
                hv_Indices.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        public static void DispImage(HObject image, HWindow window)
        {
            int imgWidth, imgHeight, winRow, winCol, winWidth, winHeight, partWidth, partHeight;
            try
            {
                if (image != null && image.IsInitialized())
                {
                    HOperatorSet.GetImageSize(image, out HTuple hv_imgWidth, out HTuple hv_imgHeight);
                    imgWidth = hv_imgWidth.I;
                    imgHeight = hv_imgHeight.I;
                    window.GetWindowExtents(out winRow, out winCol, out winWidth, out winHeight);
                    if (winWidth < winHeight)
                    {
                        partWidth = imgWidth;
                        partHeight = imgWidth * winHeight / winWidth;
                    }
                    else
                    {
                        partWidth = imgHeight * winWidth / winHeight;
                        partHeight = imgHeight;
                    }
                    window.SetPart(0, 0, partHeight - 1, partWidth - 1);
 
                    HOperatorSet.DispObj(image, window);
                }
                else
                    HOperatorSet.ClearWindow(window);
            }
            catch { }
        }
 
        public static void DispObj(HObject hObject, HWindow window)
        {
            try
            {
                if (hObject != null && hObject.IsInitialized())
                    HOperatorSet.DispObj(hObject, window);
            }
            catch { }
        }
 
        public static void DispMsg(string msg, HWindow window, string color, HTuple row, HTuple column)
        {
            try
            {
                SetColor(window, color);
 
                disp_message(window, msg, "image", row, column, color, "true");
            }
            catch { }
        }
 
        public static void DispMsg(string msg, HWindow window, bool result = true, int x = 0, int y = 0)
        {
            try
            {
                if (result)
                    SetColor(window, "green");
                else
                    SetColor(window, "red");
 
                disp_message(window, msg, "image", y, x, result ? "green" : "red", "true");
            }
            catch { }
        }
 
        public static void ClearObj(HWindow window)
        {
            try
            {
                HOperatorSet.ClearWindow(window);
            }
            catch { }
        }
 
        static void ShowHoImage(HObject ho_image, HWindow hWindow, bool autoSize = true)
        {
            if (ho_image == null || !ho_image.IsInitialized())
            {
                HOperatorSet.ClearWindow(hWindow);
                return;
            }
 
            try
            {
                HOperatorSet.ClearWindow(hWindow);
 
                if (autoSize)
                {
                    HOperatorSet.GetImageSize(ho_image, out HTuple imgWidth, out HTuple imgHeight);
                    if (imgWidth.Length > 0)
                    {
                        hWindow.GetWindowExtents(out int winRow, out int winCol, out int winWidth, out int winHeight);
                        int partWidth = imgHeight * winWidth / winHeight;
                        int partHeight = imgHeight;
                        if (winWidth < winHeight)
                        {
                            partWidth = imgWidth;
                            partHeight = imgWidth * winHeight / winWidth;
                        }
 
                        //计算比例
                        double scale = Math.Max(1.0 * imgWidth.I / winWidth, 1.0 * imgHeight.I / winHeight);
                        double w = winWidth * scale;
                        double h = winHeight * scale;
 
                        double row1 = -(h - imgHeight.D) / 2;
                        double col1 = -(w - imgWidth.D) / 2;
                        double row2 = imgHeight + (h - imgHeight.D) / 2;
                        double col2 = imgWidth + (w - imgWidth.D) / 2;
 
                        //居中等比例
                        hWindow.SetPart(row1, col1, row2, col2);
                    }
                }
 
                HOperatorSet.DispObj(ho_image, hWindow);
            }
            catch { }
        }
 
        public static void scale_gray_map(HObject ho_Image, out HObject ho_Image1, HTuple hv_Min, HTuple hv_Max)
        {
            HTuple hv_Mult = new HTuple(), hv_Add = new HTuple();
            HOperatorSet.GenEmptyObj(out ho_Image1);
            try
            {
                hv_Mult.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_Mult = 255.0 / (hv_Max - hv_Min);
                }
                hv_Add.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_Add = (-hv_Mult) * hv_Min;
                }
                ho_Image1.Dispose();
                HOperatorSet.ScaleImage(ho_Image, out ho_Image1, hv_Mult, hv_Add);
                hv_Mult.Dispose();
                hv_Add.Dispose();
                return;
            }
            catch (HalconException HDevExpDefaultException)
            {
 
                hv_Mult.Dispose();
                hv_Add.Dispose();
 
                throw HDevExpDefaultException;
            }
        }
 
        public static void SetColor(HTuple hv_WindowHandle)
        {
            HOperatorSet.SetColor(hv_WindowHandle, GetRandomColor());
        }
 
        /// <summary>
        /// 设置颜色("dark olive green")
        /// </summary>
        /// <param name="hv_WindowHandle"></param>
        /// <param name="color"></param>
        public static void SetColor(HTuple hv_WindowHandle, string color)
        {
            //*在set_color的算子中,列出Halcon支持的颜色代码,共21种。 
            //*Suggested values: 'black', 'white', 'red', 'green', 
            //*'blue', 'cyan', 'magenta', 'yellow', 'dim gray', 'gray', 
            //*'light gray', 'medium slate blue', 'coral', 'slate blue', 
            //*'spring green', 'orange red', 'orange', 'dark olive green', 
            //*'pink', 'forest green','cadet blue'
            //* 可以用以下的代码来测试颜色
            HOperatorSet.SetColor(hv_WindowHandle, color);
        }
 
        /// <summary>
        /// 生成Halcon随机颜色
        /// </summary>
        /// <returns></returns>
        public static string GetRandomColor()
        {
            // 获取当前时间的毫秒数作为种子
            int seed = DateTime.Now.Millisecond;
 
            // 使用种子创建 Random 实例
            Random random = new Random(seed);
 
            // 生成随机数
            int randomNumber = random.Next(0, 18);
 
            // 延时随机时间变更随机种子
            Thread.Sleep(randomNumber);
 
            string[] strsColors = new string[]
            {
                "red", "green","blue", "cyan", "magenta",
                "yellow", "dim gray", "gray","light gray", "medium slate blue", "coral", "slate blue",
                "spring green", "orange red", "orange", "dark olive green","pink", "forest green", "cadet blue"
            };
            if (randomNumber <= strsColors.Length)
                return strsColors[randomNumber];
            else
                return strsColors[0];
        }
        #endregion
 
        #region OpenCVSharp
        // 根据Mat类型返回对应的无效值
        public static Scalar GetInvalidValueForMat(Mat mat)
        {
            MatType type = mat.Type();
            if (type == MatType.CV_8UC1 || type == MatType.CV_8UC3)
                return new Scalar(0); // 对于8位无符号类型,0是安全的无效值
            else if (type == MatType.CV_32FC1 || type == MatType.CV_32FC3)
                return new Scalar(-1); // 对于浮点类型,-1或NaN可用
            else if (type == MatType.CV_64FC1 || type == MatType.CV_64FC3)
                return new Scalar(double.NaN); // 双精度浮点用NaN
            else
                return new Scalar(0); // 对于8位无符号类型,0是安全的无效值
        }
 
        public static void RGB2XYZ(double sR, double sG, double sB, out double X, out double Y, out double Z)
        {
            // 输入的颜色值 (sR, sG, sB) 应为 0 到 255 之间的值
 
            // 计算归一化值
            double var_R = sR / 255.0;
            double var_G = sG / 255.0;
            double var_B = sB / 255.0;
 
            // 转换到线性空间
            var_R = (var_R > 0.04045) ? Math.Pow((var_R + 0.055) / 1.055, 2.4) : var_R / 12.92;
            var_G = (var_G > 0.04045) ? Math.Pow((var_G + 0.055) / 1.055, 2.4) : var_G / 12.92;
            var_B = (var_B > 0.04045) ? Math.Pow((var_B + 0.055) / 1.055, 2.4) : var_B / 12.92;
 
            // 转换为百分比
            var_R *= 100;
            var_G *= 100;
            var_B *= 100;
 
            // 计算 XYZ 值
            double var_X = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805;
            double var_Y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722;
            double var_Z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505;
 
            // 计算归一化值
 
            X = Math.Round(var_X / (var_X + var_Y + var_Z), 3);
            Y = Math.Round(var_Y / (var_X + var_Y + var_Z), 3);
            Z = Math.Round(var_Z / (var_X + var_Y + var_Z), 3);
        }
 
        public static void ExtractFrames(string videoPath, string outputDir)
        {
            // 检查视频文件是否存在
            if (!File.Exists(videoPath))
            {
                throw new FileNotFoundException($"视频文件不存在: {videoPath}");
            }
 
            // 创建输出目录
            if (!Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }
 
            using (var capture = new VideoCapture(videoPath))
            {
                if (!capture.IsOpened())
                {
                    throw new Exception("无法打开视频文件");
                }
 
                // 获取视频信息
                int totalFrames = (int)capture.Get(VideoCaptureProperties.FrameCount);
                double fps = capture.Get(VideoCaptureProperties.Fps);
                int width = (int)capture.Get(VideoCaptureProperties.FrameWidth);
                int height = (int)capture.Get(VideoCaptureProperties.FrameHeight);
 
                Debug.WriteLine($"视频信息: {totalFrames}帧, {fps:F2}fps, 分辨率: {width}x{height}");
 
                Mat frame = new Mat();
                int frameCount = 0;
 
                while (capture.Read(frame))
                {
                    if (frame.Empty())
                        break;
 
                    // 生成输出文件名
                    string outputPath = Path.Combine(outputDir, $"frame_{frameCount:D6}.bmp");
 
                    // 保存帧为图片
                    Cv2.ImWrite(outputPath, frame);
 
                    frameCount++;
 
                    // 显示进度
                    if (frameCount % 100 == 0)
                    {
                        Debug.WriteLine($"已处理 {frameCount}/{totalFrames} 帧");
                    }
                }
 
                Debug.WriteLine($"提取完成! 共提取 {frameCount} 帧图片");
            }
        }
        #endregion
    }
 
    public class ObjectRecord : IDisposable
    {
        //创建虚拟HWindow用于显示图片[需要释放]
        protected HWindowControl hWindowControl = null;
 
        public bool bCompleted = false;
 
        public HObject Image
        {
            set
            {
                try
                {
                    if (value == null)
                        return;
                    HOperatorSet.GetImageSize(value, out HTuple ho_ImageWidth, out HTuple ho_ImageHeight);
                    hWindowControl = new HWindowControl();
                    hWindowControl.Size = new System.Drawing.Size(ho_ImageWidth, ho_ImageHeight);
                    TAlgorithm.DispImage(value, hWindowControl.HalconWindow);
                }
                catch { }
            }
        }
 
        public HObject RecordObject_OK;
        public HObject RecordObject_NG;
 
        public ObjectRecord(HObject Image = null)
        {
            if (RecordObject_OK != null)
                RecordObject_OK.Dispose();
            HOperatorSet.GenEmptyObj(out RecordObject_OK);
            RecordObject_OK = null;
 
            if (RecordObject_NG != null)
                RecordObject_NG.Dispose();
            HOperatorSet.GenEmptyObj(out RecordObject_NG);
            RecordObject_NG = null;
 
            this.Image = Image;
        }
 
        public void AddRecord(HObject RecordObject, bool result = true)
        {
            if (RecordObject == null || !RecordObject.IsInitialized())
                return;
 
            try
            {
                HOperatorSet.GenContourRegionXld(RecordObject, out RecordObject, "border_holes");
            }
            catch { }
 
            if (result)
            {
                if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                    this.RecordObject_OK = RecordObject;
                else
                    HOperatorSet.ConcatObj(this.RecordObject_OK, RecordObject, out this.RecordObject_OK);
            }
            else
            {
                if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                    this.RecordObject_NG = RecordObject;
                else
                    HOperatorSet.ConcatObj(this.RecordObject_NG, RecordObject, out this.RecordObject_NG);
            }
        }
 
        public void AddXld(HObject RecordObjectXld, bool result = true)
        {
            if (RecordObjectXld == null || !RecordObjectXld.IsInitialized())
                return;
 
            if (result)
            {
                if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                    this.RecordObject_OK = RecordObjectXld;
                else
                    HOperatorSet.ConcatObj(this.RecordObject_OK, RecordObjectXld, out this.RecordObject_OK);
            }
            else
            {
                if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                    this.RecordObject_NG = RecordObjectXld;
                else
                    HOperatorSet.ConcatObj(this.RecordObject_NG, RecordObjectXld, out this.RecordObject_NG);
            }
        }
 
        public void GetRecord(out HObject RecordObject)
        {
            try
            {
                if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                {
                    RecordObject = this.RecordObject_NG;
                    return;
                }
 
                if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                {
                    RecordObject = this.RecordObject_NG;
                    return;
                }
 
                HOperatorSet.ConcatObj(this.RecordObject_OK, this.RecordObject_NG, out RecordObject);
                return;
            }
            catch { RecordObject = null; }
        }
 
        public virtual void ChangeAll2False()
        {
            if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                return;
 
            if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                this.RecordObject_NG = RecordObject_OK.CopyObj(1, -1);
            else
                HOperatorSet.ConcatObj(RecordObject_OK, RecordObject_NG, out RecordObject_NG);
 
            if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                RecordObject_OK.Dispose();
        }
 
        public virtual void ChangeAll2True()
        {
            if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                return;
 
            if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                this.RecordObject_OK = RecordObject_NG.CopyObj(1, -1);
            else
                HOperatorSet.ConcatObj(RecordObject_OK, RecordObject_NG, out RecordObject_OK);
 
            if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                RecordObject_OK.Dispose();
        }
 
        public virtual HObject GetRecordImage()
        {
            bCompleted = false;
            try
            {
                if (hWindowControl == null)
                    return null;
 
 
                if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                {
                    HOperatorSet.SetColor(hWindowControl.HalconWindow, "true");
                    HOperatorSet.DispObj(RecordObject_OK, hWindowControl.HalconWindow);
                }
 
                if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                {
                    HOperatorSet.SetColor(hWindowControl.HalconWindow, "red");
                    HOperatorSet.DispObj(RecordObject_NG, hWindowControl.HalconWindow);
                }
 
                HOperatorSet.DumpWindowImage(out HObject hoRecordImage, hWindowControl.HalconWindow);
                return hoRecordImage;
            }
            catch { return null; }
            finally
            {
                if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                    RecordObject_OK.Dispose();
                HOperatorSet.GenEmptyObj(out RecordObject_OK);
 
                if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                    RecordObject_NG.Dispose();
                HOperatorSet.GenEmptyObj(out RecordObject_NG);
                bCompleted = true;
            }
        }
 
        public virtual void Display(HWindow hWindow)
        {
            try
            {
                if (hWindow != null)
                {
                    if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                    {
                        HOperatorSet.SetColor(hWindow, "red");
                        HOperatorSet.DispObj(RecordObject_NG, hWindow);
                    }
 
                    if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                    {
                        HOperatorSet.SetColor(hWindow, "green");
                        HOperatorSet.DispObj(RecordObject_OK, hWindow);
                    }
                }
            }
            catch { }
        }
 
        public virtual void Dispose()
        {
            try
            {
                if (hWindowControl != null)
                    hWindowControl.Dispose();
 
                if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                {
                    RecordObject_OK.Dispose();
                    RecordObject_OK = null;
                }
                if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                {
                    RecordObject_NG.Dispose();
                    RecordObject_NG = null;
                }
            }
            catch { }
        }
    }
 
    public class MsgRecord : ObjectRecord
    {
        public HTuple Msg = new HTuple();
        public HTuple Row = new HTuple();
        public HTuple Column = new HTuple();
        public HTuple Result = new HTuple();
 
        public MsgRecord(HObject RecordImage = null) : base(RecordImage)  // 这里调用基类构造函数
        {
            Msg = new HTuple();
            Row = new HTuple();
            Column = new HTuple();
            Result = new HTuple();
        }
 
        public void AddRecord(HTuple Msg, HTuple Row, HTuple Column, HObject RecordObject = null, bool Result = true)
        {
 
            if (Msg.Length <= 0 || Row.Length <= 0 || Column.Length <= 0)
                return;
 
            if (RecordObject != null && RecordObject.IsInitialized())
            {
                try
                {
                    HOperatorSet.GenContourRegionXld(RecordObject, out RecordObject, "border_holes");
                }
                catch { }
 
                if (Result)
                {
                    if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                        this.RecordObject_OK = RecordObject;
                    else
                        HOperatorSet.ConcatObj(this.RecordObject_OK, RecordObject, out this.RecordObject_OK);
                }
                else
                {
                    if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                        this.RecordObject_NG = RecordObject;
                    else
                        HOperatorSet.ConcatObj(this.RecordObject_NG, RecordObject, out this.RecordObject_NG);
                }
            }
 
            HOperatorSet.TupleConcat(this.Msg, Msg, out this.Msg);
            HOperatorSet.TupleConcat(this.Row, Row, out this.Row);
            HOperatorSet.TupleConcat(this.Column, Column, out this.Column);
            HOperatorSet.TupleConcat(this.Result, Result ? 1 : 0, out this.Result);
        }
 
        public void GetRecord(out HObject RecordObject, out HTuple Msg, out HTuple Row, out HTuple Column)
        {
            Msg = this.Msg;
            Row = this.Row;
            Column = this.Column;
            try
            {
                if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                {
                    RecordObject = this.RecordObject_NG;
                    return;
                }
 
                if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                {
                    RecordObject = this.RecordObject_NG;
                    return;
                }
 
                HOperatorSet.ConcatObj(this.RecordObject_OK, this.RecordObject_NG, out RecordObject);
                return;
            }
            catch { RecordObject = null; }
        }
 
        public override void ChangeAll2False()
        {
            for (int i = 0; i < Result.Length; i++)
                Result[i] = 0;
 
            if (this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized())
                this.RecordObject_NG = RecordObject_OK.CopyObj(1, -1);
            else if (!(this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized()))
                HOperatorSet.ConcatObj(RecordObject_OK, RecordObject_NG, out RecordObject_NG);
 
            if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                RecordObject_OK.Dispose();
        }
 
        public override void ChangeAll2True()
        {
            for (int i = 0; i < Result.Length; i++)
                Result[i] = 1;
 
            if (this.RecordObject_OK == null || !this.RecordObject_OK.IsInitialized())
                this.RecordObject_OK = RecordObject_NG.CopyObj(1, -1);
            else if (!(this.RecordObject_NG == null || !this.RecordObject_NG.IsInitialized()))
                HOperatorSet.ConcatObj(RecordObject_OK, RecordObject_NG, out RecordObject_OK);
 
            if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                RecordObject_NG.Dispose();
        }
 
        public override HObject GetRecordImage()
        {
            bCompleted = false;
            try
            {
                if (hWindowControl == null)
                    return null;
 
                if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                {
                    HOperatorSet.SetColor(hWindowControl.HalconWindow, "true");
                    HOperatorSet.DispObj(RecordObject_OK, hWindowControl.HalconWindow);
                }
 
                if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                {
                    HOperatorSet.SetColor(hWindowControl.HalconWindow, "red");
                    HOperatorSet.DispObj(RecordObject_NG, hWindowControl.HalconWindow);
                }
 
                for (int i = 0; i < Msg.Length; i++)
                {
                    if (1 == Result[i])
                        HOperatorSet.SetColor(hWindowControl.HalconWindow, "green");
                    else
                        HOperatorSet.SetColor(hWindowControl.HalconWindow, "red");
 
                    TAlgorithm.DispMsg(Msg[i], hWindowControl.HalconWindow, 1 == Result[i] ? "green" : "red", Row[i], Column[i]);
                }
 
                HOperatorSet.DumpWindowImage(out HObject hoRecordImage, hWindowControl.HalconWindow);
                bCompleted = true;
 
                return hoRecordImage;
            }
            catch { return null; }
            finally
            {
                if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                    RecordObject_OK.Dispose();
                HOperatorSet.GenEmptyObj(out RecordObject_OK);
 
                if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                    RecordObject_NG.Dispose();
                HOperatorSet.GenEmptyObj(out RecordObject_NG);
 
                Msg = new HTuple();
                Row = new HTuple();
                Column = new HTuple();
                Result = new HTuple();
                bCompleted = true;
            }
        }
 
        public override void Display(HWindow hWindow)
        {
            try
            {
                base.Display(hWindow);
 
                for (int i = 0; i < Msg.Length; i++)
                {
                    if (hWindow != null)
                    {
                        if (1 == Result[i])
                            HOperatorSet.SetColor(hWindow, "green");
                        else
                            HOperatorSet.SetColor(hWindow, "red");
 
                        TAlgorithm.DispMsg(Msg[i], hWindow, 1 == Result[i] ? "green" : "red", Row[i], Column[i]);
                    }
                }
            }
            catch { }
        }
 
        public override void Dispose()
        {
            try
            {
                if (hWindowControl != null)
                    hWindowControl.Dispose();
 
                if (RecordObject_OK != null && RecordObject_OK.IsInitialized())
                {
                    RecordObject_OK.Dispose();
                    RecordObject_OK = null;
                }
                if (RecordObject_NG != null && RecordObject_NG.IsInitialized())
                {
                    RecordObject_NG.Dispose();
                    RecordObject_NG = null;
                }
 
                Msg = new HTuple();
                Row = new HTuple();
                Column = new HTuple();
                Result = new HTuple();
            }
            catch { }
        }
    }
}