File: imageio.py

package info (click to toggle)
pythoncad 0.1.23-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,044 kB
  • ctags: 3,568
  • sloc: python: 54,867; sh: 100; makefile: 39
file content (2839 lines) | stat: -rw-r--r-- 123,212 bytes parent folder | download
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
#
# Copyright (c) 2002, 2003, 2004, 2005 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
#
#
# code for doing file saves/loads
#

import sys
import xml.dom.minidom
import os

from PythonCAD.Generic import layer
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 units
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 fileio

def _save_graph_bits(obj, node, doc, map):
    _st = obj.getStyle()
    assert _st in map, "Object %s style %s not in map!" % (str(obj), `_st`)
    _attr = doc.createAttributeNS("image", "style")
    node.setAttributeNodeNS(_attr)
    node.setAttributeNS("image", "style", `map[_st]`)
    _color = obj.getColor()
    if _color != _st.getColor():
        assert _color in map, "Object %s color not in map!" % str(obj)
        _attr = doc.createAttributeNS("image", "color")
        node.setAttributeNodeNS(_attr)
        node.setAttributeNS("image", "color", `map[_color]`)
    _linetype = obj.getLinetype()
    if _linetype != _st.getLinetype():
        assert _linetype in map, "Object %s linetype not in map!" % str(obj)
        _attr = doc.createAttributeNS("image", "linetype")
        node.setAttributeNodeNS(_attr)
        node.setAttributeNS("image", "linetype", `map[_linetype]`)
    _thickness = obj.getThickness()
    _tdiff = _thickness - _st.getThickness()
    if abs(_tdiff) > 1e-10:
        _attr = doc.createAttributeNS("image", "thickness")
        node.setAttributeNodeNS(_attr)
        node.setAttributeNS("image", "thickness", `_thickness`)

def _save_state_bits(obj, node, doc):
    if not obj.isVisible():
        _attr = doc.createAttributeNS("image", "visibility")
        node.setAttributeNodeNS(_attr)
        node.setAttributeNS("image", "visibility", "False")
    if obj.isLocked():
        _attr = doc.createAttributeNS("image", "locked")
        node.setAttributeNodeNS(_attr)
        node.setAttributeNS("image", "locked", "True")
        
def _save_leaders(lyr, node, doc, map):
    _leaders = lyr.getLayerEntities("leader")
    if len(_leaders):
        _leads = doc.createElementNS("image", "image:Leaders")
        node.appendChild(_leads)
        _i = 0
        for _lead in _leaders:
            _p1, _p2, _p3 = _lead.getPoints()
            _i1 = id(_p1)
            _i2 = id(_p2)
            _i3 = id(_p3)
            assert _i1 in map, "Leader %s p1 not in map!" % str(_lead)
            assert _i2 in map, "Leader %s p2 not in map!" % str(_lead)
            assert _i3 in map, "Leader %s p3 not in map!" % str(_lead)
            _child = doc.createElementNS("image", "image:Leader")
            _leads.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "p1")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "p1", `map[_i1]`)
            _attr = doc.createAttributeNS("image", "p2")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "p2", `map[_i2]`)
            _attr = doc.createAttributeNS("image", "p3")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "p3", `map[_i3]`)
            _attr = doc.createAttributeNS("image", "size")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "size", `_lead.getArrowSize()`)
            _save_graph_bits(_lead, _child, doc, map)
            _save_state_bits(_lead, _child, doc)
            _i = _i + 1

def _save_polylines(lyr, node, doc, map):
    _polylines = lyr.getLayerEntities("polyline")
    if len(_polylines):
        _polys = doc.createElementNS("image", "image:Polylines")
        node.appendChild(_polys)
        _i = 0
        for _polyline in _polylines:
            _pts = _polyline.getPoints()
            _mpts = []
            for _pt in _pts:
                _ip = id(_pt)
                assert _ip in map, "Polyline point not in map: " + str(_pt)
                _mpts.append(map[_ip])
            _child = doc.createElementNS("image", "image:Polyline")
            _polys.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "points")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "points", `_mpts`)
            _save_graph_bits(_polyline, _child, doc, map)
            _save_state_bits(_polyline, _child, doc)
            _i = _i + 1

def _save_textblocks(lyr, node, doc, map):
    _textblocks = lyr.getLayerEntities("text")
    if len(_textblocks):
        _tbs = doc.createElementNS("image", "image:TextBlocks")
        node.appendChild(_tbs)
        _i = 0
        for _textblock in _textblocks:
            _x, _y = _textblock.getLocation()
            _text = _textblock.getText()
            _style = _textblock.getTextStyle()
            assert _style in map, "TextBlock textstyle not in map: " + `_style`
            _child = doc.createElementNS("image", "image:TextBlock")
            _tbs.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "x")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "x", `_x`)
            _attr = doc.createAttributeNS("image", "y")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "y", `_y`)
            _attr = doc.createAttributeNS("image", "tsid")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "tsid", `map[_style]`)
            _save_state_bits(_textblock, _child, doc)
            #
            # save any override values as attributes
            #
            _tbfamily = _textblock.getFamily()
            if _tbfamily != _style.getFamily():
                assert _tbfamily in map, "Missing font family in map: " + _tbfamily
                _attr = doc.createAttributeNS("image", "fid")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "fid", `map[_tbfamily]`)
            _weight = _textblock.getWeight()
            if _weight != _style.getWeight():
                _tbweight = text.font_weight_string(_weight)
                _attr = doc.createAttributeNS("image", "weight")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "weight", _tbweight)
            _st = _textblock.getStyle()
            if _st != _style.getStyle():
                _tbstyle = text.font_style_string(_st)
                _attr = doc.createAttributeNS("image", "style")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "style", _tbstyle)
            _tbsize = _textblock.getSize()
            if _tbsize != _style.getSize():
                _attr = doc.createAttributeNS("image", "size")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "size", `_tbsize`)
            _tbcolor = _textblock.getColor()
            if _tbcolor != _style.getColor():
                assert _tbcolor in map, "Missing font color in map: " + str(_tbcolor)
                _attr = doc.createAttributeNS("image", "cid")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "cid", `map[_tbcolor]`)
            _tbangle = _textblock.getAngle()
            if abs(_tbangle - _style.getAngle()) > 1e-10:
                _attr = doc.createAttributeNS("image", "angle")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "angle", `_tbangle`)
            _tbalign = _textblock.getAlignment()
            if _tbalign != _style.getAlignment():
                _attr = doc.createAttributeNS("image", "halign")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "halign", `_tbalign`)
            #
            # the following is a workaround for the odd way
            # minidom prints text nodes and also the easiest
            # way I see right now to store multiline text ...
            #
            if sys.platform == 'mac':
                _sep = '\r'
            else:
                _sep = '\n'
            _lines = _text.split(_sep)
            for _line in _lines:
                _textline = doc.createElementNS("image", "image:TextLine")
                _child.appendChild(_textline)
                _attr = doc.createAttributeNS("image", "text")
                _textline.setAttributeNodeNS(_attr)
                _textline.setAttributeNS("image", "text", _line)
                #
                # I'd really rather not use an attribute to store
                # the text - using a TextNode would be better so
                # the file would look like the following:
                #
                # ...
                # <image:TextLine>blah blah blah</image:TextLine>
                # <image:TextLine>foo bar blah</image:TextLine>
                # ...
                #
                # minidom adds in newlines and spaces though
                # is there a known workaround or a magic call to
                # have it not do this?
                #
                # _textnode = doc.createTextNode(_line)
                # _textline.appendChild(_textnode)
            _i = _i + 1

def _save_chamfers(lyr, node, doc, map):
    _chamferlist = lyr.getLayerEntities("chamfer")
    if len(_chamferlist):
        _chs = doc.createElementNS("image", "image:Chamfers")
        node.appendChild(_chs)
        _i = 0
        for _ch in _chamferlist:
            _s1, _s2 = _ch.getSegments()
            _i1 = id(_s1)
            _i2 = id(_s2)
            assert _i1 in map, "Chamfer %s seg1 not in map!" % `_ch`
            assert _i2 in map, "Chamfer %s seg2 not in map!" % `_ch`
            _child = doc.createElementNS("image", "image:Chamfer")
            _chs.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "s1")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "s1", `map[_i1]`)
            _attr = doc.createAttributeNS("image", "s2")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "s2", `map[_i2]`)
            _attr = doc.createAttributeNS("image", "length")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "length", `_ch.getLength()`)
            _save_graph_bits(_ch, _child, doc, map)
            _save_state_bits(_ch, _child, doc)
            _i = _i + 1

def _save_fillets(lyr, node, doc, map):
    _filletlist = lyr.getLayerEntities("fillet")
    if len(_filletlist):
        _flts = doc.createElementNS("image", "image:Fillets")
        node.appendChild(_flts)
        _i = 0
        for _flt in _filletlist:
            _s1, _s2 = _flt.getSegments()
            _i1 = id(_s1)
            _i2 = id(_s2)
            assert _i1 in map, "Fillet %s seg1 not in map!" % `_flt`
            assert _i2 in map, "Fillet %s seg2 not in map!" % `_flt`
            _child = doc.createElementNS("image", "image:Fillet")
            _flts.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "_s1")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "s1", `map[_i1]`)
            _attr = doc.createAttributeNS("image", "s2")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "s2", `map[_i2]`)
            _attr = doc.createAttributeNS("image", "radius")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "radius", `_flt.getRadius()`)
            _save_graph_bits(_flt, _child, doc, map)
            _save_state_bits(_flt, _child, doc)
            _i = _i + 1

def _save_dim_override(node, doc, map, stkey, value):
    _child = doc.createElementNS("image", "image:DimOption")
    node.appendChild(_child)
    _attr = doc.createAttributeNS("image", "opt")
    _child.setAttributeNodeNS(_attr)
    _child.setAttributeNS("image", "opt", stkey)
    if (stkey == 'DIM_COLOR' or
        stkey == 'DIM_PRIMARY_FONT_COLOR' or
        stkey == 'DIM_SECONDARY_FONT_COLOR'):
        _attr = doc.createAttributeNS("image", "cid")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "cid", `map[value]`)
    elif (stkey == 'DIM_PRIMARY_FONT_FAMILY' or
          stkey == 'DIM_SECONDARY_FONT_FAMILY'):
        _attr = doc.createAttributeNS("image", "fid")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "fid", `map[value]`)
    else:
        _attr = doc.createAttributeNS("image", "val")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "val", str(value))

