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
// Copyright (c) IxMilia.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

//! This crate provides the ability to read and write DXF CAD files.
//!
//! # Examples
//!
//! Open a DXF file from disk:
//!
//! ``` rust
//! # fn main() { }
//! # fn ex() -> dxf::DxfResult<()> {
//! use dxf::Drawing;
//! use dxf::entities::*;
//!
//! let drawing = try!(Drawing::load_file("path/to/file.dxf"));
//! for e in drawing.entities {
//!     println!("found entity on layer {}", e.common.layer);
//!     match e.specific {
//!         EntityType::Circle(ref circle) => {
//!             // do something with the circle
//!         },
//!         EntityType::Line(ref line) => {
//!             // do something with the line
//!         },
//!         _ => (),
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! Saving a DXF file to disk:
//!
//! ``` rust
//! # fn main() { }
//! # fn ex() -> dxf::DxfResult<()> {
//! use dxf::Drawing;
//! use dxf::entities::*;
//!
//! let mut drawing = Drawing::new();
//! drawing.entities.push(Entity::new(EntityType::Line(Line::default())));
//! try!(drawing.save_file("path/to/file.dxf"));
//! # Ok(())
//! # }
//! ```
//!
//! # Reference
//!
//! Since I don't want to fall afoul of Autodesk's lawyers, this repo can't include the actual DXF documentation.  It can,
//! however contain links to the official documents that I've been able to scrape together.  For most scenarios the 2014
//! documentation should suffice, but all other versions are included here for backwards compatibility and reference
//! between versions.
//!
//! [R10 (non-Autodesk source)](http://www.martinreddy.net/gfx/3d/DXF10.spec)
//!
//! [R11 (differences between R10 and R11)](http://autodesk.blogs.com/between_the_lines/ACAD_R11.html)
//!
//! [R12 (non-Autodesk source)](http://www.martinreddy.net/gfx/3d/DXF12.spec)
//!
//! [R13 (self-extracting 16-bit executable)](http://www.autodesk.com/techpubs/autocad/dxf/dxf13_hlp.exe)
//!
//! [R14](http://www.autodesk.com/techpubs/autocad/acadr14/dxf/index.htm)
//!
//! [2000](http://www.autodesk.com/techpubs/autocad/acad2000/dxf/index.htm)
//!
//! [2002](http://www.autodesk.com/techpubs/autocad/dxf/dxf2002.pdf)
//!
//! [2004](http://download.autodesk.com/prodsupp/downloads/dxf.pdf)
//!
//! [2005](http://download.autodesk.com/prodsupp/downloads/acad_dxf.pdf)
//!
//! [2006](http://images.autodesk.com/adsk/files/dxf_format.pdf)
//!
//! 2007 (Autodesk's link erroneously points to the R2008 documentation)
//!
//! [2008](http://images.autodesk.com/adsk/files/acad_dxf0.pdf)
//!
//! [2009](http://images.autodesk.com/adsk/files/acad_dxf.pdf)
//!
//! [2010](http://images.autodesk.com/adsk/files/acad_dxf1.pdf)
//!
//! [2011](http://images.autodesk.com/adsk/files/acad_dxf2.pdf)
//!
//! [2012](http://images.autodesk.com/adsk/files/autocad_2012_pdf_dxf-reference_enu.pdf)
//!
//! [2013](http://images.autodesk.com/adsk/files/autocad_2013_pdf_dxf_reference_enu.pdf)
//!
//! [2014](http://images.autodesk.com/adsk/files/autocad_2014_pdf_dxf_reference_enu.pdf)
//!
//! These links were compiled from the archive.org May 9, 2013 snapshot of http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=12272454&linkID=10809853
//! (https://web.archive.org/web/20130509144333/http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=12272454&linkID=10809853)

#[macro_use] extern crate enum_primitive;
extern crate itertools;

mod drawing;
pub use drawing::Drawing;

pub mod enums;

mod generated;
pub mod entities {
    pub use generated::entities::*;
}
pub mod header {
    pub use generated::header::*;
}
pub mod tables {
    pub use generated::tables::*;
}

use entities::*;
use header::*;
use tables::*;

use self::enums::*;
use enum_primitive::FromPrimitive;

use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::io;
use std::io::{Read, Write};
use std::num;

use itertools::PutBack;

include!("expected_type.rs");

mod helper_functions;
use helper_functions::*;

//------------------------------------------------------------------------------
//                                                                 CodePairValue
//------------------------------------------------------------------------------
#[doc(hidden)]
pub enum CodePairValue {
    Boolean(bool),
    Integer(i32),
    Long(i64),
    Short(i16),
    Double(f64),
    Str(String),
}

impl CodePairValue {
    pub fn assert_bool(&self) -> bool {
        match self {
            &CodePairValue::Boolean(b) => b,
            _ => panic!("this should never have happened, please file a bug"),
        }
    }
    pub fn assert_i64(&self) -> i64 {
        match self {
            &CodePairValue::Long(l) => l,
            _ => panic!("this should never have happened, please file a bug"),
        }
    }
    pub fn assert_i32(&self) -> i32 {
        match self {
            &CodePairValue::Integer(i) => i,
            _ => panic!("this should never have happened, please file a bug"),
        }
    }
    pub fn assert_f64(&self) -> f64 {
        match self {
            &CodePairValue::Double(f) => f,
            _ => panic!("this should never have happened, please file a bug"),
        }
    }
    pub fn assert_string(&self) -> String {
        match self {
            &CodePairValue::Str(ref s) => s.clone(),
            _ => panic!("this should never have happened, please file a bug"),
        }
    }
    pub fn assert_i16(&self) -> i16 {
        match self {
            &CodePairValue::Short(s) => s,
            _ => panic!("this should never have happened, please file a bug"),
        }
    }
}

