File: layer.py

package info (click to toggle)
pythoncad 0.1.35-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,536 kB
  • ctags: 4,286
  • sloc: python: 62,752; sh: 743; makefile: 39
file content (3534 lines) | stat: -rw-r--r-- 136,798 bytes parent folder | download | duplicates (2)
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
#
# Copyright (c) 2002, 2003, 2004, 2005, 2006 Art Haas
#
# This file is part of PythonCAD.
#
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# The Layer class
#

import sys
import types
from math import pi, atan2

from PythonCAD.Generic import color
from PythonCAD.Generic import linetype
from PythonCAD.Generic import style
from PythonCAD.Generic import point
from PythonCAD.Generic import segment
from PythonCAD.Generic import circle
from PythonCAD.Generic import arc
from PythonCAD.Generic import hcline
from PythonCAD.Generic import vcline
from PythonCAD.Generic import acline
from PythonCAD.Generic import cline
from PythonCAD.Generic import ccircle
from PythonCAD.Generic import segjoint
from PythonCAD.Generic import leader
from PythonCAD.Generic import polyline
from PythonCAD.Generic import text
from PythonCAD.Generic import dimension
from PythonCAD.Generic import dimtrees
from PythonCAD.Generic import tolerance
from PythonCAD.Generic import entity
from PythonCAD.Generic import logger
from PythonCAD.Generic import graphicobject
from PythonCAD.Generic import units
from PythonCAD.Generic import util

