librepcb_rust_core/ffi/
ibom_ffi.rs

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
//! FFI for
//! [`interactive-html-bom::InteractiveHtmlBom`](https://docs.rs/interactive-html-bom/latest/struct.interactiveHtmlBom.html)

use super::cpp_ffi::*;
use interactive_html_bom::*;
use std::convert::From;

/// Wrapper for [ViewMode]
#[repr(C)]
#[allow(dead_code)]
#[derive(Copy, Clone)]
enum InteractiveHtmlBomViewMode {
  /// BOM only
  BomOnly,
  /// BOM left, drawings right
  LeftRight,
  /// BOM top, drawings bottom
  TopBottom,
}

impl From<InteractiveHtmlBomViewMode> for ViewMode {
  fn from(val: InteractiveHtmlBomViewMode) -> Self {
    match val {
      InteractiveHtmlBomViewMode::BomOnly => ViewMode::BomOnly,
      InteractiveHtmlBomViewMode::LeftRight => ViewMode::LeftRight,
      InteractiveHtmlBomViewMode::TopBottom => ViewMode::TopBottom,
    }
  }
}

/// Wrapper for [HighlightPin1Mode]
#[repr(C)]
#[allow(dead_code)]
#[derive(Copy, Clone)]
enum InteractiveHtmlBomHighlightPin1Mode {
  /// No pins
  None,
  /// Selected pins
  Selected,
  /// All pins
  All,
}

impl From<InteractiveHtmlBomHighlightPin1Mode> for HighlightPin1Mode {
  fn from(val: InteractiveHtmlBomHighlightPin1Mode) -> Self {
    match val {
      InteractiveHtmlBomHighlightPin1Mode::None => HighlightPin1Mode::None,
      InteractiveHtmlBomHighlightPin1Mode::Selected => {
        HighlightPin1Mode::Selected
      }
      InteractiveHtmlBomHighlightPin1Mode::All => HighlightPin1Mode::All,
    }
  }
}

/// Wrapper for [Layer]
#[repr(C)]
#[allow(dead_code)]
#[derive(Copy, Clone)]
enum InteractiveHtmlBomLayer {
  /// Front layer
  Front,
  /// Back layer
  Back,
}

impl From<InteractiveHtmlBomLayer> for Layer {
  fn from(val: InteractiveHtmlBomLayer) -> Self {
    match val {
      InteractiveHtmlBomLayer::Front => Layer::Front,
      InteractiveHtmlBomLayer::Back => Layer::Back,
    }
  }
}

/// Wrapper for [DrawingKind]
#[repr(C)]
#[allow(dead_code)]
#[derive(Copy, Clone)]
enum InteractiveHtmlBomDrawingKind {
  /// Polygon
  Polygon,
  /// Component reference designator text
  ReferenceText,
  /// Component value text
  ValueText,
}

impl From<InteractiveHtmlBomDrawingKind> for DrawingKind {
  fn from(val: InteractiveHtmlBomDrawingKind) -> Self {
    match val {
      InteractiveHtmlBomDrawingKind::Polygon => DrawingKind::Polygon,
      InteractiveHtmlBomDrawingKind::ReferenceText => {
        DrawingKind::ReferenceText
      }
      InteractiveHtmlBomDrawingKind::ValueText => DrawingKind::ValueText,
    }
  }
}

/// Wrapper for [DrawingLayer]
#[repr(C)]
#[allow(dead_code)]
#[derive(Copy, Clone)]
enum InteractiveHtmlBomDrawingLayer {
  /// PCB edge
  Edge,
  /// Silkscreen front
  SilkscreenFront,
  /// Silkscreen back
  SilkscreenBack,
  /// Fabrication front
  FabricationFront,
  /// Fabrication back
  FabricationBack,
}

impl From<InteractiveHtmlBomDrawingLayer> for DrawingLayer {
  fn from(val: InteractiveHtmlBomDrawingLayer) -> Self {
    match val {
      InteractiveHtmlBomDrawingLayer::Edge => DrawingLayer::Edge,
      InteractiveHtmlBomDrawingLayer::SilkscreenFront => {
        DrawingLayer::SilkscreenFront
      }
      InteractiveHtmlBomDrawingLayer::SilkscreenBack => {
        DrawingLayer::SilkscreenBack
      }
      InteractiveHtmlBomDrawingLayer::FabricationFront => {
        DrawingLayer::FabricationFront
      }
      InteractiveHtmlBomDrawingLayer::FabricationBack => {
        DrawingLayer::FabricationBack
      }
    }
  }
}

/// Enum to define board side(s)
#[repr(C)]
#[allow(dead_code)]
enum InteractiveHtmlBomSides {
  /// Front
  Front,
  /// Back
  Back,
  /// Front + Back
  Both,
}