impl Clone for CodePairValue {
    fn clone(&self) -> Self {
        match self {
            &CodePairValue::Boolean(b) => CodePairValue::Boolean(b),
            &CodePairValue::Integer(i) => CodePairValue::Integer(i),
            &CodePairValue::Long(l) => CodePairValue::Long(l),
            &CodePairValue::Short(s) => CodePairValue::Short(s),
            &CodePairValue::Double(d) => CodePairValue::Double(d),
            &CodePairValue::Str(ref s) => CodePairValue::Str(String::from(s.as_str())),
        }
    }
}

impl Debug for CodePairValue {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            &CodePairValue::Boolean(b) => write!(formatter, "{}", if b { 1 } else { 0 }),
            &CodePairValue::Integer(i) => write!(formatter, "{}", i),
            &CodePairValue::Long(l) => write!(formatter, "{}", l),
            &CodePairValue::Short(s) => write!(formatter, "{}", s),
            &CodePairValue::Double(d) => write!(formatter, "{:.12}", d),
            &CodePairValue::Str(ref s) => write!(formatter, "{}", s),
        }
    }
}

//------------------------------------------------------------------------------
//                                                                      CodePair
//------------------------------------------------------------------------------
#[doc(hidden)]
#[derive(Clone)]
pub struct CodePair {
    code: i32,
    value: CodePairValue,
}

impl CodePair {
    pub fn new(code: i32, val: CodePairValue) -> CodePair {
        CodePair { code: code, value: val }
    }
    pub fn new_str(code: i32, val: &str) -> CodePair {
        CodePair::new(code, CodePairValue::Str(val.to_string()))
    }
    pub fn new_string(code: i32, val: &String) -> CodePair {
        CodePair::new(code, CodePairValue::Str(val.clone()))
    }
    pub fn new_i16(code: i32, val: i16) -> CodePair {
        CodePair::new(code, CodePairValue::Short(val))
    }
    pub fn new_f64(code: i32, val: f64) -> CodePair {
        CodePair::new(code, CodePairValue::Double(val))
    }
    pub fn new_i64(code: i32, val: i64) -> CodePair {
        CodePair::new(code, CodePairValue::Long(val))
    }
    pub fn new_i32(code: i32, val: i32) -> CodePair {
        CodePair::new(code, CodePairValue::Integer(val))
    }
    pub fn new_bool(code: i32, val: bool) -> CodePair {
        CodePair::new(code, CodePairValue::Boolean(val))
    }
}

impl Debug for CodePair {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(formatter, "{}/{:?}", self.code, &self.value)
    }
}

//------------------------------------------------------------------------------
//                                                                  DxfResult<T>
//------------------------------------------------------------------------------
pub type DxfResult<T> = Result<T, DxfError>;

#[derive(Debug)]
pub enum DxfError {
    IoError(io::Error),
    ParseFloatError(num::ParseFloatError),
    ParseIntError(num::ParseIntError),
    ParseError,
    UnexpectedCode(i32),
    UnexpectedCodePair(CodePair, String),
    UnexpectedEndOfInput,
    UnexpectedEnumValue,
    UnexpectedEmptySet,
    ExpectedTableType,
}

impl From<io::Error> for DxfError {
    fn from(ioe: io::Error) -> DxfError {
        DxfError::IoError(ioe)
    }
}

impl Display for DxfError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &DxfError::IoError(ref e) => write!(formatter, "{}", e),
            &DxfError::ParseFloatError(ref e) => write!(formatter, "{}", e),
            &DxfError::ParseIntError(ref e) => write!(formatter, "{}", e),
            &DxfError::ParseError => write!(formatter, "there was a general parsing error"),
            &DxfError::UnexpectedCode(c) => write!(formatter, "an unexpected code '{}' was encountered", c),
            &DxfError::UnexpectedCodePair(ref cp, ref s) => write!(formatter, "the code pair '{:?}' was not expected at this time: {}", cp, s),
            &DxfError::UnexpectedEndOfInput => write!(formatter, "the input unexpectedly ended before the drawing was completely loaded"),
            &DxfError::UnexpectedEnumValue => write!(formatter, "the specified enum value does not fall into the expected range"),
            &DxfError::UnexpectedEmptySet => write!(formatter, "the set was not expected to be empty"),
            &DxfError::ExpectedTableType => write!(formatter, "a 2/<table-type> code pair was expected"),
        }
    }
}

impl std::error::Error for DxfError {
    fn description(&self) -> &str {
        match self {
            &DxfError::IoError(ref e) => e.description(),
            &DxfError::ParseFloatError(ref e) => e.description(),
            &DxfError::ParseIntError(ref e) => e.description(),
            &DxfError::ParseError => "there was a general parsing error",
            &DxfError::UnexpectedCode(_) => "an unexpected code was encountered",
            &DxfError::UnexpectedCodePair(_, _) => "an unexpected code pair was encountered",
            &DxfError::UnexpectedEndOfInput => "the input unexpectedly ended before the drawing was completely loaded",
            &DxfError::UnexpectedEnumValue => "the specified enum value does not fall into the expected range",
            &DxfError::UnexpectedEmptySet => "the set was not expected to be empty",
            &DxfError::ExpectedTableType => "a 2/<table-type> code pair was expected",
        }
    }
    fn cause(&self) -> Option<&std::error::Error> {
        match self {
            &DxfError::IoError(ref e) => Some(e),
            &DxfError::ParseFloatError(ref e) => Some(e),
            &DxfError::ParseIntError(ref e) => Some(e),
            _ => None,
        }
    }
}

//------------------------------------------------------------------------------
//                                                             CodePairAsciiIter
//------------------------------------------------------------------------------
struct CodePairAsciiIter<T>
    where T: Read
{
    reader: T,
}

