using System;
using System.Collections.Generic;
using System.IO;
namespace OpenNest.IO.Bom;
///
/// Builds the import dialog's part rows from the BOM items and the drawing
/// folder.
///
public static class BomImportRows
{
///
/// Returns one row per BOM item, in BOM order. Every row with a file
/// name gets the drawing it matches in ,
/// whether or not its material and thickness are filled in. A blank
/// BOM quantity becomes 1, marked as assumed.
///
public static List Build(List items, string dxfFolder)
{
var index = DrawingFileIndex.Load(dxfFolder);
var rows = new List();
foreach (var item in items)
{
rows.Add(
new BomPartRow
{
ItemNum = item.ItemNum,
FileName = item.FileName,
BomQty = item.Qty,
Qty = item.Qty ?? 1,
QtyAssumed = item.Qty == null,
Description = item.Description,
Material = item.Material,
Thickness = item.Thickness,
DxfPath = string.IsNullOrWhiteSpace(item.FileName)
? null
: index.Find(item.FileName),
}
);
}
return rows;
}
///
/// The job a BOM file is for: its file name without the extension or a trailing
/// " BOM" ("Frame A BOM.xlsx" is "Frame A").
///
public static string JobName(string bomPath)
{
var name = Path.GetFileNameWithoutExtension(bomPath ?? "");
if (name.EndsWith(" BOM", StringComparison.OrdinalIgnoreCase))
name = name.Substring(0, name.Length - 4).TrimEnd();
return name;
}
}