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
//! 最小 DXF (ASCII R12) 导入/导出。
//! 导出:LINE/CIRCLE/ARC/POLYLINE/POINT/ELLIPSE(分解)/SPLINE(分解)/SOLID(填充) + LAYER 表。
//! 导入:LINE/CIRCLE/ARC/LWPOLYLINE/POLYLINE/POINT/ELLIPSE/SOLID + 图层(code 8)。

use std::collections::HashMap;

use crate::data_structure::{
    make_arc, make_circle, make_ellipse, make_line, make_point, make_polyline, make_solid,
    Document, Entity, EntityGeometry, Layer, ObjectId,
};
use crate::geometry::{Ellipse, Point};
use crate::io::{Error as ImportError, Importer};
use crate::render::tessellation::entity_polylines;

fn num(v: f64) -> String {
    if v == 0.0 {
        "0.0".to_string()
    } else {
        format!("{v:.6}")
    }
}

fn pair(out: &mut String, code: i32, value: &str) {
    out.push_str(&code.to_string());
    out.push('\n');
    out.push_str(value);
    out.push('\n');
}

fn norm_deg(deg: f64) -> f64 {
    let d = deg % 360.0;
    if d < 0.0 {
        d + 360.0
    } else {
        d
    }
}

/// DXF ACI 颜色 → RGB 近似
pub fn aci_to_rgb(aci: i64) -> (u8, u8, u8) {
    match aci {
        1 => (255, 0, 0),
        2 => (255, 255, 0),
        3 => (0, 255, 0),
        4 => (0, 255, 255),
        5 => (0, 0, 255),
        6 => (255, 0, 255),
        _ => (255, 255, 255),
    }
}

/// DXF ACI 颜色索引近似映射
pub fn rgb_to_aci(color: (u8, u8, u8)) -> i64 {
    let palette: [(i64, (u8, u8, u8)); 7] = [
        (1, (255, 0, 0)),
        (2, (255, 255, 0)),
        (3, (0, 255, 0)),
        (4, (0, 255, 255)),
        (5, (0, 0, 255)),
        (6, (255, 0, 255)),
        (7, (255, 255, 255)),
    ];
    let mut best = 7;
    let mut best_d = i64::MAX;
    for (idx, (r, g, b)) in palette {
        let d = (r as i64 - color.0 as i64).abs()
            + (g as i64 - color.1 as i64).abs()
            + (b as i64 - color.2 as i64).abs();
        if d < best_d {
            best_d = d;
            best = idx;
        }
    }
    best
}

/// 实体所在图层名(导出用;找不到返回 "0")
fn layer_name(doc: &Document, layer_id: &ObjectId) -> String {
    doc.get_layer(layer_id)
        .map(|l| l.name().to_string())
        .filter(|n| !n.is_empty())
        .unwrap_or_else(|| "0".to_string())
}

/// 写一个 SOLID(四点;DXF 渲染顺序 1-2-4-3,故 12/13 交换写入)
fn write_solid(out: &mut String, layer: &str, pts: &[Point; 4], color: (u8, u8, u8)) {
    pair(out, 0, "SOLID");
    pair(out, 8, layer);
    for (code_x, code_y, p) in [(10, 20, pts[0]), (11, 21, pts[1]), (12, 22, pts[3]), (13, 23, pts[2])] {
        pair(out, code_x, &num(p.x));
        pair(out, code_y, &num(p.y));
        pair(out, code_x + 20, "0.0");
    }
    pair(out, 62, &rgb_to_aci(color).to_string());
}

/// 多边形扇形三角化后写为多个 SOLID
fn write_solid_fan(out: &mut String, layer: &str, pts: &[Point], color: (u8, u8, u8)) {
    for i in 1..pts.len().saturating_sub(1) {
        let tri = [pts[0], pts[i], pts[i + 1], pts[i + 1]];
        write_solid(out, layer, &tri, color);
    }
}

