Save BOM excel file with dxf files.
This commit is contained in:
@@ -60,6 +60,10 @@
|
||||
<SignManifests>false</SignManifests>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="EPPlus, Version=4.5.1.0, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\EPPlus.4.5.1\lib\net40\EPPlus.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="PresentationCore" />
|
||||
<Reference Include="SolidWorks.Interop.sldworks, Version=24.1.0.45, Culture=neutral, PublicKeyToken=7c4797c3e4eeac03, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<EmbedInteropTypes>False</EmbedInteropTypes>
|
||||
@@ -71,8 +75,12 @@
|
||||
<HintPath>C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.swconst.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.configuration" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Security" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bend.cs" />
|
||||
@@ -104,6 +112,7 @@
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
</Compile>
|
||||
<None Include="packages.config" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
@@ -116,6 +125,9 @@
|
||||
<Content Include="Templates\Blank.drwdot">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Templates\BomTemplate.xlsx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\edit_alt.png" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using SolidWorks.Interop.sldworks;
|
||||
using OfficeOpenXml;
|
||||
using SolidWorks.Interop.sldworks;
|
||||
using SolidWorks.Interop.swconst;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -216,7 +217,11 @@ namespace ExportDXF.Forms
|
||||
|
||||
var items = GetItems(bom);
|
||||
|
||||
Print("Found " + items.Count);
|
||||
foreach (var item in items)
|
||||
{
|
||||
}
|
||||
|
||||
Print("Found " + items.Count);
|
||||
Print("");
|
||||
|
||||
ExportToDXF(items);
|
||||
@@ -261,9 +266,9 @@ namespace ExportDXF.Forms
|
||||
private void ExportToDXF(IEnumerable<Item> items)
|
||||
{
|
||||
var savePath = UserSelectFolder();
|
||||
var prefix = textBox2.Text;
|
||||
var prefix = textBox2.Text;
|
||||
|
||||
if (savePath == null)
|
||||
if (savePath == null)
|
||||
{
|
||||
Print("Cancelled\n", Color.Red);
|
||||
return;
|
||||
@@ -278,17 +283,41 @@ namespace ExportDXF.Forms
|
||||
if (worker.CancellationPending)
|
||||
break;
|
||||
|
||||
var fileName = prefix + item.PartNo + ".dxf";
|
||||
var savepath = Path.Combine(savePath, fileName);
|
||||
var part = item.Component.GetModelDoc2() as PartDoc;
|
||||
item.ItemNo = prefix + item.ItemNo;
|
||||
|
||||
if (part == null)
|
||||
var fileName = item.ItemNo + ".dxf";
|
||||
var savepath = Path.Combine(savePath, fileName);
|
||||
var model = item.Component.GetModelDoc2() as ModelDoc2;
|
||||
var part = model as PartDoc;
|
||||
|
||||
var config = item.Component.ReferencedConfiguration;
|
||||
|
||||
var sheetMetal = model.GetFeatureByTypeName("SheetMetal");
|
||||
var thickness = sheetMetal.GetDimension("Thickness").GetValue2(config);
|
||||
var db = string.Empty;
|
||||
var material = part.GetMaterialPropertyName2(config, out db);
|
||||
|
||||
item.Thickness = thickness;
|
||||
item.Material = material;
|
||||
|
||||
if (part == null)
|
||||
continue;
|
||||
|
||||
SavePartToDXF(part, item.Component.ReferencedConfiguration, savepath);
|
||||
SavePartToDXF(part, config, savepath);
|
||||
Application.DoEvents();
|
||||
}
|
||||
}
|
||||
|
||||
var bomFile = Path.Combine(savePath, "BOM.xlsx");
|
||||
CreateBOMExcelFile(bomFile, items.ToList());
|
||||
}
|
||||
|
||||
private string ChangePathExtension(string fullpath, string newExtension)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(fullpath);
|
||||
var name = Path.GetFileNameWithoutExtension(fullpath);
|
||||
|
||||
return Path.Combine(dir, name + newExtension);
|
||||
}
|
||||
|
||||
private bool SavePartToDXF(PartDoc part, string savePath)
|
||||
{
|
||||
@@ -348,6 +377,37 @@ namespace ExportDXF.Forms
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
partsSheet.Cells[row, 1].Value = item.ItemNo;
|
||||
partsSheet.Cells[row, 2].Value = item.Quantity;
|
||||
partsSheet.Cells[row, 3].Value = item.Description;
|
||||
partsSheet.Cells[row, 4].Value = item.PartNo;
|
||||
partsSheet.Cells[row, 5].Value = item.Thickness;
|
||||
partsSheet.Cells[row, 6].Value = item.Material;
|
||||
}
|
||||
|
||||
workbook.Calculate();
|
||||
pkg.Save();
|
||||
}
|
||||
}
|
||||
|
||||
private string UserSelectFolder()
|
||||
{
|
||||
string path = null;
|
||||
@@ -363,7 +423,7 @@ namespace ExportDXF.Forms
|
||||
return path;
|
||||
}
|
||||
|
||||
private bool ShouldFlipView(SolidWorks.Interop.sldworks.View view)
|
||||
private bool ShouldFlipView(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
return viewFlipDecider.ShouldFlip(view);
|
||||
}
|
||||
@@ -513,4 +573,6 @@ namespace ExportDXF.Forms
|
||||
get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -156,5 +156,28 @@ namespace ExportDXF
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static Dimension GetDimension(this Feature feature, string dimName)
|
||||
{
|
||||
return feature?.Parameter(dimName) as Dimension;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Units
|
||||
{
|
||||
/// <summary>
|
||||
/// Multiply factor needed to convert the desired units to meters.
|
||||
/// </summary>
|
||||
public static double ScaleFactor = 0.0254; // inches to meters
|
||||
|
||||
public static double ToSldWorks(this double d)
|
||||
{
|
||||
return d * ScaleFactor;
|
||||
}
|
||||
|
||||
public static double FromSldWorks(this double d)
|
||||
{
|
||||
return d / ScaleFactor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,10 @@ namespace ExportDXF
|
||||
|
||||
public string Description { get; set; }
|
||||
|
||||
public double Thickness { get; set; }
|
||||
|
||||
public string Material { get; set; }
|
||||
|
||||
public Component2 Component { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
BIN
ExportDXF/Templates/BomTemplate.xlsx
Normal file
BIN
ExportDXF/Templates/BomTemplate.xlsx
Normal file
Binary file not shown.
4
ExportDXF/packages.config
Normal file
4
ExportDXF/packages.config
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="EPPlus" version="4.5.1" targetFramework="net40" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user