class Layer(entity.Entity):
    """The Layer class.

A Layer object holds all the various entities that can be
in a drawing. Each layer can have sublayers, and there is
no limit to the depth of the sublayering.

A Layer object has several attributes:

name: The Layer's name
parent: The parent Layer of the Layer
scale: The scale factor for object contained in the Layer

A Layer object has the following methods:

{get/set}Name(): Get/Set the Layer's name.
{get/set}ParentLayer(): Get/Set the Layer's parent.
{add/del}Sublayer(): Add/Remove a sublayer to this Layer.
hasSublayers(): Test if this Layer has sublayers.
getSublayers(): Return any sublayers of this Layer.
{add/del}Object(): Store/Remove a Point, Segment, etc. in the Layer.
{get/set}Autosplit(): Get/Set the autosplitting state of the Layer.
findObject(): Return an object in the layer equivalent to a test object.
find(): Search for an object within the Layer.
getObject(): Return an object with a specified ID
mapPoint(): See if a non-Point object in the layer crosses some location.
hasEntities(): Test if the Layer contains any entities
hasEntityType(): Test if the Layer contains a particular entity type.
getLayerEntities(): Return all the instances of an entity within the Layer.
getBoundary(): Find the maximum and minimum coordinates of the Layer.
objsInRegion(): Return all the objects in the Layer that can be seen
                within some view.
{get/set}DeletedEntityData(): Get/Set the deleted entity values in the Layer
    """

    __messages = {
        'name_changed' : True,
        'scale_changed' : True,
        'added_sublayer' : True,
        'deleted_sublayer' : True,
        }
    
    def __init__(self, name=None, **kw):
        """Initializee a Layer.

Layer([name)

Argument name is optional. The name should be a unicode string
if specified, otherwise a default name of 'Layer' is given.
        """
        _n = name
        if _n is None:
            _n = u'Layer'
        if not isinstance(_n, types.StringTypes):
            raise TypeError, "Invalid layer name type: " + `type(name)`
        if isinstance(name, str):
            _n = unicode(name)
        super(Layer, self).__init__(**kw)
        self.__name = _n
        self.__points = point.PointQuadtree()
        self.__segments = segment.SegmentQuadtree()
        self.__circles = circle.CircleQuadtree()
        self.__arcs = arc.ArcQuadtree()
        self.__hclines = hcline.HCLineQuadtree()
        self.__vclines = vcline.VCLineQuadtree()
        self.__aclines = acline.ACLineQuadtree()
        self.__clines = cline.CLineQuadtree()
        self.__ccircles = ccircle.CCircleQuadtree()
        self.__chamfers = [] # should be Quadtree
        self.__fillets = [] # should be Quadtree
        self.__leaders = leader.LeaderQuadtree()
        self.__polylines = polyline.PolylineQuadtree()
        self.__textblocks = [] # should be Quadtree
        self.__ldims = dimtrees.LDimQuadtree()
        self.__hdims = dimtrees.HDimQuadtree()
        self.__vdims = dimtrees.VDimQuadtree()
        self.__rdims = dimtrees.RDimQuadtree()
        self.__adims = dimtrees.ADimQuadtree()
        self.__scale = 1.0
        self.__parent_layer = None
        self.__sublayers = None
        self.__asplit = True
        #
        # self.__objects keeps a reference to all objects stored in the layer
        #
        self.__objects = {}
        self.__objids = {}
        self.__logs = {}

    def __str__(self):
        _p = self.__parent_layer
        if _p is None:
            _s = "Layer: %s [No Parent Layer]" % self.__name
        else:
            _s = "Layer: %s; Parent Layer: %s" % (self.__name, _p.getName())
        return _s

    def __contains__(self, obj):
        """Find an object in the Layer.

This method permits the use of 'in' for test conditions.

if obj in layer:
    ....

This function tests for Point, Segment, Circle, etc. It returns
True if there is an equivalent object held in the Layer. Otherwise
the function returns False.
        """
        _seen = False
        if id(obj) in self.__objects:
            _seen = True
        if not _seen:
            if isinstance(obj, point.Point):
                _x, _y = obj.getCoords()
                _seen = (len((self.__points.find(_x, _y))) > 0)
            elif isinstance(obj, segment.Segment):
                _p1, _p2 = obj.getEndpoints()
                _x1, _y1 = _p1.getCoords()
                _x2, _y2 = _p2.getCoords()
                _seen = (len(self.__segments.find(_x1, _y1, _x2, _y2)) > 0)
            elif isinstance(obj, arc.Arc):
                _x, _y = obj.getCenter().getCoords()
                _r = obj.getRadius()
                _sa = obj.getStartAngle()
                _ea = obj.getEndAngle()
                _seen = (len(self.__arcs.find(_x, _y, _r, _sa, _ea)) > 0)
            elif isinstance(obj, circle.Circle):
                _x, _y = obj.getCenter().getCoords()
                _r = obj.getRadius()
                _seen = (len(self.__circles.find(_x, _y, _r)) > 0)
            elif isinstance(obj, hcline.HCLine):
                _y = obj.getLocation().y
                _seen = (len(self.__hclines.find(_y)) > 0)
            elif isinstance(obj, vcline.VCLine):
                _x = obj.getLocation().x
                _seen = (len(self.__vclines.find(_x)) > 0)
            elif isinstance(obj, acline.ACLine):
                _x, _y = obj.getLocation().getCoords()
                _angle = obj.getAngle()
                _seen = (len(self.__aclines.find(_x, _y, _angle)) > 0)
            elif isinstance(obj, cline.CLine):
                _p1, _p2 = obj.getKeypoints()
                _x1, _y1 = _p1.getCoords()
                _x2, _y2 = _p2.getCoords()
                _seen = (len(self.__clines.find(_x1, _y1, _x2, _y2)) > 0)
            elif isinstance(obj, ccircle.CCircle):
                _x, _y = obj.getCenter().getCoords()
                _r = obj.getRadius()
                _seen = (len(self.__ccircles.find(_x, _y, _r)) > 0)
            elif isinstance(obj, segjoint.Fillet):
                _seen = obj in self.__fillets
            elif isinstance(obj, segjoint.Chamfer):
                _seen = obj in self.__chamfers
            elif isinstance(obj, leader.Leader):
                _p1, _p2, _p3 = obj.getPoints()
                _x1, _y1 = _p1.getCoords()
                _x2, _y2 = _p2.getCoords()
                _x3, _y3 = _p3.getCoords()
                _seen = (len(self.__leaders.find(_x1, _y1, _x2, _y2,
                                                 _x3, _y3)) > 0)
            elif isinstance(obj, polyline.Polyline):
                _coords = []
                for _pt in obj.getPoints():
                    _coords.extend(_pt.getCoords())
                _seen = (len(self.__polylines.find(_coords)) > 0)
            elif isinstance(obj, text.TextBlock):
                _seen = obj in self.__textblocks
            elif isinstance(obj, dimension.HorizontalDimension):
                _p1, _p2 = obj.getDimPoints()
                _seen = (len(self.__hdims.find(_p1, _p2)) > 0)
            elif isinstance(obj, dimension.VerticalDimension):
                _p1, _p2 = obj.getDimPoints()
                _seen = (len(self.__vdims.find(_p1, _p2)) > 0)
            elif isinstance(obj, dimension.LinearDimension):
                _p1, _p2 = obj.getDimPoints()
                _seen = (len(self.__ldims.find(_p1, _p2)) > 0)
            elif isinstance(obj, dimension.RadialDimension):
                _c1 = obj.getDimCircle()
                _seen = (len(self.__rdims.find(_c1)) > 0)
            elif isinstance(obj, dimension.AngularDimension):
                _vp, _p1, _p2 = obj.getDimPoints()
                _dims = self.__adims.find(_vp, _p1, _p2)
                _seen = len(_dims) > 0
            else:
                raise TypeError, "Invalid type for in operation: " + `type(obj)`
        return _seen

    def finish(self):
        self.__name = None
        self.__points = None
        self.__segments = None
        self.__circles = None
        self.__arcs = None
        self.__hclines = None
        self.__vclines = None
        self.__aclines = None
        self.__clines = None
        self.__ccircles = None
        self.__chamfers = None
        self.__fillets = None
        self.__leaders = None
        self.__polylines = None
        self.__textblocks = None
        self.__ldims = None
        self.__hdims = None
        self.__vdims = None
        self.__rdims = None
        self.__adims = None
        super(Layer, self).finish()
        
    def clear(self):
        """Remove all the entities stored in this layer

clear()
        """
        if self.isLocked():
            raise RuntimeError, "Clearing layer not allowed - layer locked."
        for _obj in self.__adims.getObjects():
            self.delObject(_obj)
        for _obj in self.__rdims.getObjects():
            self.delObject(_obj)
        for _obj in self.__vdims.getObjects():
            self.delObject(_obj)
        for _obj in self.__hdims.getObjects():
            self.delObject(_obj)
        for _obj in self.__ldims.getObjects():
            self.delObject(_obj)
        for _obj in self.__textblocks:
            self.delObject(_obj)
        for _obj in self.__polylines.getObjects():
            self.delObject(_obj)
        for _obj in self.__leaders.getObjects():
            self.delObject(_obj)
        for _obj in self.__chamfers:
            self.delObject(_obj)
        for _obj in self.__fillets:
            self.delObject(_obj)
        for _obj in self.__ccircles.getObjects():
            self.delObject(_obj)
        for _obj in self.__clines.getObjects():
            self.delObject(_obj)
        for _obj in self.__aclines.getObjects():
            self.delObject(_obj)
        for _obj in self.__vclines.getObjects():
            self.delObject(_obj)
        for _obj in self.__hclines.getObjects():
            self.delObject(_obj)
        for _obj in self.__arcs.getObjects():
            self.delObject(_obj)
        for _obj in self.__circles.getObjects():
            self.delObject(_obj)
        for _obj in self.__segments.getObjects():
            self.delObject(_obj)
        for _obj in self.__points.getObjects():
            self.delObject(_obj)
        self.setScale(1.0)

    def getName(self):
        """Return the name of the Layer.

getName()
        """
        return self.__name

    def setName(self, name):
        """Set the name of the Layer.

setName(name)

The new must be a string, and cannot be None.
        """
        _n = name
        if _n is None:
            raise ValueError, "Layers must have a name."
        if not isinstance(_n, types.StringTypes):
            raise TypeError, "Invalid name type: " + `type(_n)`
        if isinstance(_n, str):
            _n = unicode(_n)
        _on = self.__name
        if _on != _n:
            self.startChange('name_changed')
            self.__name = _n
            self.endChange('name_changed')
            self.sendMessage('name_changed', _on)
            self.modified()

    name = property(getName, setName, None, "Layer name.")

    def getValues(self):
        """Return values comprising the Layer.

getValues()

This method extends the Entity::getValues() method.
        """
        _data = super(Layer, self).getValues()
        _data.setValue('type', 'layer')        
        _pid = None
        if self.__parent_layer is not None:
            _pid = self.__parent_layer.getID()
        _data.setValue('parent_layer', _pid)
        _data.setValue('name', self.__name)
        _data.setValue('scale', self.__scale)
        return _data

    def setDeletedEntityData(self, data):
        """Fill in the deleted entity data.

setDeletedEntityData(data)

Argument 'data' must be a dictionary with the keys being
entity id values (integers) and the dictionary values as Logger
instances.
        """
        if not isinstance(data, dict):
            raise TypeError, "Invalid dictionary type: " + `type(data)`
        if len(self.__logs) != 0:
            raise ValueError, "Deleted data already stored"
        for _key in data:
            if not isinstance(_key, int):
                raise TypeError, "Invalid entity id type: " + `type(_key)`
            _val = data[_key]
            if not isinstance(_val, logger.Logger):
                raise TypeError, "Invalid entity log type: " + `type(_val)`
            self.__logs[_key] = _val
        
    def getDeletedEntityData(self):
        """Return the stored log data for deleted entities.

getDeletedEntityData()

This method returns a dictionary.
        """
        return self.__logs.copy()

    def __splitObject(self, obj, pt):
        """Split a Segment/Circle/Arc/Polyline on a Point in the Layer.

splitObject(obj, pt)

Argument 'obj' must be a Segment, Circle, Arc, or Polyline, and argument
'pt' must be Point. Both arguments must be in stored in the Layer, and
the point must lie on the entity to be split.

This method is private to the Layer.
        """
        if self.isLocked():
            raise RuntimeError, "Splitting entity not allowed - layer locked."
        if not isinstance(obj, (segment.Segment, circle.Circle,
                                arc.Arc, polyline.Polyline)):
            raise TypeError, "Invalid object: " + `type(obj)`
        if obj.getParent() is not self:
            raise ValueError, "Object not in layer: " + `obj`
        if not isinstance(pt, point.Point):
            raise TypeError, "Invalid point: " + `type(pt)`
        if pt.getParent() is not self:
            raise ValueError, "Point not in layer: " + `pt`
        _x, _y = pt.getCoords()
        _mp = obj.mapCoords(_x, _y)
        if _mp is None:
            raise RuntimeError, "Point not on object: " + `pt`
        _split = False
        _objs = []
        if isinstance(obj, segment.Segment):
            _p1, _p2 = obj.getEndpoints()
            if _p1 != pt and _p2 != pt:
                _split = True
                _s = obj.getStyle()
                _l = obj.getLinetype()
                _c = obj.getColor()
                _t = obj.getThickness()
                _seg = segment.Segment(_p1, pt, _s, _l, _c, _t)
                self.addObject(_seg)
                _objs.append(_seg)
                _seg = segment.Segment(pt, _p2, _s, _l, _c, _t)
                self.addObject(_seg)
                _objs.append(_seg)
        elif isinstance(obj, (circle.Circle, arc.Arc)):
            _cp = obj.getCenter()
            _r = obj.getRadius()
            _s = obj.getStyle()
            _l = obj.getLinetype()
            _c = obj.getColor()
            _t = obj.getThickness()
            _angle = (180.0/pi) * atan2((_y - _cp.y),(_x - _cp.x))
            if _angle < 0.0:
                _angle = _angle + 360.0
            if isinstance(obj, circle.Circle):
                _split = True
                _arc = arc.Arc(_cp, _r, _angle, _angle, _s, _l, _c, _t)
                self.addObject(_arc)
                _objs.append(_arc)
            else:
                _ep1, _ep2 = obj.getEndpoints()
                if pt != _ep1 and pt != _ep2:
                    _split = True
                    _sa = obj.getStartAngle()
                    _ea = obj.getEndAngle()
                    _arc = arc.Arc(_cp, _r, _sa, _angle, _s, _l, _c, _t)
                    self.addObject(_arc)
                    _objs.append(_arc)
                    _arc = arc.Arc(_cp, _r, _angle, _ea, _s, _l, _c, _t)
                    self.addObject(_arc)
                    _objs.append(_arc)
        elif isinstance(obj, polyline.Polyline):
            _pts = obj.getPoints()
            for _i in range(len(_pts) - 1):
                _p1x, _p1y = _pts[_i].getCoords()
                _p2x, _p2y = _pts[_i + 1].getCoords()
                _p = util.map_coords(_x, _y, _p1x, _p1y, _p2x, _p2y)
                if _p is None:
                    continue
                _px, _py = _p
                if ((abs(_px - _p1x) < 1e-10 and abs(_py - _p1y) < 1e-10) or
                    (abs(_px - _p2x) < 1e-10 and abs(_py - _p2y) < 1e-10)):
                    continue
                _split = True
                obj.addPoint((_i + 1), pt)
                break
        else:
            raise TypeError, "Unexpected type: " + `type(obj)`
        return (_split, _objs)

    def setAutosplit(self, as):
        """Set the autosplit state of the Layer.
        
setAutosplit(as)

Argument 'as' must be a Boolean.
        """
        util.test_boolean(as)
        self.__asplit = as

    def getAutosplit(self):
        """Retrieve the autosplit state of the Layer.

getAutosplit()

This method returns a Boolean.
        """
        return self.__asplit

    def addObject(self, obj):
        """Add an object to this Layer.

addObject(obj)

The object should be a Point, Segment, Arc, Circle,
HCLine, VCLine, ACLine, CLine, CCircle, TextBlock, Chamfer,
Fillet, Leader, Polyline, or Dimension. Anything else raises
a TypeError exception.
        """
        if self.isLocked():
            raise RuntimeError, "Adding entity not allowed - layer locked."
        if id(obj) in self.__objects:
            return
        if isinstance(obj, point.Point):
            _res = self.__addPoint(obj)
        elif isinstance(obj, segment.Segment):
            _res = self.__addSegment(obj)
        elif isinstance(obj, arc.Arc):
            _res = self.__addArc(obj)
        elif isinstance(obj, circle.Circle):
            _res = self.__addCircle(obj)
        elif isinstance(obj, hcline.HCLine):
            _res = self.__addHCLine(obj)
        elif isinstance(obj, vcline.VCLine):
            _res = self.__addVCLine(obj)
        elif isinstance(obj, acline.ACLine):
            _res = self.__addACLine(obj)
        elif isinstance(obj, ccircle.CCircle):
            _res = self.__addCCircle(obj)
        elif isinstance(obj, cline.CLine):
            _res = self.__addCLine(obj)
        elif isinstance(obj, segjoint.Chamfer):
            _res = self.__addChamfer(obj)
        elif isinstance(obj, segjoint.Fillet):
            _res = self.__addFillet(obj)
        elif isinstance(obj, leader.Leader):
            _res = self.__addLeader(obj)
        elif isinstance(obj, polyline.Polyline):
            _res = self.__addPolyline(obj)
        elif isinstance(obj, text.TextBlock):
            _res = self.__addTextBlock(obj)
        elif isinstance(obj, dimension.AngularDimension):
            _res = self.__addAngularDimension(obj)
        elif isinstance(obj, dimension.RadialDimension):
            _res = self.__addRadialDimension(obj)
        elif isinstance(obj, dimension.HorizontalDimension):
            _res = self.__addHorizontalDimension(obj)
        elif isinstance(obj, dimension.VerticalDimension):
            _res = self.__addVerticalDimension(obj)
        elif isinstance(obj, dimension.LinearDimension):
            _res = self.__addLinearDimension(obj)
        else:
            raise TypeError, "Invalid object type for storage: " + `type(obj)`
        if _res:
            #
            # call setParent() before connecting to layer log (if
            # it exists) so that the log will not recieve a 'modified'
            # message ...
            #
            self.__objects[id(obj)] = obj
            _oid = obj.getID()
            self.__objids[_oid] = obj
            obj.setParent(self)
            _log = obj.getLog()
            if _log is not None: # make this an error?
                _oldlog = self.__logs.get(_oid)
                if _oldlog is not None: # re-attach old log
                    _log.transferData(_oldlog)
                    del self.__logs[_oid]
            if isinstance(obj, dimension.Dimension):
                _ds1, _ds2 = obj.getDimstrings()
                _oid = _ds1.getID()
                _log = _ds1.getLog()
                if _log is not None:
                    _oldlog = self.__logs.get(_oid)
                    if _oldlog is not None:
                        _log.transferData(_oldlog)
                        del self.__logs[_oid]
                _oid = _ds2.getID()
                _log = _ds2.getLog()
                if _log is not None:
                    _oldlog = self.__logs.get(_oid)
                    if _oldlog is not None:
                        _log.transferData(_oldlog)
                        del self.__logs[_oid]
            #
            # Automatically split a segment, circle, arc, or
            # polyline if a point was added and the autosplit
            # flag is True
            #
            if (isinstance(obj, point.Point) and
                self.__asplit is True and
                not self.inUndo() and not self.inRedo()):
                _x, _y = obj.getCoords()
                _types = {'segment' : True,
                          'circle' : True,
                          'arc' : True,
                          'polyline' : True}
                _hits = self.mapCoords(_x, _y, types=_types)
                if len(_hits) > 0:
                    for _mobj, _mpt in _hits:
                        _split, _sobjs = self.__splitObject(_mobj, obj)
                        if _split and len(_sobjs):
                            self.delObject(_mobj)
        #
        # Reset the autosplit flag to True
        #
        self.__asplit = True

    def __addPoint(self, p):
        """Add a Point object to the Layer.

_addPoint(p)

This method is private to the Layer object.
        """
        self.__points.addObject(p)
        if p.getLog() is None:
            _log = point.PointLog(p)
            p.setLog(_log)
        return True

    def __addSegment(self, s):
        """Add a Segment object to the Layer.

_addSegment(s)

This method is private to the Layer object.
        """
        _p1, _p2 = s.getEndpoints()
        if id(_p1) not in self.__objects:
            raise ValueError, "Segment p1 Point not found in Layer."
        if id(_p2) not in self.__objects:
            raise ValueError, "Segment p2 Point not found in Layer."
        self.__segments.addObject(s)
        if s.getLog() is None:
            _log = segment.SegmentLog(s)
            s.setLog(_log)
        return True

    def __addCircle(self, c):
        """Add a Circle object to the Layer.

_addCircle(c)

This method is private to the layer object.
        """
        _cp = c.getCenter()
        if id(_cp) not in self.__objects:
            raise ValueError, "Circle center Point not found in Layer."
        self.__circles.addObject(c)
        if c.getLog() is None:
            _log = circle.CircleLog(c)
            c.setLog(_log)
        return True

    def __addArc(self, a):
        """Add an Arc object to the Layer.

_addArc(a)

This method is private to the Layer object.
        """
        _cp = a.getCenter()
        if id(_cp) not in self.__objects:
            raise ValueError, "Arc center Point not found in Layer."
        self.__arcs.addObject(a)
        if a.getLog() is None:
            _log = arc.ArcLog(a)
            a.setLog(_log)
        for _ex, _ey in a.getEndpoints():
            _pts = self.__points.find(_ex, _ey)
            if len(_pts) == 0:
                _lp = point.Point(_ex, _ey)
                self.addObject(_lp)
            else:
                _lp = _pts.pop()
                _max = _lp.countUsers()
                for _pt in _pts:
                    _count = _pt.countUsers()
                    if _count > _max:
                        _max = _count
                        _lp = _pt
            _lp.storeUser(a)
            if abs(a.getStartAngle() - a.getEndAngle()) < 1e-10:
                break
        return True

    def __addHCLine(self, hcl):
        """Add an HCLine object to the Layer.

_addHCLine(hcl)

This method is private to the Layer object.
        """
        _lp = hcl.getLocation()
        if id(_lp) not in self.__objects:
            raise ValueError, "HCLine location Point not found in Layer."
        self.__hclines.addObject(hcl)
        if hcl.getLog() is None:
            _log = hcline.HCLineLog(hcl)
            hcl.setLog(_log)
        return True

    def __addVCLine(self, vcl):
        """Add an VCLine object to the Layer.

_addVCLine(vcl)

This method is private to the Layer object.
        """
        _lp = vcl.getLocation()
        if id(_lp) not in self.__objects:
            raise ValueError, "VCLine location Point not found in Layer."
        self.__vclines.addObject(vcl)
        if vcl.getLog() is None:
            _log = vcline.VCLineLog(vcl)
            vcl.setLog(_log)
        return True

    def __addACLine(self, acl):
        """Add an ACLine object to the Layer.

_addACLine(acl)

This method is private to the Layer object.
        """
        _lp = acl.getLocation()
        if id(_lp) not in self.__objects:
            raise ValueError, "ACLine location Point not found in Layer."
        self.__aclines.addObject(acl)
        if acl.getLog() is None:
            _log = acline.ACLineLog(acl)
            acl.setLog(_log)
        return True

    def __addCCircle(self, cc):
        """Add an CCircle object to the Layer.

_addCCircle(cc)

This method is private to the Layer object.
        """
        _cp = cc.getCenter()
        if id(_cp) not in self.__objects:
            raise ValueError, "CCircle center Point not found in Layer."
        self.__ccircles.addObject(cc)
        if cc.getLog() is None:
            _log = ccircle.CCircleLog(cc)
            cc.setLog(_log)
        return True

    def __addCLine(self, cl):
        """Add an CLine object to the Layer.

_addCLine(cl)

This method is private to the Layer object.
        """
        _p1, _p2 = cl.getKeypoints()
        if id(_p1) not in self.__objects:
            raise ValueError, "CLine p1 Point not found in Layer."
        if id(_p2) not in self.__objects:
            raise ValueError, "CLine p2 Point not found in Layer."
        self.__clines.addObject(cl)
        if cl.getLog() is None:
            _log = cline.CLineLog(cl)
            cl.setLog(_log)
        return True

    def __addChamfer(self, ch):
        """Add a Chamfer object to the Layer.

_addChamfer(ch)

This method is private to the Layer object.
        """
        _s1, _s2 = ch.getSegments()
        if id(_s1) not in self.__objects:
            raise ValueError, "Chamfer s1 Segment not found in Layer."
        if id(_s2) not in self.__objects:
            raise ValueError, "Chamfer s2 Segment not found in Layer."
        self.__chamfers.append(ch)
        if ch.getLog() is None:
            _log = segjoint.ChamferLog(ch)
            ch.setLog(_log)
        return True

    def __addFillet(self, f):
        """Add a Fillet object to the Layer.

_addFillet(f)

This method is private to the Layer object.
        """
        _s1, _s2 = f.getSegments()
        if id(_s1) not in self.__objects:
            raise ValueError, "Fillet s1 Segment not found in Layer."
        if id(_s2) not in self.__objects:
            raise ValueError, "Fillet s2 Segment not found in Layer."
        self.__fillets.append(f)
        if f.getLog() is None:
            _log = segjoint.FilletLog(f)
            f.setLog(_log)
        return True

    def __addLeader(self, l):
        """Add a Leader object to the Layer.

_addLeader(l)

This method is private to the Layer object.
        """
        _p1, _p2, _p3 = l.getPoints()
        if id(_p1) not in self.__objects:
            raise ValueError, "Leader p1 Point not found in Layer."
        if id(_p2) not in self.__objects:
            raise ValueError, "Leader p2 Point not found in Layer."
        if id(_p3) not in self.__objects:
            raise ValueError, "Leader p3 Point not found in Layer."
        self.__leaders.addObject(l)
        if l.getLog() is None:
            _log = leader.LeaderLog(l)
            l.setLog(_log)
        return True

    def __addPolyline(self, pl):
        """Add a Polyline object to the Layer.

_addPolyline(pl)

This method is private to the Layer object.
        """
        for _pt in pl.getPoints():
            if id(_pt) not in self.__objects:
                raise ValueError, "Polyline point not in layer: " + str(_pt)
        self.__polylines.addObject(pl)
        if pl.getLog() is None:
            _log = polyline.PolylineLog(pl)
            pl.setLog(_log)
        return True

    def __addTextBlock(self, tb):
        """Add a TextBlock object to the Layer.

_addTextBlock(tb)

The TextBlock object 'tb' is added to the layer if there is not
already a TextBlock in the layer at the same location and with the
same text.
        """
        self.__textblocks.append(tb)
        if tb.getLog() is None:
            _log = text.TextBlockLog(tb)
            tb.setLog(_log)
        return True

    def __addAngularDimension(self, adim):
        """Add an AngularDimension object to the Layer.

_addAngularDimension(adim)

This method is private to the Layer object.
        """
        _p1, _p2, _p3 = adim.getDimPoints()
        if _p1.getParent() is None:
            raise ValueError, "Dimension Point P1 not found in a Layer"
        if _p2.getParent() is None:
            raise ValueError, "Dimension Point P2 not found in a Layer!"
        if _p3.getParent() is None:
            raise ValueError, "Dimension Point P3 not found in a Layer!"
        self.__adims.addObject(adim)
        if adim.getLog() is None:
            _log = dimension.DimLog(adim)
            adim.setLog(_log)
            _ds1, _ds2 = adim.getDimstrings()
            _log = dimension.DimStringLog(_ds1)
            _ds1.setLog(_log)
            _log = dimension.DimStringLog(_ds2)
            _ds2.setLog(_log)
        return True

    def __addRadialDimension(self, rdim):
        """Add a RadialDimension object to the Layer.

_addRadialDimension(rdim)

This method is private to the Layer object.
        """
        _dc = rdim.getDimCircle()
        if _dc.getParent() is None:
            raise ValueError, "RadialDimension circular object not found in a Layer"
        self.__rdims.addObject(rdim)
        if rdim.getLog() is None:        
            _log = dimension.DimLog(rdim)
            rdim.setLog(_log)
            _ds1, _ds2 = rdim.getDimstrings()
            _log = dimension.DimStringLog(_ds1)
            _ds1.setLog(_log)
            _log = dimension.DimStringLog(_ds2)
            _ds2.setLog(_log)
        return True

    def __addHorizontalDimension(self, hdim):
        """Add a HorizontalDimension object to the Layer.

_addHorizontalDimension(hdim)

This method is private to the Layer object.
        """
        _p1, _p2 = hdim.getDimPoints()
        if _p1.getParent() is None:
            raise ValueError, "HorizontalDimension Point P1 not found in layer"
        if _p1.getParent() is None:
            raise ValueError, "HorizontalDimension Point P2 not found in layer"
        self.__hdims.addObject(hdim)
        if hdim.getLog() is None:
            _log = dimension.DimLog(hdim)
            hdim.setLog(_log)
            _ds1, _ds2 = hdim.getDimstrings()
            _log = dimension.DimStringLog(_ds1)
            _ds1.setLog(_log)
            _log = dimension.DimStringLog(_ds2)
            _ds2.setLog(_log)
        return True

    def __addVerticalDimension(self, vdim):
        """Add a VerticalDimension object to the Layer.

_addVerticalDimension(vdim)

This method is private to the Layer object.
        """
        _p1, _p2 = vdim.getDimPoints()
        if _p1.getParent() is None:
            raise ValueError, "VerticalDimension Point P1 not found in layer"
        if _p2.getParent() is None:
            raise ValueError, "VerticalDimension Point P2 not found in layer"
        self.__vdims.addObject(vdim)
        if vdim.getLog() is None:
            _log = dimension.DimLog(vdim)
            vdim.setLog(_log)
            _ds1, _ds2 = vdim.getDimstrings()
            _log = dimension.DimStringLog(_ds1)
            _ds1.setLog(_log)
            _log = dimension.DimStringLog(_ds2)
            _ds2.setLog(_log)
        return True

    def __addLinearDimension(self, ldim):
        """Add a LinearDimension object to the Layer.

_addLinearDimension(ldim)

This method is private to the Layer object.
        """
        _p1, _p2 = ldim.getDimPoints()
        if _p1.getParent() is None:
            raise ValueError, "LinearDimension Point P1 not found in layer"
        if _p2.getParent() is None:
            raise ValueError, "LinearDimension Point P2 not found in layer"
        self.__ldims.addObject(ldim)
        if ldim.getLog() is None:
            _log = dimension.DimLog(ldim)
            ldim.setLog(_log)
            _ds1, _ds2 = ldim.getDimstrings()
            _log = dimension.DimStringLog(_ds1)
            _ds1.setLog(_log)
            _log = dimension.DimStringLog(_ds2)
            _ds2.setLog(_log)
        return True

    def delObject(self, obj):
        """Remove an object from this Layer.

delObject(obj)

The object should be a Point, Segment, Arc, Circle,
HCLine, VCLine, ACLine, CLine, CCircle, Chamfer,
Fillet, Leader, or Dimension. Anything else raises
a TypeError exception.
        """
        if self.isLocked():
            raise RuntimeError, "Deleting entity not allowed - layer locked."
        if id(obj) not in self.__objects:
            raise ValueError, "Object not found in layer: " + `obj`
        if isinstance(obj, point.Point):
            self.__delPoint(obj)
        elif isinstance(obj, segment.Segment):
            self.__delSegment(obj)
        elif isinstance(obj, arc.Arc):
            self.__delArc(obj)
        elif isinstance(obj, circle.Circle):
            self.__delCircle(obj)
        elif isinstance(obj, hcline.HCLine):
            self.__delHCLine(obj)
        elif isinstance(obj, vcline.VCLine):
            self.__delVCLine(obj)
        elif isinstance(obj, acline.ACLine):
            self.__delACLine(obj)
        elif isinstance(obj, cline.CLine):
            self.__delCLine(obj)
        elif isinstance(obj, ccircle.CCircle):
            self.__delCCircle(obj)
        elif isinstance(obj, segjoint.Chamfer):
            self.__delChamfer(obj)
        elif isinstance(obj, segjoint.Fillet):
            self.__delFillet(obj)
        elif isinstance(obj, leader.Leader):
            self.__delLeader(obj)
        elif isinstance(obj, polyline.Polyline):
            self.__delPolyline(obj)
        elif isinstance(obj, text.TextBlock):
            self.__delTextBlock(obj)
        elif isinstance(obj, dimension.AngularDimension):
            self.__delAngularDimension(obj)
        elif isinstance(obj, dimension.RadialDimension):
            self.__delRadialDimension(obj)
        elif isinstance(obj, dimension.HorizontalDimension):
            self.__delHorizontalDimension(obj)
        elif isinstance(obj, dimension.VerticalDimension):
            self.__delVerticalDimension(obj)
        elif isinstance(obj, dimension.LinearDimension):
            self.__delLinearDimension(obj)
        else:
            raise TypeError, "Invalid object type for removal: " + `type(obj)`

    def __freeObj(self, obj):
        #
        # disconnect object before calling setParent() so that
        # the layer log will not recieve a 'modified' message
        # from the object ...
        #
        del self.__objects[id(obj)]
        _oid = obj.getID()
        del self.__objids[_oid]
        if isinstance(obj, dimension.Dimension):
            _ds1, _ds2 = obj.getDimstrings()
            _log = _ds1.getLog()
            if _log is not None:
                _oid = _ds1.getID()
                self.__logs[_oid] = _log
            _log = _ds2.getLog()
            if _log is not None:
                _oid = _ds2.getID()
                self.__logs[_oid] = _log
        _log = obj.getLog()
        if _log is not None: # store the object's log
            _log.detatch()
            self.__logs[_oid] = _log
            obj.setLog(None)
        _log = self.getLog()        
        if _log is not None:
            obj.disconnect(_log)
        obj.setParent(None)
        
    def __delPoint(self, p):
        """Delete a Point from the Layer.

_delPoint(p)

This method is private to the Layer object.
        """
        _delete = True
        _users = p.getUsers()
        for _user in _users:
            if not isinstance(_user, dimension.Dimension):
                _delete = False
                break
        if _delete:
            for _user in _users:
                _layer = _user.getParent()
                if _layer is self:
                    if not self.inUndo() and not self.inRedo():
                        self.delObject(_user)
                    else:
                        p.disconnect(_user)
                        p.freeUser(_user)
                elif _layer is not None:
                    if not isinstance(_user, dimension.Dimension):
                        raise RuntimeError, "Point " + `p` + " bound to non-layer object: " + `_user`
                    if not self.inUndo() and not self.inRedo():
                        _layer.delObject(_user)
                    else:
                        p.disconnect(_user)
                        p.freeUser(_user)
                else:
                    pass
            self.__points.delObject(p)
            self.__freeObj(p)
            p.finish()

    def __delSegment(self, s):
        """Delete a Segment from the Layer.

_delSegment(s)

This method is private to the Layer object.
        """
        for _user in s.getUsers(): # chamfers, fillets, or hatching
            _layer = _user.getParent()
            if _layer is self:
                if not self.inUndo() and not self.inRedo():
                    self.delObject(_user) # restore segments on ch/fl?
                else:
                    s.freeUser(_user)
                    s.disconnect(_user)
            elif _layer is not None:
                raise RuntimeError, "Segment " + `s` + " bound to non-layer object: " + `_user`
            else:
                pass
        _p1, _p2 = s.getEndpoints()
        assert id(_p1) in self.__objects, "Segment p1 Point not in objects"
        assert id(_p2) in self.__objects, "Segment p2 Point not in objects"
        self.__segments.delObject(s)
        self.__freeObj(s)
        s.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_p1) # remove possibly unused point _p1
            self.delObject(_p2) # remove possibly unused point _p2

    def __delCircle(self, c):
        """Delete a Circle from the Layer.

_delCircle(c)

This method is private to the Layer object.
        """
        assert not isinstance(c, arc.Arc), "Arc in _delCircle()"
        for _user in c.getUsers(): # dimensions or hatching
            _layer = _user.getParent()
            if _layer is self:
                if not self.inUndo() and not self.inRedo():
                    self.delObject(_user)
                else:
                    c.freeUser(_user)
                    c.disconnect(_user)
            elif _layer is not None:
                if not isinstance(_user, dimension.Dimension):
                    raise RuntimeError, "Circle " + `c` + " bound to non-layer object: " + `_user`
                if not self.inUndo() and not self.inRedo():
                    _layer.delObject(_user)
                else:
                    c.freeUser(_user)
                    c.disconnect(_user)
            else:
                pass
        _cp = c.getCenter()
        assert id(_cp) in self.__objects, "Circle center point not in objects"
        self.__circles.delObject(c)
        self.__freeObj(c)
        c.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_cp) # remove possibly unused point _cp

    def __delArc(self, a):
        """Delete an Arc from the Layer.

_delArc(a)

This method is private to the Layer object.
        """
        for _user in a.getUsers(): # dimensions or hatching
            _layer = _user.getParent()
            if _layer is self:
                if not self.inUndo() and not self.inRedo():
                    self.delObject(_user)
                else:
                    a.freeUser(_user)
                    a.disconnect(_user)
            elif _layer is not None:
                if not isinstance(_user, dimension.Dimension):
                    raise RuntimeError, "Arc " + `a` + " bound to non-layer object: " + `_user`
                if not self.inUndo() and not self.inRedo():
                    _layer.delObject(_user)
                else:
                    a.freeUser(_user)
                    a.disconnect(_user)
            else:
                pass
        _cp = a.getCenter()
        assert id(_cp) in self.__objects, "Arc center point not in objects"
        self.__arcs.delObject(a)
        self.__freeObj(a)
        for _ep in a.getEndpoints():
            _pts = self.find('point', _ep[0], _ep[1])
            _p = None
            for _pt in _pts:
                for _user in _pt.getUsers():
                    if _user is a:
                        _p = _pt
                        break
                if _p is not None:
                    break
            assert _p is not None, "Arc endpoint not found in layer"
            assert id(_p) in self.__objects, "Arc endpoint not in objects"
            _p.disconnect(a)
            _p.freeUser(a)
            if not self.inUndo() and not self.inRedo():
                self.delObject(_p) # remove possibly unused point _p
            if abs(a.getStartAngle() - a.getEndAngle()) < 1e-10:
                break
        a.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_cp) # remove possibly unused point _cp

    def __delHCLine(self, hcl):
        """Remove a HCLine object from the Layer.

_delHCLine(hcl)

This method is private to the Layer object.
        """
        _lp = hcl.getLocation()
        assert id(_lp) in self.__objects, "HCLine point not in objects"
        self.__hclines.delObject(hcl)
        self.__freeObj(hcl)
        hcl.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_lp) # remove possibly unused point _lp

    def __delVCLine(self, vcl):
        """Remove a VCLine object from the Layer.

_delVCLine(vcl)

This method is private to the Layer object.
        """
        _lp = vcl.getLocation()
        assert id(_lp) in self.__objects, "VCLine point not in objects"
        self.__vclines.delObject(vcl)
        self.__freeObj(vcl)
        vcl.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_lp) # remove possibly unused point _lp

    def __delACLine(self, acl):
        """Remove an ACLine object from the Layer.

_delACLine(acl)

This method is private to the Layer object.
        """
        _lp = acl.getLocation()
        assert id(_lp) in self.__objects, "ACLine point not in objects"
        self.__aclines.delObject(acl)
        self.__freeObj(acl)
        acl.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_lp) # remove possibly unused point _lp

    def __delCLine(self, cl):
        """Delete a CLine from the Layer.

_delCLine(cl)

This method is private to the Layer object.
        """
        _p1, _p2 = cl.getKeypoints()
        assert id(_p1) in self.__objects, "CLine point p1 not in objects"
        assert id(_p2) in self.__objects, "CLine point p2 not in objects"
        self.__clines.delObject(cl)
        self.__freeObj(cl)
        cl.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_p1) # remove possibly unused point _p1
            self.delObject(_p2) # remove possibly unused point _p2

    def __delCCircle(self, cc):
        """Delete a CCircle from the Layer.

_delCCircle(cc)

This method is private to the Layer object.
        """
        _cp = cc.getCenter()
        assert id(_cp) in self.__objects, "CCircle center point not in objects"
        self.__ccircles.delObject(cc)
        self.__freeObj(cc)
        cc.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_cp) # remove possibly unused point _cp

    def __delChamfer(self, ch):
        """Remove a Chamfer from the Layer.

_delChamfer(ch)

This method is private to the Layer.
        """
        _chamfers = self.__chamfers
        _idx = None
        for _i in range(len(_chamfers)):
            if ch is _chamfers[_i]:
                _idx = _i
                break
        assert _idx is not None, "lost chamfer from list"
        for _user in ch.getUsers(): # could be hatching ...
            _layer = _user.getParent()
            if _layer is self:
                if not self.inUndo() and not self.inRedo():
                    self.delObject(_user)
                else:
                    ch.freeUser(_user)
                    ch.disconnect(_user)
            elif _layer is not None:
                raise RuntimeError, "Chamfer " + `ch` + " bound to non-layer object: " + `_user`
            else:
                pass
        _s1, _s2 = ch.getSegments()
        assert id(_s1) in self.__objects, "Chamfer s1 segment not in objects"
        assert id(_s2) in self.__objects, "Chamfer s2 segment not in objects"
        del _chamfers[_idx] # restore the things the chamfer connects?
        self.__freeObj(ch)
        ch.finish()

    def __delFillet(self, fl):
        """Remove a Fillet from the Layer.

_delFillet(fl)

This method is private to the Layer.
        """
        _fillets = self.__fillets
        _idx = None
        for _i in range(len(_fillets)):
            if fl is _fillets[_i]:
                _idx = _i
                break
        assert _idx is not None, "lost fillet from list"
        for _user in fl.getUsers(): # could be hatching ...
            _layer = _user.getParent()
            if _layer is self:
                if not self.inUndo() and not self.inRedo():
                    self.delObject(_user)
                else:
                    fl.freeUser(_user)
                    fl.disconnect(_user)
            elif _layer is not None:
                raise RuntimeError, "Fillet " + `fl` + " bound to non-layer object: " + `_user`
            else:
                pass
        _s1, _s2 = fl.getSegments()
        assert id(_s1) in self.__objects, "Fillet s1 segment not in objects"
        assert id(_s2) in self.__objects, "Fillet s2 segment not in objects"
        del _fillets[_idx] # restore the things the fillet connects?
        self.__freeObj(fl)
        fl.finish()

    def __delLeader(self, l):
        """Delete a Leader from the Layer.

_delLeader(l, f)

This method is private to the Layer object.
        """
        _p1, _p2, _p3 = l.getPoints()
        assert id(_p1) in self.__objects, "Leader p1 Point not in objects"
        assert id(_p2) in self.__objects, "Leader p2 Point not in objects"
        assert id(_p3) in self.__objects, "Leader p3 Point not in objects"
        self.__leaders.delObject(l)
        self.__freeObj(l)
        l.finish()
        if not self.inUndo() and not self.inRedo():
            self.delObject(_p1) # remove possibly unused point _p1
            self.delObject(_p2) # remove possibly unused point _p2
            self.delObject(_p3) # remove possibly unused point _p3

    def __delPolyline(self, pl):
        """Delete a Polyline from the Layer.

_delPolyline(pl, f)

This method is private to the Layer object.
        """
        for _user in pl.getUsers(): # could be hatching
            _layer = _user.getParent()
            if _layer is self:
                if not self.inUndo() and not self.inRedo():
                    self.delObject(_user)
                else:
                    pl.freeUser(_user)
                    pl.disconnect(_user)
            elif _layer is not None:
                raise RuntimeError, "Polyline " + `pl` + " bound to non-layer object: " + `_user`
            else:
                pass
        _pts = pl.getPoints()
        for _pt in _pts:
            assert id(_pt) in self.__objects, "Polyline point not in objects"
        self.__polylines.delObject(pl)
        self.__freeObj(pl)
        pl.finish()
        if not self.inUndo() and not self.inRedo():
            for _pt in _pts:
                self.delObject(_pt) # remove possibly unused point _pt

    def __delTextBlock(self, tb):
        """Delete a TextBlock from the Layer.

_delTextBlock(tb)

This method is private to the Layer object.
        """
        _tbs = self.__textblocks
        _idx = None
        for _i in range(len(_tbs)):
            if tb is _tbs[_i]:
                _idx = _i
                break
        assert _idx is not None, "lost textblock in list"
        del _tbs[_idx]
        self.__freeObj(tb)
        tb.finish()

    def __delAngularDimension(self, adim):
        """Delete an AngularDimension from the Layer.

_delAngularDimension(adim, f)

This method is private to the Layer object.
        """
        _p1, _p2, _p3 = adim.getDimPoints()
        if _p1.getParent() is self:
            assert id(_p1) in self.__objects, "ADim P1 not in objects"
        if _p2.getParent() is self:
            assert id(_p2) in self.__objects, "ADim P2 not in objects"
        if _p3.getParent() is self:
            assert id(_p3) in self.__objects, "ADim P3 not in objects"
        self.__adims.delObject(adim)
        self.__freeObj(adim)
        adim.finish()

    def __delRadialDimension(self, rdim):
        """Delete a RadialDimension from the Layer.

_delRadialDimension(rdim, f)

This method is private to the Layer object.
        """
        _circ = rdim.getDimCircle()
        if _circ.getParent() is self:
            assert id(_circ) in self.__objects, "RDim circle not in objects"
        self.__rdims.delObject(rdim)
        self.__freeObj(rdim)
        rdim.finish()

    def __delHorizontalDimension(self, hdim):
        """Delete an HorizontalDimension from the Layer.

_delHorizontalDimension(hdim, f)

This method is private to the Layer object.
        """
        _p1, _p2 = hdim.getDimPoints()
        if _p1.getParent() is self:
            assert id(_p1) in self.__objects, "HDim P1 not in objects"
        if _p2.getParent() is self:
            assert id(_p2) in self.__objects, "HDim P2 not in objects"
        self.__hdims.delObject(hdim)
        self.__freeObj(hdim)
        hdim.finish()

    def __delVerticalDimension(self, vdim):
        """Delete an VerticalDimension from the Layer.

_delVerticalDimension(vdim, f)

This method is private to the Layer object.
        """
        _p1, _p2 = vdim.getDimPoints()
        if _p1.getParent() is self:
            assert id(_p1) in self.__objects, "VDim P1 not in objects"
        if _p2.getParent() is self:
            assert id(_p2) in self.__objects, "VDim P2 not in objects"
        self.__vdims.delObject(vdim)
        self.__freeObj(vdim)
        vdim.finish()

    def __delLinearDimension(self, ldim):
        """Delete an LinearDimension from the Layer.

_delLinearDimension(ldim, f)

This method is private to the Layer object.
        """
        _p1, _p2 = ldim.getDimPoints()
        if _p1.getParent() is self:
            assert id(_p1) in self.__objects, "LDim P1 not in objects"
        if _p2.getParent() is self:
            assert id(_p2) in self.__objects, "LDim P2 not in objects"
        self.__ldims.delObject(ldim)
        self.__freeObj(ldim)
        ldim.finish()

    def getObject(self, eid):
        """Return an object of with a specified entity ID.

getObject(eid)

Argument eid is an entity ID.
        """
        return self.__objids.get(eid)

    def hasObject(self, eid):
        """

hasObject(eid)

Argument eid is an entity ID.
        """
        return eid in self.__objids

    def findObject(self, obj):
        """Return an object in the layer that is equivalent to a test object.

findObject(obj)

This method returns None if a suitable object is not found.
        """
        _retobj = None
        if id(obj) in self.__objects:
            _retobj = obj
        else:
            _objs = []
            if isinstance(obj, point.Point):
                _x, _y = obj.getCoords()
                _objs.extend(self.__points.find(_x, _y))
            elif isinstance(obj, segment.Segment):
                _p1, _p2 = obj.getEndpoints()
                _x1, _y1 = _p1.getCoords()
                _x2, _y2 = _p2.getCoords()
                _objs.extend(self.__segments.find(_x1, _y1, _x2, _y2))
            elif isinstance(obj, arc.Arc):
                _x, _y = obj.getCenter().getCoords()
                _r = obj.getRadius()
                _sa = obj.getStartAngle()
                _ea = obj.getEndAngle()
                _objs.extend(self.__arcs.find(_x, _y, _r, _sa, _ea))
            elif isinstance(obj, circle.Circle):
                _x, _y = obj.getCenter().getCoords()
                _r = obj.getRadius()
                _objs.extend(self.__circles.find(_x, _y, _r))
            elif isinstance(obj, hcline.HCLine):
                _y = obj.getLocation().y
                _objs.extend(self.__hclines.find(_y))
            elif isinstance(obj, vcline.VCLine):
                _x = obj.getLocation().x
                _objs.extend(self.__vclines.find(_x))
            elif isinstance(obj, acline.ACLine):
                _x, _y = obj.getLocation().getCoords()
                _angle = obj.getAngle()
                _objs.extend(self.__aclines.find(_x, _y, _angle))
            elif isinstance(obj, cline.CLine):
                _p1, _p2 = obj.getKeypoints()
                _x1, _y1 = _p1.getCoords()
                _x2, _y2 = _p2.getCoords()
                _objs.extend(self.__clines.find(_x1, _y1, _x2, _y2))
            elif isinstance(obj, ccircle.CCircle):
                _x, _y = obj.getCenter().getCoords()
                _r = obj.getRadius()
                _objs.extend(self.__ccircles.find(_x, _y, _r))
            elif isinstance(obj, segjoint.Fillet):
                for _f in self.__fillets:
                    if _f is obj:
                        _retobj = _f
                        break
            elif isinstance(obj, segjoint.Chamfer):
                for _c in self.__chamfers:
                    if _c == obj:
                        _retobj = _f
                        break
            elif isinstance(obj, leader.Leader):
                _p1, _p2, _p3 = obj.getPoints()
                _x1, _y1 = _p1.getCoords()
                _x2, _y2 = _p2.getCoords()
                _x3, _y3 = _p3.getCoords()
                _objs.extend(self.__leaders.find(_x1, _y1, _x2, _y2, _x3, _y3))
            elif isinstance(obj, polyline.Polyline):
                _coords = []
                for _pt in obj.getPoints():
                    _coords.append(_pt.getCoords())
                _objs.extend(self.__polylines.find(_coords))
            elif isinstance(obj, text.TextBlock):
                for _tb in self.__textblocks:
                    if _tb == obj:
                        _retobj = _f
                        break
            elif isinstance(obj, dimension.HorizontalDimension):
                _p1, _p2 = obj.getDimPoints()
                _objs.extend(self.__hdims.find(_p1, _p2))
            elif isinstance(obj, dimension.VerticalDimension):
                _p1, _p2 = obj.getDimPoints()
                _objs.extend(self.__vdims.find(_p1, _p2))
            elif isinstance(obj, dimension.LinearDimension):
                _p1, _p2 = obj.getDimPoints()
                _objs.extend(self.__ldims.find(_p1, _p2))
            elif isinstance(obj, dimension.RadialDimension):
                _c1 = obj.getDimCircle()
                _objs.extend(self.__rdims.find(_c1))
            elif isinstance(obj, dimension.AngularDimension):
                _vp, _p1, _p2 = obj.getDimPoints()
                _objs.extend(self.__adims.find(_vp, _p1, _p2))
            else:
                raise TypeError, "Invalid object type: " + `type(obj)`
            if _retobj is not None:
                for _obj in _objs:
                    if _obj is obj:
                        _retobj = _obj
                        break
        return _retobj

    def find(self, typestr, *args):
        """Find an existing entity in the drawing.

find(typestr, *args)

typestr: A string giving the type of entity to find
*args: A variable number of arguments used for searching
        """
        if not isinstance(typestr, str):
            raise TypeError, "Invalid type string: " + `type(typestr)`
        _objs = []
        if typestr == 'point':
            _objs.extend(self.__points.find(*args))
        elif typestr == 'segment':
            _objs.extend(self.__segments.find(*args))
        elif typestr == 'circle':
            _objs.extend(self.__circles.find(*args))
        elif typestr == 'arc':
            _objs.extend(self.__arcs.find(*args))
        elif typestr == 'hcline':
            _objs.extend(self.__hclines.find(*args))
        elif typestr == 'vcline':
            _objs.extend(self.__vclines.find(*args))
        elif typestr == 'acline':
            _objs.extend(self.__aclines.find(*args))
        elif typestr == 'cline':
            _objs.extend(self.__clines.find(*args))
        elif typestr == 'ccircle':
            _objs.extend(self.__ccircles.find(*args))
        elif typestr == 'leader':
            _objs.extend(self.__leaders.find(*args))
        elif typestr == 'polyline':
            _objs.extend(self.__polylines.find(*args))
        elif typestr == 'ldim':
            _objs.extend(self.__ldims.find(*args))
        elif typestr == 'hdim':
            _objs.extend(self.__hdims.find(*args))
        elif typestr == 'vdim':
            _objs.extend(self.__vdims.find(*args))
        elif typestr == 'rdim':
            _objs.extend(self.__rdims.find(*args))
        elif typestr == 'adim':
            _objs.extend(self.__adims.find(*args))
        else:
            raise ValueError, "Unexpected type string '%s'" % typestr
        return _objs

    def mapPoint(self, p, tol=tolerance.TOL, count=2):
        """Find a Point in the layer

mapPoint(p [,tol, count])

There is a single required argument:

p: Either a Point object or a tuple of two-floats

There are two optional arguments:

tol: A float equal or greater than 0 for distance tolerance comparisons.
count: An integer value indicating the largest number of objects to
       return. By default this value is 2.

Setting 'count' to None or a negative value will result in the maximum
number of objects being unlimited.

This method tests the objects in the Layer to see if the
Point can can be mapped on to any of them. The returned list
consists of tuples in the form:

(obj, pt)

Where 'obj' is the object the point was mapped to and 'pt'
is the projected point on the object.
        """
        _hits = []
        _p = p
        if not isinstance(_p, point.Point):
            _p = point.Point(p)
        _t = tolerance.toltest(tol)
        _count = count
        if _count is None:
            _count = sys.maxint
        else:
            if not isinstance(_count, int):
                _count = int(count)
            if _count < 0:
                _count = sys.maxint
        if _count < 1: # bail out, but why set count to this value?
            return _hits
        _x, _y = _p.getCoords()
        _xmin = _x - _t
        _xmax = _x + _t
        _ymin = _y - _t
        _ymax = _y + _t
        #
        # scan for non-point objects first, then look at points,
        # because there will not be any way the same object can
        # be found in differnt trees/lists
        #
        for _obj in self.__segments.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__circles.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__arcs.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__hclines.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__vclines.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__aclines.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__ccircles.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__clines.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__leaders.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__polylines.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__ldims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__hdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__vdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__rdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        for _obj in self.__adims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _pt = _obj.mapCoords(_x, _y, _t)
            if _pt is not None:
                _px, _py = _pt
                _pts = self.__points.find(_px, _py)
                if len(_pts) == 0:
                    _pts.append(point.Point(_px, _py))
                for _pt in _pts:
                    _hits.append((_obj, _pt))
                    if len(_hits) == _count:
                        return _hits
        #
        # scan for point, but do not append any object that
        # has already been added to the hit list
        #
        _objs = {}
        for _obj, _pt in _hits:
            _objs[id(_obj)] = True
        _pts = self.find('point', _x, _y, _t)
        if len(_pts) != 0:
            for _pt in _pts:
                assert id(_pt) in self.__objects, "Point not in objects"
                for _user in _pt.getUsers():
                    _uid = id(_user)
                    if _uid not in _objs:
                        _objs[_uid] = True
                        _hits.append((_user, _pt))
                        if len(_hits) == _count:
                            break
        return _hits

    def mapCoords(self, x, y, **kw):
        """Find objects at coordinates in the Layer.

mapCoords(x, y, **kw)

Arguments 'x' and 'y' are mandatory and should be float values.
Non-float values will be converted to that type if possible.

There are several optional keyword arguments:

tolerance: A float equal or greater than 0 for distance tolerance comparisons.
count: An integer value indicating the largest number of objects to
       return. By default this value is the sys.maxint value, essentially
       making the count unlimited.
types: A dictionary containing key/value pairs. If any key is given a
       value of 'True', only types for keys with 'True' values are examined.
       If any  key is given a 'False' value, the type corresponding to that
       key is skipped.

This method tests the objects in the Layer to see if the specified
x/y coordiantes can can be mapped on to any of them. The returned list
consists of tuples in the form:

(obj, {var})

Where 'obj' is the object the point was mapped to and '{var}'
is either an existing Point in the Layer or a tuple of the
form (x, y) giving the coordinates where a new Point can be
added.
        """
        #
        # utility function for testing whether or not an entity type
        # is to be examined
        #
        def _test_entity(tdict, skip, etype):
            _rv = True
            if tdict is not None:
                if skip is True:
                    if not tdict.has_key(etype) or tdict[etype] is not True:
                        _rv = False
                else:
                    if tdict.has_key(etype) and tdict[etype] is False:
                        _rv = False
            return _rv
        _x = util.get_float(x)
        _y = util.get_float(y)
        _t = 1e-10
        _types = None
        _skip = False
        _count = sys.maxint
        if 'tolerance' in kw:
            _val = util.get_float(kw['tolerance'])
            if _val < 0.0:
                raise ValueError, "Invalid negative tolerance: %f" % _val
            _t = _val
        if 'count' in kw:
            _val = kw['count']
            if not isinstance(_val, int):
                _val = int(kw['count'])
            if _val < 0:
                raise ValueError, "Invalid negative entity count %d" % _val
        if 'types' in kw:
            _val = kw['types']
            if not isinstance(_val, dict):
                raise TypeError, "Invalid 'types' dictionary: " + `type(_val)`
            for _k, _v in _val.items():
                if not isinstance(_k, str):
                    raise TypeError, "Invalid key %s type: %s " (str(_k), `type(_k)`)
                util.test_boolean(_v)
                if _skip is False and _v is True:
                    _skip = True
            _types = _val
        _hits = []
        if _count == 0:
            return _hits
        _xmin = _x - _t
        _xmax = _x + _t
        _ymin = _y - _t
        _ymax = _y + _t
        #
        # start testing entities
        #
        if _test_entity(_types, _skip, 'point'):
            _pts = self.__points.getInRegion(_xmin, _ymin, _xmax, _ymax)
            if len(_pts):
                _plist = []
                for _pt in _pts:
                    _sqlen = pow((_x - _pt.x), 2) + pow((_y - _pt.y), 2)
                    _plist.append((_sqlen, _pt))
                _plist.sort() # sorts tuples by first value!
                for _sqlen, _pt in _plist:
                    _hits.append((_pt, _pt))
                    if len(_hits) == _count:
                        return _hits
                # _users = {}
                # for _i in range(len(_pts)):
                    # _pt = _pts[_i]
                    # _count = _pt.countUsers()
                    # _ulist = _users.setdefault(_count, [])
                    # _ulist.append(_pt)
                # _counts = _users.keys()
                # _counts.sort(lambda _a, _b: cmp(_b, _a)) # largest to smallest
                # for _i in range(len(_counts)):
                    # _count = _counts[_i]
                    # for _pt in _users[_count]:
                        # _hits.append((_pt, _pt))
                        # if len(_hits) == _count:
                            # return _hits
        if _test_entity(_types, _skip, 'segment'):
            for _obj in self.__segments.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _px, _py = _pt
                    _p1, _p2 = _obj.getEndpoints()
                    if ((abs(_px - _p1.x) < _t) and (abs(_py - _p1.y) < _t)):
                        _hits.append((_obj, _p1))
                    elif ((abs(_px - _p2.x) < _t) and (abs(_py - _p2.y) < _t)):
                        _hits.append((_obj, _p2))
                    else:
                        _hits.append((_obj, (_px, _py)))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'circle'):
            for _obj in self.__circles.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'arc'):
            for _obj in self.__arcs.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'hcline'):
            for _obj in self.__hclines.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _px, _py = _pt
                    _lp = _obj.getLocation()
                    if ((abs(_px - _lp.x) < _t) and (abs(_py - _lp.y) < _t)):
                        _hits.append((_obj, _lp))
                    else:
                        _hits.append((_obj, (_px, _py)))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'vcline'):
            for _obj in self.__vclines.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _px, _py = _pt
                    _lp = _obj.getLocation()
                    if ((abs(_px - _lp.x) < _t) and (abs(_py - _lp.y) < _t)):
                        _hits.append((_obj, _lp))
                    else:
                        _hits.append((_obj, (_px, _py)))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'acline'):
            for _obj in self.__aclines.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _px, _py = _pt
                    _lp = _obj.getLocation()
                    if ((abs(_px - _lp.x) < _t) and (abs(_py - _lp.y) < _t)):
                        _hits.append((_obj, _lp))
                    else:
                        _hits.append((_obj, (_px, _py)))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'ccircle'):
            for _obj in self.__ccircles.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'cline'):
            for _obj in self.__clines.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _px, _py = _pt
                    _p1, _p2 = _obj.getKeypoints()
                    if ((abs(_px - _p1.x) < _t) and (abs(_py - _p1.y) < _t)):
                        _hits.append((_obj, _p1))
                    elif ((abs(_px - _p2.x) < _t) and (abs(_py - _p2.y) < _t)):
                        _hits.append((_obj, _p2))
                    else:
                        _hits.append((_obj, (_px, _py)))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'leader'):
            for _obj in self.__leaders.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _px, _py = _pt
                    _p1, _p2, _p3 = _obj.getPoints()
                    if ((abs(_px - _p1.x) < _t) and (abs(_py - _p1.y) < _t)):
                        _hits.append((_obj, _p1))
                    elif ((abs(_px - _p2.x) < _t) and (abs(_py - _p2.y) < _t)):
                        _hits.append((_obj, _p2))
                    elif ((abs(_px - _p3.x) < _t) and (abs(_py - _p3.y) < _t)):
                        _hits.append((_obj, _p3))
                    else:
                        _hits.append((_obj, (_px, _py)))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'polyline'):
            for _obj in self.__polylines.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _px, _py = _pt
                    _pp = None
                    for _tp in _obj.getPoints():
                        if ((abs(_px - _tp.x) < _t) and
                            (abs(_py - _tp.y) < _t)):
                            _pp = _tp
                            break
                    if _pp is not None:
                        _hits.append((_obj, _pp))
                    else:
                        _hits.append((_obj, (_px, _py)))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'linear_dimension'):
            for _obj in self.__ldims.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'horizontal_dimension'):
            for _obj in self.__hdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'vertical_dimension'):
            for _obj in self.__vdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'radial_dimension'):
            for _obj in self.__rdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        if _test_entity(_types, _skip, 'angular_dimension'):
            for _obj in self.__adims.getInRegion(_xmin, _ymin, _xmax, _ymax):
                _pt = _obj.mapCoords(_x, _y, _t)
                if _pt is not None:
                    _hits.append((_obj, (_pt[0], _pt[1])))
                    if len(_hits) == _count:
                        return _hits
        return _hits

    def hasEntities(self):
        """Test if the Layer has entities.

hasEntities():

This method returns a boolean
        """
        if (self.__points or
            self.__segments or
            self.__circles or
            self.__arcs or
            self.__leaders or
            self.__polylines or
            self.__hclines or
            self.__vclines or
            self.__aclines or
            self.__clines or
            self.__ccircles or
            (len(self.__chamfers) > 0) or
            (len(self.__fillets) > 0) or
            (len(self.__textblocks) > 0) or
            self.__ldims or
            self.__hdims or
            self.__vdims or
            self.__rdims or
            self.__adims):
            return True
        return False

    def getEntityCount(self, etype):
        """Return the number of an entity type stored in the  Layer

getEntityCount(etype)

The argument 'etype' should be one of the following:
point, segment, circle, arc, hcline, vcline, acline,
cline, ccircle, chamfer, fillet, leader, polyline,
textblock, linear_dimension, horizontal_dimenions,
vertical_dimension, radial_dimension, or angular_dimension.
        """
        if etype == "point":
            _res = len(self.__points)
        elif etype == "segment":
            _res = len(self.__segments)
        elif etype == "circle":
            _res = len(self.__circles)
        elif etype == "arc":
            _res = len(self.__arcs)
        elif etype == "leader":
            _res = len(self.__leaders)
        elif etype == "polyline":
            _res = len(self.__polylines)
        elif etype == "chamfer":
            _res = len(self.__chamfers)
        elif etype == "fillet":
            _res = len(self.__fillets)
        elif etype == "hcline":
            _res = len(self.__hclines)
        elif etype == "vcline":
            _res = len(self.__vclines)
        elif etype == "acline":
            _res = len(self.__aclines)
        elif etype == "cline":
            _res = len(self.__clines)
        elif etype == "ccircle":
            _res = len(self.__ccircles)
        elif etype == "text" or etype == 'textblock':
            _res = len(self.__textblocks)
        elif etype == "linear_dimension":
            _res = len(self.__ldims)
        elif etype == "horizontal_dimension":
            _res = len(self.__hdims)
        elif etype == "vertical_dimension":
            _res = len(self.__vdims)
        elif etype == "radial_dimension":
            _res = len(self.__rdims)
        elif etype == "angular_dimension":
            _res = len(self.__adims)
        else:
            raise ValueError, "Unexpected entity type string'%s'" % etype
        return _res
    
    def getLayerEntities(self, entity):
        """Get all of a particular type of entity in the Layer.

getLayerEntities(entity)

The argument 'entity' should be one of the following:
point, segment, circle, arc, hcline, vcline, acline,
cline, ccircle, chamfer, fillet, leader, polyline,
textblock, linear_dimension, horizontal_dimenions,
vertical_dimension, radial_dimension, or angular_dimension.
        """
        if not isinstance(entity, str):
            raise TypeError, "Invalid entity type: " + `type(entity)`
        if entity == "point":
            _objs = self.__points.getObjects()
        elif entity == "segment":
            _objs = self.__segments.getObjects()
        elif entity == "circle":
            _objs = self.__circles.getObjects()
        elif entity == "arc":
            _objs = self.__arcs.getObjects()
        elif entity == "hcline":
            _objs = self.__hclines.getObjects()
        elif entity == "vcline":
            _objs = self.__vclines.getObjects()
        elif entity == "acline":
            _objs = self.__aclines.getObjects()
        elif entity == "cline":
            _objs = self.__clines.getObjects()
        elif entity == "ccircle":
            _objs = self.__ccircles.getObjects()
        elif entity == "chamfer":
            _objs = self.__chamfers[:]
        elif entity == "fillet":
            _objs = self.__fillets[:]
        elif entity == "leader":
            _objs = self.__leaders.getObjects()
        elif entity == "polyline":
            _objs = self.__polylines.getObjects()
        elif entity == "text" or entity == 'textblock':
            _objs = self.__textblocks[:]
        elif entity == "linear_dimension":
            _objs = self.__ldims.getObjects()
        elif entity == "horizontal_dimension":
            _objs = self.__hdims.getObjects()
        elif entity == "vertical_dimension":
            _objs = self.__vdims.getObjects()
        elif entity == "radial_dimension":
            _objs = self.__rdims.getObjects()
        elif entity == "angular_dimension":
            _objs = self.__adims.getObjects()
        else:
            raise ValueError, "Invalid layer entity '%s'" % entity
        return _objs

    def canParent(self, obj):
        """Test if an Entity can be the parent of another Entity.

canParent(obj)

This method overrides the Entity::canParent() method. A layer can
be the parent of any object contained within itself.
        """
        return isinstance(obj, (point.Point, segment.Segment,
                                circle.Circle, arc.Arc,
                                leader.Leader, polyline.Polyline,
                                hcline.HCLine, vcline.VCLine,
                                acline.ACLine, cline.CLine, segjoint.SegJoint,
                                ccircle.CCircle, dimension.Dimension,
                                dimension.DimString, # ???
                                text.TextBlock))


    def setParentLayer(self, parent):
        """Store the parent layer of a layer within itself.

setParentLayer(parent)

Argument 'parent' must be either another Layer or None.
        """
        if parent is not None and not isinstance(parent, Layer):
            raise TypeError, "Invalid layer type: " + `type(parent)`
        _p = self.__parent_layer
        if _p is not parent:
            if _p is not None:
                _p.delSublayer(self)
            if parent is not None:
                parent.addSublayer(self)
            self.__parent_layer = parent
            self.sendMessage('reparented', _p)
            self.modified()

    def getParentLayer(self):
        return self.__parent_layer

    def addSublayer(self, l):
        if l is not None and not isinstance(l, Layer):
            raise TypeError, "Invalid layer type: " + `type(l)`
        if self.__sublayers is None:
            self.__sublayers = []
        if l in self.__sublayers:
            raise ValueError, "Layer already a sublayer: " + `l`
        self.__sublayers.append(l)
        self.sendMessage('added_sublayer', l)
        self.modified()

    def delSublayer(self, l):
        if l is not None and not isinstance(l, Layer):
            raise TypeError, "Invalid layer type: " + `type(l)`
        if self.__sublayers is None:
            raise ValueError, "Layer has no sublayers: " + `self`
        if l not in self.__sublayers:
            raise ValueError, "Layer not a sublayer: " + `l`
        self.__sublayers.remove(l)
        if len(self.__sublayers) == 0:
            self.__sublayers = None
        self.sendMessage('deleted_sublayer', l)
        self.modified()

    def hasSublayers(self):
        return self.__sublayers is not None and len(self.__sublayers) > 0

    def getSublayers(self):
        if self.__sublayers is not None:
            return self.__sublayers[:]
        return []

    def getScale(self):
        """Return the scale factor of the Layer.

getScale()
        """
        return self.__scale

    def setScale(self, scale):
        """Set the scale factor for the Layer.

setScale(scale)

The scale factor must be a positive float value greater than 0.0
        """
        _s = util.get_float(scale)
        if _s < 1e-10:
            raise ValueError, "Invalid scale factor: %g" % _s
        _os = self.__scale
        if abs(_os - _s) > 1e-10:
            self.startChange('scale_changed')
            self.__scale = _s
            self.endChange('scale_changed')
            self.sendMessage('scale_changed', _os)
            self.modified()

    scale = property(getScale, setScale, None, "Layer scale factor.")

    def getBoundary(self):
        """Return the maximum and minimum values of the object in the Layer.

getBoundary()

The function returns a tuple holding four float values:

(xmin, ymin, xmax, _ymax)

A default value of (-1.0, -1.0, 1.0, 1.0) is returned for a Layer
containing no objects.
        """
        _xmin = None
        _ymin = None
        _xmax = None
        _ymax = None
        for _obj in self.__points.getObjects():
            _x, _y = _obj.getCoords()
            if _xmin is None or _x < _xmin:
                _xmin = _x
            if _ymin is None or _y < _ymin:
                _ymin = _y
            if _xmax is None or _x > _xmax:
                _xmax = _x
            if _ymax is None or _y > _ymax:
                _ymax = _y
        for _obj in self.__arcs.getObjects():
            _axmin, _aymin, _axmax, _aymax = _obj.getBounds()
            if _xmin is None or _axmin < _xmin:
                _xmin = _axmin
            if _ymin is None or _aymin < _ymin:
                _ymin = _aymin
            if _xmax is None or _axmax > _xmax:
                _xmax = _axmax
            if _ymax is None or _aymax > _ymax:
                _ymax = _aymax
        for _obj in self.__circles.getObjects() + self.__ccircles.getObjects():
            _x, _y = _obj.getCenter().getCoords()
            _r = _obj.getRadius()
            _val = _x - _r
            if _xmin is None or _val < _xmin:
                _xmin = _val
            _val = _y - _r
            if _ymin is None or _val < _ymin:
                _ymin = _val
            _val = _x + _r
            if _xmax is None or _val > _xmax:
                _xmax = _val
            _val = _y + _r
            if _ymax is None or _val > _ymax:
                _ymax = _val
        _dims = (self.__ldims.getObjects() +
                 self.__hdims.getObjects() +
                 self.__vdims.getObjects() +
                 self.__rdims.getObjects() +
                 self.__adims.getObjects())
        for _obj in _dims:
            _dxmin, _dymin, _dxmax, _dymax = _obj.getBounds()
            if _xmin is None or _dxmin < _xmin:
                _xmin = _dxmin
            if _ymin is None or _dymin < _ymin:
                _ymin = _dymin
            if _xmax is None or _dxmax > _xmax:
                _xmax = _dxmax
            if _ymax is None or _dymax > _ymax:
                _ymax = _dymax
            _ds1, _ds2 = _obj.getDimstrings()
            _x, _y = _ds1.getLocation()
            _bounds = _ds1.getBounds()
            if _bounds is not None:
                _w, _h = _bounds
                if _x < _xmin:
                    _xmin = _x
                if (_y - _h) < _ymin:
                    _ymin = (_y - _h)
                if (_x + _w) > _xmax:
                    _xmax = (_x + _w)
                if _y > _ymax:
                    _ymax = _y
            if _obj.getDualDimMode():
                _x, _y = _ds2.getLocation()
                _bounds = _ds2.getBounds()
                if _bounds is not None:
                    _w, _h = _bounds
                    if _x < _xmin:
                        _xmin = _x
                    if (_y - _h) < _ymin:
                        _ymin = (_y - _h)
                    if (_x + _w) > _xmax:
                        _xmax = (_x + _w)
                    if _y > _ymax:
                        _ymax = _y
        for _textblock in self.__textblocks:
            _x, _y = _textblock.getLocation() # upper left corner
            _w = _h = 0.0
            _bounds = _textblock.getBounds()
            if _bounds is not None:
                _w, _h = _bounds
                _align = _textblock.getAlignment()
                if _align != text.TextStyle.ALIGN_LEFT:
                    if _align == text.TextStyle.ALIGN_CENTER:
                        _x = _x - _w/2.0
                    elif _align == text.TextStyle.ALIGN_RIGHT:
                        _x = _x - _w
            if _xmin is None or _x < _xmin:
                _xmin = _x
            if _ymin is None or (_y - _h) < _ymin:
                _ymin = (_y - _h)
            if _xmax is None or (_x + _w) > _xmax:
                _xmax = (_x + _w)
            if _ymax is None or _y > _ymax:
                _ymax = _y
        if _xmin is None: _xmin = -1.0
        if _ymin is None: _ymin = -1.0
        if _xmax is None: _xmax = 1.0
        if _ymax is None: _ymax = 1.0
        return _xmin, _ymin, _xmax, _ymax

    def objsInRegion(self, xmin, ymin, xmax, ymax, fully=False):
        """Return a all the objects in the Layer visible within the bounds.

objsInRegion(xmin, ymin, xmax, ymax[, fully])

The function has four required arguments:

xmin: The minimum x-value of the region
ymin: The minimum y-value of the region
xmax: The maximum x-value of the region
ymax: The maximum y-value of the region

There is a single optional argument:

fully: A True/False value indicating if the object must be
       entirely within the region [fully=True], or can
       merely pass through [fully=False]. The default value
       is False.

The function returns a list of objects.
        """
        _xmin = util.get_float(xmin)
        _ymin = util.get_float(ymin)
        _xmax = util.get_float(xmax)
        if xmax < xmin:
            raise ValueError, "Value error: xmax < xmin"
        _ymax = util.get_float(ymax)
        if _ymax < _ymin:
            raise ValueError, "Value error: ymax < ymin"
        util.test_boolean(fully)
        _objs = []
        _objs.extend(self.__points.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__segments.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__circles.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__arcs.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__hclines.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__vclines.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__aclines.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__clines.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__ccircles.getInRegion(_xmin, _ymin, _xmax, _ymax))
        for _obj in self.__chamfers:
            if _obj.inRegion(_xmin, _ymin, _xmax, _ymax):
                _objs.append(_obj)
        _objs.extend(self.__leaders.getInRegion(_xmin, _ymin, _xmax, _ymax))
        _objs.extend(self.__polylines.getInRegion(_xmin, _ymin, _xmax, _ymax))
        for _obj in self.__fillets:
            if _obj.inRegion(_xmin, _ymin, _xmax, _ymax):
                _objs.append(_obj)
        for _obj in self.__ldims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _objs.append(_obj)
            _ds1, _ds2 = _obj.getDimstrings()
            _objs.append(_ds1)
            if _obj.getDualDimMode():
                _objs.append(_ds2)
        for _obj in self.__hdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _objs.append(_obj)
            _ds1, _ds2 = _obj.getDimstrings()
            _objs.append(_ds1)
            if _obj.getDualDimMode():
                _objs.append(_ds2)
        for _obj in self.__vdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _objs.append(_obj)
            _ds1, _ds2 = _obj.getDimstrings()
            _objs.append(_ds1)
            if _obj.getDualDimMode():
                _objs.append(_ds2)
        for _obj in self.__rdims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _objs.append(_obj)
            _ds1, _ds2 = _obj.getDimstrings()
            _objs.append(_ds1)
            if _obj.getDualDimMode():
                _objs.append(_ds2)
        for _obj in self.__adims.getInRegion(_xmin, _ymin, _xmax, _ymax):
            _objs.append(_obj)
            _ds1, _ds2 = _obj.getDimstrings()
            _objs.append(_ds1)
            if _obj.getDualDimMode():
                _objs.append(_ds2)
        for _obj in self.__textblocks:
            _x, _y = _obj.getLocation()
            _bounds = _obj.getBounds()
            if _bounds is not None:
                _w, _h = _bounds
            else:
                _w = _h = 0                
            _txmin = _x
            _txmax = _x + _w
            _tymin = _y - _h
            _tymax = _y
            if not ((_txmax < _xmin) or
                    (_txmin > _xmax) or
                    (_tymax < _ymin) or
                    (_tymin > _ymax)):
                _objs.append(_obj)
        return _objs

    def sendsMessage(self, m):
        if m in Layer.__messages:
            return True
        return super(Layer, self).sendsMessage(m)

    def update(self):
        """Check that the objects in this layer are stored correctly.

update()

This function checks that the objects held in this layer are kept
in the proper order. Also, any duplicated objects that may have
be created due to modifying entities in the layer are removed.
        """
        raise RuntimeError, "Layer::update() called."