/// 导出为 ASCII DXF (R12)
pub fn export(doc: &Document) -> String {
    let mut s = String::new();
    pair(&mut s, 0, "SECTION");
    pair(&mut s, 2, "HEADER");
    pair(&mut s, 9, "$ACADVER");
    pair(&mut s, 1, "AC1009");
    pair(&mut s, 0, "ENDSEC");

    // TABLES / LAYER 表(负颜色 = 图层关闭)
    pair(&mut s, 0, "SECTION");
    pair(&mut s, 2, "TABLES");
    pair(&mut s, 0, "TABLE");
    pair(&mut s, 2, "LAYER");
    pair(&mut s, 70, &doc.layer_count().to_string());
    for layer in doc.layers().values() {
        pair(&mut s, 0, "LAYER");
        pair(&mut s, 2, layer.name());
        pair(&mut s, 70, "0");
        let color = layer.color();
        let aci = rgb_to_aci((color.red, color.green, color.blue));
        let aci = if layer.is_visible() { aci } else { -aci };
        pair(&mut s, 62, &aci.to_string());
        pair(&mut s, 6, "CONTINUOUS");
    }
    pair(&mut s, 0, "ENDTAB");
    pair(&mut s, 0, "ENDSEC");

    pair(&mut s, 0, "SECTION");
    pair(&mut s, 2, "ENTITIES");
    for entity in doc.entities().values() {
        let lname = layer_name(doc, &entity.layer_id);
        let lname = lname.as_str();
        match entity.geometry() {
            EntityGeometry::Line(l) => {
                pair(&mut s, 0, "LINE");
                pair(&mut s, 8, lname);
                pair(&mut s, 10, &num(l.start.x));
                pair(&mut s, 20, &num(l.start.y));
                pair(&mut s, 30, "0.0");
                pair(&mut s, 11, &num(l.end.x));
                pair(&mut s, 21, &num(l.end.y));
                pair(&mut s, 31, "0.0");
            }
            EntityGeometry::Circle(c) => {
                pair(&mut s, 0, "CIRCLE");
                pair(&mut s, 8, lname);
                pair(&mut s, 10, &num(c.center.x));
                pair(&mut s, 20, &num(c.center.y));
                pair(&mut s, 30, "0.0");
                pair(&mut s, 40, &num(c.radius));
            }
            EntityGeometry::Arc(a) => {
                // DXF 圆弧总是逆时针:顺时针弧交换起止角
                let (start_deg, end_deg) = if a.is_counter_clockwise {
                    (a.start_angle.to_degrees(), a.end_angle.to_degrees())
                } else {
                    (a.end_angle.to_degrees(), a.start_angle.to_degrees())
                };
                pair(&mut s, 0, "ARC");
                pair(&mut s, 8, lname);
                pair(&mut s, 10, &num(a.center.x));
                pair(&mut s, 20, &num(a.center.y));
                pair(&mut s, 30, "0.0");
                pair(&mut s, 40, &num(a.radius));
                pair(&mut s, 50, &num(norm_deg(start_deg)));
                pair(&mut s, 51, &num(norm_deg(end_deg)));
            }
            EntityGeometry::Point(p) => {
                pair(&mut s, 0, "POINT");
                pair(&mut s, 8, lname);
                pair(&mut s, 10, &num(p.x));
                pair(&mut s, 20, &num(p.y));
                pair(&mut s, 30, "0.0");
            }
            EntityGeometry::Polyline(p) => {
                if p.vertices.len() < 2 {
                    continue;
                }
                pair(&mut s, 0, "POLYLINE");
                pair(&mut s, 8, lname);
                pair(&mut s, 66, "1");
                pair(&mut s, 70, if p.is_closed { "1" } else { "0" });
                for v in &p.vertices {
                    pair(&mut s, 0, "VERTEX");
                    pair(&mut s, 8, lname);
                    pair(&mut s, 10, &num(v.x));
                    pair(&mut s, 20, &num(v.y));
                    pair(&mut s, 30, "0.0");
                }
                pair(&mut s, 0, "SEQEND");
                pair(&mut s, 8, lname);
            }
            EntityGeometry::Dimension { .. }
            | EntityGeometry::Text { .. }
            | EntityGeometry::Ellipse(_)
            | EntityGeometry::BSpline(_)
            | EntityGeometry::NURBS(_) => {
                // 标注 / 文字 / 椭圆 / 样条:分解为折线笔画,以 POLYLINE 写出(R12 兼容)
                for (pts, closed) in entity_polylines(entity) {
                    if pts.len() < 2 {
                        continue;
                    }
                    pair(&mut s, 0, "POLYLINE");
                    pair(&mut s, 8, lname);
                    pair(&mut s, 66, "1");
                    pair(&mut s, 70, if closed { "1" } else { "0" });
                    for p in &pts {
                        pair(&mut s, 0, "VERTEX");
                        pair(&mut s, 8, lname);
                        pair(&mut s, 10, &num(p.x));
                        pair(&mut s, 20, &num(p.y));
                        pair(&mut s, 30, "0.0");
                    }
                    pair(&mut s, 0, "SEQEND");
                    pair(&mut s, 8, lname);
                }
            }
            EntityGeometry::Solid { points, color } => {
                write_solid(&mut s, lname, points, *color);
            }
            EntityGeometry::Hatch {
                solid_fill,
                fill_color,
                boundary_paths,
                ..
            } if *solid_fill => {
                // 实心填充:边界扇形三角化为 SOLID
                for bp in boundary_paths {
                    if !bp.is_polyline {
                        continue;
                    }
                    let pts: Vec<Point> = bp.edges.iter().map(|e| e.start_point).collect();
                    if pts.len() >= 3 {
                        write_solid_fan(&mut s, lname, &pts, *fill_color);
                    }
                }
            }
            _ => {}
        }
    }
    pair(&mut s, 0, "ENDSEC");
    pair(&mut s, 0, "EOF");
    s
}