def _save_dim_common(dim, node, doc, map):
    _ds = dim.getDimStyle()
    _offset = dim.getOffset()
    if abs(_ds.getValue('DIM_OFFSET') - _offset) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_OFFSET', _offset)
    _ext = dim.getExtension()
    if abs(_ds.getValue('DIM_EXTENSION') - _ext) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_EXTENSION', _ext)
    _endpt = dim.getEndpointType()
    if _ds.getValue('DIM_ENDPOINT') != _endpt:
        _save_dim_override(node, doc, map, 'DIM_ENDPOINT', _endpt)
    _size = dim.getEndpointSize()
    if abs(_ds.getValue('DIM_ENDPOINT_SIZE') - _size) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_ENDPOINT_SIZE', _size)
    _color = dim.getColor()
    if _ds.getValue('DIM_COLOR') != _color:
        _save_dim_override(node, doc, map, 'DIM_COLOR', _color)
    _dual_mode = dim.getDualDimMode()
    if _ds.getValue('DIM_DUAL_MODE') != _dual_mode:
        _save_dim_override(node, doc, map, 'DIM_DUAL_MODE', _dual_mode)
    _poff = dim.getPositionOffset()
    if abs(_ds.getValue('DIM_POSITION_OFFSET') - _poff) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_POSITION_OFFSET', _poff)
    _dmo = dim.getDualModeOffset()
    if abs(_ds.getValue('DIM_DUAL_MODE_OFFSET') - _dmo) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_DUAL_MODE_OFFSET', _dmo)
    _t = dim.getThickness()
    if abs(_ds.getValue('DIM_THICKNESS') - _t) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_THICKNESS', _ext)
    #
    # primary dimension string
    #
    _pds = dim.getPrimaryDimstring()
    _prefix = _pds.getPrefix()
    if isinstance(dim, dimension.LinearDimension):
        if _ds.getValue('DIM_PRIMARY_PREFIX') != _prefix:
            _save_dim_override(node, doc, map,
                               'DIM_PRIMARY_PREFIX', _prefix)
    elif isinstance(dim, dimension.RadialDimension):
        if _ds.getValue('RADIAL_DIM_PRIMARY_PREFIX') != _prefix:
            _save_dim_override(node, doc, map,
                               'RADIAL_DIM_PRIMARY_PREFIX', _prefix)
    elif isinstance(dim, dimension.AngularDimension):
        if _ds.getValue('ANGULAR_DIM_PRIMARY_PREFIX') != _prefix:
            _save_dim_override(node, doc, map,
                               'ANGULAR_DIM_PRIMARY_PREFIX', _prefix)
    else:
        raise TypeError, "Unexpected dimension type: " + `dim`
    _suffix = _pds.getSuffix()
    if isinstance(dim, dimension.LinearDimension):
        if _ds.getValue('DIM_PRIMARY_SUFFIX') != _suffix:
            _save_dim_override(node, doc, map,
                               'DIM_PRIMARY_SUFFIX', _suffix)
    elif isinstance(dim, dimension.RadialDimension):
        if _ds.getValue('RADIAL_DIM_PRIMARY_SUFFIX') != _suffix:
            _save_dim_override(node, doc, map,
                               'RADIAL_DIM_PRIMARY_SUFFIX', _suffix)
    elif isinstance(dim, dimension.AngularDimension):
        if _ds.getValue('ANGULAR_DIM_PRIMARY_SUFFIX') != _suffix:
            _save_dim_override(node, doc, map,
                               'ANGULAR_DIM_PRIMARY_SUFFIX', _prefix)
    else:
        raise TypeError, "Unexpected dimension type: " + `dim`
    _unit = _pds.getUnits()
    if _ds.getValue('DIM_PRIMARY_UNITS') != _unit:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_UNITS', _unit)
    _precision = _pds.getPrecision()
    if _ds.getValue('DIM_PRIMARY_PRECISION') != _precision:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_PRECISION',
                           _precision)
    _print_zero = _pds.getPrintZero()
    if _ds.getValue('DIM_PRIMARY_LEADING_ZERO') != _print_zero:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_LEADING_ZERO',
                           _print_zero)
    _print_dec = _pds.getPrintDecimal()
    if _ds.getValue('DIM_PRIMARY_TRAILING_DECIMAL') != _print_dec:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_TRAILING_DECIMAL',
                           _print_dec)
    _family = _pds.getFamily()
    if _ds.getValue('DIM_PRIMARY_FONT_FAMILY') != _family:
        assert _family in map, "Font family %s missing in map" % _family
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_FONT_FAMILY', _family)
    _size = _pds.getSize()
    if _ds.getValue('DIM_PRIMARY_FONT_SIZE') != _size:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_FONT_SIZE', _size)
    _font_style = _pds.getStyle()
    if _ds.getValue('DIM_PRIMARY_FONT_STYLE') != _font_style:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_FONT_STYLE',
                           _font_style)
    _weight = _pds.getWeight()
    if _ds.getValue('DIM_PRIMARY_FONT_WEIGHT') != _weight:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_FONT_WEIGHT', _weight)
    _font_color = _pds.getColor()
    if _ds.getValue('DIM_PRIMARY_FONT_COLOR') != _font_color:
        assert _font_color in map, "Font color missing in map: " + `_font_color`
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_FONT_COLOR',
                           _font_color)
    _size = _pds.getSize()
    if abs(_ds.getValue('DIM_PRIMARY_TEXT_SIZE') - _size) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_TEXT_SIZE', _size)
    _angle = _pds.getAngle()
    if abs(_ds.getValue('DIM_PRIMARY_TEXT_ANGLE') - _angle) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_TEXT_ANGLE', _angle)
    _align = _pds.getAlignment()
    if _ds.getValue('DIM_PRIMARY_TEXT_ALIGNMENT') != _align:
        _save_dim_override(node, doc, map, 'DIM_PRIMARY_TEXT_ALIGN', _align)
    #
    # secondary dimension string
    #
    _sds = dim.getSecondaryDimstring()
    _prefix = _sds.getPrefix()
    if isinstance(dim, dimension.LinearDimension):
        if _ds.getValue('DIM_SECONDARY_PREFIX') != _prefix:
            _save_dim_override(node, doc, map,
                               'DIM_SECONDARY_PREFIX', _prefix)
    elif isinstance(dim, dimension.RadialDimension):
        if _ds.getValue('RADIAL_DIM_SECONDARY_PREFIX') != _prefix:
            _save_dim_override(node, doc, map,
                               'RADIAL_DIM_SECONDARY_PREFIX', _prefix)
    elif isinstance(dim, dimension.AngularDimension):
        if _ds.getValue('ANGULAR_DIM_SECONDARY_PREFIX') != _prefix:
            _save_dim_override(node, doc, map,
                               'ANGULAR_DIM_SECONDARY_PREFIX', _prefix)
    else:
        raise TypeError, "Unexpected dimension type: " + `dim`
    _suffix = _sds.getSuffix()
    if isinstance(dim, dimension.LinearDimension):
        if _ds.getValue('DIM_SECONDARY_SUFFIX') != _suffix:
            _save_dim_override(node, doc, map,
                               'DIM_SECONDARY_SUFFIX', _suffix)
    elif isinstance(dim, dimension.RadialDimension):
        if _ds.getValue('RADIAL_DIM_SECONDARY_SUFFIX') != _suffix:
            _save_dim_override(node, doc, map,
                               'RADIAL_DIM_SECONDARY_SUFFIX', _suffix)
    elif isinstance(dim, dimension.AngularDimension):
        if _ds.getValue('ANGULAR_DIM_SECONDARY_SUFFIX') != _suffix:
            _save_dim_override(node, doc, map,
                               'ANGULAR_DIM_SECONDARY_SUFFIX', _prefix)
    else:
        raise TypeError, "Unexpected dimension type: " + `dim`
    _unit = _sds.getUnits()
    if _ds.getValue('DIM_SECONDARY_UNITS') != _unit:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_UNITS', _unit)
    _precision = _sds.getPrecision()
    if _ds.getValue('DIM_SECONDARY_PRECISION') != _precision:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_PRECISION',
                           _precision)
    _print_zero = _sds.getPrintZero()
    if _ds.getValue('DIM_SECONDARY_LEADING_ZERO') != _print_zero:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_LEADING_ZERO',
                           _print_zero)
    _print_dec = _sds.getPrintDecimal()
    if _ds.getValue('DIM_SECONDARY_TRAILING_DECIMAL') != _print_dec:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_TRAILING_DECIMAL',
                           _print_dec)
    _family = _sds.getFamily()
    if _ds.getValue('DIM_SECONDARY_FONT_FAMILY') != _family:
        assert _family in map, "Font family %s missing in map" % _family
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_FONT_FAMILY', _family)
    _size = _sds.getSize()
    if _ds.getValue('DIM_SECONDARY_FONT_SIZE') != _size:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_FONT_SIZE', _size)
    _font_style = _sds.getStyle()
    if _ds.getValue('DIM_SECONDARY_FONT_STYLE') != _font_style:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_FONT_STYLE',
                           _font_style)
    _weight = _sds.getWeight()
    if _ds.getValue('DIM_SECONDARY_FONT_WEIGHT') != _weight:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_FONT_WEIGHT', _weight)
    _font_color = _sds.getColor()
    if _ds.getValue('DIM_SECONDARY_FONT_COLOR') != _font_color:
        assert _font_color in map, "Font color missing in map: " + `_font_color`
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_FONT_COLOR',
                           _font_color)
    _size = _pds.getSize()
    if abs(_ds.getValue('DIM_SECONDARY_TEXT_SIZE') - _size) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_TEXT_SIZE', _size)
    _angle = _pds.getAngle()
    if abs(_ds.getValue('DIM_SECONDARY_TEXT_ANGLE') - _angle) > 1e-10:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_TEXT_ANGLE', _angle)
    _align = _pds.getAlignment()
    if _ds.getValue('DIM_SECONDARY_TEXT_ALIGNMENT') != _align:
        _save_dim_override(node, doc, map, 'DIM_SECONDARY_TEXT_ALIGN', _align)

def _save_linear_dims(l, node, doc, map, entmap, dimtype):
    if dimtype == 'linear_dimension':
        _tag = 'image:LDims'
    elif dimtype == 'horizontal_dimension':
        _tag = 'image:HDims'
    elif dimtype == 'vertical_dimension':
        _tag = 'image:VDims'
    else:
        raise ValueError, "Unexpected linear dimension type: " + `dimtype`
    _ldimlist = l.getLayerEntities(dimtype)
    if len(_ldimlist):
        _item_tag = _tag[:-1]
        _block = doc.createElementNS("image", _tag)
        node.appendChild(_block)
        _i = 0
        for _dim in _ldimlist:
            _p1, _p2 = _dim.getDimPoints()
            _l1, _l2 = _dim.getDimLayers()
            _x, _y = _dim.getLocation()
            _ds = _dim.getDimStyle()
            _child = doc.createElementNS("image", _item_tag)
            _block.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "l1")
            _child.setAttributeNodeNS(_attr)
            _il = id(_l1)
            if _il in map:
                _child.setAttributeNS("image", "l1", `map[_il]`)
            else:
                entmap.saveEntity(_child, 'l1', _l1)
            _attr = doc.createAttributeNS("image", "p1")
            _child.setAttributeNodeNS(_attr)
            _ip = id(_p1)
            if _ip in map:
                _child.setAttributeNS("image", "p1", `map[_ip]`)
            else:
                entmap.saveEntity(_child, 'p1', _p1)
            _attr = doc.createAttributeNS("image", "l2")
            _child.setAttributeNodeNS(_attr)
            _il = id(_l2)
            if _il in map:
                _child.setAttributeNS("image", "l2", `map[_il]`)
            else:
                entmap.saveEntity(_child, 'l2', _l2)
            _attr = doc.createAttributeNS("image", "p2")
            _child.setAttributeNodeNS(_attr)
            _ip = id(_p2)
            if _ip in map:
                _child.setAttributeNS("image", "p2", `map[_ip]`)
            else:
                entmap.saveEntity(_child, 'p2', _p2)
            _attr = doc.createAttributeNS("image", "x")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "x", `_x`)
            _attr = doc.createAttributeNS("image", "y")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "y", `_y`)
            _attr = doc.createAttributeNS("image", "ds")
            assert _ds in map, "Missing DimStyle in map: " + `_ds`
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "ds", `map[_ds]`)
            _save_dim_common(_dim, _child, doc, map)
            _save_state_bits(_dim, _child, doc)

def _save_radial_dims(lyr, node, doc, map, entmap):
    _rdimlist = lyr.getLayerEntities("radial_dimension")
    if len(_rdimlist):
        _rdims = doc.createElementNS("image", "image:RDims")
        node.appendChild(_rdims)
        _i = 0
        for _rdim in _rdimlist:
            _dl = _rdim.getDimLayer()
            _dc = _rdim.getDimCircle()
            _x, _y = _rdim.getLocation()
            _ds = _rdim.getDimStyle()
            _child = doc.createElementNS("image", "image:RDim")
            _rdims.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "l")
            _child.setAttributeNodeNS(_attr)
            _il = id(_dl)
            if _il in map:
                _child.setAttributeNS("image", "l", `map[_il]`)
            else:
                entmap.saveEntity(_child, 'l', _dl)
            _attr = doc.createAttributeNS("image", "c")
            _child.setAttributeNodeNS(_attr)
            _ic = id(_dc)
            if _ic in map:
                _child.setAttributeNS("image", "c", `map[_ic]`)
            else:
                entmap.saveEntity(_child, 'c', _dc)
            _attr = doc.createAttributeNS("image", "x")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "x", `_x`)
            _attr = doc.createAttributeNS("image", "y")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "y", `_y`)
            _attr = doc.createAttributeNS("image", "ds")
            assert _ds in map, "Missing DimStyle in map: " + `_ds`
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "ds", `map[_ds]`)
            _save_dim_common(_rdim, _child, doc, map)
            _save_state_bits(_rdim, _child, doc)
            _dia_mode = _rdim.getDiaMode()
            if _ds.getValue('RADIAL_DIM_DIA_MODE') != _dia_mode:
                _opt = doc.createElementNS("image", "image:DimOption")
                _child.appendChild(_opt)
                _attr = doc.createAttributeNS("image", "opt")
                _opt.setAttributeNodeNS(_attr)
                _opt.setAttributeNS("image", "opt", 'RADIAL_DIM_DIA_MODE')
                _attr = doc.createAttributeNS("image", "val")
                _opt.setAttributeNodeNS(_attr)
                _opt.setAttributeNS("image", "val", `_dia_mode`)