// Used to turn Result<T> into Option<Result<T>>.
macro_rules! try_into_result {
    ($expr : expr) => (
        match $expr {
            Ok(v) => v,
            Err(e) => return Some(Err(e)),
        }
    )
}

// because I don't want to depend on BufRead here
fn read_line<T>(reader: &mut T, result: &mut String) -> DxfResult<()>
    where T: Read {
    for c in reader.bytes() { // .bytes() is OK since DXF files must be ASCII encoded
        let c = try!(c) as char;
        result.push(c);
        if c == '\n' { break; }
    }

    Ok(())
}

impl<T: Read> Iterator for CodePairAsciiIter<T> {
    type Item = DxfResult<CodePair>;
    fn next(&mut self) -> Option<DxfResult<CodePair>> {
        loop {
            // Read code.  If no line is available, fail gracefully.
            let mut code_line = String::new();
            match read_line(&mut self.reader, &mut code_line) {
                Ok(_) => (),
                Err(_) => return None,
            }
            let code_line = code_line.trim();
            if code_line.is_empty() { return None; }
            let code = try_into_result!(parse_i32(String::from(code_line)));

            // Read value.  If no line is available die horribly.
            let mut value_line = String::new();
            try_into_result!(read_line(&mut self.reader, &mut value_line));
            trim_trailing_newline(&mut value_line);

            // construct the value pair
            let expected_type = match get_expected_type(code) {
                Some(t) => t,
                None => return Some(Err(DxfError::UnexpectedEnumValue)),
            };
            let value = match expected_type {
                ExpectedType::Boolean => CodePairValue::Boolean(try_into_result!(parse_bool(value_line))),
                ExpectedType::Integer => CodePairValue::Integer(try_into_result!(parse_i32(value_line))),
                ExpectedType::Long => CodePairValue::Long(try_into_result!(parse_i64(value_line))),
                ExpectedType::Short => CodePairValue::Short(try_into_result!(parse_i16(value_line))),
                ExpectedType::Double => CodePairValue::Double(try_into_result!(parse_f64(value_line))),
                ExpectedType::Str => CodePairValue::Str(value_line), // TODO: un-escape
            };

            if code != 999 {
                return Some(Ok(CodePair {
                    code: code,
                    value: value,
                }));
            }
        }
    }
}

//------------------------------------------------------------------------------
//                                                           CodePairAsciiWriter
//------------------------------------------------------------------------------
#[doc(hidden)]
pub struct CodePairAsciiWriter<T>
    where T: Write {
    writer: T,
}

impl<T: Write> CodePairAsciiWriter<T> {
    pub fn write_code_pair(&mut self, pair: &CodePair) -> DxfResult<()> {
        try!(self.writer.write_fmt(format_args!("{: >3}\r\n", pair.code)));
        try!(self.writer.write_fmt(format_args!("{:?}\r\n", &pair.value)));
        Ok(())
    }
}

//------------------------------------------------------------------------------
//                                                                        Header
//------------------------------------------------------------------------------
// implementation is in `header.rs`
impl Header {
    #[doc(hidden)]
    pub fn read<I>(iter: &mut PutBack<I>) -> DxfResult<Header>
        where I: Iterator<Item = DxfResult<CodePair>> {
        let mut header = Header::new();
        loop {
            match iter.next() {
                Some(Ok(pair)) => {
                    match pair.code {
                        0 => {
                            iter.put_back(Ok(pair));
                            break;
                        },
                        9 => {
                            let last_header_variable = pair.value.assert_string();
                            loop {
                                match iter.next() {
                                    Some(Ok(pair)) => {
                                        if pair.code == 0 || pair.code == 9 {
                                            // ENDSEC or a new header variable
                                            iter.put_back(Ok(pair));
                                            break;
                                        }
                                        else {
                                            try!(header.set_header_value(&last_header_variable, &pair));
                                        }
                                    },
                                    Some(Err(e)) => return Err(e),
                                    None => break,
                                }
                            }
                        },
                        _ => return Err(DxfError::UnexpectedCodePair(pair, String::from(""))),
                    }
                },
                Some(Err(e)) => return Err(e),
                None => break,
            }
        }

        Ok(header)
    }
    #[doc(hidden)]
    pub fn write<T>(&self, writer: &mut CodePairAsciiWriter<T>) -> DxfResult<()>
        where T: Write
    {
        try!(writer.write_code_pair(&CodePair::new_str(0, "SECTION")));
        try!(writer.write_code_pair(&CodePair::new_str(2, "HEADER")));
        try!(self.write_code_pairs(writer));
        try!(writer.write_code_pair(&CodePair::new_str(0, "ENDSEC")));
        Ok(())
    }
}