#
# Layer history class
#

class LayerLog(entity.EntityLog):
    def __init__(self, l):
        if not isinstance(l, Layer):
            raise TypeError, "Invalid layer type: " + `type(l)`
        super(LayerLog, self).__init__(l)
        l.connect('scale_changed', self.__scaleChanged)
        l.connect('name_changed', self.__nameChanged)
        l.connect('added_child', self.__addedChild)
        l.connect('removed_child', self.__removedChild)

    def __addedChild(self, l, *args):
        _alen = len(args)
        if _alen < 1:
            raise ValueError, "Invalid argument count: %d" % _alen
        _obj = args[0]
        _vals = _obj.getValues()
        if not isinstance(_vals, entity.EntityData):
            raise TypeError, "Unexpected type for values: " + `type(_obj)`
        _vals.lock()
        self.saveUndoData('added_child', _vals)

    def __removedChild(self, l, *args):
        _alen = len(args)
        if _alen < 1:
            raise ValueError, "Invalid argument count: %d" % _alen
        _obj = args[0]
        _vals = _obj.getValues()
        if not isinstance(_vals, entity.EntityData):
            raise TypeError, "Unexpected type for values: " + `type(_obj)`
        _vals.lock()
        self.saveUndoData('removed_child', _vals)

    def __nameChanged(self, l, *args):
        _alen = len(args)
        if _alen < 1:
            raise ValueError, "Invalid argument count: %d" % _alen
        _name = args[0]
        if not isinstance(_name, types.StringTypes):
            raise TypeError, "Unexpected type for name: " + `type(_name)`
        self.saveUndoData('name_changed', _name)

    def __scaleChanged(self, l, *args):
        _alen = len(args)
        if _alen < 1:
            raise ValueError, "Invalid argument count: %d" % _alen
        _scale = args[0]
        if not isinstance(_scale, float):
            raise TypeError, "Unexpected type for scale: " + `type(_scale)`
        if _scale < 1e-10:
            raise ValueError, "Invalid scale: %g" % _scale
        self.saveUndoData('scale_changed', _scale)

    def execute(self, undo, *args):
        # print "LayerLog::execute() ..."
        # print args
        util.test_boolean(undo)
        _alen = len(args)
        if len(args) == 0:
            raise ValueError, "No arguments to execute()"
        _l = self.getObject()
        _op = args[0]
        if _op == 'name_changed':
            if len(args) < 2:
                raise ValueError, "Invalid argument count: %d" % _alen
            _sdata = _l.getName()
            self.ignore(_op)
            try:
                _name = args[1]
                if undo:
                    _l.startUndo()
                    try:
                        _l.setName(_name)
                    finally:
                        _l.endUndo()
                else:
                    _l.startRedo()
                    try:
                        _l.setName(_name)
                    finally:
                        _l.endRedo()
            finally:
                self.receive(_op)
            self.saveData(undo, _op, _sdata)
        elif _op == 'scale_changed':
            if len(args) < 2:
                raise ValueError, "Invalid argument count: %d" % _alen
            _sdata = _l.getScale()
            self.ignore(_op)
            try:
                _scale = args[1]
                if undo:
                    _l.startUndo()
                    try:
                        _l.setScale(_scale)
                    finally:
                        _l.endUndo()
                else:
                    _l.startRedo()
                    try:
                        _l.setScale(_scale)
                    finally:
                        _l.endRedo()
            finally:
                self.receive(_op)
            self.saveData(undo, _op, _sdata)
        elif _op == 'added_child':
            if len(args) < 2:
                raise ValueError, "Invalid argument count: %d" % _alen
            _vals = args[1]
            if not isinstance(_vals, entity.EntityData):
                raise TypeError, "Unexpected type for values: " + `type(_vals)`
            self.ignore('modified')
            try:
                if undo:
                    _sdata = _vals
                    self.ignore('removed_child')
                    try:
                        self.__delObject(undo, _vals)
                    finally:
                        self.receive('removed_child')
                else:
                    _obj = self.__makeObject(_vals)
                    self.ignore(_op)
                    try:
                        _l.startRedo()
                        try:
                            _l.addObject(_obj)
                        finally:
                            _l.endRedo()
                    finally:
                        self.receive(_op)
                    _sdata = _obj.getValues()
                    _sdata.lock()
            finally:
                self.receive('modified')
            self.saveData(undo, _op, _sdata)
        elif _op == 'removed_child':
            if len(args) < 2:
                raise ValueError, "Invalid argument count: %d" % _alen
            _vals = args[1]
            if not isinstance(_vals, entity.EntityData):
                raise TypeError, "Unexpected type for values: " + `type(_vals)`
            self.ignore('modified')
            try:
                if undo:
                    _obj = self.__makeObject(_vals)
                    self.ignore('added_child')
                    try:
                        _l.startUndo()
                        try:
                            _l.addObject(_obj)
                        finally:
                            _l.endUndo()
                        _sdata = _obj.getValues()
                        _sdata.lock()
                    finally:
                        self.receive('added_child')
                else:
                    _sdata = _vals
                    self.ignore(_op)
                    try:
                        self.__delObject(undo, _vals)
                    finally:
                        self.receive(_op)
            finally:
                self.receive('modified')
            self.saveData(undo, _op, _sdata)
        else:
            super(LayerLog, self).execute(undo, *args)

    def __getImageColor(self, r, g, b):
        _image = self.getObject().getParent()
        _color = None
        if _image is not None:
            for _ic in _image.getImageEntities('color'):
                if _ic.r == r and _ic.g == g and _ic.b == b:
                    _color = _ic
                    break
        if _color is None:
            _color = color.Color(r, g, b)
        return _color

    def __makeImageColor(self, values):
        # print "LayerLog::__makeImageColor() ..."
        _image = self.getObject().getParent()
        _cdata = values.get('color')
        _color = None
        if _image is not None and _cdata is not None:
            # print "restoring image color: " + str(_cdata)
            _r, _g, _b = _cdata
            for _ic in _image.getImageEntities('color'):
                if _ic.r == _r and _ic.g == _g and _ic.b == _b:
                    _color = _ic
                    break
        if _color is None and _cdata is not None: # make one
            _r, _g, _b = _cdata
            _color = color.Color(_r, _g, _b)
        return _color
        
    def __makeGraphicLinetype(self, values):
        # print "LayerLog::__makeGraphicLinetype() ..."
        _image = self.getObject().getParent()
        _ltdata = values.get('linetype')
        _linetype = None
        if _image is not None and _ltdata is not None:
            # print "restoring graphic linetype: " + str(_ltdata)
            _name, _dlist = _ltdata
            for _ilt in _image.getImageEntities('linetype'):
                if ((_ilt.getName() == _name) and (_ilt.getList() == _dlist)):
                    _linetype = _ilt
                    break
        if _linetype is None and _ltdata is not None: # make one
            _name, _dlist = _ltdata
            _linetype = linetype.Linetype(_name, _dlist)
        return _linetype
    
    def __makeGraphicStyle(self, values):
        # print "LayerLog::__makeGraphicStyle() ..."
        _image = self.getObject().getParent()
        _sdata = values.get('style')
        _style = None
        if _image is not None and _sdata is not None:
            # print "restoring graphic style: " + str(_sdata)
            _name, _lt, _col, _th = _sdata
            _ln, _ld = _lt
            for _istyle in _image.getImageEntities('style'):
                if _istyle.getName() != _name:
                    continue
                _ilt = _istyle.getLinetype()
                if ((_ilt.getName() != _ln) or (_ilt.getList() != _ld)):
                    continue
                if _istyle.getColor().getColors() != _col:
                    continue
                if abs(_istyle.getThickness() - _th) > 1e-10:
                    continue
                _style = _istyle
                break
        if _style is None and _sdata is not None: # make one
            _name, _lt, _col, _th = _sdata
            _r, _g, _b = _col
            _color = self.__getImageColor(_r, _g, _b)
            _linetype = linetype.Linetype(_lt[0], _lt[1])
            _style = style.Style(_name, _linetype, _color, _th)
        return _style

    def __resetGraphicValues(self, obj, values):
        # print "LayerLog::__resetGraphicValues() ..."
        _style = self.__makeGraphicStyle(values)
        if _style is not None:
            # print "resetting style ..."
            obj.setStyle(_style)
        _linetype = self.__makeGraphicLinetype(values)
        if _linetype is not None:
            # print "resetting linetype ..."
            obj.setLinetype(_linetype)
        _color = self.__makeImageColor(values)
        if _color is not None:
            # print "resetting color ..."
            obj.setColor(_color)
        _thickness = values.get('thickness')
        if _thickness is not None:
            # print "resetting thickness ..."
            obj.setThickness(_thickness)
        
    def __makeTextStyle(self, values):
        # print "LayerLog::__makeTextStyle() ..."
        _image = self.getObject().getParent()
        _tdata = values.get('textstyle')
        _textstyle = None
        if _image is not None and _tdata is not None:
            # print "restoring textstyle: " + str(_tdata)
            for _ts in _image.getImageEntities('textstyle'):
                # print "Comparing stored TextStyle: %s " % _ts.getName()
                if _ts.getName() != _tdata['name']:
                    # print "name differs"
                    continue
                if _ts.getFamily() != _tdata['family']:
                    # print "family differs"
                    continue
                if _ts.getStyle() != _tdata['style']:
                    # print "style differs"
                    continue
                _c = _ts.getColor()
                _r, _g, _b = _tdata['color']
                if ((_c.r != _r) or (_c.g != _g) or (_c.b != _b)):
                    # print "color differs"
                    continue
                if abs(_ts.getSize() - _tdata['size']) > 1e-10:
                    # print "size differs"
                    continue
                if abs(_ts.getAngle() - _tdata['angle']) > 1e-10:
                    # print "angle differs"
                    continue
                if _ts.getAlignment() != _tdata['align']:
                    # print "alignment differs"
                    continue
                _textstyle = _ts
                break
        if _textstyle is None and _tdata is not None: # make one
            # print "Creating new TextStyle instance"
            _r, _g, _b = _tdata['color']
            _color = self.__getImageColor(_r, _g, _b)
            _textstyle = text.TextStyle(_tdata['name'],
                                        family=_tdata['family'],
                                        style=_tdata['style'],
                                        weight=_tdata['weight'],
                                        color=_color,
                                        size=_tdata['size'],
                                        angle=_tdata['angle'],
                                        align=_tdata['align'])
        return _textstyle

    def __makeDimStyle(self, values):
        # print "LayerLog::__makeDimStyle() ..."
        _image = self.getObject().getParent()
        _dsdata = values.get('dimstyle')
        _dimstyle = None
        if _image is not None and _dsdata is not None:
            # print "restoring dimstylstyle: " + str(_tdata)
            _name = _dsdata['name']
            _dscopy = {}
            for _key in _dsdata.keys():
                if _key != 'name':
                    _dscopy[_key] = _dsdata[_key]
            for _ds in _image.getImageEntities('dimstyle'):
                if _ds.getName() != _name:
                    continue
                _keys = _ds.getKeys()
                _seen = True
                for _key in _keys:
                    if _key not in _dscopy:
                        _seen = False
                        break
                if not _seen:
                    continue
                for _key in _dscopy:
                    if _key not in _keys:
                        _seen = False
                        break
                if not _seen:
                    continue
                _hit = True
                for _key, _val in _dscopy.items():
                    _dsv = _ds.getValue(_key)
                    if ((_key == 'DIM_COLOR') or
                        (_key == 'DIM_PRIMARY_FONT_COLOR') or
                        (_key == 'DIM_SECONDARY_FONT_COLOR')):
                        if _dsv.getColors() != _val:
                            _hit = False
                            break
                    else:
                        if _dsv != _val:
                            _hit = False
                            break
                if not _hit:
                    continue
                # print "hit on existing DimStyle ..."
                _dimstyle = _ds
                break
        if _dimstyle is None and _dsdata is not None: # make one
            # print "making new DimStyle ..."
            _name = _dsdata['name']
            _vals = {}
            for _key in _dsdata.keys():
                if _key != 'name':
                    _val = _dsdata[_key]
                    if ((_key == 'DIM_COLOR') or
                        (_key == 'DIM_PRIMARY_FONT_COLOR') or
                        (_key == 'DIM_SECONDARY_FONT_COLOR')):
                        _r, _g, _b = _val
                        _color = self.__getImageColor(_r, _g, _b)
                        _vals[_key] = _color
                    else:
                        _vals[_key] = _val
            _dimstyle = dimension.DimStyle(_name, _vals)
        return _dimstyle
        
    def __makeDimString(self, values):
        # print "LayerLog::__makeDimString() ..."
        _textstyle = self.__makeTextStyle(values)
        _id = values.get('id')
        if _id is None:
            raise ValueError, "Lost 'id' for recreating DimString"
        _val = values.get('location')
        if _val is None:
            raise ValueError, "Lost 'location' value for DimString"
        _x, _y = _val
        _ds = dimension.DimString(_x, _y, textstyle=_textstyle, id=_id)
        #
        # TextBlock info
        #
        _val = values.get('family')
        if _val is not None:
            _ds.setFamily(_val)
        _val = values.get('style')
        if _val is not None:
            _ds.setStyle(_val)
        _val = values.get('weight')
        if _val is not None:
            _ds.setWeight(_val)
        _val = values.get('color')
        if _val is not None:
            _r, _g, _b = _val
            _ds.setColor(self.__getImageColor(_r, _g, _b))
        _val = values.get('size')
        if _val is not None:
            _ds.setSize(_val)
        _val = values.get('angle')
        if _val is not None:
            _ds.setAngle(_val)
        _val = values.get('alignment')
        if _val is not None:
            _ds.setAlignment(_val)
        #
        # DimString info
        #
        _val = values.get('prefix')
        if _val is None:
            raise ValueError, "Lost 'prefix' value for DimString"
        _ds.setPrefix(_val)
        _val = values.get('suffix')
        if _val is None:
            raise ValueError, "Lost 'suffix' value for DimString"
        _ds.setSuffix(_val)
        _val = values.get('units')
        if _val is None:
            raise ValueError, "Lost 'units' value for DimString"
        if _val == 'millimeters':
            _unit = units.MILLIMETERS
        elif _val == 'micrometers':
            _unit = units.MICROMETERS
        elif _val == 'meters':
            _unit = units.METERS
        elif _val == 'kilometers':
            _unit = units.KILOMETERS
        elif _val == 'inches':
            _unit = units.INCHES
        elif _val == 'feet':
            _unit = units.FEET
        elif _val == 'yards':
            _unit = units.YARDS
        elif _val == 'miles':
            _unit = units.MILES
        else:
            raise ValueError, "Unexpected unit: %s" % _val
        _ds.setUnits(_unit)
        _val = values.get('precision')
        if _val is None:
            raise ValueError, "Lost 'precision' value for DimString"
        _ds.setPrecision(_val)
        _val = values.get('print_zero')
        if _val is None:
            raise ValueError, "Lost 'print_zero' value for DimString"
        _ds.setPrintZero(_val)
        _val = values.get('print_decimal')
        if _val is None:
            raise ValueError, "Lost 'print_decimal' value for DimString"
        _ds.setPrintDecimal(_val)
        return _ds

    def __adjustDimension(self, obj, values):
        # print "LayerLog::__adjustDimension() ..."
        _val = values.get('offset')
        if _val is not None:
            obj.setOffset(_val)
        _val = values.get('extension')
        if _val is not None:
            obj.setExtension(_val)
        _val = values.get('position')
        if _val is not None:
            obj.setPosition(_val)
        _val = values.get('eptype')
        if _val is not None:
            obj.setEndpointType(_val)
        _val = values.get('epsize')
        if _val is not None:
            obj.setEndpointSize(_val)
        _val = values.get('color')
        if _val is not None:
            _r, _g, _b = _val
            obj.setColor(self.__getImageColor(_r, _g, _b))
        _val = values.get('dualmode')
        if _val is not None:
            obj.setDualDimMode(_val)
        _val = values.get('poffset')
        if _val is not None:
            obj.setPositionOffset(_val)
        _val = values.get('dmoffset')
        if _val is not None:
            obj.setDualModeOffset(_val)
        _val = values.get('thickness')
        if _val is not None:
            obj.setThickness(_val)
        
    def __makeObject(self, values):
        # print "LayerLog::__makeObject() ..."
        _type = values.get('type')
        if _type is None:
            _keys = values.keys()
            _keys.sort()
            for _key in _keys:
                print "key: %s: value: %s" % (_key, str(values.get(_key)))
            raise RuntimeError, "No type defined for these values"
        _id = values.get('id')
        if _id is None:
            raise ValueError, "Lost 'id' for recreating object"
        _l = self.getObject()
        _obj = None
        #
        if _type == 'point':
            _x = values.get('x')
            if _x is None:
                raise ValueError, "Lost 'x' value for Point"
            _y = values.get('y')
            if _y is None:
                raise ValueError, "Lost 'y' value for Point"
            _obj = point.Point(_x, _y, id=_id)
        elif _type == 'segment':
            _p1id = values.get('p1')
            if _p1id is None:
                raise ValueError, "Lost 'p1' value for Segment"
            _p1 = _l.getObject(_p1id)
            if _p1 is None or not isinstance(_p1, point.Point):
                raise ValueError, "Segment P1 point missing; id=%d" % _p1id
            _p2id = values.get('p2')
            if _p2id is None:
                raise ValueError, "Lost 'p2' value for Segment"
            _p2 = _l.getObject(_p2id)
            if _p2 is None or not isinstance(_p2, point.Point):
                raise ValueError, "Segment P2 point missing; id=%d" % _p2id
            _obj = segment.Segment(_p1, _p2, id=_id)
            self.__resetGraphicValues(_obj, values)
        elif _type == 'circle':
            _cid = values.get('center')
            if _cid is None:
                raise ValueError, "Lost 'center' value for Circle"
            _cp = _l.getObject(_cid)
            if _cp is None or not isinstance(_cp, point.Point):
                raise ValueError, "Circle center missing: id=%d" % _cid
            _r = values.get('radius')
            if _r is None:
                raise ValueError, "Lost 'radius' value for Circle"
            _obj = circle.Circle(_cp, _r, id=_id)
            self.__resetGraphicValues(_obj, values)
        elif _type == 'arc':
            _cid = values.get('center')
            if _cid is None:
                raise ValueError, "Lost 'center' value for Arc."
            _cp = _l.getObject(_cid)
            if _cp is None or not isinstance(_cp, point.Point):
                raise ValueError, "Arc center missing: id=%d" % _cid
            _r = values.get('radius')
            if _r is None:
                raise ValueError, "Lost 'radius' value for Arc."
            _sa = values.get('start_angle')
            if _sa is None:
                raise ValueError, "Lost 'start_angle' value for Arc."
            _ea = values.get('end_angle')
            if _ea is None:
                raise ValueError, "Lost 'end_angle' value for Arc."
            _obj = arc.Arc(_cp, _r, _sa, _ea, id=_id)
            self.__resetGraphicValues(_obj, values)
        elif _type == 'ellipse':
            raise TypeError, "Ellipse not yet handled ..."
        elif _type == 'leader':
            _p1id = values.get('p1')
            if _p1id is None:
                raise ValueError, "Lost 'p1' value for Leader."
            _p1 = _l.getObject(_p1id)
            if _p1 is None or not isinstance(_p1, point.Point):
                raise ValueError, "Leader P1 point missing: id=%d" % _p1id
            _p2id = values.get('p2')
            if _p2id is None:
                raise ValueError, "Lost 'p2' value for Leader."
            _p2 = _l.getObject(_p2id)
            if _p2 is None or not isinstance(_p2, point.Point):
                raise ValueError, "Leader P2 point missing: id=%d" % _p2id
            _p3id = values.get('p3')
            if _p3id is None:
                raise ValueError, "Lost 'p3' value for Leader."
            _p3 = _l.getObject(_p3id)
            if _p3 is None or not isinstance(_p3, point.Point):
                raise ValueError, "Leader P3 point missing: id=%d" % _p3id
            _size = values.get('size')
            if _size is None:
                raise ValueError, "Lost 'size' value for Leader."
            _obj = leader.Leader(_p1, _p2, _p3, _size, id=_id)
            self.__resetGraphicValues(_obj, values)
        elif _type == 'polyline':
            _pids = values.get('points')
            _pts = []
            for _ptid in _pids:
                _p = _l.getObject(_ptid)
                if _p is None or not isinstance(_p, point.Point):
                    raise ValueError, "Polyline point missing: id=%d" % _ptid
                _pts.append(_p)
            _obj = polyline.Polyline(_pts, id=_id)
            self.__resetGraphicValues(_obj, values)
        elif _type == 'textblock':
            _loc = values.get('location')
            if _loc is None:
                raise ValueError, "Lost 'location' value for TextBlock."
            _x, _y = _loc
            _text = values.get('text')
            if _text is None:
                raise ValueError, "Lost 'text' value for TextBlock."
            _tstyle = self.__makeTextStyle(values)
            _obj = text.TextBlock(_x, _y, _text, textstyle=_tstyle, id=_id)
            _val = values.get('family')
            if _val is not None:
                _obj.setFamily(_val)
            _val = values.get('style')
            if _val is not None:
                _obj.setStyle(_val)
            _val = values.get('weight')
            if _val is not None:
                _obj.setWeight(_val)
            _val = values.get('color')
            if _val is not None:
                _r, _g, _b = _val
                _obj.setColor(self.__getImageColor(_r, _g, _b))
            _val = values.get('size')
            if _val is not None:
                _obj.setSize(_val)
            _val = values.get('angle')
            if _val is not None:
                _obj.setAngle(_val)
            _val = values.get('alignment')
            if _val is not None:
                _obj.setAlignment(_val)
        elif _type == 'hcline':
            _kid = values.get('keypoint')
            if _kid is None:
                raise ValueError, "Lost 'keypoint' value for HCLine."
            _p = _l.getObject(_kid)
            if _p is None or not isinstance(_p, point.Point):
                raise ValueError, "HCLine point missing: id=%d" % _kid
            _obj = hcline.HCLine(_p, id=_id)
        elif _type == 'vcline':
            _kid = values.get('keypoint')
            if _kid is None:
                raise ValueError, "Lost 'keypoint' value for VCLine."
            _p = _l.getObject(_kid)
            if _p is None or not isinstance(_p, point.Point):
                raise ValueError, "VCLine point missing: id=%d" % _kid
            _obj = vcline.VCLine(_p, id=_id)
        elif _type == 'acline':
            _kid = values.get('keypoint')
            if _kid is None:
                raise ValueError, "Lost 'keypoint' value for ACLine."
            _p = _l.getObject(_kid)
            if _p is None or not isinstance(_p, point.Point):
                raise ValueError, "ACLine point missing: id=%d" % _kid
            _angle = values.get('angle')
            if _angle is None:
                raise ValueError, "Lost 'angle' value for ACLine."
            _obj = acline.ACLine(_p, _angle, id=_id)
        elif _type == 'cline':
            _p1id = values.get('p1')
            if _p1id is None:
                raise ValueError, "Lost 'p1' value for CLine"
            _p1 = _l.getObject(_p1id)
            if _p1 is None or not isinstance(_p1, point.Point):
                raise ValueError, "CLine P1 point missing: id=%d" % _p1id
            _p2id = values.get('p2')
            if _p2id is None:
                raise ValueError, "Lost 'p2' value for CLine"
            _p2 = _l.getObject(_p2id)
            if _p2 is None or not isinstance(_p2, point.Point):
                raise ValueError, "CLine P2 point missing: id=%d" % _p2id
            _obj = cline.CLine(_p1, _p2, id=_id)
        elif _type == 'ccircle':
            _cid = values.get('center')
            if _cid is None:
                raise ValueError, "Lost 'center' value for CCircle"
            _cp = _l.getObject(_cid)
            if _cp is None or not isinstance(_cp, point.Point):
                raise ValueError, "CCircle center missing: id=%d" % _cid
            _r = values.get('radius')
            if _r is None:
                raise ValueError, "Lost 'radius' value for CCircle"
            _obj = ccircle.CCircle(_cp, _r, id=_id)
        elif _type == 'fillet':
            _s1id = values.get('s1')
            if _s1id is None:
                raise ValueError, "Lost 's1' value for Fillet"
            _s1 = _l.getObject(_s1id)
            if _s1 is None or not isinstance(_s1, segment.Segment):
                raise ValueError, "Fillet S1 segment missing: id=%d" % _s1id
            _s2id = values.get('s2')
            if _s2id is None:
                raise ValueError, "Lost 's2' value for Fillet"
            _s2 = _l.getObject(_s2id)
            if _s2 is None or not isinstance(_s2, segment.Segment):
                raise ValueError, "Fillet S2 segment missing: id=%d" % _s2id
            _r = values.get('radius')
            if _r is None:
                raise ValueError, "Lost 'radius' value for Fillet"
            _obj = segjoint.Fillet(_s1, _s2, _r, id=_id)
        elif _type == 'chamfer':
            _s1id = values.get('s1')
            if _s1id is None:
                raise ValueError, "Lost 's1' value for Chamfer"
            _s1 = _l.getObject(_s1id)
            if _s1 is None or not isinstance(_s1, segment.Segment):
                raise ValueError, "Fillet S1 segment missing: id=%d" % _s1id
            _s2id = values.get('s2')
            if _s2id is None:
                raise ValueError, "Lost 's2' value for Chamfer"
            _s2 = _l.getObject(_s2id)
            if _s2 is None or not isinstance(_s2, segment.Segment):
                raise ValueError, "Fillet S2 segment missing: id=%d" % _s2id
            _len = values.get('length')
            if _len is None:
                raise ValueError, "Lost 'length' value for Chamfer"
            _obj = segjoint.Chamfer(_s1, _s2, _len, id=_id)
        elif _type == 'ldim' or _type == 'hdim' or _type == 'vdim':
            _loc = values.get('location')
            if _loc is None:
                raise ValueError, "Lost 'location' value for L/H/V Dimension."
            _x, _y = _loc
            _l1id = values.get('l1')
            if _l1id is None:
                raise ValueError, "Lost 'l1' value for L/H/V Dimension."
            _p1id = values.get('p1')
            if _p1id is None:
                raise ValueError, "Lost 'p1' value for L/H/V Dimension."
            _l2id = values.get('l2')
            if _l2id is None:
                raise ValueError, "Lost 'l2' value for L/H/V Dimension."
            _p2id = values.get('p2')
            if _p2id is None:
                raise ValueError, "Lost 'p2' value for L/H/V Dimension."
            _l1 = _p1 = _l2 = _p2 = None
            _lid = _l.getID()
            _img = _l.getParent()
            if _img is None:
                raise ValueError, "Layer has no parent Image"
            if _l1id == _lid:
                _l1 = _l
            else:
                _l1 = _img.getObject(_l1id)
                if _l1 is None or not isinstance(_l1, Layer):
                    raise ValueError, "Dimension L1 layer missing: id=%d" % _l1id
            _p1 = _l1.getObject(_p1id)
            if _p1 is None or not isinstance(_p1, point.Point):
                raise ValueError, "Dimension P1 point missing: id=%d" % _p1id
            if _l2id == _lid:
                _l2 = _l
            else:
                _l2 = _img.getObject(_l2id)
                if _l2 is None or not isinstance(_l2, Layer):
                    raise ValueError, "Dimension L2 layer missing: id=%d" % _l2id
            _p2 = _l2.getObject(_p2id)
            if _p2 is None or not isinstance(_p2, point.Point):
                raise ValueError, "Dimension P2 point missing: id=%d" % _p2id
            _ds = self.__makeDimStyle(values)
            _dsdata = values.get('ds1')
            if _dsdata is None:
                raise ValueError, "Lost 'ds1' value for L/H/V Dimension."
            _ds1 = self.__makeDimString(_dsdata)
            _dsdata = values.get('ds2')
            if _dsdata is None:
                raise ValueError, "Lost 'ds2' value for L/H/V Dimension."
            _ds2 = self.__makeDimString(_dsdata)
            if _ds is None:
                _ds = _img.getOption('DIM_STYLE')
            if _type == 'ldim':
                _objtype = dimension.LinearDimension
            elif _type == 'hdim':
                _objtype = dimension.HorizontalDimension
            elif _type == 'vdim':
                _objtype = dimension.VerticalDimension
            else:
                raise ValueError, "Unexpected type: %s" % _type
            _obj = _objtype(_p1, _p2, _x, _y, _ds, ds1=_ds1, ds2=_ds2, id=_id)
            self.__adjustDimension(_obj, values)
        elif _type == 'rdim':
            _loc = values.get('location')
            if _loc is None:
                raise ValueError, "Lost 'location' value for RadialDimension."
            _x, _y = _loc
            _lid = values.get('layer')
            if _lid is None:
                raise ValueError, "Lost 'layer' value for RadialDimension."
            _cid = values.get('circle')
            if _cid is None:
                raise ValueError, "Lost 'circle' value for RadialDimension."
            _cl = _c = None
            _img = _l.getParent()
            if _img is None:
                raise ValueError, "Layer has no parent Image"
            if _lid == _l.getID():
                _cl = _l
            else:
                _cl = _img.getObject(_lid)
                if _cl is None or not isinstance(_cl, Layer):
                    raise ValueError, "Dimension Layer missing: id=%d" % _lid
            _c = _cl.getObject(_cid)
            if _c is None or not isinstance(_c, (circle.Circle, arc.Arc)):
                raise ValueError, "Dimension Circle/Arc missing: id=%d" % _cid
            _ds = self.__makeDimStyle(values)
            _dsdata = values.get('ds1')
            if _dsdata is None:
                raise ValueError, "Lost 'ds1' value for RadialDimension."
            _ds1 = self.__makeDimString(_dsdata)
            _dsdata = values.get('ds2')
            if _dsdata is None:
                raise ValueError, "Lost 'ds2' value for RadialDimension."
            _ds2 = self.__makeDimString(values.get('ds2'))
            _obj = dimension.RadialDimension(_c, _x, _y, _ds,
                                             ds1=_ds1, ds2=_ds2, id=_id)
            self.__adjustDimension(_obj, values)
            _mode = values.get('dia_mode')
            if _mode is None:
                raise ValueError, "Lost 'dia_mode' value for RadialDimension."
            _obj.setDiaMode(_mode)
        elif _type == 'adim':
            _x, _y = values.get('location')
            _vlid = values.get('vl')
            if _vlid is None:
                raise ValueError, "Lost 'vl' value for AngularDimension."
            _vpid = values.get('vp')
            if _vpid is None:
                raise ValueError, "Lost 'vp' value for AngularDimension."
            _l1id = values.get('l1')
            if _l1id is None:
                raise ValueError, "Lost 'l1' value for AngularDimension."
            _p1id = values.get('p1')
            if _p1id is None:
                raise ValueError, "Lost 'p1' value for AngularDimension."
            _l2id = values.get('l2')
            if _l2id is None:
                raise ValueError, "Lost 'l2' value for AngularDimension."
            _p2id = values.get('p2')
            if _p2id is None:
                raise ValueError, "Lost 'p2' value for AngularDimension."
            _vl = _vp = _l1 = _p1 = _l2 = _p2 = None
            _lid = _l.getID()
            _img = _l.getParent()
            if _img is None:
                raise ValueError, "Layer has no parent Image"
            if _vlid == _lid:
                _vl = _l
            else:
                _vl = _img.getObject(_vlid)
                if _vl is None or not isinstance(_vl, Layer):
                    raise ValueError, "Dimension vertex layer missing: id=%d" % _vlid
            _vp = _vl.getObject(_vpid)
            if _vp is None or not isinstance(_vp, point.Point):
                raise ValueError, "Dimension vertex point missing: id=%d" % _vpid
            if _l1id == _lid:
                _l1 = _l
            else:
                _l1 = _img.getObject(_l1id)
                if _l1 is None or not isinstance(_l1, Layer):
                    raise ValueError, "Dimension L1 layer missing: id=%d" % _l1id
            _p1 = _l1.getObject(_p1id)
            if _p1 is None or not isinstance(_p1, point.Point):
                raise ValueError, "Dimension P1 point missing: id=%d" % _p1id
            if _l2id == _lid:
                _l2 = _l
            else:
                _l2 = _img.getObject(_l2id)
                if _l2 is None or not isinstance(_l2, Layer):
                    raise ValueError, "Dimension L2 layer missing: id=%d" % _l2id
            _p2 = _l2.getObject(_p2id)
            if _p2 is None or not isinstance(_p2, point.Point):
                raise ValueError, "Dimension P2 point missing: id=%d" % _p2id
            _ds = self.__makeDimStyle(values)
            _dsdata = values.get('ds1')
            if _dsdata is None:
                raise ValueError, "Lost 'ds1' value for AngularDimension."
            _ds1 = self.__makeDimString(_dsdata)
            _dsdata = values.get('ds2')
            if _dsdata is None:
                raise ValueError, "Lost 'ds2' value for AngularDimension."
            _ds2 = self.__makeDimString(values.get('ds2'))
            _obj = dimension.AngularDimension(_vp, _p1, _p2, _x, _y, _ds,
                                              ds1=_ds1, ds2=_ds2, id=_id)
            self.__adjustDimension(_obj, values)
        else:
            raise TypeError, "Unexpected type: %s"  % _type
        return _obj

    def __delObject(self, undo, values):
        # print "LayerLog::__delObject() ..."
        _type = values.get('type')
        _id = values.get('id')
        # print "id: %d" % _id
        _l = self.getObject()
        _obj = _l.getObject(_id)
        if _obj is None:
            raise ValueError, "Missed object: %d, %s" % (_id, _type)
        #
        # layer still has to send messages out like 'removed_child'
        #
        if undo:
            _l.startUndo()
            try:
                _l.delObject(_obj)
            finally:
                _l.endUndo()
        else:
            _l.startRedo()
            try:
                _l.delObject(_obj)
            finally:
                _l.endRedo()