def _save_angular_dims(lyr, node, doc, map, entmap):
    _adimlist = lyr.getLayerEntities("angular_dimension")
    if len(_adimlist):
        _adims = doc.createElementNS("image", "image:ADims")
        node.appendChild(_adims)
        _i = 0
        for _adim in _adimlist:
            _p1, _p2, _p3 = _adim.getDimPoints()
            _l1, _l2, _l3 = _adim.getDimLayers()
            _x, _y = _adim.getLocation()
            _ds = _adim.getDimStyle()
            _child = doc.createElementNS("image", "image:ADim")
            _adims.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "l1")
            _child.setAttributeNodeNS(_attr)
            _il = id(_l1)
            if _il in map:
                _child.setAttributeNS("image", "l1", `map[_il]`)
            else:
                entmap.saveEntity(_child, 'l1', _l1)
            _attr = doc.createAttributeNS("image", "p1")
            _child.setAttributeNodeNS(_attr)
            _ip = id(_p1)
            if _ip in map:
                _child.setAttributeNS("image", "p1", `map[_ip]`)
            else:
                entmap.saveEntity(_child, 'p1', _p1)
            _attr = doc.createAttributeNS("image", "l2")
            _child.setAttributeNodeNS(_attr)
            _il = id(_l2)
            if _il in map:
                _child.setAttributeNS("image", "l2", `map[_il]`)
            else:
                entmap.saveEntity(_child, 'l2', _l2)
            _attr = doc.createAttributeNS("image", "p2")
            _child.setAttributeNodeNS(_attr)
            _ip = id(_p2)
            if _ip in map:
                _child.setAttributeNS("image", "p2", `map[_ip]`)
            else:
                entmap.saveEntity(_child, 'p2', _p2)
            _attr = doc.createAttributeNS("image", "l3")
            _child.setAttributeNodeNS(_attr)
            _il = id(_l3)
            if _il in map:
                _child.setAttributeNS("image", "l3", `map[_il]`)
            else:
                entmap.saveEntity(_child, 'l3', _l3)
            _attr = doc.createAttributeNS("image", "p3")
            _child.setAttributeNodeNS(_attr)
            _ip = id(_p3)
            if _ip in map:
                _child.setAttributeNS("image", "p3", `map[_ip]`)
            else:
                entmap.saveEntity(_child, 'p3', _p3)
            _attr = doc.createAttributeNS("image", "x")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "x", `_x`)
            _attr = doc.createAttributeNS("image", "y")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "y", `_y`)
            _attr = doc.createAttributeNS("image", "ds")
            assert _ds in map, "Missing DimStyle in map: " + `_ds`
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "ds", `map[_ds]`)
            _save_dim_common(_adim, _child, doc, map)
            _save_state_bits(_adim, _child, doc)

def _save_ccircles(lyr, node, doc, map):
    _cclist = lyr.getLayerEntities("ccircle")
    if len(_cclist):
        _ccircs = doc.createElementNS("image", "image:CCircles")
        node.appendChild(_ccircs)
        _i = 0
        for _cc in _cclist:
            _cp = _cc.getCenter()
            _ic = id(_cp)
            assert _ic in map, "CCircle %s center not in map!" % str(_cc)
            _child = doc.createElementNS("image", "image:CCircle")
            _ccircs.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "cp")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "cp", `map[_ic]`)
            _attr = doc.createAttributeNS("image", "r")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "r", `_cc.getRadius()`)
            _save_state_bits(_cc, _child, doc)
            _i = _i + 1

def _save_clines(lyr, node, doc, map):
    _cllist = lyr.getLayerEntities("cline")
    if len(_cllist):
        _clines = doc.createElementNS("image", "image:CLines")
        node.appendChild(_clines)
        _i = 0
        for _cl in _cllist:
            _p1, _p2 = _cl.getKeypoints()
            _i1 = id(_p1)
            _i2 = id(_p2)
            assert _i1 in map, "CLine %s p1 not in map!" % str(_cl)
            assert _i2 in map, "CLine %s p2 not in map!" % str(_cl)
            _child = doc.createElementNS("image", "image:CLine")
            _clines.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "p1")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "p1", `map[_i1]`)
            _attr = doc.createAttributeNS("image", "p2")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "p2", `map[_i2]`)
            _save_state_bits(_cl, _child, doc)
            _i = _i + 1

def _save_aclines(lyr, node, doc, map):
    _acllist = lyr.getLayerEntities("acline")
    if len(_acllist):
        _acls = doc.createElementNS("image", "image:ACLines")
        node.appendChild(_acls)
        _i = 0
        for _acl in _acllist:
            _loc = _acl.getLocation()
            _il = id(_loc)
            assert _il in map, "ACline %s point not in map!" % str(_acl)
            _child = doc.createElementNS("image", "image:ACLine")
            _acls.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "angle")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "angle", `_acl.getAngle()`)
            _attr = doc.createAttributeNS("image", "location")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "location", `map[_il]`)
            _save_state_bits(_acl, _child, doc)
            _i = _i + 1

def _save_vclines(lyr, node, doc, map):
    _vcllist = lyr.getLayerEntities("vcline")
    if len(_vcllist):
        _vcls = doc.createElementNS("image", "image:VCLines")
        node.appendChild(_vcls)
        _i = 0
        for _vcl in _vcllist:
            _loc = _vcl.getLocation()
            _il = id(_loc)
            assert _il in map, "VCline %s point not in map!" % str(_vcl)
            _child = doc.createElementNS("image", "image:VCLine")
            _vcls.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "location")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "location", `map[_il]`)
            _save_state_bits(_vcl, _child, doc)
            _i = _i + 1

def _save_hclines(lyr, node, doc, map):
    _hcllist = lyr.getLayerEntities("hcline")
    if len(_hcllist):
        _hcls = doc.createElementNS("image", "image:HCLines")
        node.appendChild(_hcls)
        _i = 0
        for _hcl in _hcllist:
            _loc = _hcl.getLocation()
            _il = id(_loc)
            assert _il in map, "HCline %s point not in map!" % str(_hcl)
            _child = doc.createElementNS("image", "image:HCLine")
            _hcls.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "location")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "location", `map[_il]`)
            _save_state_bits(_hcl, _child, doc)
            _i = _i + 1

def _save_arcs(lyr, node, doc, map):
    _arclist = lyr.getLayerEntities("arc")
    if len(_arclist):
        _arcs = doc.createElementNS("image", "image:Arcs")
        node.appendChild(_arcs)
        _i = 0
        for _arc in _arclist:
            _cp = _arc.getCenter()
            _ic = id(_cp)
            assert _ic in map, "Arc %s center not in map!" % str(_arc)
            _child = doc.createElementNS("image", "image:Arc")
            _arcs.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "cp")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "cp", `map[_ic]`)
            _attr = doc.createAttributeNS("image", "r")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "r", `_arc.getRadius()`)
            _attr = doc.createAttributeNS("image", "sa")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "sa", `_arc.getStartAngle()`)
            _attr = doc.createAttributeNS("image", "ea")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "ea", `_arc.getEndAngle()`)
            _save_graph_bits(_arc, _child, doc, map)
            _save_state_bits(_arc, _child, doc)
            if _arc.hasUsers():
                map[id(_arc)] = _i
            _i = _i + 1

def _save_circles(lyr, node, doc, map):
    _circles = lyr.getLayerEntities("circle")
    if len(_circles):
        _circs = doc.createElementNS("image", "image:Circles")
        node.appendChild(_circs)
        _i = 0
        for _circle in _circles:
            _cp = _circle.getCenter()
            _ic = id(_cp)
            assert _ic in map, "Circle %s center not in map!" % str(_circle)
            _child = doc.createElementNS("image", "image:Circle")
            _circs.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "cp")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "cp", `map[_ic]`)
            _attr = doc.createAttributeNS("image", "r")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "r", `_circle.getRadius()`)
            _save_graph_bits(_circle, _child, doc, map)
            _save_state_bits(_circle, _child, doc)
            if _circle.hasUsers():
                map[id(_circle)] = _i
            _i = _i + 1

def _save_segments(lyr, node, doc, map):
    _segments = lyr.getLayerEntities("segment")
    if len(_segments):
        _segs = doc.createElementNS("image", "image:Segments")
        node.appendChild(_segs)
        _i = 0
        for _seg in _segments:
            _p1, _p2 = _seg.getEndpoints()
            _i1 = id(_p1)
            _i2 = id(_p2)
            assert _i1 in map, "Segment %s p1 not in map!" % str(_seg)
            assert _i2 in map, "Segment %s p2 not in map!" % str(_seg)
            _child = doc.createElementNS("image", "image:Segment")
            _segs.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "p1")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "p1", `map[_i1]`)
            _attr = doc.createAttributeNS("image", "p2")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "p2", `map[_i2]`)
            _save_graph_bits(_seg, _child, doc, map)
            _save_state_bits(_seg, _child, doc)
            if _seg.hasUsers():
                map[id(_seg)] = _i
            _i = _i + 1

def _save_points(lyr, node, doc, map):
    _points = lyr.getLayerEntities("point")
    if len(_points):
        _pts = doc.createElementNS("image", "image:Points")
        node.appendChild(_pts)
        _i = 0
        for _pt in _points:
            _x, _y = _pt.getCoords()
            _child = doc.createElementNS("image", "image:Point")
            _pts.appendChild(_child)
            _attr = doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = doc.createAttributeNS("image", "x")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "x", `_x`)
            _attr = doc.createAttributeNS("image", "y")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "y", `_y`)
            _save_state_bits(_pt, _child, doc)
            if _pt.hasUsers():
                map[id(_pt)] = _i
            _i = _i + 1

def _make_xml_doc():
    _newdoc = xml.dom.minidom.Document()
    _root = _newdoc.createElementNS("image", "image:Image")
    _newdoc.appendChild(_root)
    _attr = _newdoc.createAttributeNS("xmlns", "xmlns:image")
    _root.setAttributeNodeNS(_attr)
    _root.setAttributeNS("xmlns", "xmlns:image", "http://www.pythoncad.org")
    _attr = _newdoc.createAttributeNS("xmlns", "xmlns:xsi")
    _root.setAttributeNodeNS(_attr)
    _root.setAttributeNS("xmlns", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    _attr = _newdoc.createAttributeNS("xsi", "xsi:schemaLocation")
    _root.setAttributeNodeNS(_attr)
    _root.setAttributeNS("xmlns", "xsi:schemaLocation", "http://www.pythoncad.org")
    return _newdoc

def _save_dimstyles(attmap, node, doc, map):
    _dimstyles = doc.createElementNS("image", "image:DimStyles")
    node.appendChild(_dimstyles)
    _dslist = attmap['dimstyles']
    _i = 0
    for _ds in _dslist:
        _child = doc.createElementNS("image", "image:DimStyle")
        _dimstyles.appendChild(_child)
        _attr = doc.createAttributeNS("image", "id")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "id", `_i`)
        _attr = doc.createAttributeNS("image", "name")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "name", _ds.getName())
        _stkeys = _ds.getOptions()
        _stkeys.sort()
        for _stkey in _stkeys:
            _value = _ds.getValue(_stkey)
            _dsfield = doc.createElementNS("image", "image:DimOption")
            _child.appendChild(_dsfield)
            _attr = doc.createAttributeNS("image", "opt")
            _dsfield.setAttributeNodeNS(_attr)
            _dsfield.setAttributeNS("image", "opt", _stkey)
            if (_stkey == 'DIM_COLOR' or
                _stkey == 'DIM_PRIMARY_FONT_COLOR' or
                _stkey == 'DIM_SECONDARY_FONT_COLOR'):
                assert _value in map, "color %s not found in map!" % str(_value)
                _attr = doc.createAttributeNS("image", "cid")
                _dsfield.setAttributeNodeNS(_attr)
                _dsfield.setAttributeNS("image", "cid", `map[_value]`)
            elif (_stkey == 'DIM_PRIMARY_FONT_FAMILY' or
                  _stkey == 'DIM_SECONDARY_FONT_FAMILY'):
                assert _value in map, "font %s not found in map!" % str(_value)
                _attr = doc.createAttributeNS("image", "fid")
                _dsfield.setAttributeNodeNS(_attr)
                _dsfield.setAttributeNS("image", "fid", `map[_value]`)
            elif (_stkey == 'DIM_PRIMARY_PREFIX' or
                  _stkey == 'DIM_PRIMARY_SUFFIX' or
                  _stkey == 'DIM_SECONDARY_PREFIX' or
                  _stkey == 'DIM_SECONDARY_SUFFIX' or
                  _stkey == 'RADIAL_DIM_PRIMARY_PREFIX' or
                  _stkey == 'RADIAL_DIM_PRIMARY_SUFFIX' or
                  _stkey == 'RADIAL_DIM_SECONDARY_PREFIX' or
                  _stkey == 'RADIAL_DIM_SECONDARY_SUFFIX' or
                  _stkey == 'ANGULAR_DIM_PRIMARY_PREFIX' or
                  _stkey == 'ANGULAR_DIM_PRIMARY_SUFFIX' or
                  _stkey == 'ANGULAR_DIM_SECONDARY_PREFIX' or
                  _stkey == 'ANGULAR_DIM_SECONDARY_SUFFIX'):
                _attr = doc.createAttributeNS("image", "val")
                _dsfield.setAttributeNodeNS(_attr)
                _dsfield.setAttributeNS("image", "val", str(_value))
            else:
                _attr = doc.createAttributeNS("image", "val")
                _dsfield.setAttributeNodeNS(_attr)
                _dsfield.setAttributeNS("image", "val", `_value`)
        map[_ds] = _i
        _i = _i + 1