//------------------------------------------------------------------------------
//                                                                        Entity
//------------------------------------------------------------------------------
// returns the next CodePair that's not 0, or bails out early
macro_rules! next_pair {
    ($expr : expr) => (
        match $expr.next() {
            Some(Ok(pair @ CodePair { code: 0, .. })) => {
                $expr.put_back(Ok(pair));
                return Ok(true);
            },
            Some(Ok(pair)) => pair,
            Some(Err(e)) => return Err(e),
            None => return Ok(true),
        }
    )
}
// Used to turn Option<T> into DxfResult<T>.
macro_rules! try_result {
    ($expr : expr) => (
        match $expr {
            Some(v) => v,
            None => return Err(DxfError::UnexpectedEnumValue)
        }
    )
}
// Used to safely access the last element in a Vec<T>
macro_rules! vec_last {
    ($expr : expr) => (
        match $expr.len() {
            0 => return Err(DxfError::UnexpectedEmptySet),
            l => &mut $expr[l - 1],
        }
    )
}
// implementation is in `entity.rs`
impl Entity {
    /// Creates a new `Entity` with the default common values.
    pub fn new(specific: EntityType) -> Self {
        Entity {
            common: EntityCommon::new(),
            specific: specific,
        }
    }
    #[doc(hidden)]
    pub fn read<I>(iter: &mut PutBack<I>) -> DxfResult<Option<Entity>>
        where I: Iterator<Item = DxfResult<CodePair>> {
        loop {
            match iter.next() {
                // first code pair must be 0/entity-type
                Some(Ok(pair @ CodePair { code: 0, .. })) => {
                    let type_string = pair.value.assert_string();
                    if type_string == "ENDSEC" {
                        iter.put_back(Ok(pair));
                        return Ok(None);
                    }

                    match EntityType::from_type_string(&type_string) {
                        Some(e) => {
                            let mut entity = Entity::new(e);
                            if !try!(entity.apply_custom_reader(iter)) {
                                // no custom reader, use the auto-generated one
                                loop {
                                    match iter.next() {
                                        Some(Ok(pair @ CodePair { code: 0, .. })) => {
                                            // new entity or ENDSEC
                                            iter.put_back(Ok(pair));
                                            break;
                                        },
                                        Some(Ok(pair)) => try!(entity.apply_code_pair(&pair)),
                                        Some(Err(e)) => return Err(e),
                                        None => return Err(DxfError::UnexpectedEndOfInput),
                                    }
                                }

                                try!(entity.post_parse());
                            }

                            return Ok(Some(entity));
                        },
                        None => {
                            // swallow unsupported entity
                            loop {
                               match iter.next() {
                                    Some(Ok(pair @ CodePair { code: 0, .. })) => {
                                        // found another entity or ENDSEC
                                        iter.put_back(Ok(pair));
                                        break;
                                    },
                                    Some(Ok(_)) => (), // part of the unsupported entity
                                    Some(Err(e)) => return Err(e),
                                    None => return Err(DxfError::UnexpectedEndOfInput),
                                }
                            }
                        }
                    }
                },
                Some(Ok(pair)) => return Err(DxfError::UnexpectedCodePair(pair, String::from("expected 0/entity-type or 0/ENDSEC"))),
                Some(Err(e)) => return Err(e),
                None => return Err(DxfError::UnexpectedEndOfInput),
            }
        }
    }
    fn apply_code_pair(&mut self, pair: &CodePair) -> DxfResult<()> {
        if !try!(self.specific.try_apply_code_pair(&pair)) {
            try!(self.common.apply_individual_pair(&pair));
        }
        Ok(())
    }
    fn post_parse(&mut self) -> DxfResult<()> {
        match self.specific {
            EntityType::Image(ref mut image) => {
                combine_points_2(&mut image._clipping_vertices_x, &mut image._clipping_vertices_y, &mut image.clipping_vertices, Point::new);
            },
            EntityType::Leader(ref mut leader) => {
                combine_points_3(&mut leader._vertices_x, &mut leader._vertices_y, &mut leader._vertices_z, &mut leader.vertices, Point::new);
            },
            EntityType::MLine(ref mut mline) => {
                combine_points_3(&mut mline._vertices_x, &mut mline._vertices_y, &mut mline._vertices_z, &mut mline.vertices, Point::new);
                combine_points_3(&mut mline._segment_direction_x, &mut mline._segment_direction_y, &mut mline._segment_direction_z, &mut mline.segment_directions, Vector::new);
                combine_points_3(&mut mline._miter_direction_x, &mut mline._miter_direction_y, &mut mline._miter_direction_z, &mut mline.miter_directions, Vector::new);
            },
            EntityType::Section(ref mut section) => {
                combine_points_3(&mut section._vertices_x, &mut section._vertices_y, &mut section._vertices_z, &mut section.vertices, Point::new);
                combine_points_3(&mut section._back_line_vertices_x, &mut section._back_line_vertices_y, &mut section._back_line_vertices_z, &mut section.back_line_vertices, Point::new);
            },
            EntityType::Spline(ref mut spline) => {
                combine_points_3(&mut spline._control_point_x, &mut spline._control_point_y, &mut spline._control_point_z, &mut spline.control_points, Point::new);
                combine_points_3(&mut spline._fit_point_x, &mut spline._fit_point_y, &mut spline._fit_point_z, &mut spline.fit_points, Point::new);
            },
            EntityType::DgnUnderlay(ref mut underlay) => {
                combine_points_2(&mut underlay._point_x, &mut underlay._point_y, &mut underlay.points, Point::new);
            },
            EntityType::DwfUnderlay(ref mut underlay) => {
                combine_points_2(&mut underlay._point_x, &mut underlay._point_y, &mut underlay.points, Point::new);
            },
            EntityType::PdfUnderlay(ref mut underlay) => {
                combine_points_2(&mut underlay._point_x, &mut underlay._point_y, &mut underlay.points, Point::new);
            },
            EntityType::Wipeout(ref mut wo) => {
                combine_points_2(&mut wo._clipping_vertices_x, &mut wo._clipping_vertices_y, &mut wo.clipping_vertices, Point::new);
            },
            _ => (),
        }

        Ok(())
    }
    fn apply_custom_reader<I>(&mut self, iter: &mut PutBack<I>) -> DxfResult<bool>
        where I: Iterator<Item = DxfResult<CodePair>>
    {
        match self.specific {
            EntityType::Attribute(ref mut att) => {
                let xrecord_text = "AcDbXrecord";
                let mut last_subclass_marker = String::new();
                let mut is_version_set = false;
                let mut xrec_code_70_count = 0;
                loop {
                    let pair = next_pair!(iter);
                    match pair.code {
                        100 => { last_subclass_marker = pair.value.assert_string(); },
                        1 => { att.value = pair.value.assert_string(); },
                        2 => {
                            if last_subclass_marker == xrecord_text {
                                att.x_record_tag = pair.value.assert_string();
                            }
                            else {
                                att.attribute_tag = pair.value.assert_string();
                            }
                        },
                        7 => { att.text_style_name = pair.value.assert_string(); },
                        10 => {
                            if last_subclass_marker == xrecord_text {
                                att.alignment_point.x = pair.value.assert_f64();
                            }
                            else {
                                att.location.x = pair.value.assert_f64();
                            }
                        },
                        20 => {
                            if last_subclass_marker == xrecord_text {
                                att.alignment_point.y = pair.value.assert_f64();
                            }
                            else {
                                att.location.y = pair.value.assert_f64();
                            }
                        },
                        30 => {
                            if last_subclass_marker == xrecord_text {
                                att.alignment_point.z = pair.value.assert_f64();
                            }
                            else {
                                att.location.z = pair.value.assert_f64();
                            }
                        },
                        11 => { att.second_alignment_point.x = pair.value.assert_f64(); },
                        21 => { att.second_alignment_point.y = pair.value.assert_f64(); },
                        31 => { att.second_alignment_point.z = pair.value.assert_f64(); },
                        39 => { att.thickness = pair.value.assert_f64(); },
                        40 => {
                            if last_subclass_marker == xrecord_text {
                                att.annotation_scale = pair.value.assert_f64();
                            }
                            else {
                                att.text_height = pair.value.assert_f64();
                            }
                        },
                        41 => { att.relative_x_scale_factor = pair.value.assert_f64(); },
                        50 => { att.rotation = pair.value.assert_f64(); },
                        51 => { att.oblique_angle = pair.value.assert_f64(); },
                        70 => {
                            if last_subclass_marker == xrecord_text {
                                match xrec_code_70_count {
                                    0 => att.m_text_flag = try_result!(MTextFlag::from_i16(pair.value.assert_i16())),
                                    1 => att.is_really_locked = as_bool(pair.value.assert_i16()),
                                    2 => att._secondary_attribute_count = pair.value.assert_i16() as i32,
                                    _ => return Err(DxfError::UnexpectedCodePair(pair, String::new())),
                                }
                                xrec_code_70_count += 1;
                            }
                            else {
                                att.flags = pair.value.assert_i16() as i32;
                            }
                        },
                        71 => { att.text_generation_flags = pair.value.assert_i16() as i32; },
                        72 => { att.horizontal_text_justification = try_result!(HorizontalTextJustification::from_i16(pair.value.assert_i16())); },
                        73 => { att.field_length = pair.value.assert_i16(); },
                        74 => { att.vertical_text_justification = try_result!(VerticalTextJustification::from_i16(pair.value.assert_i16())); },
                        210 => { att.normal.x = pair.value.assert_f64(); },
                        220 => { att.normal.y = pair.value.assert_f64(); },
                        230 => { att.normal.z = pair.value.assert_f64(); },
                        280 => {
                            if last_subclass_marker == xrecord_text {
                                att.keep_duplicate_records = as_bool(pair.value.assert_i16());
                            }
                            else if !is_version_set {
                                att.version = try_result!(Version::from_i16(pair.value.assert_i16()));
                                is_version_set = true;
                            }
                            else {
                                att.is_locked_in_block = as_bool(pair.value.assert_i16());
                            }
                        },
                        340 => { att.secondary_attributes.push(try!(as_u32(pair.value.assert_string()))); },
                        -1 => { att.m_text = try!(as_u32(pair.value.assert_string())); },
                        _ => { try!(self.common.apply_individual_pair(&pair)); },
                    }
                }
            },
            EntityType::AttributeDefinition(ref mut att) => {
                let xrecord_text = "AcDbXrecord";
                let mut last_subclass_marker = String::new();
                let mut is_version_set = false;
                let mut xrec_code_70_count = 0;
                loop {
                    let pair = next_pair!(iter);
                    match pair.code {
                        100 => { last_subclass_marker = pair.value.assert_string(); },
                        1 => { att.value = pair.value.assert_string(); },
                        2 => {
                            if last_subclass_marker == xrecord_text {
                                att.x_record_tag = pair.value.assert_string();
                            }
                            else {
                                att.text_tag = pair.value.assert_string();
                            }
                        },
                        3 => { att.prompt = pair.value.assert_string(); },
                        7 => { att.text_style_name = pair.value.assert_string(); },
                        10 => {
                            if last_subclass_marker == xrecord_text {
                                att.alignment_point.x = pair.value.assert_f64();
                            }
                            else {
                                att.location.x = pair.value.assert_f64();
                            }
                        },
                        20 => {
                            if last_subclass_marker == xrecord_text {
                                att.alignment_point.y = pair.value.assert_f64();
                            }
                            else {
                                att.location.y = pair.value.assert_f64();
                            }
                        },
                        30 => {
                            if last_subclass_marker == xrecord_text {
                                att.alignment_point.z = pair.value.assert_f64();
                            }
                            else {
                                att.location.z = pair.value.assert_f64();
                            }
                        },
                        11 => { att.second_alignment_point.x = pair.value.assert_f64(); },
                        21 => { att.second_alignment_point.y = pair.value.assert_f64(); },
                        31 => { att.second_alignment_point.z = pair.value.assert_f64(); },
                        39 => { att.thickness = pair.value.assert_f64(); },
                        40 => {
                            if last_subclass_marker == xrecord_text {
                                att.annotation_scale = pair.value.assert_f64();
                            }
                            else {
                                att.text_height = pair.value.assert_f64();
                            }
                        },
                        41 => { att.relative_x_scale_factor = pair.value.assert_f64(); },
                        50 => { att.rotation = pair.value.assert_f64(); },
                        51 => { att.oblique_angle = pair.value.assert_f64(); },
                        70 => {
                            if last_subclass_marker == xrecord_text {
                                match xrec_code_70_count {
                                    0 => att.m_text_flag = try_result!(MTextFlag::from_i16(pair.value.assert_i16())),
                                    1 => att.is_really_locked = as_bool(pair.value.assert_i16()),
                                    2 => att._secondary_attribute_count = pair.value.assert_i16() as i32,
                                    _ => return Err(DxfError::UnexpectedCodePair(pair, String::new())),
                                }
                                xrec_code_70_count += 1;
                            }
                            else {
                                att.flags = pair.value.assert_i16() as i32;
                            }
                        },
                        71 => { att.text_generation_flags = pair.value.assert_i16() as i32; },
                        72 => { att.horizontal_text_justification = try_result!(HorizontalTextJustification::from_i16(pair.value.assert_i16())); },
                        73 => { att.field_length = pair.value.assert_i16(); },
                        74 => { att.vertical_text_justification = try_result!(VerticalTextJustification::from_i16(pair.value.assert_i16())); },
                        210 => { att.normal.x = pair.value.assert_f64(); },
                        220 => { att.normal.y = pair.value.assert_f64(); },
                        230 => { att.normal.z = pair.value.assert_f64(); },
                        280 => {
                            if last_subclass_marker == xrecord_text {
                                att.keep_duplicate_records = as_bool(pair.value.assert_i16());
                            }
                            else if !is_version_set {
                                att.version = try_result!(Version::from_i16(pair.value.assert_i16()));
                                is_version_set = true;
                            }
                            else {
                                att.is_locked_in_block = as_bool(pair.value.assert_i16());
                            }
                        },
                        340 => { att.secondary_attributes.push(try!(as_u32(pair.value.assert_string()))); },
                        -1 => { att.m_text = try!(as_u32(pair.value.assert_string())); },
                        _ => { try!(self.common.apply_individual_pair(&pair)); },
                    }
                }
            },
            EntityType::LwPolyline(ref mut poly) => {
                loop {
                    let pair = next_pair!(iter);
                    match pair.code {
                        // vertex-specific pairs
                        10 => {
                            // start a new vertex
                            poly.vertices.push(LwPolylineVertex::new());
                            vec_last!(poly.vertices).x = pair.value.assert_f64();
                        },
                        20 => { vec_last!(poly.vertices).y = pair.value.assert_f64(); },
                        40 => { vec_last!(poly.vertices).starting_width = pair.value.assert_f64(); },
                        41 => { vec_last!(poly.vertices).ending_width = pair.value.assert_f64(); },
                        42 => { vec_last!(poly.vertices).bulge = pair.value.assert_f64(); },
                        91 => { vec_last!(poly.vertices).id = pair.value.assert_i32(); },
                        // other pairs
                        39 => { poly.thickness = pair.value.assert_f64(); },
                        43 => { poly.constant_width = pair.value.assert_f64(); },
                        70 => { poly.flags = pair.value.assert_i16() as i32; },
                        210 => { poly.extrusion_direction.x = pair.value.assert_f64(); },
                        220 => { poly.extrusion_direction.y = pair.value.assert_f64(); },
                        230 => { poly.extrusion_direction.z = pair.value.assert_f64(); },
                        _ => { try!(self.common.apply_individual_pair(&pair)); },
                    }
                }
            },
            EntityType::MText(ref mut mtext) => {
                let mut reading_column_data = false;
                let mut read_column_count = false;
                loop {
                    let pair = next_pair!(iter);
                    match pair.code {
                        10 => { mtext.insertion_point.x = pair.value.assert_f64(); },
                        20 => { mtext.insertion_point.y = pair.value.assert_f64(); },
                        30 => { mtext.insertion_point.z = pair.value.assert_f64(); },
                        40 => { mtext.initial_text_height = pair.value.assert_f64(); },
                        41 => { mtext.reference_rectangle_width = pair.value.assert_f64(); },
                        71 => { mtext.attachment_point = try_result!(AttachmentPoint::from_i16(pair.value.assert_i16())); },
                        72 => { mtext.drawing_direction = try_result!(DrawingDirection::from_i16(pair.value.assert_i16())); },
                        3 => { mtext.extended_text.push(pair.value.assert_string()); },
                        1 => { mtext.text = pair.value.assert_string(); },
                        7 => { mtext.text_style_name = pair.value.assert_string(); },
                        210 => { mtext.extrusion_direction.x = pair.value.assert_f64(); },
                        220 => { mtext.extrusion_direction.y = pair.value.assert_f64(); },
                        230 => { mtext.extrusion_direction.z = pair.value.assert_f64(); },
                        11 => { mtext.x_axis_direction.x = pair.value.assert_f64(); },
                        21 => { mtext.x_axis_direction.y = pair.value.assert_f64(); },
                        31 => { mtext.x_axis_direction.z = pair.value.assert_f64(); },
                        42 => { mtext.horizontal_width = pair.value.assert_f64(); },
                        43 => { mtext.vertical_height = pair.value.assert_f64(); },
                        50 => {
                            if reading_column_data {
                                if read_column_count {
                                    mtext.column_heights.push(pair.value.assert_f64());
                                }
                                else {
                                    mtext.column_count = pair.value.assert_f64() as i32;
                                    read_column_count = true;
                                }
                            }
                            else {
                                mtext.rotation_angle = pair.value.assert_f64();
                            }
                        },
                        73 => { mtext.line_spacing_style = try_result!(MTextLineSpacingStyle::from_i16(pair.value.assert_i16())); },
                        44 => { mtext.line_spacing_factor = pair.value.assert_f64(); },
                        90 => { mtext.background_fill_setting = try_result!(BackgroundFillSetting::from_i32(pair.value.assert_i32())); },
                        420 => { mtext.background_color_rgb = pair.value.assert_i32(); },
                        430 => { mtext.background_color_name = pair.value.assert_string(); },
                        45 => { mtext.fill_box_scale = pair.value.assert_f64(); },
                        63 => { mtext.background_fill_color = Color::from_raw_value(pair.value.assert_i16()); },
                        441 => { mtext.background_fill_color_transparency = pair.value.assert_i32(); },
                        75 => {
                            mtext.column_type = pair.value.assert_i16();
                            reading_column_data = true;
                        },
                        76 => { mtext.column_count = pair.value.assert_i16() as i32; },
                        78 => { mtext.is_column_flow_reversed = as_bool(pair.value.assert_i16()); },
                        79 => { mtext.is_column_auto_height = as_bool(pair.value.assert_i16()); },
                        48 => { mtext.column_width = pair.value.assert_f64(); },
                        49 => { mtext.column_gutter = pair.value.assert_f64(); },
                        _ => { try!(self.common.apply_individual_pair(&pair)); },
                    }
                }
            },
            _ => return Ok(false), // no custom reader
        }

        Ok(true)
    }
    #[doc(hidden)]
    pub fn write<T>(&self, version: &AcadVersion, write_handles: bool, writer: &mut CodePairAsciiWriter<T>) -> DxfResult<()>
        where T: Write {
        if self.specific.is_supported_on_version(version) {
            try!(writer.write_code_pair(&CodePair::new_str(0, self.specific.to_type_string())));
            try!(self.common.write(version, write_handles, writer));
            try!(self.specific.write(&self.common, version, writer));
            try!(self.post_write(&version, write_handles, writer));
        }

        Ok(())
    }
    fn post_write<T>(&self, version: &AcadVersion, write_handles: bool, writer: &mut CodePairAsciiWriter<T>) -> DxfResult<()>
        where T: Write {
        match self.specific {
            // TODO: write trailing MText on Attribute and AttributeDefinition
            EntityType::Polyline(ref poly) => {
                for v in &poly.vertices {
                    let v = Entity { common: Default::default(), specific: EntityType::Vertex(v.clone()) };
                    try!(v.write(&version, write_handles, writer));
                }
                let seqend = Entity { common: Default::default(), specific: EntityType::Seqend(Default::default()) };
                try!(seqend.write(&version, write_handles, writer));
            },
            _ => (),
        }

        Ok(())
    }
}

