Compare commits

..

2 Commits

Author SHA1 Message Date
AJ
0b4173014f ExportDrawingToPDF 2021-03-04 21:47:25 -05:00
AJ
eee6bfdff3 ExportBomToExcel 2021-03-04 21:30:10 -05:00
3 changed files with 99 additions and 59 deletions

View File

@@ -0,0 +1,69 @@
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
namespace ExportDXF
{
public class ExportBomToExcel
{
public string TemplatePath
{
get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "BomTemplate.xlsx"); }
}
public void CreateBOMExcelFile(string filepath, IList<Item> items)
{
File.Copy(TemplatePath, filepath, true);
var newFile = new FileInfo(filepath);
using (var pkg = new ExcelPackage(newFile))
{
var workbook = pkg.Workbook;
var partsSheet = workbook.Worksheets["Parts"];
for (int i = 0; i < items.Count; i++)
{
var item = items[i];
var row = i + 2;
var col = 1;
partsSheet.Cells[row, col++].Value = item.ItemNo;
partsSheet.Cells[row, col++].Value = item.FileName;
partsSheet.Cells[row, col++].Value = item.Quantity;
partsSheet.Cells[row, col++].Value = item.Description;
partsSheet.Cells[row, col++].Value = item.PartName;
partsSheet.Cells[row, col++].Value = item.Configuration;
if (item.Thickness > 0)
partsSheet.Cells[row, col].Value = item.Thickness;
col++;
partsSheet.Cells[row, col++].Value = item.Material;
if (item.KFactor > 0)
partsSheet.Cells[row, col].Value = item.KFactor;
col++;
if (item.BendRadius > 0)
partsSheet.Cells[row, col].Value = item.BendRadius;
}
for (int i = 1; i <= 8; i++)
{
var column = partsSheet.Column(i);
if (column.Style.WrapText)
continue;
column.AutoFit();
column.Width += 1;
}
workbook.Calculate();
pkg.Save();
}
}
}
}

View File

@@ -92,6 +92,7 @@
<Compile Include="BendOrientation.cs" />
<Compile Include="Bounds.cs" />
<Compile Include="DrawingInfo.cs" />
<Compile Include="ExportBomToExcel.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="ItemExtractor.cs" />
<Compile Include="Forms\ViewFlipDeciderComboboxItem.cs" />

View File

@@ -234,6 +234,26 @@ namespace ExportDXF.Forms
}
}
private void ExportDrawingToPDF(DrawingDoc drawingDoc)
{
var model = drawingDoc as ModelDoc2;
var pdfPath = model.GetPathName();
var ext = Path.GetExtension(pdfPath);
pdfPath = pdfPath.Remove(pdfPath.Length - ext.Length) + ".pdf";
var exportData = sldWorks.GetExportFileData((int)swExportDataFileType_e.swExportPdfData) as ExportPdfData;
exportData.ViewPdfAfterSaving = false;
exportData.SetSheets((int)swExportDataSheetsToExport_e.swExportData_ExportAllSheets, drawingDoc);
int errors = 0;
int warnings = 0;
var modelExtension = model.Extension;
modelExtension.SaveAs(pdfPath, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, exportData, ref errors, ref warnings);
Print("Saved drawing to PDF file \"" + pdfPath + "\"", Color.Green);
}
private void ExportToDXF(DrawingDoc drawing)
{
Print("Finding BOM tables...");
@@ -264,6 +284,8 @@ namespace ExportDXF.Forms
}
Print($"Found {items.Count} component(s)");
ExportDrawingToPDF(drawing);
ExportToDXF(items);
}
@@ -401,7 +423,9 @@ namespace ExportDXF.Forms
var drawingInfo = DrawingInfo.Parse(prefix);
var bomName = drawingInfo != null ? string.Format("{0} {1} BOM", drawingInfo.JobNo, drawingInfo.DrawingNo) : "BOM";
var bomFile = Path.Combine(savePath, bomName + ".xlsx");
CreateBOMExcelFile(bomFile, items.ToList());
var excelReport = new ExportBomToExcel();
excelReport.CreateBOMExcelFile(bomFile, items.ToList());
}
catch (Exception ex)
{
@@ -573,63 +597,6 @@ namespace ExportDXF.Forms
return modelChanged;
}
private void CreateBOMExcelFile(string filepath, IList<Item> items)
{
var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "BomTemplate.xlsx");
File.Copy(templatePath, filepath, true);
var newFile = new FileInfo(filepath);
using (var pkg = new ExcelPackage(newFile))
{
var workbook = pkg.Workbook;
var partsSheet = workbook.Worksheets["Parts"];
for (int i = 0; i < items.Count; i++)
{
var item = items[i];
var row = i + 2;
var col = 1;
partsSheet.Cells[row, col++].Value = item.ItemNo;
partsSheet.Cells[row, col++].Value = item.FileName;
partsSheet.Cells[row, col++].Value = item.Quantity;
partsSheet.Cells[row, col++].Value = item.Description;
partsSheet.Cells[row, col++].Value = item.PartName;
partsSheet.Cells[row, col++].Value = item.Configuration;
if (item.Thickness > 0)
partsSheet.Cells[row, col].Value = item.Thickness;
col++;
partsSheet.Cells[row, col++].Value = item.Material;
if (item.KFactor > 0)
partsSheet.Cells[row, col].Value = item.KFactor;
col++;
if (item.BendRadius > 0)
partsSheet.Cells[row, col].Value = item.BendRadius;
col++;
}
for (int i = 1; i <= 8; i++)
{
var column = partsSheet.Column(i);
if (column.Style.WrapText)
continue;
column.AutoFit();
column.Width += 1;
}
workbook.Calculate();
pkg.Save();
}
}
private string UserSelectFolder()
{
string path = null;
@@ -639,7 +606,10 @@ namespace ExportDXF.Forms
var dlg = new FolderBrowserDialog();
dlg.Description = "Where do you want to save the DXF files?";
path = dlg.ShowDialog() == DialogResult.OK ? dlg.SelectedPath : null;
if (dlg.ShowDialog() != DialogResult.OK)
throw new Exception("Export canceled by user.");
path = dlg.SelectedPath;
}));
return path;