def _save_textstyles(attmap, node, doc, map):
    _textstyles = doc.createElementNS("image", "image:TextStyles")
    node.appendChild(_textstyles)
    _tslist = attmap['textstyles']
    _i = 0
    for _ts in _tslist:
        _child = doc.createElementNS("image", "image:TextStyle")
        _textstyles.appendChild(_child)
        _attr = doc.createAttributeNS("image", "id")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "id", `_i`)
        _attr = doc.createAttributeNS("image", "name")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "name", _ts.getName())
        _family = _ts.getFamily()
        assert _family in map, "Missing font family in map: " + _family
        _attr = doc.createAttributeNS("image", "fid")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "fid", `map[_family]`)
        _weight = text.font_weight_string(_ts.getWeight())
        _attr = doc.createAttributeNS("image", "weight")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "weight", _weight)
        _style = text.font_style_string(_ts.getStyle())
        _attr = doc.createAttributeNS("image", "style")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "style", _style)
        _size = _ts.getSize()
        _attr = doc.createAttributeNS("image", "size")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "size", `_size`)
        _angle = _ts.getAngle()
        _attr = doc.createAttributeNS("image", "angle")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "angle", `_angle`)
        _align = _ts.getAlignment()
        _attr = doc.createAttributeNS("image", "halign")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "halign", `_align`)
        _color = _ts.getColor()
        assert _color in map, "Missing font color in map: " + str(_color)
        _attr = doc.createAttributeNS("image", "cid")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "cid", `map[_color]`)
        map[_ts] = _i
        _i = _i + 1

def _save_font_families(attmap, node, doc, map):
    _families = doc.createElementNS("image", "image:FontFamilies")
    node.appendChild(_families)
    _i = 0
    _fontlist = attmap['fonts']
    for _font in _fontlist:
        _child = doc.createElementNS("image", "image:FontFamily")
        _families.appendChild(_child)
        _attr = doc.createAttributeNS("image", "id")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "id", `_i`)
        _attr = doc.createAttributeNS("image", "name")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "name", _font)
        map[_font] = _i
        _i = _i + 1

def _save_styles(attmap, node, doc, map):
    _styles = doc.createElementNS("image", "image:Styles")
    node.appendChild(_styles)
    _i = 0
    _stylelist = attmap['styles']
    for _style in _stylelist:
        _child = doc.createElementNS("image", "image:Style")
        _styles.appendChild(_child)
        _attr = doc.createAttributeNS("image", "id")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "id", `_i`)
        _attr = doc.createAttributeNS("image", "name")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "name", _style.getName())
        _color = _style.getColor()
        assert _color in map, "Color not found in map!"
        _attr = doc.createAttributeNS("image", "cid")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "cid", `map[_color]`)
        _linetype = _style.getLinetype()
        assert _linetype in map, "Linetype not found in map!"
        _attr = doc.createAttributeNS("image", "ltid")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "ltid", `map[_linetype]`)
        _attr = doc.createAttributeNS("image", "thickness")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "thickness", `_style.getThickness()`)
        map[_style] = _i
        _i = _i + 1

def _save_colors(attmap, node, doc, map):
    _colors = doc.createElementNS("image", "image:Colors")
    node.appendChild(_colors)
    _i = 0
    _colorlist = attmap['colors']
    for _color in _colorlist:
        _r, _g, _b = _color.getColors()
        _child = doc.createElementNS("image", "image:Color")
        _colors.appendChild(_child)
        _attr = doc.createAttributeNS("image", "id")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "id", `_i`)
        _attr = doc.createAttributeNS("image", "r")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "r", `_r`)
        _attr = doc.createAttributeNS("image", "g")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "g", `_g`)
        _attr = doc.createAttributeNS("image", "b")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "b", `_b`)
        #
        # save color as hex string as well
        #
        # It may be better to save colors like ...
        #
        # <image:Color>#ffffff</image:Color>
        #
        _attr = doc.createAttributeNS("image", "color")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "color", str(_color))
        map[_color] = _i
        _i = _i + 1

def _save_linetypes(attmap, node, doc, map):
    _linetypes = doc.createElementNS("image", "image:Linetypes")
    node.appendChild(_linetypes)
    _i = 0
    _linetypelist = attmap['linetypes']
    for _linetype in _linetypelist:
        _child = doc.createElementNS("image", "image:Linetype")
        _linetypes.appendChild(_child)
        _attr = doc.createAttributeNS("image", "id")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "id", `_i`)
        _attr = doc.createAttributeNS("image", "name")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "name", _linetype.getName())
        _attr = doc.createAttributeNS("image", "pattern")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "pattern", `_linetype.getList()`)
        map[_linetype] = _i
        _i = _i + 1

def _save_units(image, node, doc):
    _uts = doc.createElementNS("image", "image:Units")
    node.appendChild(_uts)
    _attr = doc.createAttributeNS("image", "unit")
    _uts.setAttributeNodeNS(_attr)
    _ustr = units.unit_string(image.getUnits())
    _uts.setAttributeNS("image", "unit", _ustr)

def _save_globals(attmap, node, doc, map):
#
# for now, assumes globals are ONLY colors
#
    _globals = doc.createElementNS("image", "image:Globals")
    node.appendChild(_globals)
    _globaldict = attmap['globals']
    _keylist = _globaldict.keys()
    for _key in _keylist:
        _child = doc.createElementNS("image", "image:Global")
        _globals.appendChild(_child)
        _attr = doc.createAttributeNS("image", "key")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "key", _key)
        _val = _globaldict[_key]
        assert _val in map, "Color not found in map!"
        _attr = doc.createAttributeNS("image", "cid")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("image", "cid", `map[_val]`)



_entlist = [
    'segment',
    'circle',
    'arc',
    'chamfer',
    'fillet',
    'leader',
    'polyline',
    ]

_conobjs = [
    'hcline',
    'vcline',
    'acline',
    'cline',
    'ccircle',
    ]

_dimlist = [
    'linear_dimension',
    'horizontal_dimension',
    'vertical_dimension',
    'radial_dimension',
    'angular_dimension',
    ]

def _get_image_attributes(image):
    _attmap = {}
    _styles = []
    _attmap['styles'] = _styles
    _colors = []
    _attmap['colors'] = _colors
    _linetypes = []
    _attmap['linetypes'] = _linetypes
    _fonts = []
    _attmap['fonts'] = _fonts
    _dimstyles = []
    _attmap['dimstyles'] = _dimstyles
    _textstyles = []
    _attmap['textstyles'] = _textstyles
    _globals = {}
    _attmap['globals'] = _globals
    #
    # Get global properties
    #
    for _key in ['BACKGROUND_COLOR', 'INACTIVE_LAYER_COLOR', 'SINGLE_POINT_COLOR', 'MULTI_POINT_COLOR']:
        _val = _globals[_key] = image.getOption(_key)
        if _val not in _colors:
            _colors.append(_val)
    _check_conobjs = True
    _layers = [image.getTopLayer()]
    while len(_layers):
        _layer = _layers.pop()
        for _ent in _entlist:
            for _obj in _layer.getLayerEntities(_ent):
                #
                # get style properties
                #
                _style = _obj.getStyle()
                if _style not in _styles:
                    _styles.append(_style)
                _color = _style.getColor() # style color
                if _color not in _colors:
                    _colors.append(_color)
                _linetype = _style.getLinetype() # style linetype
                if _linetype not in _linetypes:
                    _linetypes.append(_linetype)
                #
                # object may have overridden style values
                #
                _color = _obj.getColor()
                if _color not in _colors:
                    _colors.append(_color)
                _linetype = _obj.getLinetype()
                if _linetype not in _linetypes:
                    _linetypes.append(_linetype)
        if _check_conobjs:
            for _conobj in _conobjs:
                for _obj in _layer.getLayerEntities(_conobj):
                    _check_conobjs = False
                    _style = _obj.getStyle()
                    if _style not in _styles:
                        _styles.append(_style)
                    _color = _style.getColor()
                    if _color not in _colors:
                        _colors.append(_color)
                    _linetype = _style.getLinetype()
                    if _linetype not in _linetypes:
                        _linetypes.append(_linetype)
                    break
                if not _check_conobjs:
                    break
        for _dim in _dimlist:
            for _obj in _layer.getLayerEntities(_dim):
                #
                # get style properties
                #
                _ds = _obj.getDimStyle()
                if _ds not in _dimstyles:
                    _dimstyles.append(_ds)
                _color = _ds.getOption('DIM_COLOR')
                if _color not in _colors:
                    _colors.append(_color)
                _color = _ds.getOption('DIM_PRIMARY_FONT_COLOR')
                if _color not in _colors:
                    _colors.append(_color)
                _color = _ds.getOption('DIM_SECONDARY_FONT_COLOR')
                if _color not in _colors:
                    _colors.append(_color)
                _font = _ds.getOption('DIM_PRIMARY_FONT_FAMILY')
                if _font not in _fonts:
                    _fonts.append(_font)
                _font = _ds.getOption('DIM_SECONDARY_FONT_FAMILY')
                if _font not in _fonts:
                    _fonts.append(_font)
                #
                # object may have overridden style values
                #
                _color = _obj.getColor()
                if _color not in _colors: # dimension bar colors
                    _colors.append(_color)
                _pds = _obj.getPrimaryDimstring()
                _color = _pds.getColor() # primary dim text color
                if _color not in _colors:
                    _colors.append(_color)
                _font = _pds.getFamily() # primary dim font
                if _font not in _fonts:
                    _fonts.append(_font)
                _sds = _obj.getSecondaryDimstring()
                _color = _sds.getColor() # secondary dim color
                if _color not in _colors:
                    _colors.append(_color)
                _font = _sds.getFamily() # secondary dim font
                if _font not in _fonts:
                    _fonts.append(_font)
        for _textblock in _layer.getLayerEntities("text"):
            _style = _textblock.getTextStyle()
            if _style not in _textstyles:
                _textstyles.append(_style)
            _font = _style.getFamily()
            if _font not in _fonts:
                _fonts.append(_font)
            _color = _style.getColor()
            if _color not in _colors:
                _colors.append(_color)
            #
            # objects may have overridden the styles
            #
            _font = _textblock.getFamily()
            if _font not in _fonts:
                _fonts.append(_font)
            _color = _textblock.getColor()
            if _color not in _colors:
                _colors.append(_color)
        if _layer.hasSublayers():
            _layers.extend(_layer.getSublayers())
    _colors.sort()
    _styles.sort()
    _linetypes.sort()
    _fonts.sort()
    _dimstyles.sort()
    return _attmap

class EntityMap(object):
    def __init__(self):
        self.__elements = {}
        self.__entities = {}

    def __nonzero__(self):
        return len(self.__elements) != 0

    def saveEntity(self, elem, key, entity):
        _eid = id(elem)
        if _eid not in self.__elements:
            self.__elements[_eid] = elem
        if _eid not in self.__entities:
            self.__entities[_eid] = {}
        _edict = self.__entities[_eid]
        if key in _edict:
            raise KeyError, "Key '%s' already stored for element" % key
        _edict[key] = entity

    def getElements(self):
        return self.__elements.values()

    def getElementKeys(self, element):
        _eid = id(element)
        return self.__entities[_eid].keys()

    def getEntity(self, element, key):
        _eid = id(element)
        return self.__entities[_eid][key]

