File: dxfvectorialreader.pas

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (2285 lines) | stat: -rw-r--r-- 89,145 bytes parent folder | download | duplicates (3)
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
{
Reads DXF files

License: The same modified LGPL as the Free Pascal RTL
         See the file COPYING.modifiedLGPL for more details

AUTHORS: Felipe Monteiro de Carvalho

DXF is composed by records written in ASCII with the following structure:

0
SECTION
section_number
SECTION_NAME
<data>
0
ENDSEC
0

after all sections there is:

EOF

}
unit dxfvectorialreader;

{$mode objfpc}{$H+}

{.$define FPVECTORIALDEBUG}
{.$define FPVECTORIALDEBUG_POLYLINE}
{.$define FPVECTORIALDEBUG_LINE}

interface

uses
  Classes, SysUtils, Math,
  fpcanvas, fpimage,
  fpvectorial, fpvutils,
  lconvencoding;

type
  TDXFToken = class;

  TDXFTokens = class(TFPList)
  public
    destructor Destroy; override;
    procedure Clear;
  end;

  TDXFToken = class
    GroupCode: Integer;
    StrValue: string;
    FloatValue: double;
    IntValue: Integer;
    Childs: TDXFTokens;
    constructor Create;
    Destructor Destroy; override;
  end;

  TPolylineElement = record
    X, Y: Double;
    Color: TFPColor;
  end;

  TSPLineElement = record
    X, Y: Double;
    KnotValue: Integer;
  end;

  TLWPOLYLINEElement = record
    X, Y: Double;
  end;

  { TDXFTokenizer }

  TDXFTokenizer = class
  public
    Tokens: TDXFTokens;
    constructor Create;
    Destructor Destroy; override;
    procedure ReadFromStrings(AStrings: TStrings);
    function  IsTABLES_Subsection(AStr: string): Boolean;
    function  IsBLOCKS_Subsection(AStr: string): Boolean;
    function  IsENTITIES_Subsection(AStr: string): Boolean;
  end;

  { TvDXFVectorialReader }

  TvDXFVectorialReader = class(TvCustomVectorialReader)
  private
    FPointSeparator: TFormatSettings;
    // HEADER data
    ANGBASE: Double;
    ANGDIR: Integer;
    INSBASE, EXTMIN, EXTMAX, LIMMIN, LIMMAX: T3DPoint;
    // Calculated HEADER data
    DOC_OFFSET: T3DPoint; // The DOC_OFFSET compensates for documents with huge coordinates
    ENCODING: string; // In the format utilized by lazutils.lconvencoding
    // For building the POLYLINE objects which is composed of multiple records
    IsReadingPolyline: Boolean;
    IsReadingAttrib: Boolean;
    Polyline: array of TPolylineElement;
    //
    procedure ReadHEADER(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadTABLES(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadTABLES_TABLE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadBLOCKS(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadBLOCKS_BLOCK(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadBLOCKS_ENDBLK(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadENTITIES(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    function  ReadENTITIES_3DFACE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvCircularArc;
    function  ReadENTITIES_LINE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
    function  ReadENTITIES_ARC(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvCircularArc;
    function  ReadENTITIES_CIRCLE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvCircle;
    function  ReadENTITIES_DIMENSION(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvEntity;
    function  ReadENTITIES_ELLIPSE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvEllipse;
    function  ReadENTITIES_INSERT(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvInsert;
    function  ReadENTITIES_TEXT(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvText;
    function  ReadENTITIES_LWPOLYLINE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
    function  ReadENTITIES_SPLINE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
    procedure ReadENTITIES_ATTRIB(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadENTITIES_POLYLINE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    procedure ReadENTITIES_VERTEX(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
    function  ReadENTITIES_SEQEND(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
    function  ReadENTITIES_MTEXT(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvText;
    function  ReadENTITIES_LEADER(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvArrow;
    function  ReadENTITIES_POINT(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvEntity;
    function  InternalReadENTITIES(ATokenStr: string; ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvEntity;
    function  GetCoordinateValue(AStr: shortstring): Double;
    function  ConvertDXFStringToUTF8(AStr: string): string;
    //
    function DXFColorIndexToFPColor(AColorIndex: Integer): TFPColor;
    procedure DXFCoordsToFPCoords(AInX, AInY, AInZ: Double; out AOutX, AOutY, AOutZ: Double);
  public
    { General reading methods }
    Tokenizer: TDXFTokenizer;
    constructor Create; override;
    Destructor Destroy; override;
    procedure ReadFromStrings(AStrings: TStrings; AData: TvVectorialDocument); override;
  end;

implementation

const
  // Items in the HEADER section

  // $ACADVER
  DXF_AUTOCAD_2010        = 'AC1024'; // AutoCAD 2011 and 2012 too
  DXF_AUTOCAD_2007        = 'AC1021'; // AutoCAD 2008 and 2009 too
  DXF_AUTOCAD_2004        = 'AC1018'; // AutoCAD 2005 and 2006 too
  DXF_AUTOCAD_2000        = 'AC1015'; // 1999  In some docs it is proposed as AC1500, but in practice I found AC1015
                                      // http://www.autodesk.com/techpubs/autocad/acad2000/dxf/
                                      // AutoCAD 2000i and 2002 too
  DXF_AUTOCAD_R14         = 'AC1014'; // 1997  http://www.autodesk.com/techpubs/autocad/acadr14/dxf/index.htm
  DXF_AUTOCAD_R13         = 'AC1012'; // 1994
  DXF_AUTOCAD_R11_and_R12 = 'AC1009'; // 1990
  DXF_AUTOCAD_R10         = 'AC1006'; // 1988
  DXF_AUTOCAD_R9          = 'AC1004';

  // Group Codes for ENTITIES
  DXF_ENTITIES_TYPE = 0;
  DXF_ENTITIES_HANDLE = 5;
  DXF_ENTITIES_LINETYPE_NAME = 6;
  DXF_ENTITIES_APPLICATION_GROUP = 102;
  DXF_ENTITIES_AcDbEntity = 100;
  DXF_ENTITIES_MODEL_OR_PAPER_SPACE = 67; // default=0=model, 1=paper
  DXF_ENTITIES_VISIBILITY = 60; // default=0 = Visible, 1 = Invisible

  // Obtained from http://www.generalcadd.com/pdf/LivingWithAutoCAD_v4.pdf
  // and from http://sub-atomic.com/~moses/acadcolors.html
  // Valid for DXF up to AutoCad 2004, after that RGB is available
  AUTOCAD_COLOR_PALETTE: array[0..255] of TFPColor =
  (
    { QBASIC palette
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 0 - Black
    (Red: $0000; Green: $0000; Blue: $8080; Alpha: alphaOpaque), // 1 - Dark blue
    (Red: $0000; Green: $8080; Blue: $0000; Alpha: alphaOpaque), // 2 - Dark green
    (Red: $0000; Green: $8080; Blue: $8080; Alpha: alphaOpaque), // 3 - Dark cyan
    (Red: $8080; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 4 - Dark red
    (Red: $8080; Green: $0000; Blue: $8080; Alpha: alphaOpaque), // 5 - Dark Magenta
    (Red: $8080; Green: $8080; Blue: $0000; Alpha: alphaOpaque), // 6 - Dark
    (Red: $c0c0; Green: $c0c0; Blue: $c0c0; Alpha: alphaOpaque), // 7 - Light Gray
    (Red: $8080; Green: $8080; Blue: $8080; Alpha: alphaOpaque), // 8 - Medium Gray
    (Red: $0000; Green: $0000; Blue: $ffff; Alpha: alphaOpaque), // 9 - Light blue
    (Red: $0000; Green: $ffff; Blue: $0000; Alpha: alphaOpaque), // 10 - Light green
    (Red: $0000; Green: $ffff; Blue: $ffff; Alpha: alphaOpaque), // 11 - Light cyan
    (Red: $ffff; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 12 - Light red
    (Red: $ffff; Green: $0000; Blue: $ffff; Alpha: alphaOpaque), // 13 - Light Magenta
    (Red: $ffff; Green: $ffff; Blue: $0000; Alpha: alphaOpaque), // 14 - Light Yellow
    (Red: $ffff; Green: $ffff; Blue: $ffff; Alpha: alphaOpaque),  // 15 - White }
    // AutoCAD for DOS initial palette from http://www.generalcadd.com/pdf/LivingWithAutoCAD_v4.pdf
    {(Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 0 - Black
    (Red: $ffff; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 1 - Light red
    (Red: $ffff; Green: $ffff; Blue: $0000; Alpha: alphaOpaque), // 2 - Light Yellow
    (Red: $0000; Green: $ffff; Blue: $0000; Alpha: alphaOpaque), // 3 - Light green
    (Red: $0000; Green: $ffff; Blue: $ffff; Alpha: alphaOpaque), // 4 - Light cyan
    (Red: $0000; Green: $0000; Blue: $ffff; Alpha: alphaOpaque), // 5 - Light blue
    (Red: $ffff; Green: $0000; Blue: $ffff; Alpha: alphaOpaque), // 6 - Light Magenta
    (Red: $ffff; Green: $ffff; Blue: $ffff; Alpha: alphaOpaque), // 7 - White
    (Red: $8080; Green: $8080; Blue: $8080; Alpha: alphaOpaque), // 8 - Medium Gray
    (Red: $8080; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 9 - Dark red
    (Red: $8080; Green: $8080; Blue: $0000; Alpha: alphaOpaque), // 10 - Dark Yellow
    (Red: $0000; Green: $8080; Blue: $0000; Alpha: alphaOpaque), // 11 - Dark green
    (Red: $0000; Green: $8080; Blue: $8080; Alpha: alphaOpaque), // 12 - Dark cyan
    (Red: $0000; Green: $0000; Blue: $8080; Alpha: alphaOpaque), // 13 - Dark blue
    (Red: $8080; Green: $0000; Blue: $8080; Alpha: alphaOpaque), // 14 - Dark Magenta
    (Red: $c0c0; Green: $c0c0; Blue: $c0c0; Alpha: alphaOpaque), // 15 - Light Gray}
    // Initial palette from http://sub-atomic.com/~moses/acadcolors.html
    // Agrees with http://gallery.proficad.eu/tools/AutoCAD-Viewer.aspx
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 0 - Black
    (Red: $ffff; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 1 - Light red
    (Red: $ffff; Green: $ffff; Blue: $0000; Alpha: alphaOpaque), // 2 - Light Yellow
    (Red: $0000; Green: $ffff; Blue: $0000; Alpha: alphaOpaque), // 3 - Light green
    (Red: $0000; Green: $ffff; Blue: $ffff; Alpha: alphaOpaque), // 4 - Light cyan
    (Red: $0000; Green: $0000; Blue: $ffff; Alpha: alphaOpaque), // 5 - Light blue
    (Red: $ffff; Green: $0000; Blue: $ffff; Alpha: alphaOpaque), // 6 - Light Magenta
    (Red: $ffff; Green: $ffff; Blue: $ffff; Alpha: alphaOpaque), // 7 - White
    (Red: $4141; Green: $4141; Blue: $4141; Alpha: alphaOpaque), // 8 - Dark Gray
    (Red: $8080; Green: $8080; Blue: $8080; Alpha: alphaOpaque), // 9 - Gray
    (Red: $ffff; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 10 - Light red
    (Red: $ffff; Green: $AAAA; Blue: $AAAA; Alpha: alphaOpaque), // 11
    (Red: $BDBD; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 12
    (Red: $BDBD; Green: $7E7E; Blue: $7E7E; Alpha: alphaOpaque), // 13
    (Red: $8181; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 14
    (Red: $8181; Green: $5656; Blue: $5656; Alpha: alphaOpaque), // 15
    // expanded palette from http://sub-atomic.com/~moses/acadcolors.html
    (Red: $6868; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 16
    (Red: $6868; Green: $4545; Blue: $4545; Alpha: alphaOpaque), // 17
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 18
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 19
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 20
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 21
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 22
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 23
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 24
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 25
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 26
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 27
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 28
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 29
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 30
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 31
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 32
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 33
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 34
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 35
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 36
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 37
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 38
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 39
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 40
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 41
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 42
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 43
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 44
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 45
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 46
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 47
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 48
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 49
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 50
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 51
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 52
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 53
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 54
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 55
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 56
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 57
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 58
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 59
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 60
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 61
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 62
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 63
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 64
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 65
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 66
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 67
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 68
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 69
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 70
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 71
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 72
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 73
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 74
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 75
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 76
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 77
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 78
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 79
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 80
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 81
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 82
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 83
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 84
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 85
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 86
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 87
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 88
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 89
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 90
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 91
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 92
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 93
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 94
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 95
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 96
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 97
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 98
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 99
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 100
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 101
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 102
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 103
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 104
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 105
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 106
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 107
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 108
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 109
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 110
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 111
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 112
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 113
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 114
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 115
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 116
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 117
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 118
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 119
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 120
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 121
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 122
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 123
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 124
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 125
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 126
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 127
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 128
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 129
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 130
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 131
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 132
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 133
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 134
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 135
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 136
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 137
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 138
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 139
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 140
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 141
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 142
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 143
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 144
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 145
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 146
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 147
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 148
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 149
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 150
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 151
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 152
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 153
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 154
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 155
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 156
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 157
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 158
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 159
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 160
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 161
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 162
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 163
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 164
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 165
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 166
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 167
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 168
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 169
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 170
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 171
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 172
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 173
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 174
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 175
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 176
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 177
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 178
    (Red: $3535; Green: $3535; Blue: $4F4F; Alpha: alphaOpaque), // 179
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 180
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 181
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 182
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 183
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 184
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 185
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 186
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 187
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 188
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 189
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 190
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 191
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 192
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 193
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 194
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 195
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 196
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 197
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 198
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 199
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 200
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 201
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 202
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 203
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 204
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 205
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 206
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 207
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 208
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 209
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 210
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 211
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 212
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 213
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 214
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 215
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 216
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 217
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 218
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 219
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 220
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 221
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 222
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 223
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 224
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 225
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 226
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 227
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 228
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 229
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 230
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 231
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 232
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 233
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 234
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 235
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 236
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 237
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 238
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 239
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 240
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 241
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 242
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 243
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 244
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 245
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 246
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 247
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 248
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 249
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 250
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 251
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 252
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 253
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque), // 254
    (Red: $0000; Green: $0000; Blue: $0000; Alpha: alphaOpaque)  // 255
  );

{ TDXFTokens }

destructor TDXFTokens.Destroy;
begin
  Clear;
  inherited;
end;

procedure TDXFTokens.Clear;
var
  j: Integer;
begin
  for j := Count-1 downto 0 do begin
    TObject(Items[j]).Free;
    Delete(j);
  end;
  inherited;
end;


{ TDXFToken }

constructor TDXFToken.Create;
begin
  inherited Create;
  Childs := TDXFTokens.Create;
end;

destructor TDXFToken.Destroy;
begin
  Childs.Free;
  inherited Destroy;
end;

{ TDXFTokenizer }

constructor TDXFTokenizer.Create;
begin
  inherited Create;
  Tokens := TDXFTokens.Create;
end;

destructor TDXFTokenizer.Destroy;
begin
  Tokens.Free;
  inherited Destroy;
end;

procedure TDXFTokenizer.ReadFromStrings(AStrings: TStrings);
var
  i: Integer;
  StrSectionGroupCode, StrSectionName: string;
  IntSectionGroupCode: Integer;
  CurTokenBase, NextTokenBase, SectionTokenBase, LastBlockToken: TDXFTokens;
  NewToken: TDXFToken;
  ParserState: Integer;
begin
  //  Tokens.ForEachCall(); deletecallback
  Tokens.Clear;

  CurTokenBase := Tokens;
  NextTokenBase := Tokens;
  i := 0;
  ParserState := 0;

  while i < AStrings.Count - 1 do
  begin
    CurTokenBase := NextTokenBase;

    // Now read and process the section name
    StrSectionGroupCode := AStrings.Strings[i];
    IntSectionGroupCode := StrToInt(Trim(StrSectionGroupCode));
    StrSectionName := AStrings.Strings[i+1];

    NewToken := TDXFToken.Create;
    NewToken.GroupCode := IntSectionGroupCode;
    NewToken.StrValue := StrSectionName;

    // Waiting for a section
    if ParserState = 0 then
    begin
      if (StrSectionName = 'SECTION') then
      begin
        ParserState := 1;
        NextTokenBase := NewToken.Childs;
      end
      else if (StrSectionName = 'EOF') then
      begin
        NewToken.Free;
        Exit;
      end
      // Comments can be in the beginning of the file and start with 999
      else if (IntSectionGroupCode = 999) then
      begin
        // nothing to be done, let it add the token
      end
      else
      begin
        raise Exception.Create(Format(
          'TDXFTokenizer.ReadFromStrings: Expected SECTION, but got: %s', [StrSectionname]));
      end;
    end
    // Processing the section name
    else if ParserState = 1 then
    begin
      if (StrSectionName = 'HEADER') or
        (StrSectionName = 'CLASSES') or
        (StrSectionName = 'OBJECTS') or
        (StrSectionName = 'THUMBNAILIMAGE') then
      begin
        ParserState := 2;
        SectionTokenBase := CurTokenBase;
      end
      else if (StrSectionName = 'BLOCKS') or (StrSectionName = 'TABLES') then
      begin
        ParserState := 4;
        SectionTokenBase := CurTokenBase;
      end
      else if (StrSectionName = 'ENTITIES') then
      begin
        ParserState := 3;
        SectionTokenBase := CurTokenBase;
      end
      else
      begin
        raise Exception.Create(Format(
          'TDXFTokenizer.ReadFromStrings: Invalid section name: %s', [StrSectionname]));
      end;
    end
    // Reading a generic section
    else if ParserState = 2 then
    begin
      if StrSectionName = 'ENDSEC' then
      begin
        ParserState := 0;
        CurTokenBase := SectionTokenBase;
        NextTokenBase := Tokens;
      end;
    end
    // Reading the ENTITIES section
    else if ParserState = 3 then
    begin
      if IsENTITIES_Subsection(StrSectionName) then
      begin
        CurTokenBase := SectionTokenBase;
        NextTokenBase := NewToken.Childs;
      end
      else if StrSectionName = 'ENDSEC' then
      begin
        ParserState := 0;
        CurTokenBase := SectionTokenBase;
        NextTokenBase := Tokens;
      end;
    end
    // Reading the TABLES or BLOCKS sections
    else if ParserState = 4 then
    begin
      // This orders the blocks themselves
      if IsTABLES_Subsection(StrSectionName) or IsBLOCKS_Subsection(StrSectionName) then
      begin
        CurTokenBase := SectionTokenBase;
        NextTokenBase := NewToken.Childs;
        LastBlockToken := NewToken.Childs;
      end
      // This orders the entities inside blocks
      else if IsENTITIES_Subsection(StrSectionName) and (LastBlockToken <> nil) then
      begin
        CurTokenBase := LastBlockToken;
        NextTokenBase := NewToken.Childs;
      end
      else if StrSectionName = 'ENDSEC' then
      begin
        ParserState := 0;
        CurTokenBase := SectionTokenBase;
        NextTokenBase := Tokens;
      end;
    end;

    CurTokenBase.Add(NewToken);

    Inc(i, 2);
  end;
end;

function TDXFTokenizer.IsTABLES_Subsection(AStr: string): Boolean;
begin
  Result :=
    (AStr = 'TABLE');
end;

function TDXFTokenizer.IsBLOCKS_Subsection(AStr: string): Boolean;
begin
  Result :=
    (AStr = 'BLOCK') or
    (AStr = 'ENDBLK');
end;

function TDXFTokenizer.IsENTITIES_Subsection(AStr: string): Boolean;
begin
  Result :=
    (AStr = '3DFACE') or
    (AStr = '3DSOLID') or
    (AStr = 'ACAD_PROXY_ENTITY') or
    (AStr = 'ARC') or
    (AStr = 'ATTDEF') or
    (AStr = 'ATTRIB') or
    (AStr = 'BODY') or
    (AStr = 'CIRCLE') or
    (AStr = 'DIMENSION') or
    (AStr = 'ELLIPSE') or
    (AStr = 'HATCH') or
    (AStr = 'IMAGE') or
    (AStr = 'INSERT') or
    (AStr = 'LEADER') or
    (AStr = 'LINE') or
    (AStr = 'LWPOLYLINE') or
    (AStr = 'MLINE') or
    (AStr = 'MTEXT') or
    (AStr = 'OLEFRAME') or
    (AStr = 'OLE2FRAME') or
    (AStr = 'POINT') or
    (AStr = 'POLYLINE') or
    (AStr = 'RAY') or
    (AStr = 'REGION') or
    (AStr = 'SEQEND') or
    (AStr = 'SHAPE') or
    (AStr = 'SOLID') or
    (AStr = 'SPLINE') or
    (AStr = 'TEXT') or
    (AStr = 'TOLERANCE') or
    (AStr = 'TRACE') or
    (AStr = 'VERTEX') or
    (AStr = 'VIEWPORT') or
    (AStr = 'XLINE');
end;

{ TvDXFVectorialReader }

procedure TvDXFVectorialReader.ReadHEADER(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
  i, j: Integer;
  lStr: string;
  CurToken: TDXFToken;
  CurField: P3DPoint;
begin
  i := 0;
  while i < ATokens.Count do
  begin
    CurToken := TDXFToken(ATokens.Items[i]);
    if CurToken.StrValue = '$ANGBASE' then
    begin
      CurToken := TDXFToken(ATokens.Items[i+1]);
      ANGBASE := StrToFloat(CurToken.StrValue, FPointSeparator);
      Inc(i);
    end
    else if CurToken.StrValue = '$ANGDIR' then
    begin
      CurToken := TDXFToken(ATokens.Items[i+1]);
      ANGDIR := StrToInt(CurToken.StrValue);
      Inc(i);
    end
    // This indicates the size of the document
    else if (CurToken.StrValue = '$INSBASE') or
      (CurToken.StrValue = '$EXTMIN') or (CurToken.StrValue = '$EXTMAX') or
      (CurToken.StrValue = '$LIMMIN') or (CurToken.StrValue = '$LIMMAX') then
    begin
      if (CurToken.StrValue = '$INSBASE') then CurField := @INSBASE
      else if (CurToken.StrValue = '$EXTMIN') then CurField := @EXTMIN
      else if (CurToken.StrValue = '$EXTMAX') then CurField := @EXTMAX
      else if (CurToken.StrValue = '$LIMMIN') then CurField := @LIMMIN
      else if (CurToken.StrValue = '$LIMMAX') then CurField := @LIMMAX;

      // Check the next 2 items and verify if they are the values of the size of the document
      for j := 0 to 1 do
      begin
        CurToken := TDXFToken(ATokens.Items[i+1]);
        case CurToken.GroupCode of
        10:
        begin;
          CurField^.X := StrToFloat(CurToken.StrValue, FPointSeparator);
          Inc(i);
        end;
        20:
        begin
          CurField^.Y := StrToFloat(CurToken.StrValue, FPointSeparator);
          Inc(i);
        end;
        end;
      end;
    end
    else if CurToken.StrValue = '$DWGCODEPAGE' then
    begin
      // if we are forcing an encoding, don't use the value from the HEADER
      if ADoc.ForcedEncodingOnRead = '' then
      begin
        CurToken := TDXFToken(ATokens.Items[i+1]);
        lStr := CurToken.StrValue;
        if lStr = 'ANSI_1252' then ENCODING := 'CP1252';
      end;
      Inc(i);
    end;

    Inc(i);
  end;

  // After getting all the data, we can try to make some sense out of it

  // Sometimes EXTMIN comes as 10^20 and EXTMAX as -10^20, which makes no sence
  // In these cases we need to ignore them.
  if (EXTMIN.X > 10000000000) or (EXTMIN.X < -10000000000)
  or (EXTMAX.X > 10000000000) or (EXTMAX.X < -10000000000) then
  begin
    DOC_OFFSET.X := 0;
    DOC_OFFSET.Y := 0;

    AData.Width := LIMMAX.X;
    AData.Height := LIMMAX.Y;
  end
  else
  begin
    // The size of the document seams to be given by:
    // DOC_SIZE = min(EXTMAX, LIMMAX) - DOC_OFFSET;
    // if EXTMIN is <> -infinite then DOC_OFFSET = EXTMIN else DOC_OFFSET = (0, 0)
    // We will shift the whole document so that it has only positive coordinates and
    // DOC_OFFSET will be utilized for that

    if EXTMIN.X > -100 then
    begin
      DOC_OFFSET.X := EXTMIN.X;
      DOC_OFFSET.Y := EXTMIN.Y;
    end
    else FillChar(DOC_OFFSET, sizeof(T3DPoint), #0);

    AData.Width := min(EXTMAX.X, LIMMAX.X) - DOC_OFFSET.X;
    AData.Height := min(EXTMAX.Y, LIMMAX.Y) - DOC_OFFSET.Y;
  end;
end;

procedure TvDXFVectorialReader.ReadTABLES(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
  i: Integer;
  CurToken: TDXFToken;
begin
  for i := 0 to ATokens.Count - 1 do
  begin
    CurToken := TDXFToken(ATokens.Items[i]);
    if CurToken.StrValue = 'TABLE' then ReadTABLES_TABLE(CurToken.Childs, AData, ADoc)
    else
    begin
      // ...
    end;
  end;
end;

procedure TvDXFVectorialReader.ReadTABLES_TABLE(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
  CurToken: TDXFToken;
  i: Integer;
  // BLOCK data
  lName: string;
  PosX, PosY, PosZ: Double;
  lBlock: TvBlock = nil;
  lEntity: TvEntity = nil;
begin
  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      2: lName := CurToken.StrValue;
      10: PosX := CurToken.FloatValue;
      20: PosY := CurToken.FloatValue;
      30: PosZ := CurToken.FloatValue;
      0:
      begin
        if lBlock = nil then
          lBlock := AData.AddBlock(lName, PosX, PosY, PosZ);
        lEntity := InternalReadENTITIES(CurToken.StrValue, CurToken.Childs, AData, ADoc, True);

        if lEntity <> nil then
          lBlock.AddEntity(lEntity);
      end;
    end;
  end;
end;

procedure TvDXFVectorialReader.ReadBLOCKS(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
  i: Integer;
  CurToken: TDXFToken;
begin
  for i := 0 to ATokens.Count - 1 do
  begin
    CurToken := TDXFToken(ATokens.Items[i]);
    if CurToken.StrValue = 'BLOCK' then ReadBLOCKS_BLOCK(CurToken.Childs, AData, ADoc)
    else if CurToken.StrValue = 'ENDBLK' then ReadBLOCKS_ENDBLK(CurToken.Childs, AData, ADoc)
    else
    begin
      // ...
    end;
  end;
end;

(*
The following group codes apply to block entities. For information about abbreviations and formatting used in this table, see "Formatting Conventions in This Reference."
Block group codes Group codes	Description
0 Entity type (BLOCK)
5 Handle
102 (optional) Start of application-defined group "{application_name". For example, "{ACAD_REACTORS" indicates the start of the AutoCAD persistent reactors group.
application-defined codes (optional) Codes and values within the 102 groups are application defined
102 (optional) End of group, "}"
330 Soft-pointer ID/handle to owner object
100 Subclass marker (AcDbEntity)
8 Layer name
100 Subclass marker (AcDbBlockBegin)
2 Block name
70 Block-type flags (bit coded values, may be combined):
1 = This is an anonymous block generated by hatching, associative dimensioning, other internal operations, or an application.
2 = This block has non-constant attribute definitions (this bit is not set if the block has any attribute definitions that are constant, or has no attribute definitions at all).
4 = This block is an external reference (xref).
8 = This block is an xref overlay.
16 = This block is externally dependent.
32 = This is a resolved external reference, or dependent of an external reference (ignored on input).
64 = This definition is a referenced external reference (ignored
on input).
10 Base point DXF: X value; APP: 3D point
20, 30 DXF: Y and Z values of base point
3 Block name
1 Xref path name
4 Block description (optional)
*)
procedure TvDXFVectorialReader.ReadBLOCKS_BLOCK(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
  CurToken: TDXFToken;
  i: Integer;
  // BLOCK data
  lName: string;
  PosX, PosY, PosZ: Double;
  lBlock: TvBlock = nil;
  lEntity: TvEntity = nil;
begin
  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      2: lName := CurToken.StrValue;
      10: PosX := CurToken.FloatValue;
      20: PosY := CurToken.FloatValue;
      30: PosZ := CurToken.FloatValue;
      0:
      begin
        if lBlock = nil then
          lBlock := AData.AddBlock(lName, PosX, PosY, PosZ);
        lEntity := InternalReadENTITIES(CurToken.StrValue, CurToken.Childs, AData, ADoc, True);

        if lEntity <> nil then
          lBlock.AddEntity(lEntity);
      end;
    end;
  end;
end;

procedure TvDXFVectorialReader.ReadBLOCKS_ENDBLK(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
begin

end;

procedure TvDXFVectorialReader.ReadENTITIES(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
  i: Integer;
  CurToken: TDXFToken;
  lEntity: TvEntity; // only to help debugging
begin
  IsReadingPolyline := False;

  for i := 0 to ATokens.Count - 1 do
  begin
    CurToken := TDXFToken(ATokens.Items[i]);
    lEntity := InternalReadENTITIES(CurToken.StrValue, CurToken.Childs, AData, ADoc);
  end;
end;

function TvDXFVectorialReader.ReadENTITIES_3DFACE(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean
  ): TvCircularArc;
var
  CurToken: TDXFToken;
  lPolygon: TvPolygon;
  i: Integer;
begin
  Result := nil;
  lPolygon := TvPolygon.Create(nil);
  SetLength(lPolygon.Points, 3);

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 11, 21, 31, 12, 22, 32, 13, 23, 33, 70] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      10: lPolygon.Points[0].X := CurToken.FloatValue;
      20: lPolygon.Points[0].Y := CurToken.FloatValue;
      30: lPolygon.Points[0].Z := CurToken.FloatValue;
      11: lPolygon.Points[1].X := CurToken.FloatValue;
      21: lPolygon.Points[1].Y := CurToken.FloatValue;
      31: lPolygon.Points[1].Z := CurToken.FloatValue;
      12: lPolygon.Points[2].X := CurToken.FloatValue;
      22: lPolygon.Points[2].Y := CurToken.FloatValue;
      32: lPolygon.Points[2].Z := CurToken.FloatValue;
      13:
      begin
        SetLength(lPolygon.Points, 4);
        lPolygon.Points[3].X := CurToken.FloatValue;
      end;
      23:
      begin
        SetLength(lPolygon.Points, 4);
        lPolygon.Points[3].Y := CurToken.FloatValue;
      end;
      33:
      begin
        SetLength(lPolygon.Points, 4);
        lPolygon.Points[3].Z := CurToken.FloatValue;
      end;
      {
        Invisible edge flags (optional; default = 0):
        1 = First edge is invisible
        2 = Second edge is invisible
        4 = Third edge is invisible
        8 = Fourth edge is invisible
      }
      70:
      begin
      end;
    end;
  end;

  DXFCoordsToFPCoords(lPolygon.Points[0].X, lPolygon.Points[0].Y, lPolygon.Points[0].Z,
    lPolygon.Points[0].X, lPolygon.Points[0].Y, lPolygon.Points[0].Z);
  DXFCoordsToFPCoords(lPolygon.Points[1].X, lPolygon.Points[1].Y, lPolygon.Points[1].Z,
    lPolygon.Points[1].X, lPolygon.Points[1].Y, lPolygon.Points[1].Z);
  DXFCoordsToFPCoords(lPolygon.Points[2].X, lPolygon.Points[2].Y, lPolygon.Points[2].Z,
    lPolygon.Points[2].X, lPolygon.Points[2].Y, lPolygon.Points[2].Z);
  if Length(lPolygon.Points) >= 4 then
    DXFCoordsToFPCoords(lPolygon.Points[3].X, lPolygon.Points[3].Y, lPolygon.Points[3].Z,
      lPolygon.Points[3].X, lPolygon.Points[3].Y, lPolygon.Points[3].Z);

  AData.AddEntity(lPolygon);
end;

function TvDXFVectorialReader.ReadENTITIES_LINE(ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
var
  CurToken: TDXFToken;
  i: Integer;
  // LINE
  LineStartX, LineStartY, LineStartZ: Double;
  LineEndX, LineEndY, LineEndZ: Double;
  LLineColor: TFPColor;
begin
  Result := nil;

  // Initial values
  LineStartX := 0;
  LineStartY := 0;
  LineStartZ := 0;
  LineEndX := 0;
  LineEndY := 0;
  LineEndZ := 0;
  LLineColor := colBlack;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 11, 21, 31, 62] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      10: LineStartX := CurToken.FloatValue;
      20: LineStartY := CurToken.FloatValue;
      30: LineStartZ := CurToken.FloatValue;
      11: LineEndX := CurToken.FloatValue;
      21: LineEndY := CurToken.FloatValue;
      31: LineEndZ := CurToken.FloatValue;
      62: LLineColor := DXFColorIndexToFPColor(Trunc(CurToken.FloatValue));
    end;
  end;

  // Position fixing for documents with negative coordinates
  LineStartX := LineStartX - DOC_OFFSET.X;
  LineStartY := LineStartY - DOC_OFFSET.Y;
  LineEndX := LineEndX - DOC_OFFSET.X;
  LineEndY := LineEndY - DOC_OFFSET.Y;

  // And now write it
  {$ifdef FPVECTORIALDEBUG_LINE}
  WriteLn(Format('Adding Line from %f^%f to %f^%f RGB=%d %d %d A=%d', [LineStartX, LineStartY, LineEndX, LineEndY,
    LLineColor.red div $100, LLineColor.Green div $100, LLineColor.Blue div $100, LLineColor.Alpha div $100]));
  {$endif}
  if AOnlyCreate then
  begin
    Result := TPath.Create(AData);
    Result.AppendMoveToSegment(LineStartX, LineStartY);
    Result.AppendLineToSegment(LineEndX, LineEndY);
  end
  else
  begin
    AData.StartPath(LineStartX, LineStartY);
    AData.AddLineToPath(LineEndX, LineEndY, LLineColor);
    AData.EndPath();
  end;
end;

{
Arcs are always counter-clockwise in DXF

100 Subclass marker (AcDbCircle)
39 Thickness (optional; default = 0)
10 Center point (in OCS) DXF: X value; APP: 3D point
20, 30 DXF: Y and Z values of center point (in OCS)
40 Radius
100 Subclass marker (AcDbArc)
50 Start angle (degrees)
51 End angle (degrees)
210 Extrusion direction. (optional; default = 0, 0, 1) DXF: X value; APP: 3D vector
220, 230 DXF: Y and Z values of extrusion direction (optional)
}
function TvDXFVectorialReader.ReadENTITIES_ARC(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvCircularArc;
var
  CurToken: TDXFToken;
  i: Integer;
  CenterX, CenterY, CenterZ, Radius, StartAngle, EndAngle: Double;
  LColor: TFPColor;
begin
  CenterX := 0.0;
  CenterY := 0.0;
  CenterZ := 0.0;
  Radius := 0.0;
  StartAngle := 0.0;
  EndAngle := 0.0;
  LColor := colBlack;
  Result := nil;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 40, 50, 51, 62] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      10: CenterX := CurToken.FloatValue;
      20: CenterY := CurToken.FloatValue;
      30: CenterZ := CurToken.FloatValue;
      40: Radius := CurToken.FloatValue;
      50: StartAngle := CurToken.FloatValue;
      51: EndAngle := CurToken.FloatValue;
      62: LColor := DXFColorIndexToFPColor(Trunc(CurToken.FloatValue));
    end;
  end;

  // In DXF the EndAngle is always greater then the StartAngle.
  // If it isn't then sum 360 to it to make sure we don't get wrong results
  if EndAngle < StartAngle then EndAngle := EndAngle + 360;

  // Position fixing for documents with negative coordinates
  CenterX := CenterX - DOC_OFFSET.X;
  CenterY := CenterY - DOC_OFFSET.Y;

  {$ifdef FPVECTORIALDEBUG}
  WriteLn(Format('Adding Arc Center=%f,%f Radius=%f StartAngle=%f EndAngle=%f AOnlyCreate=%d',
    [CenterX, CenterY, Radius, StartAngle, EndAngle, Integer(AOnlyCreate)]));
  {$endif}
  Result := AData.AddCircularArc(CenterX, CenterY, Radius, StartAngle, EndAngle, LColor, AOnlyCreate);
end;

{
Group codes	Description
100 Subclass marker (AcDbCircle)
39 Thickness (optional; default = 0)
10 Center point (in OCS) DXF: X value; APP: 3D point
20, 30 DXF: Y and Z values of center point (in OCS)
40 Radius
210 Extrusion direction (optional; default = 0, 0, 1) DXF: X value; APP: 3D vector
220, 230 DXF: Y and Z values of extrusion direction  (optional)
}
function TvDXFVectorialReader.ReadENTITIES_CIRCLE(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvCircle;
var
  CurToken: TDXFToken;
  i: Integer;
  CircleCenterX, CircleCenterY, CircleCenterZ, CircleRadius: Double;
begin
  CircleCenterX := 0.0;
  CircleCenterY := 0.0;
  CircleCenterZ := 0.0;
  CircleRadius := 0.0;
  Result := nil;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 40] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      10: CircleCenterX := CurToken.FloatValue;
      20: CircleCenterY := CurToken.FloatValue;
      30: CircleCenterZ := CurToken.FloatValue;
      40: CircleRadius := CurToken.FloatValue;
    end;
  end;

  // Position fixing for documents with negative coordinates
  CircleCenterX := CircleCenterX - DOC_OFFSET.X;
  CircleCenterY := CircleCenterY - DOC_OFFSET.Y;

  Result := AData.AddCircle(CircleCenterX, CircleCenterY, CircleRadius, AOnlyCreate);
end;

{
Group codes Description
100 Subclass marker (AcDbDimension)
2 Name of the block that contains the entities that make up the dimension picture
10 Definition point (in WCS) DXF: X value; APP: 3D point
20, 30 DXF: Y and Z values of definition point (in WCS)
11 Middle point of dimension text (in OCS) DXF: X value; APP: 3D point
21, 31 DXF: Y and Z values of middle point of dimension text (in OCS)
70 Dimension type.
  Values 0-6 are integer values that represent the dimension type.
  Values 32, 64, and 128 are bit values, which are added to the integer values
  (value 32 is always set in R13 and later releases).
  0 = Rotated, horizontal, or vertical; 1 = Aligned;
  2 = Angular; 3 = Diameter; 4 = Radius;
  5 = Angular 3 point; 6 = Ordinate;
  32 = Indicates that the block reference (group code 2) is referenced by this dimension only.
  64 = Ordinate type. This is a bit value (bit 7) used only with integer value 6.
    If set, ordinate is X-type; if not set, ordinate is Y-type.
  128 = This is a bit value (bit 8) added to the other group 70 values
    if the dimension text has been positioned at a user-defined location
    rather than at the default location.
71 Attachment point:
  1 = Top left; 2 = Top center; 3 = Top right;
  4 = Middle left; 5 = Middle center; 6 = Middle right;
  7 = Bottom left; 8 = Bottom center; 9 = Bottom right
72 Dimension text line spacing style (optional):
  1(or missing) = At least (taller characters will override)
  2 = Exact (taller characters will not override)
41 Dimension text line spacing factor (optional):
  Percentage of default (3-on-5) line spacing to be applied. Valid values range from 0.25 to 4.00.
42 Actual measurement (optional; read-only value)
1 Dimension text explicitly entered by the user. Optional; default is the measurement.
  If null or "<>", the dimension measurement is drawn as the text,
  if " " (one blank space), the text is suppressed. Anything else is drawn as the text.
53 The optional group code 53 is the rotation angle of the dimension
  text away from its default orientation (the direction of the dimension line)  (optional).
51 All dimension types have an optional 51 group code, which indicates the
  horizontal direction for the dimension entity. The dimension entity determines
  the orientation of dimension text and lines for horizontal, vertical, and
  rotated linear dimensions.
  This group value is the negative of the angle between the OCS X axis
  and the UCS X axis. It is always in the XY plane of the OCS.
210 Extrusion direction (optional; default = 0, 0, 1) DXF: X value; APP: 3D vector
220, 230 DXF: Y and Z values of extrusion direction  (optional)
3 Dimension style name

*Aligned Dimension Group Codes

100 Subclass marker (AcDbAlignedDimension)
12 Insertion point for clones of a dimension-Baseline and Continue (in OCS) DXF: X value; APP: 3D point
22, 32 DXF: Y and Z values of insertion point for clones of a dimension-Baseline and Continue (in OCS)
13 Definition point for linear and angular dimensions (in WCS) DXF: X value; APP: 3D point
23, 33 DXF: Y and Z values of definition point for linear and angular dimensions (in WCS)
14 Definition point for linear and angular dimensions (in WCS) DXF: X value; APP: 3D point
24, 34 DXF: Y and Z values of definition point for linear and angular dimensions (in WCS)

  |--text--|->10,20
  |        |
  |        |
  X->14,24 X->13,23

*Radial and Diameter Dimension Group Codes
http://www.autodesk.com/techpubs/autocad/acadr14/dxf/index.htm

100 Subclass marker (AcDbRadialDimension or AcDbDiametricDimension)
15  Definition point for diameter, radius, and angular dimensions (in WCS). DXF: X value; APP: 3D point
25, 35  DXF: Y and Z values of definition point for diameter, radius, and angular dimensions (in WCS).
40 Leader length for radius and diameter dimensions.

The point (15,25,35) specifies the first point of the dimension line on the circle/arc
and the point (10,20,30) specifies the point opposite the first point.
The point (11,21,31) specifies the midpoint of the dimension text.
}
function TvDXFVectorialReader.ReadENTITIES_DIMENSION(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvEntity;
type
  TDXFDimensionType = (ddtUnknown, ddtAligned, ddtRadial, ddtDiametric, ddt2LineAngular);
var
  CurToken: TDXFToken;
  i: Integer;
  // DIMENSION
  BaseLeft, BaseRight, DimensionRight, DimensionLeft, lCenter, lTextPos, TmpPoint: T3DPoint;
  TmpDimensionRight, TmpDimensionLeft: T3DPoint;
  Dim0, Dim1, Dim3, Dim4, Dim5, Dim6: T3DPoint;
  lAngleLeft, lAngleRight, al, bl, lRadius, lBaskaraDelta, ae, be, ce: Double;
  DXFDimensionType: TDXFDimensionType = ddtUnknown;
begin
  // Initial values
  BaseLeft.X := 0;
  BaseLeft.Y := 0;
  BaseRight.X := 0;
  BaseRight.X := 0;
  DimensionRight.X := 0;
  DimensionRight.Y := 0;
  DimensionLeft.X := 0;
  DimensionLeft.Y := 0;
  Result := nil;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 11, 21, 31, 13, 23, 33, 14, 24, 34, 15, 25, 35, 16, 26, 36] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      10: Dim0.X := CurToken.FloatValue;
      20: Dim0.Y := CurToken.FloatValue;
      30: Dim0.Z := CurToken.FloatValue;
      11: Dim1.X := CurToken.FloatValue;
      21: Dim1.Y := CurToken.FloatValue;
      31: Dim1.Z := CurToken.FloatValue;
      13: Dim3.X := CurToken.FloatValue;
      23: Dim3.Y := CurToken.FloatValue;
      33: Dim3.Z := CurToken.FloatValue;
      14: Dim4.X := CurToken.FloatValue;
      24: Dim4.Y := CurToken.FloatValue;
      34: Dim4.Z := CurToken.FloatValue;
      15: Dim5.X := CurToken.FloatValue;
      25: Dim5.Y := CurToken.FloatValue;
      35: Dim5.Z := CurToken.FloatValue;
      16: Dim6.X := CurToken.FloatValue;
      26: Dim6.Y := CurToken.FloatValue;
      36: Dim6.Z := CurToken.FloatValue;
      100:
      begin
        if CurToken.StrValue = 'AcDbAlignedDimension' then DXFDimensionType := ddtAligned
        else if CurToken.StrValue = 'AcDbRadialDimension' then DXFDimensionType := ddtRadial
        else if CurToken.StrValue = 'AcDbDiametricDimension' then DXFDimensionType := ddtDiametric
        else if CurToken.StrValue = 'AcDb2LineAngularDimension' then DXFDimensionType := ddt2LineAngular;
      end;
    end;
  end;

  // Position fixing for documents with negative coordinates
  Dim0.X := Dim0.X - DOC_OFFSET.X;
  Dim0.Y := Dim0.Y - DOC_OFFSET.Y;
  Dim1.X := Dim1.X - DOC_OFFSET.X;
  Dim1.Y := Dim1.Y - DOC_OFFSET.Y;
  Dim3.X := Dim3.X - DOC_OFFSET.X;
  Dim3.Y := Dim3.Y - DOC_OFFSET.Y;
  Dim4.X := Dim4.X - DOC_OFFSET.X;
  Dim4.Y := Dim4.Y - DOC_OFFSET.Y;
  Dim5.X := Dim5.X - DOC_OFFSET.X;
  Dim5.Y := Dim5.Y - DOC_OFFSET.Y;
  Dim6.X := Dim6.X - DOC_OFFSET.X;
  Dim6.Y := Dim6.Y - DOC_OFFSET.Y;

  // Standard value meaning
  DimensionRight := Dim0;
  BaseRight := Dim3;
  BaseLeft := Dim4;

  // And now write it
  {$ifdef FPVECTORIALDEBUG}
//  WriteLn(Format('Adding Line from %f,%f to %f,%f', [LineStartX, LineStartY, LineEndX, LineEndY]));
  {$endif}
  // -----------------------------------------------
  // Aligned dimension
  // -----------------------------------------------
  if DXFDimensionType = ddtAligned then
  begin
    // Now make sure that we actually that BaseLeft is to the left of BaseRight
    if BaseRight.X < BaseLeft.X then
    begin
      TmpPoint := BaseRight;
      BaseRight := BaseLeft;
      BaseLeft := TmpPoint;
    end;

    // Now check if we are a horizontal or vertical dimension

    // horizontal
    //
    //DL____ DR
    //  |  |
    //  |  |
    // BL  BR
    if DimensionRight.X = BaseRight.X then
    begin
      DimensionLeft.X := BaseLeft.X;
      DimensionLeft.Y := DimensionRight.Y;
    end
    // Inverted horizontal where DL was provided
    else if DimensionRight.X = BaseLeft.X then
    begin
      DimensionLeft.X := DimensionRight.X;
      DimensionLeft.Y := DimensionRight.Y;
      DimensionRight.X := BaseRight.X;
    end
    // vertical
    //
    // BL ----|DR
    //  BR  --|DL
    //
    // In this case we invert then DR and DL
    else if DimensionRight.Y = BaseLeft.Y then
    begin
      DimensionLeft := DimensionRight;
      DimensionRight.Y := BaseRight.Y;
    end
    // vertical
    //
    // BL ----|DL
    //  BR  --|DR
    //
    else if DimensionRight.Y = BaseRight.Y then
    begin
      DimensionLeft.X := DimensionRight.X;
      DimensionLeft.Y := BaseLeft.Y;
    end;

    Result := AData.AddAlignedDimension(BaseLeft, BaseRight, DimensionLeft, DimensionRight, AOnlyCreate);
  end
  // -----------------------------------------------
  // Radius and Diameters are very similar
  // -----------------------------------------------
  else if DXFDimensionType in [ddtRadial, ddtDiametric] then
  begin
    if DXFDimensionType = ddtRadial then
    begin
      lCenter := DimensionRight;
      DimensionLeft := Dim5;
    end
    else
    begin
      lCenter := Dim1;
      DimensionLeft := Dim5;
      DimensionRight.X := DimensionRight.X;
      DimensionRight.Y := DimensionRight.Y;
    end;

    Result := AData.AddRadialDimension(DXFDimensionType = ddtDiametric, lCenter, DimensionLeft, DimensionRight, AOnlyCreate);
  end
  // -----------------------------------------------
  // A arc dimension
  // -----------------------------------------------
  else if DXFDimensionType = ddt2LineAngular then
  begin
    lCenter := Dim6;
    lTextPos := Dim1;

    // Sometimes Dim0 and Dim3 are the base, but sometimes Dim4 and Dim5 are the base!
    // So we need to check which points are nearer o.O
    // I plotted the given points to come to this conclusion
    // and it doesn't match the provided example image:
    // http://docs.autodesk.com/ACD/2013/RUS/images/GUID-E2F42FD3-2684-4F50-88A9-3AF3A5824FF1-low.png
    // but it works. The pairs of points in the same line are 3,4 and 0,5
    if Abs(Dim0.X-Dim3.X)+Abs(Dim0.Y-Dim3.Y) < Abs(Dim5.X-Dim4.X)+Abs(Dim5.Y-Dim4.Y) then
    begin
      BaseLeft := Dim0;
      BaseRight := Dim3;
      TmpDimensionLeft := Dim5;
      TmpDimensionRight := Dim4;
    end
    else
    begin
      BaseLeft := Dim4;
      BaseRight := Dim5;
      TmpDimensionLeft := Dim3;
      TmpDimensionRight := Dim0;
    end;

    // Calculate where the arc hits the left and right lines to obtain DimensionLeft and DimensionRight
    // Left line is: Y = al.X + bl
    // Circle equation for the arc is (X - BaseLeft.X)^2 + (Y - BaseLeft.Y)^2 = R^2
    // This goes to a second degree equation of ae * X^2 + be * X + ce = 0
    lAngleLeft := arctan(Abs(BaseLeft.Y-TmpDimensionLeft.Y)/Abs(BaseLeft.X-TmpDimensionLeft.X));
    if TmpDimensionLeft.X<BaseLeft.X then lAngleLeft := Pi-lAngleLeft;
    lRadius := sqrt(sqr(lCenter.Y-BaseLeft.Y) + sqr(lCenter.X-BaseLeft.X));
    al := Tan(lAngleLeft);
    bl := BaseLeft.Y - al * BaseLeft.X;
    ae := 1 + al*al;
    be := -2 * BaseLeft.X + 2 * al * bl - 2 * al * BaseLeft.Y;
    ce := sqr(BaseLeft.X)+bl*bl-2*bl*BaseLeft.Y+sqr(BaseLeft.Y)-lRadius*lRadius;
    lBaskaraDelta := be*be-4*ae*ce;
    // The equation has 2 solutions, get the one nearest to TmpDimension
    DimensionLeft.X := (-be+sqrt(lBaskaraDelta)) / (2 * ae);
    DimensionLeft.Y := (-be-sqrt(lBaskaraDelta)) / (2 * ae);
    if Abs(DimensionLeft.X-TmpDimensionLeft.X)>Abs(DimensionLeft.Y-TmpDimensionLeft.X)then
      DimensionLeft.X := DimensionLeft.Y;
    DimensionLeft.Y := al * DimensionLeft.X + bl;

    lAngleRight := arctan(Abs(BaseRight.Y-TmpDimensionRight.Y)/Abs(BaseRight.X-TmpDimensionRight.X));
    if TmpDimensionRight.X<BaseRight.X then lAngleRight := Pi-lAngleRight;
    al := Tan(lAngleRight);
    bl := BaseRight.Y - al * BaseRight.X;
    ae := 1 + al*al;
    be := -2 * BaseRight.X + 2 * al * bl - 2 * al * BaseRight.Y;
    ce := sqr(BaseRight.X)+bl*bl-2*bl*BaseRight.Y+sqr(BaseRight.Y)-lRadius*lRadius;
    lBaskaraDelta := be*be-4*ae*ce;
    // The equation has 2 solutions, get the one nearest to TmpDimension
    DimensionRight.X := (-be+sqrt(lBaskaraDelta)) / (2 * ae);
    DimensionRight.Y := (-be-sqrt(lBaskaraDelta)) / (2 * ae);
    if Abs(DimensionRight.X-TmpDimensionRight.X)>Abs(DimensionRight.Y-TmpDimensionRight.X)then
      DimensionRight.X := DimensionRight.Y;
    DimensionRight.Y := al * DimensionRight.X + bl;

    Result := AData.AddArcDimension(180*Abs(lAngleRight-lAngleLeft)/Pi,
      lRadius, BaseLeft, BaseRight, DimensionLeft, DimensionRight, lTextPos, AOnlyCreate);
  end;
end;

{
100 Subclass marker (AcDbEllipse)
10 Center point (in WCS) DXF: X value; APP: 3D point
20, 30 DXF: Y and Z values of center point (in WCS)
11 Endpoint of major axis, relative to the center (in WCS) DXF: X value; APP: 3D point
21, 31 DXF: Y and Z values of endpoint of major axis, relative to the center (in WCS)
210 Extrusion direction (optional; default = 0, 0, 1) DXF: X value; APP: 3D vector
220, 230 DXF: Y and Z values of extrusion direction  (optional)
40 Ratio of minor axis to major axis
41 Start parameter (this value is 0.0 for a full ellipse)
42 End parameter (this value is 2pi for a full ellipse)

Example:
0
ELLIPSE
10
284.1193488089
20
246.9070153869
30
0.0
11
-0.0880072471
21
1.1224779542
31
0.0
40
0.6532930174
41
-0.0511763442
42
1.5196199825
}
function TvDXFVectorialReader.ReadENTITIES_ELLIPSE(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvEllipse;
var
  CurToken: TDXFToken;
  i: Integer;
  CenterX, CenterY, CenterZ, MajorAxisEndX, MajorAxisEndY, MajorAxisEndZ, MinorAxisRatio: Double;
  MajorHalfAxis, MinorHalfAxis, Angle: Double;
begin
  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      10: CenterX := CurToken.FloatValue;
      20: CenterY := CurToken.FloatValue;
      30: CenterZ := CurToken.FloatValue;
      11: MajorAxisEndX := CurToken.FloatValue;
      21: MajorAxisEndY := CurToken.FloatValue;
      31: MajorAxisEndZ := CurToken.FloatValue;
      40: MinorAxisRatio := CurToken.FloatValue;
      41: MinorAxisRatio := CurToken.FloatValue;
      42: MinorAxisRatio := CurToken.FloatValue;
    end;
  end;

  // Position fixing for documents with negative coordinates
  CenterX := CenterX - DOC_OFFSET.X;
  CenterY := CenterY - DOC_OFFSET.Y;

  // Calculate the axis info
  MajorHalfAxis := Sqrt(Sqr(MajorAxisEndX - CenterX)+Sqr(MajorAxisEndY - CenterY));
  MinorHalfAxis := MajorHalfAxis * MinorAxisRatio;
  Angle := 0.0;

  //
  Result := AData.AddEllipse(CenterX, CenterY, MajorHalfAxis, MinorHalfAxis, Angle, AOnlyCreate);
end;

{
The following group codes apply to insert (block reference) entities. In addition to the group codes described here, see "Common Group Codes for Entities." For information about abbreviations and formatting used in this table, see "Formatting Conventions in This Reference."
Insert group codes Group codes	Description

100 Subclass marker (AcDbBlockReference)
66 Variable attributes-follow flag (optional; default = 0); if the value of attributes-follow flag is 1, a series of attribute entities is expected to follow the insert, terminated by a seqend entity
2 Block name
10 Insertion point (in OCS)
  DXF: X value; APP: 3D point
20, 30 DXF: Y and Z values of insertion point (in OCS)
41 X scale factor (optional; default = 1)
42 Y scale factor (optional; default = 1)
43 Z scale factor (optional; default = 1)
50 Rotation angle (optional; default = 0)
70 Column count (optional; default = 1)
71 Row count (optional; default = 1)
44 Column spacing (optional; default = 0)
45 Row spacing (optional; default = 0)
210 Extrusion direction (optional; default = 0, 0, 1)
  DXF: X value; APP: 3D vector
220, 230 DXF: Y and Z values of extrusion direction (optional)
}
function TvDXFVectorialReader.ReadENTITIES_INSERT(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvInsert;
var
  CurToken: TDXFToken;
  i: Integer;
  lName: string;
  lBlock: TvBlock;
  PosX, PosY, PosZ: Double;
  lRotationAngle: Double = 0.0;
begin
  PosX := 0.0;
  PosY := 0.0;
  PosZ := 0.0;
  Result := nil;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 50] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      2: lName := CurToken.StrValue;
      10: PosX := CurToken.FloatValue;
      20: PosY := CurToken.FloatValue;
      30: PosZ := CurToken.FloatValue;
      50: lRotationAngle := -1 * CurToken.FloatValue * Pi / 180;
    end;
  end;

  // Find the block by its name
  lBlock := TvBlock(AData.FindEntityWithNameAndType(lName, TvBlock));
  if lBlock = nil then Exit;

  // write the data
  Result := TvInsert.Create(AData);
  Result.X := PosX;
  Result.Y := PosY;
  Result.Z := PosZ;
  Result.InsertEntity := lBlock;
  Result.RotationAngle := lRotationAngle;
  if not AOnlyCreate then AData.AddEntity(Result);
end;

{
100 Subclass marker (AcDbText)
39 Thickness (optional; default = 0)
10 First alignment point (in OCS) DXF: X value; APP: 3D point
20, 30 DXF: Y and Z values of first alignment point (in OCS)
40 Text height
1 Default value (the string itself)
50 Text rotation (optional; default = 0)
41 Relative X scale factor-width (optional; default = 1)
  This value is also adjusted when fit-type text is used.
51 Oblique angle (optional; default = 0)
7 Text style name (optional, default = STANDARD)
71 Text generation flags (optional, default = 0):
  2 = Text is backward (mirrored in X).
  4 = Text is upside down (mirrored in Y).
72 Horizontal text justification type (optional, default = 0) integer codes (not bit-coded)
  0 = Left; 1= Center; 2 = Right
  3 = Aligned (if vertical alignment = 0)
  4 = Middle (if vertical alignment = 0)
  5 = Fit (if vertical alignment = 0)
  See the Group 72 and 73 integer codes table for clarification.
11 Second alignment point (in OCS) (optional)
  DXF: X value; APP: 3D point
  This value is meaningful only if the value of a 72 or 73 group is nonzero (if the justification is anything other than baseline/left).
21, 31 DXF: Y and Z values of second alignment point (in OCS) (optional)
210 Extrusion direction (optional; default = 0, 0, 1)
  DXF: X value; APP: 3D vector
220, 230 DXF: Y and Z values of extrusion direction (optional)
73 Vertical text justification type (optional, default = 0): integer codes (not bit- coded):
  0 = Baseline; 1 = Bottom; 2 = Middle; 3 = Top
  See the Group 72 and 73 integer codes table for clarification.
}
function TvDXFVectorialReader.ReadENTITIES_TEXT(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvText;
var
  CurToken: TDXFToken;
  i: Integer;
  PosX: Double = 0.0;
  PosY: Double = 0.0;
  PosZ: Double = 0.0;
  FontSize: Double = 10.0;
  Str: string = '';
begin
  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 40] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      1:  Str := CurToken.StrValue;
      10: PosX := CurToken.FloatValue;
      20: PosY := CurToken.FloatValue;
      30: PosZ := CurToken.FloatValue;
      40: FontSize := CurToken.FloatValue;
    end;
  end;

  // Position fixing for documents with negative coordinates
  PosX := PosX - DOC_OFFSET.X;
  PosY := PosY - DOC_OFFSET.Y;

  // Convert the string if necessary
  Str := ConvertDXFStringToUTF8(Str);

  //
  Result := TvText.Create(AData);
  Result.Value.Text := Str;
  Result.X := PosX;
  Result.Y := PosY;
  Result.Font.Size := Round(FontSize);
  Result.Font.Color := colWhite;
  if not AOnlyCreate then AData.AddEntity(Result);
end;

{.$define FPVECTORIALDEBUG_LWPOLYLINE}
{
100 Subclass marker (AcDbPolyline)
90  Number of vertices
70  Polyline flag (bit-coded); default is 0:
    1 = Closed; 128 = Plinegen
43  Constant width (optional; default = 0). Not used if variable width (codes 40 and/or 41) is set
38  Elevation (optional; default = 0)
39  Thickness (optional; default = 0)
10  Vertex coordinates (in OCS), multiple entries; one entry for each vertex
    DXF: X value; APP: 2D point
20  DXF: Y value of vertex coordinates (in OCS), multiple entries; one entry for each vertex
40  Starting width (multiple entries; one entry for each vertex) (optional; default = 0; multiple entries). Not used if constant width (code 43) is set
41  End width (multiple entries; one entry for each vertex) (optional; default = 0; multiple entries). Not used if constant width (code 43) is set
42  Bulge (multiple entries; one entry for each vertex) (optional; default = 0)
210 Extrusion direction (optional; default = 0, 0, 1)
    DXF: X value; APP: 3D vector
220, 230 DXF: Y and Z values of extrusion direction (optional)
}
function TvDXFVectorialReader.ReadENTITIES_LWPOLYLINE(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
var
  CurToken: TDXFToken;
  i, curPoint: Integer;
  // LINE
  LWPolyline: array of TLWPOLYLINEElement;
  LWFlags: Integer = 0;
begin
  curPoint := -1;
  Result := nil;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 11, 21, 31, 70] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    // Loads the coordinates
    // With Position fixing for documents with negative coordinates
    case CurToken.GroupCode of
      10:
      begin
        // Starting a new point
        Inc(curPoint);
        SetLength(LWPolyline, curPoint+1);

        LWPolyline[curPoint].X := CurToken.FloatValue - DOC_OFFSET.X;
      end;
      20: LWPolyline[curPoint].Y := CurToken.FloatValue - DOC_OFFSET.Y;
      70: LWFlags := Round(CurToken.FloatValue);
    end;
  end;

  // In case of a Flag="Closed" then we need to close the line
  if LWFlags = 1 then
  begin
    Inc(curPoint);
    SetLength(LWPolyline, curPoint+1);
    LWPolyline[curPoint].X := LWPolyline[0].X;
    LWPolyline[curPoint].Y := LWPolyline[0].Y;
  end;

  // And now write it
  if curPoint >= 0 then // otherwise the polyline is empty of points
  begin
    AData.StartPath(LWPolyline[0].X, LWPolyline[0].Y);
    {$ifdef FPVECTORIALDEBUG_LWPOLYLINE}
    Write(Format('LWPOLYLINE ID=%d %f,%f', [AData.PathCount-1, LWPolyline[0].X, LWPolyline[0].Y]));
    {$endif}
    for i := 1 to curPoint do
    begin
      AData.AddLineToPath(LWPolyline[i].X, LWPolyline[i].Y);
      {$ifdef FPVECTORIALDEBUG_LWPOLYLINE}
       Write(Format(' %f,%f', [LWPolyline[i].X, LWPolyline[i].Y]));
      {$endif}
    end;
    {$ifdef FPVECTORIALDEBUG_LWPOLYLINE}
     WriteLn('');
    {$endif}
    Result := AData.EndPath(AOnlyCreate);
  end;
end;

{.$define FPVECTORIALDEBUG_SPLINE}
function TvDXFVectorialReader.ReadENTITIES_SPLINE(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
var
  CurToken: TDXFToken;
  i, curPoint: Integer;
  // LINE
  SPLine: array of TSPLineElement;
begin
  curPoint := -1;
  Result := nil;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 11, 21, 31] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    // Loads the coordinates
    // With Position fixing for documents with negative coordinates
    case CurToken.GroupCode of
      10:
      begin
        // Starting a new point
        Inc(curPoint);
        SetLength(SPLine, curPoint+1);

        SPLine[curPoint].X := CurToken.FloatValue - DOC_OFFSET.X;
      end;
      20: SPLine[curPoint].Y := CurToken.FloatValue - DOC_OFFSET.Y;
    end;
  end;

  // And now write it
  if curPoint >= 0 then // otherwise the polyline is empty of points
  begin
    AData.StartPath(SPLine[0].X, SPLine[0].Y);
    {$ifdef FPVECTORIALDEBUG_SPLINE}
    Write(Format('SPLINE ID=%d %f,%f', [AData.PathCount-1, SPLine[0].X, SPLine[0].Y]));
    {$endif}
    for i := 1 to curPoint do
    begin
      AData.AddLineToPath(SPLine[i].X, SPLine[i].Y);
      {$ifdef FPVECTORIALDEBUG_SPLINE}
       Write(Format(' %f,%f', [SPLine[i].X, SPLine[i].Y]));
      {$endif}
    end;
    {$ifdef FPVECTORIALDEBUG_SPLINE}
     WriteLn('');
    {$endif}
    Result := AData.EndPath(AOnlyCreate);
  end;
end;

procedure TvDXFVectorialReader.ReadENTITIES_ATTRIB(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
begin

end;

procedure TvDXFVectorialReader.ReadENTITIES_POLYLINE(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
begin
  SetLength(Polyline, 0);
end;

procedure TvDXFVectorialReader.ReadENTITIES_VERTEX(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
  CurToken: TDXFToken;
  i, curPoint: Integer;
begin
  if not IsReadingPolyline then raise Exception.Create('[TvDXFVectorialReader.ReadENTITIES_VERTEX] Unexpected record: VERTEX before a POLYLINE');

  curPoint := Length(Polyline);
  SetLength(Polyline, curPoint+1);
  Polyline[curPoint].X := 0;
  Polyline[curPoint].Y := 0;
  Polyline[curPoint].Color := colBlack;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 62] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    // Loads the coordinates
    // With Position fixing for documents with negative coordinates
    case CurToken.GroupCode of
      10: Polyline[curPoint].X := CurToken.FloatValue - DOC_OFFSET.X;
      20: Polyline[curPoint].Y := CurToken.FloatValue - DOC_OFFSET.Y;
      62: Polyline[curPoint].Color := DXFColorIndexToFPColor(Trunc(CurToken.FloatValue));
    end;
  end;
end;

function TvDXFVectorialReader.ReadENTITIES_SEQEND(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TPath;
var
  i: Integer;
begin
  Result := nil;
  if (not IsReadingPolyline) and (not IsReadingAttrib) then raise Exception.Create('[TvDXFVectorialReader.ReadENTITIES_SEQEND] Unexpected record: SEQEND before a POLYLINE or ATTRIB');

  if IsReadingPolyline then
  begin
    // Write the Polyline to the document
    if Length(Polyline) >= 0 then // otherwise the polyline is empty of points
    begin
      AData.StartPath(Polyline[0].X, Polyline[0].Y);
      {$ifdef FPVECTORIALDEBUG_POLYLINE}
       Write(Format('POLYLINE %f,%f', [Polyline[0].X, Polyline[0].Y]));
      {$endif}
      for i := 1 to Length(Polyline)-1 do
      begin
        AData.AddLineToPath(Polyline[i].X, Polyline[i].Y, Polyline[i].Color);
        {$ifdef FPVECTORIALDEBUG_POLYLINE}
         Write(Format(' %f,%f', [Polyline[i].X, Polyline[i].Y]));
        {$endif}
      end;
      {$ifdef FPVECTORIALDEBUG_POLYLINE}
       WriteLn('');
      {$endif}
      Result := AData.EndPath(AOnlyCreate);
    end;
  end;
end;

function TvDXFVectorialReader.ReadENTITIES_MTEXT(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvText;
var
  CurToken: TDXFToken;
  i: Integer;
  PosX: Double = 0.0;
  PosY: Double = 0.0;
  PosZ: Double = 0.0;
  FontSize: Double = 10.0;
  Str: string = '';
begin
  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 40] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      1:  Str := CurToken.StrValue;
      10: PosX := CurToken.FloatValue;
      20: PosY := CurToken.FloatValue;
      30: PosZ := CurToken.FloatValue;
      40: FontSize := CurToken.FloatValue;
    end;
  end;

  // Position fixing for documents with negative coordinates
  PosX := PosX - DOC_OFFSET.X;
  PosY := PosY + FontSize - DOC_OFFSET.Y;

  //
  Result := AData.AddText(PosX, PosY, 0, '', Round(FontSize), Str, AOnlyCreate);
  Result.Font.Color := colWhite;
end;

function TvDXFVectorialReader.ReadENTITIES_LEADER(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvArrow;
var
  CurToken: TDXFToken;
  i, curPoint: Integer;
  lValueX, lValueY: Double;
  lArrow: TvArrow;
  LElementColor: TFPColor;
begin
  lArrow := TvArrow.Create(AData);
  curPoint := 0;
  LElementColor := colBlack;
  Result := nil;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 11, 21, 31, 62] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    // Loads the coordinates
    // With Position fixing for documents with negative coordinates
    case CurToken.GroupCode of
      10:
      begin
        // Starting a new point
        Inc(curPoint);

        lValueX := CurToken.FloatValue - DOC_OFFSET.X;

        case curPoint of
        1: lArrow.X := lValueX;
        2: lArrow.Base.X := lValueX;
        3: lArrow.ExtraLineBase.X := lValueX;
        end;
      end;
      20:
      begin
        lValueY := CurToken.FloatValue - DOC_OFFSET.Y;

        case curPoint of
        1: lArrow.Y := lValueY;
        2: lArrow.Base.Y := lValueY;
        3: lArrow.ExtraLineBase.Y := lValueY;
        end;
      end;
      62: LElementColor := DXFColorIndexToFPColor(Trunc(CurToken.FloatValue));
    end;
  end;

  // Give a % of the line length to the arrow head
  lArrow.ArrowLength := 0.2 * sqrt(sqr(lArrow.Base.Y - lArrow.Y) + sqr(lArrow.Base.X - lArrow.X));
  lArrow.ArrowBaseLength := lArrow.ArrowLength / 2;

  // And now write it
  lArrow.HasExtraLine := True;
  lArrow.Pen.Color := LElementColor;
  lArrow.Brush.Style := bsSolid;
  lArrow.Brush.Color := LElementColor;
  Result := lArrow;
  if not AOnlyCreate then AData.AddEntity(lArrow);
end;

function TvDXFVectorialReader.ReadENTITIES_POINT(ATokens: TDXFTokens;
  AData: TvVectorialPage; ADoc: TvVectorialDocument; AOnlyCreate: Boolean = False): TvEntity;
var
  CurToken: TDXFToken;
  i: Integer;
  CircleCenterX, CircleCenterY, CircleCenterZ, CircleRadius: Double;
begin
  CircleCenterX := 0.0;
  CircleCenterY := 0.0;
  CircleCenterZ := 0.0;
  CircleRadius := 1.0;

  for i := 0 to ATokens.Count - 1 do
  begin
    // Now read and process the item name
    CurToken := TDXFToken(ATokens.Items[i]);

    // Avoid an exception by previously checking if the conversion can be made
    if CurToken.GroupCode in [10, 20, 30, 40] then
    begin
      CurToken.FloatValue :=  StrToFloat(Trim(CurToken.StrValue), FPointSeparator);
    end;

    case CurToken.GroupCode of
      10: CircleCenterX := CurToken.FloatValue;
      20: CircleCenterY := CurToken.FloatValue;
      30: CircleCenterZ := CurToken.FloatValue;
//      40: CircleRadius := CurToken.FloatValue;
    end;
  end;

  // Position fixing for documents with negative coordinates
  CircleCenterX := CircleCenterX - DOC_OFFSET.X;
  CircleCenterY := CircleCenterY - DOC_OFFSET.Y;

  Result := AData.AddCircle(CircleCenterX, CircleCenterY, CircleRadius, AOnlyCreate);
end;

function TvDXFVectorialReader.InternalReadENTITIES(ATokenStr: string;
  ATokens: TDXFTokens; AData: TvVectorialPage; ADoc: TvVectorialDocument;
  AOnlyCreate: Boolean = False): TvEntity;
begin
  Result := nil;
  case ATokenStr of
    '3DFACE':    Result := ReadENTITIES_3DFACE(ATokens, AData, ADoc, AOnlyCreate);
    'ARC':       Result := ReadENTITIES_ARC(ATokens, AData, ADoc, AOnlyCreate);
    'CIRCLE':    Result := ReadENTITIES_CIRCLE(ATokens, AData, ADoc, AOnlyCreate);
    'DIMENSION': Result := ReadENTITIES_DIMENSION(ATokens, AData, ADoc, AOnlyCreate);
    'ELLIPSE':   Result := ReadENTITIES_ELLIPSE(ATokens, AData, ADoc, AOnlyCreate);
    'INSERT':    Result := ReadENTITIES_INSERT(ATokens, AData, ADoc, AOnlyCreate);
    'LINE':      Result := ReadENTITIES_LINE(ATokens, AData, ADoc, AOnlyCreate);
    'TEXT':      Result := ReadENTITIES_TEXT(ATokens, AData, ADoc, AOnlyCreate);
    'LWPOLYLINE':Result := ReadENTITIES_LWPOLYLINE(ATokens, AData, ADoc, AOnlyCreate);
    'SPLINE':    Result := ReadENTITIES_SPLINE(ATokens, AData, ADoc, AOnlyCreate);
    'POINT':     Result := ReadENTITIES_POINT(ATokens, AData, ADoc, AOnlyCreate);
    'MTEXT':     Result := ReadENTITIES_MTEXT(ATokens, AData, ADoc, AOnlyCreate);
    'LEADER':    Result := ReadENTITIES_LEADER(ATokens, AData, ADoc, AOnlyCreate);
    // A Attribute can have multiple child objects
    'ATTRIB':
    begin
      IsReadingAttrib := True;
      ReadENTITIES_ATTRIB(ATokens, AData, ADoc);
    end;
    // A Polyline can have multiple child objects
    'POLYLINE':
    begin
      IsReadingPolyline := True;
      ReadENTITIES_POLYLINE(ATokens, AData, ADoc);
    end;
    'VERTEX': ReadENTITIES_VERTEX(ATokens, AData, ADoc);
    'SEQEND':
    begin
      Result := ReadENTITIES_SEQEND(ATokens, AData, ADoc, AOnlyCreate);
      IsReadingPolyline := False;
      IsReadingAttrib := False;
    end;
  end;
end;

function TvDXFVectorialReader.GetCoordinateValue(AStr: shortstring): Double;
begin
  Result := 0.0;

{  if Length(AStr) <= 1 then Exit;

  Result := StrToFloat(Copy(AStr, 2, Length(AStr) - 1), FPointSeparator);}
end;

function TvDXFVectorialReader.ConvertDXFStringToUTF8(AStr: string): string;
begin
  if (ENCODING = 'UTF-8') or (ENCODING = '') then
  begin
    Result := AStr;
    Exit;
  end;

  Result := ConvertEncoding(AStr, ENCODING, 'UTF-8');
end;

function TvDXFVectorialReader.DXFColorIndexToFPColor(AColorIndex: Integer): TFPColor;
begin
  if (AColorIndex >= 0) and (AColorIndex <= 255) then
    Result := AUTOCAD_COLOR_PALETTE[AColorIndex]
  else
    raise Exception.Create(Format('[TvDXFVectorialReader.DXFColorIndexToFPVColor] Invalid DXF Color Index: %d', [AColorIndex]));
end;

procedure TvDXFVectorialReader.DXFCoordsToFPCoords(AInX, AInY, AInZ: Double;
  out AOutX, AOutY, AOutZ: Double);
begin
  AOutX := AInX - DOC_OFFSET.X;
  AOutY := AInY - DOC_OFFSET.Y;
  AOutZ := AInZ;
end;

constructor TvDXFVectorialReader.Create;
begin
  inherited Create;

  FPointSeparator := DefaultFormatSettings;
  FPointSeparator.DecimalSeparator := '.';
  FPointSeparator.ThousandSeparator := '#';// disable the thousand separator

  Tokenizer := TDXFTokenizer.Create;
end;

destructor TvDXFVectorialReader.Destroy;
begin
  Tokenizer.Free;

  inherited Destroy;
end;

{@@
  The information of each separate path is lost in G-Code files
  Only one path uniting all of them is created when reading G-Code
}
procedure TvDXFVectorialReader.ReadFromStrings(AStrings: TStrings;
  AData: TvVectorialDocument);
var
  i: Integer;
  CurToken, CurTokenFirstChild: TDXFToken;
  lPage: TvVectorialPage;
begin
  // Default HEADER data
  ANGBASE := 0.0; // Starts pointing to the right / east
  ANGDIR := 0; // counter-clock wise
  // The default encoding of DXF files if a ($DWGCODEPAGE) header is not present
  // See http://www.gdal.org/ogr/drv_dxf.html
  if AData.ForcedEncodingOnRead <> '' then ENCODING := AData.ForcedEncodingOnRead
  else ENCODING := 'cp1252';

  Tokenizer.ReadFromStrings(AStrings);

  lPage := AData.AddPage();
  lPage.BackgroundColor := colBlack;
  lPage.RenderInfo.AdjustPenColorToBackground := True;
  lPage.RenderInfo.BackgroundColor := colBlack;

  for i := 0 to Tokenizer.Tokens.Count - 1 do
  begin
    CurToken := TDXFToken(Tokenizer.Tokens.Items[i]);
    if (CurToken.Childs = nil) or (CurToken.Childs.Count=0) then Continue;
    CurTokenFirstChild := TDXFToken(CurToken.Childs.Items[0]);

    if CurTokenFirstChild.StrValue = 'HEADER' then
      ReadHEADER(CurToken.Childs, lPage, AData)
    else if CurTokenFirstChild.StrValue = 'TABLES' then
      ReadTABLES(CurToken.Childs, lPage, AData)
    else if CurTokenFirstChild.StrValue = 'BLOCKS' then
      ReadBLOCKS(CurToken.Childs, lPage, AData)
    else if CurTokenFirstChild.StrValue = 'ENTITIES' then
      ReadENTITIES(CurToken.Childs, lPage, AData);
  end;

  // Update properties from the document
  AData.Encoding := ENCODING;
end;

initialization

  RegisterVectorialReader(TvDXFVectorialReader, vfDXF);

end.