fn combine_points_2<F, T>(v1: &mut Vec<f64>, v2: &mut Vec<f64>, result: &mut Vec<T>, comb: F)
    where F: Fn(f64, f64, f64) -> T {
    for (x, y) in v1.drain(..).zip(v2.drain(..)) {
        result.push(comb(x, y, 0.0));
    }
    v1.clear();
    v2.clear();
}

fn combine_points_3<F, T>(v1: &mut Vec<f64>, v2: &mut Vec<f64>, v3: &mut Vec<f64>, result: &mut Vec<T>, comb: F)
    where F: Fn(f64, f64, f64) -> T {
    for (x, (y, z)) in v1.drain(..).zip(v2.drain(..).zip(v3.drain(..))) {
        result.push(comb(x, y, z))
    }
    v1.clear();
    v2.clear();
    v3.clear();
}

//------------------------------------------------------------------------------
//                                                  ProxyEntity-specific methods
//------------------------------------------------------------------------------
impl ProxyEntity {
    // lower word
    pub fn get_object_drawing_format_version(&self) -> i32 {
        (self._object_drawing_format & 0xFFFF) as i32
    }
    pub fn set_object_drawing_format_version(&mut self, version: i32) {
        self._object_drawing_format |= version as u32 & 0xFFFF;
    }
    // upper word
    pub fn get_object_maintenance_release_version(&self) -> i32 {
        self._object_drawing_format as i32 >> 4
    }
    pub fn set_object_mainenance_release_version(&mut self, version: i32) {
        self._object_drawing_format = (version << 4) as u32 + (self._object_drawing_format & 0xFFFF);
    }
}