def save_image(image, filehandle):
    """Save a file.

save_image(image, filehandle)

Argument 'image' should be an Image instance, and 'filehandle'
should be an opened file, pipe, or writeable object.
    """
    _map = {}
    _entmap = EntityMap()
    _doc = xml.dom.minidom.Document()
    try:
        _child = _doc.createElementNS("image", "image:Image")
        _doc.appendChild(_child)
        _attr = _doc.createAttributeNS("xmlns", "xmlns:image")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("xmlns",
                              "xmlns:image",
                              "http://www.pythoncad.org")
        _attr = _doc.createAttributeNS("xmlns", "xmlns:xsi")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("xmlns",
                              "xmlns:xsi",
                              "http://www.w3.org/2001/XMLSchema-instance")
        _attr = _doc.createAttributeNS("xsi", "xsi:schemaLocation")
        _child.setAttributeNodeNS(_attr)
        _child.setAttributeNS("xmlns",
                              "xsi:schemaLocation",
                              "http://www.pythoncad.org")
        _root = _doc.documentElement
        #
        # to only save colors, linetypes, etc. actually used
        # in the drawing, the get_image_attributes() scans
        # the image and stores them and returns them in a
        # dictionary, then the image entities are written out.
        # this approach requires two passes over the image, and
        # it would be nice if these two passes could be combined
        # into a single pass.
        #
        _attmap = _get_image_attributes(image)
        _save_colors(_attmap, _root, _doc, _map)
        _save_linetypes(_attmap, _root, _doc, _map)
        _save_styles(_attmap, _root, _doc, _map)
        _save_font_families(_attmap, _root, _doc, _map)
        _save_dimstyles(_attmap, _root, _doc, _map)
        _save_textstyles(_attmap, _root, _doc, _map)
        _save_units(image, _root, _doc)
        _save_globals(_attmap, _root, _doc, _map)
        _lyrs = _doc.createElementNS("image", "image:Layers")
        _root.appendChild(_lyrs)
        _layers = [image.getTopLayer()]
        _i = 0
        while len(_layers):
            _layer = _layers.pop()
            _map[id(_layer)] = _i
            _child = _doc.createElementNS("image", "image:Layer")
            _lyrs.appendChild(_child)
            _attr = _doc.createAttributeNS("image", "id")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "id", `_i`)
            _attr = _doc.createAttributeNS("image", "name")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "name", _layer.getName())
            _attr = _doc.createAttributeNS("image", "scale")
            _child.setAttributeNodeNS(_attr)
            _child.setAttributeNS("image", "scale", `_layer.getScale()`)
            _parent = _layer.getParentLayer()
            if _parent is not None:
                _pid = id(_parent)
                assert _pid in _map, "Layer %s parent not in map!" % _layer.getName()
                _attr = _doc.createAttributeNS("image", "parent")
                _child.setAttributeNodeNS(_attr)
                _child.setAttributeNS("image", "parent", `_map[_pid]`)
            _save_points(_layer, _child, _doc, _map)
            _save_segments(_layer, _child, _doc, _map)
            _save_circles(_layer, _child, _doc, _map)
            _save_arcs(_layer, _child, _doc, _map)
            _save_hclines(_layer, _child, _doc, _map)
            _save_vclines(_layer, _child, _doc, _map)
            _save_aclines(_layer, _child, _doc, _map)
            _save_clines(_layer, _child, _doc, _map)
            _save_ccircles(_layer, _child, _doc, _map)
            _save_chamfers(_layer, _child, _doc, _map)
            _save_fillets(_layer, _child, _doc, _map)
            _save_leaders(_layer, _child, _doc, _map)
            _save_polylines(_layer, _child, _doc, _map)
            _save_textblocks(_layer, _child, _doc, _map)
            _save_linear_dims(_layer, _child, _doc, _map,
                              _entmap, 'linear_dimension')
            _save_linear_dims(_layer, _child, _doc, _map,
                              _entmap, 'horizontal_dimension')
            _save_linear_dims(_layer, _child, _doc, _map,
                              _entmap, 'vertical_dimension')
            _save_radial_dims(_layer, _child, _doc, _map, _entmap)
            _save_angular_dims(_layer, _child, _doc, _map, _entmap)
            _save_state_bits(_layer, _child, _doc)
            _i = _i + 1
            if _layer.hasSublayers():
                _layers.extend(_layer.getSublayers())
        if _entmap:
            for _elem in _entmap.getElements():
                for _key in _entmap.getElementKeys(_elem):
                    _obj = _entmap.getEntity(_elem, _key)
                    _oid = id(_obj)
                    assert _oid in _map, "Missing object in map: " + `_obj`
                    #if _oid not in _map:
                    # print "missing object in map:"
                    # print "id(): " + `_oid`
                    # print "obj: " + str(_obj)
                    # raise KeyError, "Missing object key"
                    _elem.setAttributeNS("image", _key, `_map[_oid]`)
        #
        # write it out
        #
        _doc.writexml(filehandle, "  ", "  ", "\n")
    finally:
        _doc.unlink()

#
# reload an image
#

class DimMap(object):
    def __init__(self):
        self.__layers = {}
        self.__dims = {}

    def __nonzero__(self):
        return len(self.__layers) != 0

    def saveDim(self, lyr, dtype, dim):
        _lid = id(lyr)
        if _lid not in self.__layers:
            self.__layers[_lid] = lyr
        if _lid not in self.__dims:
            self.__dims[_lid] = {}
        _dtdict = self.__dims[_lid]
        if dtype not in _dtdict:
            _dtdict[dtype] = []
        _dtdict[dtype].append(dim)

    def getLayers(self):
        return self.__layers.values()

    def getLayerDimTypes(self, lyr):
        _lid = id(lyr)
        return self.__dims[_lid].keys()

    def getLayerDims(self, lyr, key):
        _lid = id(lyr)
        return self.__dims[_lid][key][:]

def _apply_dim_overrides(dim, overrides):
    # print "in _apply_dim_overrides()"
    for _key in overrides:
        _val = overrides[_key]
        if _key == 'DIM_OFFSET':
            dim.setOffset(_val)
        elif _key == 'DIM_EXTENSION':
            dim.setExtension(_val)
        elif _key == 'DIM_COLOR':
            dim.setColor(_val)
        elif _key == 'DIM_POSITION':
            dim.setPosition(_val)
        elif _key == 'DIM_ENDPOINT':
            dim.setEndpointType(_val)
        elif _key == 'DIM_ENDPOINT_SIZE':
            dim.setEndpointSize(_val)
        elif _key == 'DIM_DUAL_MODE':
            dim.setDualDimMode(_val)
        elif _key == 'DIM_POSITION_OFFSET':
            dim.setPositionOffset(_val)
        elif _key == 'DIM_DUAL_MODE_OFFSET':
            dim.setDualModeOffset(_val)
        elif _key == 'DIM_PRIMARY_FONT_FAMILY':
            dim.getPrimaryDimstring().setFamily(_val)
        elif _key == 'DIM_PRIMARY_FONT_SIZE':
            dim.getPrimaryDimstring().setSize(_val)
        elif _key == 'DIM_PRIMARY_FONT_WEIGHT':
            dim.getPrimaryDimstring().setWeight(_val)
        elif _key == 'DIM_PRIMARY_FONT_STYLE':
            dim.getPrimaryDimstring().setStyle(_val)
        elif _key == 'DIM_PRIMARY_FONT_COLOR':
            dim.getPrimaryDimstring().setColor(_val)
        elif (_key == 'DIM_PRIMARY_PREFIX' or
              _key == 'RADIAL_DIM_PRIMARY_PREFIX' or
              _key == 'ANGULAR_DIM_PRIMARY_PREFIX'):
            dim.getPrimaryDimstring().setPrefix(_val)
        elif (_key == 'DIM_PRIMARY_SUFFIX' or
              _key == 'RADIAL_DIM_PRIMARY_SUFFIX' or
              _key == 'ANGULAR_DIM_PRIMARY_SUFFIX'):
            dim.getPrimaryDimstring().setSuffix(_val)
        elif _key == 'DIM_PRIMARY_PRECISION':
            dim.getPrimaryDimstring().setPrecision(_val)
        elif _key == 'DIM_PRIMARY_UNITS':
            dim.getPrimaryDimstring().setUnits(_val)
        elif _key == 'DIM_PRIMARY_LEADING_ZERO':
            dim.getPrimaryDimstring().setPrintZero(_val)
        elif _key == 'DIM_PRIMARY_TRAILING_DECIMAL':
            dim.getPrimaryDimstring().setPrintDecimal(_val)
        elif _key == 'DIM_PRIMARY_TEXT_SIZE':
            dim.getPrimaryDimstring().setSize(_val)
        elif _key == 'DIM_PRIMARY_TEXT_ANGLE':
            dim.getPrimaryDimstring().setAngle(_val)
        elif _key == 'DIM_PRIMARY_TEXT_ALIGNMENT':
            dim.getPrimaryDimstring().setAlignment(_val)
        elif _key == 'DIM_SECONDARY_FONT_FAMILY':
            dim.getSecondaryDimstring().setFamily(_val)
        elif _key == 'DIM_SECONDARY_FONT_SIZE':
            dim.getSecondaryDimstring().setSize(_val)
        elif _key == 'DIM_SECONDARY_FONT_WEIGHT':
            dim.getSecondaryDimstring().setWeight(_val)
        elif _key == 'DIM_SECONDARY_FONT_STYLE':
            dim.getSecondaryDimstring().setStyle(_val)
        elif _key == 'DIM_SECONDARY_FONT_COLOR':
            dim.getSecondaryDimstring().setColor(_val)
        elif (_key == 'DIM_SECONDARY_PREFIX' or
              _key == 'RADIAL_DIM_SECONDARY_PREFIX' or
              _key == 'ANGULAR_DIM_SECONDARY_PREFIX'):
            dim.getSecondaryDimstring().setPrefix(_val)
        elif (_key == 'DIM_SECONDARY_SUFFIX' or
              _key == 'RADIAL_DIM_SECONDARY_SUFFIX' or
              _key == 'ANGULAR_DIM_SECONDARY_SUFFIX'):
            dim.getSecondaryDimstring().setSuffix(_val)
        elif _key == 'DIM_SECONDARY_PRECISION':
            dim.getSecondaryDimstring().setPrecision(_val)
        elif _key == 'DIM_SECONDARY_UNITS':
            dim.getSecondaryDimstring().setUnits(_val)
        elif _key == 'DIM_SECONDARY_LEADING_ZERO':
            dim.getSecondaryDimstring().setPrintZero(_val)
        elif _key == 'DIM_SECONDARY_TRAILING_DECIMAL':
            dim.getSecondaryDimstring().setPrintDecimal(_val)
        elif _key == 'DIM_SECONDARY_TEXT_SIZE':
            dim.getSecondaryDimstring().setSize(_val)
        elif _key == 'DIM_SECONDARY_TEXT_ANGLE':
            dim.getSecondaryDimstring().setAngle(_val)
        elif _key == 'DIM_SECONDARY_TEXT_ALIGNMENT':
            dim.getSecondaryDimstring().setAlignment(_val)
        elif _key == 'RADIAL_DIM_DIA_MODE':
            dim.setDiaMode(_val)
        else:
            pass # unused key ...

def _load_dim_options(node, map):
    # print "in _load_dim_options()"
    _options = {}
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == 'image:DimOption':
                _opt = str(_child.getAttribute('opt'))
                if (_opt == 'DIM_COLOR' or
                    _opt == 'DIM_PRIMARY_FONT_COLOR' or
                    _opt == 'DIM_SECONDARY_FONT_COLOR'):
                    _cid = int(_child.getAttribute('cid'))
                    assert _cid in map['color'], 'Color index %d missing' % _cid
                    _val = map['color'][_cid]
                elif (_opt == 'DIM_PRIMARY_FONT_FAMILY' or
                      _opt == 'DIM_SECONDARY_FONT_FAMILY'):
                    _fid = int(_child.getAttribute('fid'))
                    assert _fid in map['family'], 'Font index %d missing' % _fid
                    _val = map['family'][_fid]
                elif (_opt == 'DIM_OFFSET' or
                      _opt == 'DIM_ENDPOINT_SIZE' or
                      _opt == 'DIM_EXTENSION' or
                      _opt == 'DIM_PRIMARY_TEXT_SIZE' or
                      _opt == 'DIM_SECONDARY_TEXT_SIZE' or
                      _opt == 'DIM_DUAL_MODE_OFFSET' or
                      _opt == 'DIM_POSITION_OFFSET' or
                      _opt == 'DIM_THICKNESS' or
                      _opt == 'DIM_PRIMARY_FONT_SIZE' or
                      _opt == 'DIM_SECONDARY_FONT_SIZE' or
                      _opt == 'DIM_PRIMARY_TEXT_ANGLE' or
                      _opt == 'DIM_SECONDARY_TEXT_ANGLE'):
                    _val = float(_child.getAttribute('val'))
                elif (_opt == 'DIM_PRIMARY_PRECISION' or
                      _opt == 'DIM_SECONDARY_PRECISION' or
                      _opt == 'DIM_PRIMARY_UNITS' or
                      _opt == 'DIM_SECONDARY_UNITS' or
                      (_opt == 'DIM_PRIMARY_FONT_SIZE' and False) or
                      (_opt == 'DIM_SECONDARY_FONT_SIZE' and False) or
                      _opt == 'DIM_PRIMARY_FONT_WEIGHT' or
                      _opt == 'DIM_SECONDARY_FONT_WEIGHT' or
                      _opt == 'DIM_PRIMARY_FONT_STYLE' or
                      _opt == 'DIM_SECONDARY_FONT_STYLE' or
                      _opt == 'DIM_POSITION' or
                      _opt == 'DIM_ENDPOINT' or
                      _opt == 'DIM_PRIMARY_TEXT_ALIGNMENT' or
                      _opt == 'DIM_SECONDARY_TEXT_ALIGNMENT'):
                    _val = int(_child.getAttribute('val'))
                elif (_opt == 'DIM_PRIMARY_LEADING_ZERO' or
                      _opt == 'DIM_SECONDARY_LEADING_ZERO' or
                      _opt == 'DIM_PRIMARY_TRAILING_DECIMAL' or
                      _opt == 'DIM_SECONDARY_TRAILING_DECIMAL' or
                      _opt == 'DIM_DUAL_MODE' or
                      _opt == 'RADIAL_DIM_DIA_MODE'):
                    _val = _child.getAttribute('val')
                    if _val == 0 or _val == u'0' or _val == u'False':
                        _val = False
                    elif _val == 1 or _val == u'1' or _val == u'True':
                        _val = True
                elif _opt == 'ANGULAR_DIM_SMALL_ANGLE_MODE': # obsolete
                    _opt = None
                else:
                    _val = _child.getAttribute('val')
                if _opt is not None:
                    _options[_opt] = _val
        _child = _child.nextSibling
    return _options

