Skip to main content

geop_ops_drawing/
parts.rs

1//! [`parts_list`]: the bill of materials a drawing lists, from the part's
2//! own (see [`geop_ops_model::placed::bom`]).
3
4use geop_core_math::geop_error::GeopResult;
5use geop_core_program::reader::Reader;
6use geop_ops_model::placed::bom::{LineKind, Structure, bom};
7
8use crate::{DrawingArgs, PartsListLine};
9
10/// The bill of materials of the part `reader` reads, the program of the
11/// file `file`, as a drawing lists it (see [`PartsListLine`]): flat, each
12/// part and wire once with its count. Empty if `args` asks for none.
13pub fn parts_list(
14    reader: &Reader,
15    file: &str,
16    args: &DrawingArgs,
17) -> GeopResult<Vec<PartsListLine>> {
18    if !args.bom {
19        return Ok(Vec::new());
20    }
21    let bom = bom(reader, file, Structure::Flat)?;
22    Ok((bom.lines.into_iter())
23        .map(|line| PartsListLine {
24            item: line.item,
25            quantity: line.quantity,
26            name: line.name,
27            designation: line.designation.unwrap_or_default(),
28            material: match line.kind {
29                LineKind::Part { material, .. } => material.unwrap_or_default(),
30                LineKind::Wire { colour, .. } => format!("wire, {colour}"),
31            },
32            placements: line.placements,
33        })
34        .collect())
35}