/// Wrapper for [Pad]
#[repr(C)]
struct InteractiveHtmlBomPad {
  /// Visible on front side
  front: bool,
  /// Visible on back side
  back: bool,
  /// Position X
  pos_x: f32,
  /// Position Y
  pos_y: f32,
  /// Rotation angle
  angle: f32,
  /// SVG path
  svgpath: *const QString,
  /// Has drill
  has_drill: bool,
  /// Drill width
  drillsize_x: f32,
  /// Drill height
  drillsize_y: f32,
  /// Net name (optional)
  net: *const QString,
  /// Pin-1 or not
  pin1: bool,
}

/// Wrapper for [RefMap]
#[repr(C)]
struct InteractiveHtmlBomRefMap {
  /// Part reference
  reference: *const QString,
  /// Footprint ID
  id: usize,
}

/// Create a new [InteractiveHtmlBom] object
#[no_mangle]
extern "C" fn ffi_ibom_new(
  title: &QString,
  company: &QString,
  revision: &QString,
  date: &QString,
  bottom_left_x: f32,
  bottom_left_y: f32,
  top_right_x: f32,
  top_right_y: f32,
) -> *mut InteractiveHtmlBom {
  let ibom = InteractiveHtmlBom::new(
    &from_qstring(title),
    &from_qstring(company),
    &from_qstring(revision),
    &from_qstring(date),
    (bottom_left_x, bottom_left_y),
    (top_right_x, top_right_y),
  );
  Box::into_raw(Box::new(ibom))
}

/// Delete [InteractiveHtmlBom] object
#[no_mangle]
extern "C" fn ffi_ibom_delete(obj: *mut InteractiveHtmlBom) {
  assert!(!obj.is_null());
  unsafe { drop(Box::from_raw(obj)) };
}

/// Wrapper to set [InteractiveHtmlBom::view_mode],
/// [InteractiveHtmlBom::dark_mode] and [InteractiveHtmlBom::highlight_pin1]
#[no_mangle]
extern "C" fn ffi_ibom_set_view_config(
  obj: &mut InteractiveHtmlBom,
  mode: InteractiveHtmlBomViewMode,
  highlight_pin1: InteractiveHtmlBomHighlightPin1Mode,
  dark: bool,
) {
  obj.view_mode = mode.into();
  obj.highlight_pin1 = highlight_pin1.into();
  obj.dark_mode = dark;
}

/// Wrapper to set [InteractiveHtmlBom::board_rotation] and
/// [InteractiveHtmlBom::offset_back_rotation]
#[no_mangle]
extern "C" fn ffi_ibom_set_rotation(
  obj: &mut InteractiveHtmlBom,
  angle: f32,
  offset_back: bool,
) {
  obj.board_rotation = angle;
  obj.offset_back_rotation = offset_back;
}

/// Wrapper to set [InteractiveHtmlBom::show_silkscreen]
#[no_mangle]
extern "C" fn ffi_ibom_set_show_silkscreen(
  obj: &mut InteractiveHtmlBom,
  show: bool,
) {
  obj.show_silkscreen = show;
}

/// Wrapper to set [InteractiveHtmlBom::show_fabrication]
#[no_mangle]
extern "C" fn ffi_ibom_set_show_fabrication(
  obj: &mut InteractiveHtmlBom,
  show: bool,
) {
  obj.show_fabrication = show;
}

/// Wrapper to set [InteractiveHtmlBom::show_pads]
#[no_mangle]
extern "C" fn ffi_ibom_set_show_pads(obj: &mut InteractiveHtmlBom, show: bool) {
  obj.show_pads = show;
}

/// Wrapper to set [InteractiveHtmlBom::checkboxes]
#[no_mangle]
extern "C" fn ffi_ibom_set_checkboxes(
  obj: &mut InteractiveHtmlBom,
  checkboxes: &QStringList,
) {
  obj.checkboxes = from_qstringlist(checkboxes);
}

/// Wrapper to set [InteractiveHtmlBom::fields]
#[no_mangle]
extern "C" fn ffi_ibom_set_fields(
  obj: &mut InteractiveHtmlBom,
  fields: &QStringList,
) {
  obj.fields = from_qstringlist(fields);
}

/// Wrapper for adding a drawing to [InteractiveHtmlBom::drawings]
#[no_mangle]
extern "C" fn ffi_ibom_add_drawing(
  obj: &mut InteractiveHtmlBom,
  kind: InteractiveHtmlBomDrawingKind,
  layer: InteractiveHtmlBomDrawingLayer,
  svgpath: &QString,
  width: f32,
  filled: bool,
) {
  obj.drawings.push(Drawing::new(
    kind.into(),
    layer.into(),
    &from_qstring(svgpath),
    width,
    filled,
  ));
}