def _load_linear_dimensions(node, map):
    # print "in _load_linear_dimensions()"
    _dims = []
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if (_child.nodeName == "image:LDim" or
                _child.nodeName == "image:HDim" or
                _child.nodeName == "image:VDim"):
                _dimopts = {}
                _id = int(_child.getAttribute("id"))
                _dimopts['id'] = _id
                _ds = int(_child.getAttribute("ds"))
                _dimopts['ds'] = _ds
                assert _ds in map['dimstyle'], "Missing style %d in map" % _ds
                _l1 = int(_child.getAttribute("l1"))
                _dimopts['l1'] = _l1
                _p1 = int(_child.getAttribute("p1"))
                _dimopts['p1'] = _p1
                _l2 = int(_child.getAttribute("l2"))
                _dimopts['l2'] = _l2
                _p2 = int(_child.getAttribute("p2"))
                _dimopts['p2'] = _p2
                _x = float(_child.getAttribute("x"))
                _dimopts['x'] = _x
                _y = float(_child.getAttribute("y"))
                _dimopts['y'] = _y
                _overrides = _load_dim_options(_child, map)
                for _key in _overrides:
                    _dimopts[_key] = _overrides[_key]
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _dimopts['hidden'] = True
                if _locked:
                    _dimopts['locked'] = True
                _dims.append(_dimopts)
        _child = _child.nextSibling
    return _dims

def _load_radial_dimensions(node, map):
    # print "in _load_radial_dimensions()"
    _rdims = []
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:RDim":
                _dimopts = {}
                _id = int(_child.getAttribute("id"))
                _dimopts['id'] = _id
                _ds = int(_child.getAttribute("ds"))
                assert _ds in map['dimstyle'], "Missing style %d in map" % _ds
                _dimopts['ds'] = _ds
                _l = int(_child.getAttribute("l"))
                _dimopts['l'] = _l
                _c = int(_child.getAttribute("c"))
                _dimopts['c'] = _c
                _x = float(_child.getAttribute("x"))
                _dimopts['x'] = _x
                _y = float(_child.getAttribute("y"))
                _dimopts['y'] = _y
                _overrides = _load_dim_options(_child, map)
                for _key in _overrides:
                    _dimopts[_key] = _overrides[_key]
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _dimopts['hidden'] = True
                if _locked:
                    _dimopts['locked'] = True
                _rdims.append(_dimopts)
        _child = _child.nextSibling
    return _rdims

def _load_angular_dimensions(node, map):
    # print "in _load_angular_dimensions()"
    _adims = []
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:ADim":
                _dimopts = {}
                _id = int(_child.getAttribute("id"))
                _dimopts['id'] = _id
                _ds = int(_child.getAttribute("ds"))
                assert _ds in map['dimstyle'], "Missing style %d in map" % _ds
                _dimopts['ds'] = _ds
                _l1 = int(_child.getAttribute("l1"))
                _dimopts['l1'] = _l1
                _p1 = int(_child.getAttribute("p1"))
                _dimopts['p1'] = _p1
                _l2 = int(_child.getAttribute("l2"))
                _dimopts['l2'] = _l2
                _p2 = int(_child.getAttribute("p2"))
                _dimopts['p2'] = _p2
                _l3 = int(_child.getAttribute("l3"))
                _dimopts['l3'] = _l3
                _p3 = int(_child.getAttribute("p3"))
                _dimopts['p3'] = _p3
                _x = float(_child.getAttribute("x"))
                _dimopts['x'] = _x
                _y = float(_child.getAttribute("y"))
                _dimopts['y'] = _y
                _overrides = _load_dim_options(_child, map)
                for _key in _overrides:
                    _dimopts[_key] = _overrides[_key]
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _dimopts['hidden'] = True
                if _locked:
                    _dimopts['locked'] = True
                _adims.append(_dimopts)
        _child = _child.nextSibling
    return _adims

def _set_graphic_attrs(obj, node, map):
    _ltid = node.getAttribute("linetype")
    if _ltid != "":
        _ltid = int(_ltid)
        assert _ltid in map['linetype'], "Object linetype index %d not in map: %s" % (_ltid, str(obj))
        _lt = map['linetype'][_ltid]
        obj.setLinetype(_lt)
    _cid = node.getAttribute("color")
    if _cid != "":
        _cid = int(_cid)
        assert _cid in map['color'], "Object color index %d not in map: %s" % (_cid, str(obj))
        _c = map['color'][_cid]
        obj.setColor(_c)
    _th = node.getAttribute("thickness")
    if _th != "":
        _th = float(_th)
        obj.setThickness(_th)

def _get_state_bits(node):
    _vis = True
    _locked = False
    _attr = node.getAttribute("visibility")
    if _attr != "":
        if _attr == 0 or _attr == u'0' or _attr == u'False':
            _vis = False
    _attr = node.getAttribute("locked")
    if _attr != "":
        if _attr == 1 or _attr == u'1' or _attr == u'True':
            _locked = True
    return _vis, _locked
            
def _load_textblocks(image, node, map):
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:TextBlock":
                # _id = int(_child.getAttribute("id"))
                _tsid = int(_child.getAttribute("tsid"))
                assert _tsid in map['textstyle'], "TextBlock textstyle id not in map: %d" % _tsid
                _style = map['textstyle'][_tsid]
                _x = float(_child.getAttribute("x"))
                _y = float(_child.getAttribute("y"))
                _text = u''
                if _child.hasChildNodes():
                    _lines = []
                    _linenode = _child.firstChild
                    while _linenode is not None:
                        if _linenode.nodeType == xml.dom.Node.ELEMENT_NODE:
                            if _linenode.nodeName == "image:TextLine":
                                _text = _linenode.getAttribute("text")
                                _lines.append(unicode(_text))
                        _linenode = _linenode.nextSibling
                    if sys.platform == 'mac':
                        _sep = '\r'
                    else:
                        _sep = '\n'
                    _text = _sep.join(_lines)
                _textblock = text.TextBlock(_x, _y, _text, _style)
                #
                # get any possibly overriden style values
                #
                _attr = _child.getAttribute("fid")
                if _attr != "":
                    _fid = int(_attr)
                    _family = map['family'][_fid]
                    _textblock.setFamily(_family)
                _attr = _child.getAttribute("cid")
                if _attr != "":
                    _cid = int(_attr)
                    _color = map['color'][_cid]
                    _textblock.setColor(_color)
                _attr = _child.getAttribute("weight")
                if _attr != "":
                    _weight = text.TextStyle.WEIGHT_NORMAL
                    if _attr == 'normal' or _attr == '0':
                        _weight = text.TextStyle.WEIGHT_NORMAL
                    elif _attr == 'light' or _attr == '1':
                        _weight = text.TextStyle.WEIGHT_LIGHT
                    elif _attr == 'bold' or _attr == '2':
                        _weight = text.TextStyle.WEIGHT_BOLD
                    elif _attr == 'heavy' or _attr == '3':
                        _weight = text.TextStyle.WEIGHT_HEAVY
                    else:
                        sys.stderr.write("Unknown font weight '%s' - using NORMAL\n" % _attr)
                    _textblock.setWeight(_weight)
                _attr = _child.getAttribute("style")
                if _attr != "":
                    _style = text.TextStyle.FONT_NORMAL
                    if _attr == 'normal' or _attr == '0':
                        _style = text.TextStyle.FONT_NORMAL
                    elif _attr == 'oblique' or _attr == '1':
                        _style = text.TextStyle.FONT_OBLIQUE
                    elif _attr == 'italic' or _attr == '2':
                        _style = text.TextStyle.FONT_ITALIC
                    else:
                        sys.stderr.write("Unknown font style '%s' - using NORMAL\n" % _attr)
                    _textblock.setStyle(_style)
                _attr = _child.getAttribute("size")
                if _attr != "":
                    _size = float(_attr)
                    _textblock.setSize(_size)
                _attr = _child.getAttribute("angle")
                if _attr != "":
                    _angle = float(_attr)
                    _textblock.setAngle(_angle)
                _attr = _child.getAttribute("halign")
                if _attr != "":
                    _align = text.TextStyle.ALIGN_LEFT
                    if _attr == 'left' or _attr == '0':
                        _align = text.TextStyle.ALIGN_LEFT
                    elif _attr == 'center' or _attr == '1':
                        _align = text.TextStyle.ALIGN_CENTER
                    elif _attr == 'right' or _attr == '2':
                        _align = text.TextStyle.ALIGN_RIGHT
                    else:
                        sys.stderr.write("Unknown alignment %s - using ALIGN_LEFT\n" % _attr)
                    _textblock.setAlignment(_align)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _textblock.setVisibility(False)
                if _locked:
                    _locked.append(_textblock)
                image.addObject(_textblock)
        _child = _child.nextSibling