struct Pair {
    code: i32,
    value: String,
}

fn parse_pairs(src: &str) -> Vec<Pair> {
    let mut pairs = Vec::new();
    let mut lines = src.lines();
    while let (Some(code), Some(value)) = (lines.next(), lines.next()) {
        let code: i32 = code.trim().parse().unwrap_or(-999);
        pairs.push(Pair {
            code,
            value: value.trim().to_string(),
        });
    }
    pairs
}

/// 读取从 start 开始、到下一个 code==0 之前的字段(后出现的值覆盖前面的)
fn read_fields(pairs: &[Pair], start: usize) -> (HashMap<i32, f64>, HashMap<i32, String>, usize) {
    let mut map = HashMap::new();
    let mut strs = HashMap::new();
    let mut i = start;
    while i < pairs.len() && pairs[i].code != 0 {
        if let Ok(v) = pairs[i].value.parse::<f64>() {
            map.insert(pairs[i].code, v);
        } else {
            strs.insert(pairs[i].code, pairs[i].value.clone());
        }
        i += 1;
    }
    (map, strs, i)
}

fn f(map: &HashMap<i32, f64>, code: i32) -> f64 {
    map.get(&code).copied().unwrap_or(0.0)
}

fn layer_field(strs: &HashMap<i32, String>) -> String {
    strs.get(&8).cloned().unwrap_or_else(|| "0".to_string())
}

/// 按图层名查找/创建图层,返回图层 id
fn resolve_layer(
    doc: &mut Document,
    map: &mut HashMap<String, ObjectId>,
    name: &str,
) -> ObjectId {
    if let Some(id) = map.get(name) {
        return id.clone();
    }
    let layer = Layer::new(name.to_string());
    let id = doc.add_layer(layer);
    map.insert(name.to_string(), id.clone());
    id
}

fn add_to_layer(
    doc: &mut Document,
    map: &mut HashMap<String, ObjectId>,
    name: &str,
    mut entity: Entity,
) {
    let id = resolve_layer(doc, map, name);
    entity.layer_id = id;
    doc.add_entity(entity);
}

fn add_polyline(
    doc: &mut Document,
    map: &mut HashMap<String, ObjectId>,
    name: &str,
    verts: Vec<(f64, f64)>,
    closed: bool,
) {
    if verts.len() < 2 {
        return;
    }
    let pts: Vec<Point> = verts
        .iter()
        .map(|(x, y)| Point::new2d(*x, *y))
        .collect();
    add_to_layer(doc, map, name, make_polyline(&pts, closed));
}

