nlbn 1.0.23

Fast and reliable EasyEDA/LCSC to KiCad converter with batch processing support
Documentation
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
use crate::converter::{angle_to_ki, sanitize_name, Converter};
use crate::easyeda::{ComponentData, FootprintImporter};
use crate::easyeda::svg_parser::{parse_svg_path, SvgCommand};
use crate::error::Result;
use crate::kicad;
use crate::library::LibraryManager;
use crate::cli::Cli;

pub fn convert_footprint(
    args: &Cli,
    component_data: &ComponentData,
    lib_manager: &LibraryManager,
    lcsc_id: &str,
) -> Result<()> {
    let ee_footprint = FootprintImporter::parse(&component_data.package_detail)?;
    let converter = Converter::new(args.kicad_version());

    // Use LCSC ID as unique identifier to prevent name collisions
    let footprint_name = format!("{}_{}", sanitize_name(&component_data.title), lcsc_id);

    // Convert EasyEDA footprint to KiCad footprint
    let mut ki_footprint = kicad::KiFootprint {
        name: footprint_name,
        pads: Vec::new(),
        tracks: Vec::new(),
        circles: Vec::new(),
        arcs: Vec::new(),
        texts: Vec::new(),
        lines: Vec::new(),
        model_3d: None,
    };

    // Convert pads with bbox adjustment
    for ee_pad in &ee_footprint.pads {
        let pad_type = if ee_pad.hole_radius.is_some() {
            kicad::PadType::ThroughHole
        } else {
            kicad::PadType::Smd
        };

        // Use layer mapping based on pad type
        let layers = if pad_type == kicad::PadType::ThroughHole {
            kicad::map_pad_layers_tht(ee_pad.layer_id)
        } else {
            kicad::map_pad_layers_smd(ee_pad.layer_id)
        };

        // Create drill for through-hole pads
        let drill = if let Some(hole_radius) = ee_pad.hole_radius {
            if let Some(hole_length) = ee_pad.hole_length {
                // Elliptical drill
                let max_distance_hole = (hole_radius * 2.0).max(hole_length);
                let pos_0 = ee_pad.height - max_distance_hole;
                let pos_90 = ee_pad.width - max_distance_hole;

                if pos_0 > pos_90 {
                    // Vertical orientation
                    Some(kicad::Drill {
                        diameter: hole_radius * 2.0,
                        width: Some(hole_length),
                        offset_x: 0.0,
                        offset_y: 0.0,
                    })
                } else {
                    // Horizontal orientation
                    Some(kicad::Drill {
                        diameter: hole_length,
                        width: Some(hole_radius * 2.0),
                        offset_x: 0.0,
                        offset_y: 0.0,
                    })
                }
            } else {
                // Circular drill
                Some(kicad::Drill {
                    diameter: hole_radius * 2.0,
                    width: None,
                    offset_x: 0.0,
                    offset_y: 0.0,
                })
            }
        } else {
            None
        };

        // Apply bbox normalization for footprint coordinates
        let adjusted_x = ee_pad.x - component_data.package_bbox_x;
        let adjusted_y = ee_pad.y - component_data.package_bbox_y;
        let adjusted_x_mm = converter.px_to_mm(adjusted_x);
        let adjusted_y_mm = converter.px_to_mm(adjusted_y);

        // Handle polygon pads
        let (size_x, size_y, rotation, polygon) = if ee_pad.shape == "POLYGON" && !ee_pad.points.is_empty() {
            // Parse points: space-separated x y coordinates
            let coords: Vec<f64> = ee_pad.points
                .split_whitespace()
                .filter_map(|s| s.parse::<f64>().ok())
                .collect();

            if coords.len() >= 4 {  // At least 2 points (x,y pairs)
                // Convert coordinates to mm and make relative to pad position
                let mut poly_str = String::from("\n\t\t(primitives \n\t\t\t(gr_poly \n\t\t\t\t(pts");

                for i in (0..coords.len()).step_by(2) {
                    if i + 1 < coords.len() {
                        let abs_x_mm = converter.px_to_mm(coords[i] - component_data.package_bbox_x);
                        let abs_y_mm = converter.px_to_mm(coords[i + 1] - component_data.package_bbox_y);
                        let rel_x = abs_x_mm - adjusted_x_mm;
                        let rel_y = abs_y_mm - adjusted_y_mm;
                        poly_str.push_str(&format!(" (xy {:.2} {:.2})", rel_x, rel_y));
                    }
                }

                poly_str.push_str("\n\t\t\t\t) \n\t\t\t\t(width 0.1) \n\t\t\t)\n\t\t)\n\t");

                // Set minimal pad size (enforced minimum 0.01) and force orientation to 0
                (0.01, 0.01, 0.0, Some(poly_str))
            } else {
                let rot = angle_to_ki(ee_pad.rotation);
                (ee_pad.width.max(0.01), ee_pad.height.max(0.01), rot, None)
            }
        } else {
            let rot = angle_to_ki(ee_pad.rotation);
            (ee_pad.width.max(0.01), ee_pad.height.max(0.01), rot, None)
        };

        ki_footprint.pads.push(kicad::KiPad {
            number: ee_pad.number.clone(),
            pad_type,
            shape: kicad::PadShape::from_easyeda(&ee_pad.shape),
            pos_x: adjusted_x,
            pos_y: adjusted_y,
            size_x,
            size_y,
            rotation,
            layers,
            drill,
            polygon,
        });
    }

    // Convert tracks to lines with bbox adjustment
    // TRACK has a points string: "x1 y1 x2 y2 x3 y3..." which represents a polyline
    // We need to convert it to multiple line segments
    for ee_track in &ee_footprint.tracks {
        // Parse points string into coordinates
        let coords: Vec<f64> = ee_track.points
            .split_whitespace()
            .filter_map(|s| s.parse::<f64>().ok())
            .collect();

        // Create line segments from consecutive point pairs
        // Each pair of points (x1,y1) -> (x2,y2) becomes one line
        for i in (0..coords.len().saturating_sub(2)).step_by(2) {
            if i + 3 < coords.len() {
                let x1 = coords[i];
                let y1 = coords[i + 1];
                let x2 = coords[i + 2];
                let y2 = coords[i + 3];

                let adjusted_x1 = x1 - component_data.package_bbox_x;
                let adjusted_y1 = y1 - component_data.package_bbox_y;
                let adjusted_x2 = x2 - component_data.package_bbox_x;
                let adjusted_y2 = y2 - component_data.package_bbox_y;

                ki_footprint.lines.push(kicad::KiLine {
                    start_x: adjusted_x1,
                    start_y: adjusted_y1,
                    end_x: adjusted_x2,
                    end_y: adjusted_y2,
                    width: ee_track.stroke_width,
                    layer: kicad::map_layer(ee_track.layer_id),
                });
            }
        }
    }

    // Convert circles with bbox adjustment
    for ee_circle in &ee_footprint.circles {
        let adjusted_cx = ee_circle.cx - component_data.package_bbox_x;
        let adjusted_cy = ee_circle.cy - component_data.package_bbox_y;

        ki_footprint.circles.push(kicad::FootprintKiCircle {
            center_x: adjusted_cx,
            center_y: adjusted_cy,
            end_x: adjusted_cx + ee_circle.radius,
            end_y: adjusted_cy,
            width: converter.px_to_mm(ee_circle.stroke_width).max(0.01),
            layer: kicad::map_layer(ee_circle.layer_id),
            fill: ee_circle.fill,
        });
    }

    // Convert holes to non-plated through-hole pads
    for ee_hole in &ee_footprint.holes {
        let adjusted_x = ee_hole.x - component_data.package_bbox_x;
        let adjusted_y = ee_hole.y - component_data.package_bbox_y;

        // EasyEDA stores radius, so diameter = radius * 2
        let diameter = ee_hole.radius * 2.0;

        ki_footprint.pads.push(kicad::KiPad {
            number: String::new(),  // Empty number for non-plated holes
            pad_type: kicad::PadType::NpThroughHole,
            shape: kicad::PadShape::Circle,
            pos_x: adjusted_x,
            pos_y: adjusted_y,
            size_x: diameter,
            size_y: diameter,
            rotation: 0.0,
            layers: vec!["*.Cu".to_string(), "*.Mask".to_string()],
            drill: Some(kicad::Drill {
                diameter,
                width: None,
                offset_x: 0.0,
                offset_y: 0.0,
            }),
            polygon: None,
        });
    }

    // Convert vias to through-hole pads
    for ee_via in &ee_footprint.vias {
        let adjusted_x = ee_via.x - component_data.package_bbox_x;
        let adjusted_y = ee_via.y - component_data.package_bbox_y;

        // Via has diameter (pad size) and radius (hole radius, so drill = radius * 2)
        let pad_size = ee_via.diameter;
        let drill_diameter = ee_via.radius * 2.0;

        ki_footprint.pads.push(kicad::KiPad {
            number: String::new(),  // Vias typically don't have pad numbers
            pad_type: kicad::PadType::ThroughHole,
            shape: kicad::PadShape::Circle,
            pos_x: adjusted_x,
            pos_y: adjusted_y,
            size_x: pad_size,
            size_y: pad_size,
            rotation: 0.0,
            layers: vec!["*.Cu".to_string(), "*.Mask".to_string()],
            drill: Some(kicad::Drill {
                diameter: drill_diameter,
                width: None,
                offset_x: 0.0,
                offset_y: 0.0,
            }),
            polygon: None,
        });
    }

    // Convert arcs with bbox adjustment (SVG path format)
    for ee_arc in &ee_footprint.arcs {
        let commands = match parse_svg_path(&ee_arc.path) {
            Ok(cmds) => cmds,
            Err(e) => {
                log::warn!("Skipping arc with invalid SVG path: {} ({})", ee_arc.path, e);
                continue;
            }
        };

        let mut current_pos = (0.0_f64, 0.0_f64);
        for cmd in &commands {
            match cmd {
                SvgCommand::MoveTo { x, y } => {
                    current_pos = (*x, *y);
                }
                SvgCommand::Arc { rx, ry, angle, large_arc, sweep, x, y } => {
                    let start_x = current_pos.0;
                    let start_y = current_pos.1;
                    let end_x = *x;
                    let end_y = *y;

                    match converter.compute_arc_center(
                        (start_x, start_y),
                        (end_x, end_y),
                        (*rx, *ry),
                        *angle,
                        *large_arc,
                        *sweep,
                    ) {
                        Ok((cx, cy, start_angle_deg, end_angle_deg)) => {
                            let start_rad = start_angle_deg.to_radians();
                            let end_rad = end_angle_deg.to_radians();
                            let mut mid_angle = (start_rad + end_rad) / 2.0;

                            let angle_diff = end_rad - start_rad;
                            if (*sweep && angle_diff < 0.0) || (!*sweep && angle_diff > 0.0) {
                                mid_angle += std::f64::consts::PI;
                            }

                            let mid_x = cx + rx * mid_angle.cos();
                            let mid_y = cy + ry * mid_angle.sin();

                            let adj_start_x = start_x - component_data.package_bbox_x;
                            let adj_start_y = start_y - component_data.package_bbox_y;
                            let adj_mid_x = mid_x - component_data.package_bbox_x;
                            let adj_mid_y = mid_y - component_data.package_bbox_y;
                            let adj_end_x = end_x - component_data.package_bbox_x;
                            let adj_end_y = end_y - component_data.package_bbox_y;

                            ki_footprint.arcs.push(kicad::FootprintKiArc {
                                start_x: adj_start_x,
                                start_y: adj_start_y,
                                mid_x: adj_mid_x,
                                mid_y: adj_mid_y,
                                end_x: adj_end_x,
                                end_y: adj_end_y,
                                width: ee_arc.stroke_width,
                                layer: kicad::map_layer(ee_arc.layer_id),
                            });
                        }
                        Err(e) => {
                            log::warn!("Failed to compute arc center: {}", e);
                        }
                    }

                    current_pos = (end_x, end_y);
                }
                SvgCommand::LineTo { x, y } => {
                    current_pos = (*x, *y);
                }
                SvgCommand::ClosePath => {}
            }
        }
    }

    // Convert rectangles to 4 lines
    for ee_rect in &ee_footprint.rectangles {
        let adjusted_x = ee_rect.x - component_data.package_bbox_x;
        let adjusted_y = ee_rect.y - component_data.package_bbox_y;
        let adjusted_x2 = (ee_rect.x + ee_rect.width) - component_data.package_bbox_x;
        let adjusted_y2 = (ee_rect.y + ee_rect.height) - component_data.package_bbox_y;

        let layer = kicad::map_layer(ee_rect.layer_id);
        let width = converter.px_to_mm(ee_rect.stroke_width).max(0.01);

        // Top line
        ki_footprint.lines.push(kicad::KiLine {
            start_x: adjusted_x,
            start_y: adjusted_y,
            end_x: adjusted_x2,
            end_y: adjusted_y,
            width,
            layer: layer.clone(),
        });

        // Right line
        ki_footprint.lines.push(kicad::KiLine {
            start_x: adjusted_x2,
            start_y: adjusted_y,
            end_x: adjusted_x2,
            end_y: adjusted_y2,
            width,
            layer: layer.clone(),
        });

        // Bottom line
        ki_footprint.lines.push(kicad::KiLine {
            start_x: adjusted_x2,
            start_y: adjusted_y2,
            end_x: adjusted_x,
            end_y: adjusted_y2,
            width,
            layer: layer.clone(),
        });

        // Left line
        ki_footprint.lines.push(kicad::KiLine {
            start_x: adjusted_x,
            start_y: adjusted_y2,
            end_x: adjusted_x,
            end_y: adjusted_y,
            width,
            layer,
        });
    }

    // Convert texts with bbox adjustment
    for ee_text in &ee_footprint.texts {
        let adjusted_x = ee_text.x - component_data.package_bbox_x;
        let adjusted_y = ee_text.y - component_data.package_bbox_y;

        ki_footprint.texts.push(kicad::KiText {
            text: ee_text.text.clone(),
            pos_x: adjusted_x,
            pos_y: adjusted_y,
            rotation: ee_text.rotation as f64,
            layer: kicad::map_layer(ee_text.layer_id),
            size: ee_text.font_size,
            thickness: converter.px_to_mm(ee_text.stroke_width).max(0.01),
        });
    }

    // Add 3D model reference if available
    if let Some(model_info) = &component_data.model_3d {
        if args.model_3d || args.full {
            // Use LCSC ID as unique identifier to prevent name collisions
            let model_name = format!("{}_{}", sanitize_name(&model_info.title), lcsc_id);

            // Default to project-relative paths (KIPRJMOD) for easier setup
            // Use --project-relative flag to force global paths if needed
            // Prefer STEP format as it's more widely supported
            let lib_name = lib_manager.lib_name();
            let model_path = if args.project_relative {
                format!("${{KIPRJMOD}}/{}.3dshapes/{}.step", lib_name, model_name)
            } else {
                format!("${{{}}}/{}.3dshapes/{}.step", lib_name, lib_name, model_name)
            };

            ki_footprint.model_3d = Some(kicad::Ki3dModel {
                path: model_path,
                offset: (0.0, 0.0, 0.0),
                scale: (1.0, 1.0, 1.0),
                rotate: (0.0, 0.0, 0.0),
            });
        }
    }

    // Export footprint
    let exporter = kicad::FootprintExporter::new();
    let footprint_data = exporter.export(&ki_footprint)?;
    lib_manager.write_footprint(&ki_footprint.name, &footprint_data)?;

    println!("\u{2713} Footprint converted: {}", ki_footprint.name);

    Ok(())
}