def _load_leaders(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Leader":
                # _id = int(_child.getAttribute("id"))
                _pid = int(_child.getAttribute("p1"))
                assert _pid in _pmap, "Leader p1 index %d not in map!" % _pid
                _p1 = _pmap[_pid]
                _pid = int(_child.getAttribute("p2"))
                assert _pid in _pmap, "Leader p2 index %d not in map!" % _pid
                _p2 = _pmap[_pid]
                _pid = int(_child.getAttribute("p3"))
                assert _pid in _pmap, "Leader p3 index %d not in map!" % _pid
                _p3 = _pmap[_pid]
                _sid = int(_child.getAttribute("style"))
                _size = float(_child.getAttribute("size"))
                assert _sid in map['style'], "Leader style index %d not in map!" % _sid
                _style = map['style'][_sid]
                _leader = leader.Leader(_p1, _p2, _p3, _size, _style)
                _set_graphic_attrs(_leader, _child, map)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _leader.setVisibility(False)
                if _locked:
                    _locked.append(_leader)
                image.addObject(_leader)
        _child = _child.nextSibling

def _load_polylines(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Polyline":
                # _id = int(_child.getAttribute("id"))
                _mpts = eval(_child.getAttribute("points"))
                _pts = []
                for _mpt in _mpts:
                    assert _mpt in _pmap, "Polyline point %d not in map!" % _mpt
                    _pts.append(_pmap[_mpt])

                _sid = int(_child.getAttribute("style"))
                assert _sid in map['style'], "Polyline style index %d not in map!" % _sid
                _st = map['style'][_sid]
                _polyline = polyline.Polyline(_pts, _st)
                _set_graphic_attrs(_polyline, _child, map)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _polyline.setVisibility(False)
                if _locked:
                    _locked.append(_polyline)
                image.addObject(_polyline)
        _child = _child.nextSibling

def _load_fillets(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _smap = map[_lid]['segment']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Fillet":
                # _id = int(_child.getAttribute("id"))
                _sid = int(_child.getAttribute("s1"))
                assert _sid in _smap, "Fillet segment s1 %d not in map!" % _sid
                _s1 = _smap[_sid]
                _sid = int(_child.getAttribute("s2"))
                assert _sid in _smap, "Fillet segment s2 %s not in map!" % _sid
                _s2 = _smap[_sid]
                _sid = int(_child.getAttribute("style"))
                assert _sid in map['style'], "Fillet style index %d not in map!" % _sid
                _style = map['style'][_sid]
                _radius = float(_child.getAttribute("radius"))
                _fillet = segjoint.Fillet(_s1, _s2, _radius, _style)
                _set_graphic_attrs(_fillet, _child, map)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _fillet.setVisibility(False)
                if _locked:
                    _locked.append(_fillet)
                image.addObject(_fillet)
        _child = _child.nextSibling

def _load_chamfers(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _smap = map[_lid]['segment']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Chamfer":
                # _id = int(_child.getAttribute("id"))
                _sid = int(_child.getAttribute("s1"))
                assert _sid in _smap, "Chamfer segment s1 %d not in map!" % _sid
                _s1 = _smap[_sid]
                _sid = int(_child.getAttribute("s2"))
                assert _sid in _smap, "Chamfer segment s2 %s not in map!" % _sid
                _s2 = _smap[_sid]
                _sid = int(_child.getAttribute("style"))
                assert _sid in map['style'], "Chamfer style index %d not in map!" % _sid
                _style = map['style'][_sid]
                _length = float(_child.getAttribute("length"))
                _chamfer = segjoint.Chamfer(_s1, _s2, _length, _style)
                _set_graphic_attrs(_chamfer, _child, map)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _chamfer.setVisibility(False)
                if _locked:
                    _locked.append(_chamfer)
                image.addObject(_chamfer)
        _child = _child.nextSibling

def _load_ccircles(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:CCircle":
                # _id = int(_child.getAttribute("id"))
                _cid = int(_child.getAttribute("cp"))
                assert _cid in _pmap, "CCircle center index %d not in map!" % _cid
                _cen = _pmap[_cid]
                _rad = float(_child.getAttribute("r"))
                _cc = ccircle.CCircle(_cen, _rad)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _cc.setVisibility(False)
                if _locked:
                    _locked.append(_cc)
                image.addObject(_cc)
        _child = _child.nextSibling

def _load_clines(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:CLine":
                # _id = int(_child.getAttribute("id"))
                _pid = int(_child.getAttribute("p1"))
                assert _pid in _pmap, "CLine p1 index %d not in map!" % _pid
                _p1 = _pmap[_pid]
                _pid = int(_child.getAttribute("p2"))
                assert _pid in _pmap, "CLine p2 index %d not in map!" % _pid
                _p2 = _pmap[_pid]
                _cl = cline.CLine(_p1, _p2)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _cl.setVisibility(False)
                if _locked:
                    _locked.append(_cl)
                image.addObject(_cl)
        _child = _child.nextSibling

def _load_aclines(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:ACLine":
                #_id = int(_child.getAttribute("id"))
                _pid = int(_child.getAttribute("location"))
                _angle = float(_child.getAttribute("angle"))
                assert _pid in _pmap, "ACLine location index %d not in map!" % _pid
                _loc = _pmap[_pid]
                _acl = acline.ACLine(_loc, _angle)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _acl.setVisibility(False)
                if _locked:
                    _locked.append(_acl)
                image.addObject(_acl)
        _child = _child.nextSibling

def _load_vclines(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:VCLine":
                # _id = int(_child.getAttribute("id"))
                _pid = int(_child.getAttribute("location"))
                assert _pid in _pmap, "VCLine location index %d not in map!" % _pid
                _loc = _pmap[_pid]
                _vcl = vcline.VCLine(_loc)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _vcl.setVisibility(False)
                if _locked:
                    _locked.append(_vcl)
                image.addObject(_vcl)
        _child = _child.nextSibling

def _load_hclines(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:HCLine":
                # _id = int(_child.getAttribute("id"))
                _pid = int(_child.getAttribute("location"))
                assert _pid in _pmap, "HCLine location index %d not in map!" % _pid
                _loc = _pmap[_pid]
                _hcl = hcline.HCLine(_loc)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _hcl.setVisibility(False)
                if _locked:
                    _locked.append(_hcl)
                image.addObject(_hcl)
        _child = _child.nextSibling

def _load_arcs(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _amap = {}
    map[_lid]['arc'] = _amap
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Arc":
                _id = int(_child.getAttribute("id"))
                _cid = int(_child.getAttribute("cp"))
                assert _cid in _pmap, "Arc center index %d not in map!" % _cid
                _cen = _pmap[_cid]
                _rad = float(_child.getAttribute("r"))
                _sa = float(_child.getAttribute("sa"))
                _ea = float(_child.getAttribute("ea"))
                _sid = int(_child.getAttribute("style"))
                assert _sid in map['style'], "Arc style index %d not in map!" % _sid
                _style = map['style'][_sid]
                _arc = arc.Arc(_cen, _rad, _sa, _ea, _style)
                _set_graphic_attrs(_arc, _child, map)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _arc.setVisibility(False)
                if _locked:
                    _locked.append(_arc)
                _amap[_id] = _arc
                image.addObject(_arc)
        _child = _child.nextSibling

def _load_circles(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _cmap = {}
    map[_lid]['circle'] = _cmap
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Circle":
                _id = int(_child.getAttribute("id"))
                _cid = int(_child.getAttribute("cp"))
                assert _cid in _pmap, "Circle center index %d not in map!" % _cid
                _cen = _pmap[_cid]
                _rad = float(_child.getAttribute("r"))
                _sid = int(_child.getAttribute("style"))
                assert _sid in map['style'], "Circle style index %d not in map!" % _sid
                _style = map['style'][_sid]
                _circle = circle.Circle(_cen, _rad, _style)
                _set_graphic_attrs(_circle, _child, map)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _circle.setVisibility(False)
                if _locked:
                    _locked.append(_circle)
                image.addObject(_circle)
                _cmap[_id] = _circle
        _child = _child.nextSibling

def _load_segments(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = map[_lid]['point']
    _smap = {}
    map[_lid]['segment'] = _smap
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Segment":
                _id = int(_child.getAttribute("id"))
                _pid = int(_child.getAttribute("p1"))
                assert _pid in _pmap, "Segment p1 index %d not in map!" % _pid
                _p1 = _pmap[_pid]
                _pid = int(_child.getAttribute("p2"))
                assert _pid in _pmap, "Segment p2 index %d not in map!" % _pid
                _p2 = _pmap[_pid]
                _sid = int(_child.getAttribute("style"))
                assert _sid in map['style'], "Segment style index %d not in map!" % _sid
                _style = map['style'][_sid]
                _seg = segment.Segment(_p1, _p2, _style)
                _set_graphic_attrs(_seg, _child, map)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _seg.setVisibility(False)
                if _locked:
                    _locked.append(_seg)
                image.addObject(_seg)
                _smap[_id] = _seg
        _child = _child.nextSibling

def _load_points(image, node, map):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    _pmap = {}
    map[_lid]['point'] = _pmap
    _locked = map['locked']
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Point":
                _id = int(_child.getAttribute("id"))
                _x = float(_child.getAttribute("x"))
                _y = float(_child.getAttribute("y"))
                _p = point.Point(_x, _y)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _p.setVisibility(False)
                if _locked:
                    _locked.append(_p)
                image.addObject(_p)
                _pmap[_id] = _p
        _child = _child.nextSibling

def _load_layer(image, node, map, dimmap):
    _layer = image.getActiveLayer()
    _lid = id(_layer)
    map[_lid] = {}
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            _name = _child.nodeName
            if _name == "image:Points":
                _load_points(image, _child, map)
            elif _name == "image:Segments":
                _load_segments(image, _child, map)
            elif _name == "image:Circles":
                _load_circles(image, _child, map)
            elif _name == "image:Arcs":
                _load_arcs(image, _child, map)
            elif _name == "image:HCLines":
                _load_hclines(image, _child, map)
            elif _name == "image:VCLines":
                _load_vclines(image, _child, map)
            elif _name == "image:ACLines":
                _load_aclines(image, _child, map)
            elif _name == "image:CLines":
                _load_clines(image, _child, map)
            elif _name == "image:CCircles":
                _load_ccircles(image, _child, map)
            elif _name == "image:Fillets":
                _load_fillets(image, _child, map)
            elif _name == "image:Chamfers":
                _load_chamfers(image, _child, map)
            elif _name == "image:Leaders":
                _load_leaders(image, _child, map)
            elif _name == "image:Polylines":
                _load_polylines(image, _child, map)
            elif _name == "image:TextBlocks":
                _load_textblocks(image, _child, map)
            elif _name == "image:LDims":
                for _dim in _load_linear_dimensions(_child, map):
                    dimmap.saveDim(_layer, 'linear', _dim)
            elif _name == "image:HDims":
                for _dim in _load_linear_dimensions(_child, map):
                    dimmap.saveDim(_layer, 'horizontal', _dim)
            elif _name == "image:VDims":
                for _dim in _load_linear_dimensions(_child, map):
                    dimmap.saveDim(_layer, 'vertical', _dim)
            elif _name == "image:RDims":
                for _dim in _load_radial_dimensions(_child, map):
                    dimmap.saveDim(_layer, 'radial', _dim)
            elif _name == "image:ADims":
                for _dim in _load_angular_dimensions(_child, map):
                    dimmap.saveDim(_layer, 'angular', _dim)
        _child = _child.nextSibling

def _add_linear_dimension(dim, map, dimtype, image, lyr):
    assert 'ds' in dim, "Dim missing key 'ds': " + `dim`
    _dsid = dim['ds']
    assert _dsid in map['dimstyle'], "Missing dimstyle id %d" % _dsid
    _ds = map['dimstyle'][_dsid]
    del dim['ds']
    assert 'l1' in dim, "Dim missing key 'l1': " + `dim`
    _lid = dim['l1']
    del dim['l1']
    assert _lid in map['layer'], "Missing layer id %d" % _lid
    _l1 = map['layer'][_lid]
    assert 'p1' in dim, "Dim missing key 'p1': " + `dim`
    _pid = dim['p1']
    del dim['p1']
    _lid = id(_l1)
    assert _pid in map[_lid]['point'], "Missing p1 key %d in map" % _pid
    _p1 = map[_lid]['point'][_pid]
    assert 'l2' in dim, "Dim missing key 'l2': " + `dim`
    _lid = dim['l2']
    del dim['l2']
    assert _lid in map['layer'], "Missing layer id %d" % _lid
    _l2 = map['layer'][_lid]
    assert 'p2' in dim, "Dim missing key 'p2': " + `dim`
    _pid = dim['p2']
    del dim['p2']
    _lid = id(_l2)
    assert _pid in map[_lid]['point'], "Missing p2 key %d in map" % _pid
    _p2 = map[_lid]['point'][_pid]
    assert 'x' in dim, "Dim missing key 'x': " + `dim`
    _x = dim['x']
    del dim['x']
    assert 'y' in dim, "Dim missing key 'y': " + `dim`
    _y = dim['y']
    del dim['y']
    _ldim = dimtype(_l1, _p1, _l2, _p2, _x, _y, _ds)
    if 'id' in dim:
        del dim['id']
    # print "remaining keys ..."
    # for _key in dim:
        # print "key: %s" % _key
        # print "value: " + `dim[_key]`
    if len(dim):
        _apply_dim_overrides(_ldim, dim)
    if 'hidden' in dim:
        _ldim.setVisibility(False)
    if 'locked' in dim:
        _ldim.setLock(True)
    image.ignore('modified')
    try:
        image.addObject(_ldim, lyr)
    finally:
        image.receive('modified')
    _log = lyr.getLog() # fixme - this will go away
    if _log is not None:
        _log.clear()

def _add_radial_dimension(dim, map, image, lyr):
    assert 'id' in dim, "Dim missing key 'id': " + `dim`
    assert 'ds' in dim, "Dim missing key 'ds': " + `dim`
    _dsid = dim['ds']
    assert _dsid in map['dimstyle'], "Missing dimstyle id %d" % _dsid
    _ds = map['dimstyle'][_dsid]
    del dim['ds']
    assert 'l' in dim, "Dim missing key 'l': " + `dim`
    _lid = dim['l']
    del dim['l']
    assert _lid in map['layer'], "Missing layer id %d" % _lid
    _cl = map['layer'][_lid]
    assert 'c' in dim, "Dim missing key 'c': " + `dim`
    _cid = dim['c']
    del dim['c']
    _lid = id(_cl)
    assert _cid in map[_lid]['circle'], "Missing circle key %d" % _cid
    _circ = map[_lid]['circle'][_cid]
    assert 'x' in dim, "Dim missing key 'x': " + `dim`
    _x = dim['x']
    del dim['x']
    assert 'y' in dim, "Dim missing key 'y': " + `dim`
    _y = dim['y']
    del dim['y']
    _rdim = dimension.RadialDimension(_cl, _circ, _x, _y, _ds)
    if 'id' in dim:
        del dim['id']
    # print "remaining keys ..."
    # for _key in dim:
        # print "key: %s" % key
        # print "value: " + `dim[_key]`
    if len(dim):
        _apply_dim_overrides(_rdim, dim)
    if 'hidden' in dim:
        _rdim.setVisibility(False)
    if 'locked' in dim:
        _rdim.setLock(True)
    image.ignore('modified')
    try:
        image.addObject(_rdim, lyr)
    finally:
        image.receive('modified')
    _log = lyr.getLog() # fixme - this will go away
    if _log is not None:
        _log.clear()

def _add_angular_dimension(dim, map, image, lyr):
    assert 'id' in dim, "Dim missing key 'id': " + `dim`
    assert 'ds' in dim, "Dim missing key 'ds': " + `dim`
    _dsid = dim['ds']
    assert _dsid in map['dimstyle'], "Missing dimstyle id %d" % _dsid
    _ds = map['dimstyle'][_dsid]
    del dim['ds']
    assert 'l1' in dim, "Dim missing key 'l1': " + `dim`
    _lid = dim['l1']
    del dim['l1']
    assert _lid in map['layer'], "Missing layer id %d" % _lid
    _l1 = map['layer'][_lid]
    assert 'p1' in dim, "Dim missing key 'p1': " + `dim`
    _pid = dim['p1']
    del dim['p1']
    _lid = id(_l1)
    assert _pid in map[_lid]['point'], "Missing p1 id %d" % _pid
    _p1 = map[_lid]['point'][_pid]
    assert 'l2' in dim, "Dim missing key 'l2': " + `dim`
    _lid = dim['l2']
    del dim['l2']
    assert _lid in map['layer'], "Missing layer id %d" % _lid
    _l2 = map['layer'][_lid]
    assert 'p2' in dim, "Dim missing key 'p2': " + `dim`
    _pid = dim['p2']
    del dim['p2']
    _lid = id(_l2)
    assert _pid in map[_lid]['point'], "Missing p2 id %d" % _pid
    _p2 = map[_lid]['point'][_pid]
    assert 'l3' in dim, "Dim missing key 'l3': " + `dim`
    _lid = dim['l3']
    del dim['l3']
    assert _lid in map['layer'], "Missing layer id %d" % _lid
    _l3 = map['layer'][_lid]
    assert 'p3' in dim, "Dim missing key 'p3': " + `dim`
    _pid = dim['p3']
    del dim['p3']
    _lid = id(_l3)
    assert _pid in map[_lid]['point'], "Missing p3 id %d" % _pid
    _p3 = map[_lid]['point'][_pid]
    assert 'x' in dim, "Dim missing key 'x': " + `dim`
    _x = dim['x']
    del dim['x']
    assert 'y' in dim, "Dim missing key 'y': " + `dim`
    _y = dim['y']
    del dim['y']
    _adim = dimension.AngularDimension(_l1, _p1, _l2, _p2, _l3, _p3,
                                       _x, _y, _ds)
    if 'id' in dim:
        del dim['id']
    # print "remaining keys ..."
    # for _key in dim:
        # print "key: %s" % _key
        # print "value: " + `dim[_key]`
    if len(dim):
        _apply_dim_overrides(_adim, dim)
    if 'hidden' in dim:
        _adim.setVisibility(False)
    if 'locked' in dim:
        _adim.setLock(True)
    image.ignore('modified')
    try:
        image.addObject(_adim, lyr)
    finally:
        image.receive('modified')
    _log = lyr.getLog() # fixme - this will go away
    if _log is not None:
        _log.clear()

def _load_layers(image, node, map):
    _lmap = {}
    map['layer'] = _lmap
    map['locked'] = []
    _dimmap = DimMap()
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Layer":
                _id = int(_child.getAttribute("id"))
                _name = str(_child.getAttribute("name"))
                _scale = float(_child.getAttribute("scale"))
                _pid = _child.getAttribute("parent")
                if _pid == "": # top layer created with image
                    _layer = image.getTopLayer()
                    _layer.setName(_name)
                else:
                    _pid = int(_pid)
                    assert _pid in _lmap, "Layer parent id %d missing in map!" % _pid
                    _parent = _lmap[_pid]
                    _layer = layer.Layer(_name)
                    image.addChildLayer(_layer, _parent)
                _layer.setScale(_scale)
                _vis, _locked = _get_state_bits(_child)
                if not _vis:
                    _layer.setVisibility(False)
                if _locked:
                    _locked.append(_layer)
                image.setActiveLayer(_layer)
                _lmap[_id] = _layer
                image.ignore('modified')
                try:
                    _load_layer(image, _child, map, _dimmap)
                finally:
                    image.receive('modified')
                _log = _layer.getLog() # fixme - this will go away
                if _log is not None:
                    _log.clear()
        _child = _child.nextSibling
    if _dimmap:
        for _layer in _dimmap.getLayers():
            for _dimtype in _dimmap.getLayerDimTypes(_layer):
                for _dim in _dimmap.getLayerDims(_layer, _dimtype):
                    if _dimtype == 'linear':
                        _add_linear_dimension(_dim, map,
                                              dimension.LinearDimension,
                                              image, _layer)
                    elif _dimtype == 'horizontal':
                        _add_linear_dimension(_dim, map,
                                              dimension.HorizontalDimension,
                                              image, _layer)
                    elif _dimtype == 'vertical':
                        _add_linear_dimension(_dim, map,
                                              dimension.VerticalDimension,
                                              image, _layer)
                    elif _dimtype == 'radial':
                        _add_radial_dimension(_dim, map, image, _layer)
                    elif _dimtype == 'angular':
                        _add_angular_dimension(_dim, map, image, _layer)
                    else:
                        raise TypeError, "Unknown dimension type %s" % _dimtype

def _load_textstyles(image, node, map):
    map['textstyle'] = {}
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:TextStyle":
                _id = int(_child.getAttribute("id"))
                _name= _child.getAttribute("name")
                _cid = int(_child.getAttribute("cid"))
                assert _cid in map['color'], "Color ID missing from map: %d" % _cid
                _color = map['color'][_cid]
                _fid = int(_child.getAttribute("fid"))
                assert _fid in map['family'], "Font ID missing from map: %d" % _fid
                _family = map['family'][_fid]
                _attr = _child.getAttribute("weight")
                _weight = text.TextStyle.WEIGHT_NORMAL
                if _attr == 'normal' or _attr == '0':
                    _weight = text.TextStyle.WEIGHT_NORMAL
                elif _attr == 'light' or _attr == '1':
                    _weight = text.TextStyle.WEIGHT_LIGHT
                elif _attr == 'bold' or _attr == '2':
                    _weight = text.TextStyle.WEIGHT_BOLD
                elif _attr == 'heavy' or _attr == '3':
                    _weight = text.TextStyle.WEIGHT_HEAVY
                else:
                    sys.stderr.write("Unknown font weight '%s' - using NORMAL\n" % _attr)
                #
                # size used to be integer in older TextStyles
                #
                _size = float(_child.getAttribute("size"))
                _attr = _child.getAttribute("style")
                _style = text.TextStyle.FONT_NORMAL
                if _attr == 'normal' or _attr == '0':
                    _style = text.TextStyle.FONT_NORMAL
                elif _attr == 'oblique' or _attr == '1':
                    _style = text.TextStyle.FONT_OBLIQUE
                elif _attr == 'italic' or _attr == '2':
                    _style = text.TextStyle.FONT_ITALIC
                else:
                    sys.stderr.write("Unknown font style '%s' - using NORMAL\n" % _attr)
                #
                # angle not present in older TextStyles
                #
                _angle = 0.0
                _attr = _child.getAttribute("angle")
                if _attr != "":
                    _angle = float(_attr)
                #
                # alignment not present in older TextStyles
                #
                _align = text.TextStyle.ALIGN_LEFT
                _attr = _child.getAttribute("halign")
                if _attr != "":
                    if _attr == 'left' or _attr == '0':
                        _align = text.TextStyle.ALIGN_LEFT
                    elif _attr == 'center' or _attr == '1':
                        _align = text.TextStyle.ALIGN_CENTER
                    elif _attr == 'right' or _attr == '2':
                        _align = text.TextStyle.ALIGN_RIGHT
                    else:
                        sys.stderr.write("Unknown alignment %s - using ALIGN_LEFT\n" % _attr)
                _textstyle = text.TextStyle(_name,
                                            family=_family,
                                            size=_size,
                                            style=_style,
                                            weight=_weight,
                                            color=_color,
                                            angle=_angle,
                                            align=_align)
                image.addTextStyle(_textstyle)
                map['textstyle'][_id] = _textstyle
        _child = _child.nextSibling

def _load_dimstyles(image, node, map):
    map['dimstyle'] = {}
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:DimStyle":
                _id = int(_child.getAttribute("id"))
                _name = _child.getAttribute("name")
                _dsopts = _load_dim_options(_child, map)
                # print "print keys ..."
                _dskeys = _dsopts.keys()
                _dskeys.sort()
                # for key in dskeys:
                    # print "key: " + key + "; value: " + `dsopts[key]`
                _dimstyle = dimension.DimStyle(_name, _dsopts)
                if image.hasDimStyle(_dimstyle):
                    for _ds in image.getImageEntities('dimstyle'):
                        if _dimstyle == _ds:
                            map['dimstyle'][_id] = _ds
                else:
                    image.addDimStyle(_dimstyle)
                    map['dimstyle'][_id] = _dimstyle
        _child = _child.nextSibling

def _load_font_families(image, node, map):
    map['family'] = {}
    _child = node.firstChild
    _families = image.getImageEntities('font')
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:FontFamily":
                _id = int(_child.getAttribute("id"))
                _family = _child.getAttribute("name")
                if _family in _families:
                    for _f in _families:
                        if _f == _family:
                            map['family'][_id] = _f
                else:
                    map['family'][_id] = _family
        _child = _child.nextSibling

def _load_styles(image, node, map):
    map['style'] = {}
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Style":
                _id = int(_child.getAttribute("id"))
                _cid = int(_child.getAttribute("cid"))
                assert _cid in map['color'], "Color index %d missing" % _cid
                _color = map['color'][_cid]
                _ltid = int(_child.getAttribute("ltid"))
                assert _ltid in map['linetype'], "Linetype index %d missing" % _ltid
                _lt = map['linetype'][_ltid]
                _name = _child.getAttribute("name")
                _th = float(_child.getAttribute("thickness"))
                _style = style.Style(_name, _lt, _color, _th)
                if image.hasStyle(_style):
                    for _st in image.getImageEntities('style'):
                        if _style == _st:
                            map['style'][_id] = _st
                            break
                else:
                    image.addStyle(_style)
                    map['style'][_id] = _style
        _child = _child.nextSibling

def _load_globals(image, node, map):
#
# for now, assumes globals are ONLY colors
#
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Global":
                _key = str(_child.getAttribute("key"))
                _cid = int(_child.getAttribute("cid"))
                assert _cid in map['color'], "Color index %d missing" % _cid
                _color = map['color'][_cid]
                (_r, _g, _b) = _color.getColors()
                image.setOption(_key, color.get_color(_r, _g, _b))
        _child = _child.nextSibling


def _load_linetypes(image, node, map):
    map['linetype'] = {}
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Linetype":
                _id = int(_child.getAttribute("id"))
                _name = _child.getAttribute("name")
                _pat = _child.getAttribute("pattern")
                if _pat == "None":
                    _pat = None
                else:
                    _pat = eval(_pat)
                _linetype = linetype.Linetype(_name, _pat)
                if image.hasLinetype(_linetype):
                    for _lt in image.getImageEntities('linetype'):
                        if _lt == _linetype:
                            map['linetype'][_id] = _lt
                            break
                else:
                    image.addLinetype(_linetype)
                    map['linetype'][_id] = _linetype
        _child = _child.nextSibling

def _load_colors(image, node, map):
    map['color'] = {}
    _child = node.firstChild
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            if _child.nodeName == "image:Color":
                _id = int(_child.getAttribute("id"))
                _r = int(_child.getAttribute("r"))
                _g = int(_child.getAttribute("g"))
                _b = int(_child.getAttribute("b"))
                _color = color.get_color(_r, _g, _b)
                if image.hasColor(_color):
                    for _col in image.getImageEntities('color'):
                        if _color == _col:
                            map['color'][_id] = _col
                else:
                    image.addColor(_color)
                    map['color'][_id] = _color
        _child = _child.nextSibling

def load_image(image, filehandle):
    """Load an image from a filename or standard input

load_image(image, filehandle)

The argument image should be an empty image. The
filehandle argument should be an opened file object
that can be passed to the XML parser.
    """
    _objmap = {}
    _doc = xml.dom.minidom.parse(filehandle)
    _root = _doc.documentElement
    if _root.nodeName != "image:Image":
        _doc.unlink()
        raise ValueError, "Expected 'image:Image' but got '%s' as document root!" % _root.name
    _child = _root.firstChild
    while _child.nodeType == xml.dom.Node.TEXT_NODE:
        _child = _child.nextSibling
    if _child.nodeName != "image:Colors":
        _doc.unlink()
        raise ValueError, "Expected 'image:Colors' but got '%s'" % _child.nodeName
    _load_colors(image, _child, _objmap)
    _child = _child.nextSibling
    while _child.nodeType == xml.dom.Node.TEXT_NODE:
        _child = _child.nextSibling
    if _child.nodeName != "image:Linetypes":
        raise ValueError, "Expected 'image:Linetypes' but got '%s'" % _child.nodeName
    _load_linetypes(image, _child, _objmap)
    _child = _child.nextSibling
    while _child.nodeType == xml.dom.Node.TEXT_NODE:
        _child = _child.nextSibling
    if _child.nodeName != "image:Styles":
        _doc.unlink()
        raise ValueError, "Expected 'image:Styles' but got '%s'" % _child.nodeName
    _load_styles(image, _child, _objmap)
    _child = _child.nextSibling
    while _child is not None:
        if _child.nodeType == xml.dom.Node.ELEMENT_NODE:
            _name = _child.nodeName
            if _name == "image:Units":
                _attr = _child.getAttribute("unit")
                if _attr == 'millimeters' or _attr == '0':
                    _unit = units.MILLIMETERS
                elif _attr == 'micrometers' or _attr == '1':
                    _unit = units.MICROMETERS
                elif _attr == 'meters' or _attr == '2':
                    _unit = units.METERS
                elif _attr == 'kilometers' or _attr == '3':
                    _unit = units.KILOMETERS
                elif _attr == 'inches' or _attr == '4':
                    _unit = units.INCHES
                elif _attr == 'feet' or _attr == '5':
                    _unit = units.FEET
                elif _attr == 'yards' or _attr == '6':
                    _unit = units.YARDS
                elif _attr == 'miles' or _attr == '7':
                    _unit = units.MILES
                else:
                    sys.stderr.write("Unknown unit '%s' - using MILLIMETERS\n" % _attr)
                    _unit = units.MILLIMETERS
                image.setUnits(_unit)
            elif _name == "image:FontFamilies":
                _load_font_families(image, _child, _objmap)
            elif _name == "image:DimStyles":
                _load_dimstyles(image, _child, _objmap)
            elif _name == "image:TextStyles":
                _load_textstyles(image, _child, _objmap)
            elif _name == "image:Globals":
                _load_globals(image, _child, _objmap)
            elif _name == "image:Layers":
                _load_layers(image, _child, _objmap)
                for _obj in _objmap['locked']:
                    _obj.setLock(True)
        _child = _child.nextSibling
    _doc.unlink()