//------------------------------------------------------------------------------
//                                                                    EntityIter
//------------------------------------------------------------------------------
struct EntityIter<'a, I: 'a + Iterator<Item = DxfResult<CodePair>>> {
    iter: &'a mut PutBack<I>,
}

impl<'a, I: 'a + Iterator<Item = DxfResult<CodePair>>> Iterator for EntityIter<'a, I> {
    type Item = Entity;
    fn next(&mut self) -> Option<Entity> {
        match Entity::read(self.iter) {
            Ok(Some(e)) => Some(e),
            Ok(None) | Err(_) => None,
        }
    }
}

//------------------------------------------------------------------------------
//                                                                         Point
//------------------------------------------------------------------------------
/// Represents a simple point in Cartesian space.
#[derive(Clone, Debug, PartialEq)]
pub struct Point {
    /// The X value of the point.
    pub x: f64,
    /// The Y value of the point.
    pub y: f64,
    /// The Z value of the point.
    pub z: f64,
}

impl Point {
    /// Creates a new `Point` with the specified values.
    pub fn new(x: f64, y: f64, z: f64) -> Point {
        Point{
            x: x,
            y: y,
            z: z,
        }
    }
    /// Returns a point representing the origin of (0, 0, 0).
    pub fn origin() -> Point {
        Point::new(0.0, 0.0, 0.0)
    }
    #[doc(hidden)]
    pub fn set(&mut self, pair: &CodePair) -> DxfResult<()> {
        match pair.code {
            10 => self.x = pair.value.assert_f64(),
            20 => self.y = pair.value.assert_f64(),
            30 => self.z = pair.value.assert_f64(),
            _ => return Err(DxfError::UnexpectedCodePair(pair.clone(), String::from("expected code [10, 20, 30] for point"))),
        }

        Ok(())
    }
}