/// 导入 ASCII DXF,返回新文档
pub fn import(src: &str) -> Result<Document, String> {
    let pairs = parse_pairs(src);
    if pairs.is_empty() {
        return Err("Empty file or not an ASCII DXF".to_string());
    }
    let mut doc = Document::new("imported".to_string());
    // DXF 默认图层 "0" 映射到 ModelSpace
    let mut layer_ids: HashMap<String, ObjectId> = HashMap::new();
    layer_ids.insert("0".to_string(), doc.model_space().clone());
    let n = pairs.len();
    let mut in_entities = false;
    let mut i = 0;

    while i < n {
        let p = &pairs[i];
        if p.code != 0 {
            i += 1;
            continue;
        }
        match p.value.as_str() {
            "SECTION" => {
                if i + 1 < n && pairs[i + 1].code == 2 {
                    in_entities = pairs[i + 1].value == "ENTITIES";
                    i += 2;
                    continue;
                }
            }
            "ENDSEC" => in_entities = false,
            "LINE" if in_entities => {
                let (m, ms, j) = read_fields(&pairs, i + 1);
                let a = Point::new2d(f(&m, 10), f(&m, 20));
                let b = Point::new2d(f(&m, 11), f(&m, 21));
                if a.distance_to(&b) > 1e-12 {
                    add_to_layer(&mut doc, &mut layer_ids, &layer_field(&ms), make_line(a, b));
                }
                i = j;
                continue;
            }
            "CIRCLE" if in_entities => {
                let (m, ms, j) = read_fields(&pairs, i + 1);
                let c = Point::new2d(f(&m, 10), f(&m, 20));
                let r = f(&m, 40);
                if r > 1e-12 {
                    add_to_layer(&mut doc, &mut layer_ids, &layer_field(&ms), make_circle(c, r));
                }
                i = j;
                continue;
            }
            "ARC" if in_entities => {
                let (m, ms, j) = read_fields(&pairs, i + 1);
                let c = Point::new2d(f(&m, 10), f(&m, 20));
                let r = f(&m, 40);
                let s = f(&m, 50).to_radians();
                let e = f(&m, 51).to_radians();
                if r > 1e-12 {
                    add_to_layer(&mut doc, &mut layer_ids, &layer_field(&ms), make_arc(c, r, s, e));
                }
                i = j;
                continue;
            }
            "POINT" if in_entities => {
                let (m, ms, j) = read_fields(&pairs, i + 1);
                let p = Point::new2d(f(&m, 10), f(&m, 20));
                add_to_layer(&mut doc, &mut layer_ids, &layer_field(&ms), make_point(p));
                i = j;
                continue;
            }
            "ELLIPSE" if in_entities => {
                let (m, ms, j) = read_fields(&pairs, i + 1);
                let c = Point::new2d(f(&m, 10), f(&m, 20));
                let (mx, my) = (f(&m, 11), f(&m, 21));
                let ratio = f(&m, 40);
                let t0 = f(&m, 41);
                let t1 = f(&m, 42);
                let a = (mx * mx + my * my).sqrt();
                let rot = my.atan2(mx);
                let b = a * ratio;
                let full = (t0.abs() < 1e-9 && (t1 - std::f64::consts::TAU).abs() < 1e-3)
                    || (t0.abs() < 1e-9 && t1.abs() < 1e-9);
                if a > 1e-12 && b > 1e-12 && ratio > 0.0 && ratio <= 1.0 + 1e-9 {
                    let entity = if full {
                        make_ellipse(c, a, b, rot)
                    } else {
                        // 部分椭圆 → 折线近似(参数为弧度,SDK 参数 0..1 对应整椭圆)
                        let e = Ellipse::new(c, a, b, rot);
                        let steps = 64;
                        let pts: Vec<Point> = (0..=steps)
                            .map(|k| {
                                let t = t0 + (t1 - t0) * k as f64 / steps as f64;
                                e.point_at_parameter(t / std::f64::consts::TAU)
                            })
                            .collect();
                        make_polyline(&pts, false)
                    };
                    add_to_layer(&mut doc, &mut layer_ids, &layer_field(&ms), entity);
                }
                i = j;
                continue;
            }
            "SOLID" if in_entities => {
                let (m, ms, j) = read_fields(&pairs, i + 1);
                // DXF 顶点 1,2,3,4 渲染顺序为 1-2-4-3 → 还原为多边形顺序 1,2,4,3
                let a = Point::new2d(f(&m, 10), f(&m, 20));
                let b = Point::new2d(f(&m, 11), f(&m, 21));
                let c = Point::new2d(f(&m, 13), f(&m, 23));
                let d = Point::new2d(f(&m, 12), f(&m, 22));
                let mut pts = vec![a, b, c, d];
                // 去掉重复尾点(三角形)
                while pts.len() > 3 && pts[pts.len() - 1].distance_to(&pts[pts.len() - 2]) < 1e-12 {
                    pts.pop();
                }
                let color = aci_to_rgb(f(&m, 62) as i64);
                if let Some(entity) = make_solid(&pts, color) {
                    add_to_layer(&mut doc, &mut layer_ids, &layer_field(&ms), entity);
                }
                i = j;
                continue;
            }
            "LWPOLYLINE" if in_entities => {
                let mut verts: Vec<(f64, f64)> = Vec::new();
                let mut closed = false;
                let mut cur_x: Option<f64> = None;
                let mut lname = "0".to_string();
                let mut j = i + 1;
                while j < n && pairs[j].code != 0 {
                    let pj = &pairs[j];
                    match pj.code {
                        8 => lname = pj.value.clone(),
                        70 => {
                            closed = pj.value.parse::<f64>().map(|v| v as i64 & 1 != 0).unwrap_or(false);
                        }
                        10 => cur_x = pj.value.parse::<f64>().ok(),
                        20 => {
                            if let Some(x) = cur_x {
                                if let Ok(y) = pj.value.parse::<f64>() {
                                    verts.push((x, y));
                                }
                            }
                            cur_x = None;
                        }
                        _ => {}
                    }
                    j += 1;
                }
                add_polyline(&mut doc, &mut layer_ids, &lname, verts, closed);
                i = j;
                continue;
            }
            "POLYLINE" if in_entities => {
                let (m, ms, j) = read_fields(&pairs, i + 1);
                let closed = (f(&m, 70) as i64) & 1 != 0;
                let lname = layer_field(&ms);
                let mut verts: Vec<(f64, f64)> = Vec::new();
                let mut k = j;
                while k < n && pairs[k].code == 0 && pairs[k].value == "VERTEX" {
                    let (vf, _vs, nk) = read_fields(&pairs, k + 1);
                    verts.push((f(&vf, 10), f(&vf, 20)));
                    k = nk;
                }
                add_polyline(&mut doc, &mut layer_ids, &lname, verts, closed);
                i = k;
                continue;
            }
            _ => {}
        }
        i += 1;
    }

    if doc.entity_count() == 0 {
        return Err("No recognizable entities found (LINE/CIRCLE/ARC/POLYLINE)".to_string());
    }
    Ok(doc)
}