/// Wrapper for [InteractiveHtmlBom::add_footprint]
#[no_mangle]
extern "C" fn ffi_ibom_add_footprint(
  obj: &mut InteractiveHtmlBom,
  layer: InteractiveHtmlBomLayer,
  pos_x: f32,
  pos_y: f32,
  angle: f32,
  bottom_left_x: f32,
  bottom_left_y: f32,
  top_right_x: f32,
  top_right_y: f32,
  mount: bool,
  fields: &QStringList,
  pads_array: *const InteractiveHtmlBomPad,
  pads_size: usize,
) -> usize {
  let mut pads = Vec::new();
  unsafe {
    for pad in std::slice::from_raw_parts(pads_array, pads_size) {
      let mut layers = Vec::new();
      if pad.front {
        layers.push(Layer::Front);
      }
      if pad.back {
        layers.push(Layer::Back);
      }
      let net = if pad.net.is_null() {
        None
      } else {
        Some(from_qstring(&*pad.net))
      };
      pads.push(Pad::new(
        &layers,
        (pad.pos_x, pad.pos_y),
        pad.angle,
        &from_qstring(&*pad.svgpath),
        if pad.has_drill {
          Some((pad.drillsize_x, pad.drillsize_y))
        } else {
          None
        },
        net.as_deref(),
        pad.pin1,
      ));
    }
  }

  obj.add_footprint(Footprint::new(
    layer.into(),
    (pos_x, pos_y),
    angle,
    (bottom_left_x, bottom_left_y),
    (top_right_x, top_right_y),
    &from_qstringlist(fields),
    &pads,
    mount,
  ))
}

/// Wrapper to add BOM lines to [InteractiveHtmlBom::bom_front],
/// [InteractiveHtmlBom::bom_back] or [InteractiveHtmlBom::bom_both]
#[no_mangle]
extern "C" fn ffi_ibom_add_bom_line(
  obj: &mut InteractiveHtmlBom,
  sides: InteractiveHtmlBomSides,
  parts_array: *const InteractiveHtmlBomRefMap,
  parts_size: usize,
) {
  let mut vec = Vec::new();
  unsafe {
    for map in std::slice::from_raw_parts(parts_array, parts_size) {
      vec.push(RefMap::new(&from_qstring(&*map.reference), map.id));
    }
  }

  match sides {
    InteractiveHtmlBomSides::Front => obj.bom_front.push(vec),
    InteractiveHtmlBomSides::Back => obj.bom_back.push(vec),
    InteractiveHtmlBomSides::Both => obj.bom_both.push(vec),
  }
}

/// Wrapper for adding a track to [InteractiveHtmlBom::tracks]
#[no_mangle]
extern "C" fn ffi_ibom_add_track(
  obj: &mut InteractiveHtmlBom,
  layer: InteractiveHtmlBomLayer,
  start_x: f32,
  start_y: f32,
  end_x: f32,
  end_y: f32,
  width: f32,
  net_name: *const QString,
) {
  let net = if net_name.is_null() {
    None
  } else {
    unsafe { Some(from_qstring(&*net_name)) }
  };
  obj.tracks.push(Track::new(
    layer.into(),
    (start_x, start_y),
    (end_x, end_y),
    width,
    net.as_deref(),
  ));
}

/// Wrapper for adding a via to [InteractiveHtmlBom::vias]
#[no_mangle]
extern "C" fn ffi_ibom_add_via(
  obj: &mut InteractiveHtmlBom,
  layers_array: *const InteractiveHtmlBomLayer,
  layers_size: usize,
  pos_x: f32,
  pos_y: f32,
  diameter: f32,
  drill_diameter: f32,
  net_name: *const QString,
) {
  unsafe {
    let layers = std::slice::from_raw_parts(layers_array, layers_size)
      .iter()
      .map(|x| (*x).into())
      .collect::<Vec<_>>();
    let net = if net_name.is_null() {
      None
    } else {
      Some(from_qstring(&*net_name))
    };
    obj.vias.push(Via::new(
      &layers,
      (pos_x, pos_y),
      diameter,
      drill_diameter,
      net.as_deref(),
    ));
  }
}

/// Wrapper for adding a zone to [InteractiveHtmlBom::zones]
#[no_mangle]
extern "C" fn ffi_ibom_add_zone(
  obj: &mut InteractiveHtmlBom,
  layer: InteractiveHtmlBomLayer,
  svgpath: &QString,
  net_name: *const QString,
) {
  let net = if net_name.is_null() {
    None
  } else {
    unsafe { Some(from_qstring(&*net_name)) }
  };
  obj.zones.push(Zone::new(
    layer.into(),
    &from_qstring(svgpath),
    net.as_deref(),
  ));
}

/// Wrapper for [InteractiveHtmlBom::generate_html]
#[no_mangle]
extern "C" fn ffi_ibom_generate_html(
  obj: &InteractiveHtmlBom,
  out: &mut QString,
  err: &mut QString,
) -> bool {
  match obj.generate_html() {
    Ok(html) => {
      qstring_set(out, &html);
      true
    }
    Err(msg) => {
      qstring_set(err, &msg);
      false
    }
  }
}