//------------------------------------------------------------------------------
//                                                                        Vector
//------------------------------------------------------------------------------
/// Represents a simple vector in Cartesian space.
#[derive(Clone, Debug, PartialEq)]
pub struct Vector {
    /// The X component of the vector.
    pub x: f64,
    /// The Y component of the vector.
    pub y: f64,
    /// The Z component of the vector.
    pub z: f64,
}

impl Vector {
    /// Creates a new `Vector` with the specified values.
    pub fn new(x: f64, y: f64, z: f64) -> Vector {
        Vector {
            x: x,
            y: y,
            z: z,
        }
    }
    /// Returns a new zero vector representing (0, 0, 0).
    pub fn zero() -> Vector {
        Vector::new(0.0, 0.0, 0.0)
    }
    /// Returns a new vector representing the X axis.
    pub fn x_axis() -> Vector {
        Vector::new(1.0, 0.0, 0.0)
    }
    /// Returns a new vector representing the Y axis.
    pub fn y_axis() -> Vector {
        Vector::new(0.0, 1.0, 0.0)
    }
    /// Returns a new vector representing the Z axis.
    pub fn z_axis() -> Vector {
        Vector::new(0.0, 0.0, 1.0)
    }
    #[doc(hidden)]
    pub fn set(&mut self, pair: &CodePair) -> DxfResult<()> {
        match pair.code {
            10 => self.x = pair.value.assert_f64(),
            20 => self.y = pair.value.assert_f64(),
            30 => self.z = pair.value.assert_f64(),
            _ => return Err(DxfError::UnexpectedCodePair(pair.clone(), String::from("expected code [10, 20, 30] for vector"))),
        }

        Ok(())
    }
}