/// ASCII DXF (R12) 导入器
#[derive(Debug, Clone, Copy)]
pub struct DXFImporter;

impl DXFImporter {
    pub fn new() -> Self {
        Self
    }
}

impl Default for DXFImporter {
    fn default() -> Self {
        Self::new()
    }
}

impl Importer for DXFImporter {
    fn can_import(&self, extension: &str) -> bool {
        extension.to_lowercase() == "dxf"
    }

    fn import_from_file(&self, filename: &str) -> Result<Document, ImportError> {
        let content = std::fs::read_to_string(filename)
            .map_err(|e| ImportError::Io(e.to_string()))?;
        self.import_from_bytes(content.as_bytes(), "dxf")
    }

    fn import_from_bytes(&self, data: &[u8], extension: &str) -> Result<Document, ImportError> {
        if !self.can_import(extension) {
            return Err(ImportError::UnsupportedFormat(extension.to_string()));
        }
        let content = String::from_utf8_lossy(data);
        import(&content).map_err(ImportError::ParseError)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::io::Importer;

    #[test]
    fn test_dxf_importer_can_import() {
        let importer = DXFImporter::new();
        assert!(importer.can_import("dxf"));
        assert!(importer.can_import("DXF"));
        assert!(!importer.can_import("svg"));
    }

    #[test]
    fn test_dxf_export_import_roundtrip() {
        let mut doc = Document::new("test".to_string());
        doc.add_entity(make_line(Point::new2d(0.0, 0.0), Point::new2d(10.0, 0.0)));
        doc.add_entity(make_circle(Point::new2d(5.0, 5.0), 2.0));

        let text = export(&doc);
        assert!(text.contains("LINE"));
        assert!(text.contains("CIRCLE"));
        assert!(text.contains("AC1009"));

        let imported = import(&text).unwrap();
        assert_eq!(imported.entity_count(), 2);
    }

    #[test]
    fn test_dxf_import_rejects_empty() {
        assert!(import("").is_err());
    }
}