//------------------------------------------------------------------------------
//                                                              LwPolylineVertex
//------------------------------------------------------------------------------
/// Represents a single vertex of a `LwPolyline`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct LwPolylineVertex {
    pub x: f64,
    pub y: f64,
    pub id: i32,
    pub starting_width: f64,
    pub ending_width: f64,
    pub bulge: f64,
}

impl LwPolylineVertex {
    pub fn new() -> Self {
        Default::default()
    }
}

//------------------------------------------------------------------------------
//                                                                         Color
//------------------------------------------------------------------------------
/// Represents an indexed color.
#[derive(Clone, Debug, PartialEq)]
pub struct Color {
    raw_value: i16,
}

impl Color {
    /// Returns `true` if the color defaults back to the item's layer's color.
    pub fn is_by_layer(&self) -> bool {
        self.raw_value == 256
    }
    /// Returns `true` if the color defaults back to the entity's color.
    pub fn is_by_entity(&self) -> bool {
        self.raw_value == 257
    }
    /// Returns `true` if the color defaults back to the containing block's color.
    pub fn is_by_block(&self) -> bool {
        self.raw_value == 0
    }
    /// Returns `true` if the color represents a `Layer` that is turned off.
    pub fn is_turned_off(&self) -> bool {
        self.raw_value < 0
    }
    /// Sets the color to default back to the item's layer's color.
    pub fn set_by_layer(&mut self) {
        self.raw_value = 256
    }
    /// Sets the color to default back to the containing block's color.
    pub fn set_by_block(&mut self) {
        self.raw_value = 0
    }
    /// Sets the color to default back to the containing entity's color.
    pub fn set_by_entity(&mut self) {
        self.raw_value = 257
    }
    /// Sets the color to represent a `Layer` that is turned off.
    pub fn turn_off(&mut self) {
        self.raw_value = -1
    }
    /// Returns `true` if the color represents a proper color index.
    pub fn is_index(&self) -> bool {
        self.raw_value >= 1 && self.raw_value <= 255
    }
    /// Gets an `Option<u8>` of the indexable value of the color.
    pub fn index(&self) -> Option<u8> {
        if self.is_index() {
            Some(self.raw_value as u8)
        }
        else {
            None
        }
    }
    #[doc(hidden)]
    pub fn get_raw_value(&self) -> i16 {
        self.raw_value
    }
    #[doc(hidden)]
    pub fn from_raw_value(val: i16) -> Color {
        Color { raw_value: val }
    }
    /// Creates a `Color` that defaults to the item's layer's color.
    pub fn by_layer() -> Color {
        Color { raw_value: 256 }
    }
    /// Creates a `Color` that defaults back to the containing block's color.
    pub fn by_block() -> Color {
        Color { raw_value: 0 }
    }
    /// Creates a `Color` that defaults back to the containing entity's color.
    pub fn by_entity() -> Color {
        Color { raw_value: 257 }
    }
    /// Creates a `Color` from the specified index.
    pub fn from_index(i: u8) -> Color {
        Color { raw_value: i as i16 }
    }
    #[doc(hidden)]
    pub fn get_writable_color_value(&self, layer: &Layer) -> i16 {
       let value = match self.get_raw_value().abs() {
            0 | 256 => 7i16, // BYLAYER and BYBLOCK aren't valid
            v => v,
        };
        let value = match layer.is_layer_on {
            true => value,
            false => -value,
        };

        value
    }
}

//------------------------------------------------------------------------------
//                                                                    LineWeight
//------------------------------------------------------------------------------
/// Represents a line weight.
pub struct LineWeight {
    raw_value: i16,
}

impl LineWeight {
    /// Creates a new `LineWeight`.
    pub fn new() -> LineWeight {
        LineWeight::from_raw_value(0)
    }
    #[doc(hidden)]
    pub fn from_raw_value(v: i16) -> LineWeight {
        LineWeight { raw_value: v }
    }
    /// Creates a new `LineWeight` that defaults back to the containing block's line weight.
    pub fn by_block() -> LineWeight {
        LineWeight::from_raw_value(-1)
    }
    /// Creates a new `LineWeight` that defaults back to the item's layer's line weight.
    pub fn by_layer() -> LineWeight {
        LineWeight::from_raw_value(-2)
    }
    #[doc(hidden)]
    pub fn get_raw_value(&self) -> i16 {
        self.raw_value
    }
}