Save BOM excel file with dxf files.

This commit is contained in:
AJ
2018-04-23 08:37:12 -04:00
parent f0bc4ae7a1
commit e72f2e9948
6 changed files with 117 additions and 12 deletions

View File

@@ -60,6 +60,10 @@
<SignManifests>false</SignManifests> <SignManifests>false</SignManifests>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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"> <Reference Include="SolidWorks.Interop.sldworks, Version=24.1.0.45, Culture=neutral, PublicKeyToken=7c4797c3e4eeac03, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<EmbedInteropTypes>False</EmbedInteropTypes> <EmbedInteropTypes>False</EmbedInteropTypes>
@@ -71,8 +75,12 @@
<HintPath>C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.swconst.dll</HintPath> <HintPath>C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.swconst.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Bend.cs" /> <Compile Include="Bend.cs" />
@@ -104,6 +112,7 @@
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
</Compile> </Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -116,6 +125,9 @@
<Content Include="Templates\Blank.drwdot"> <Content Include="Templates\Blank.drwdot">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="Templates\BomTemplate.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Resources\edit_alt.png" /> <None Include="Resources\edit_alt.png" />

View File

@@ -1,4 +1,5 @@
using SolidWorks.Interop.sldworks; using OfficeOpenXml;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst; using SolidWorks.Interop.swconst;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -216,6 +217,10 @@ namespace ExportDXF.Forms
var items = GetItems(bom); var items = GetItems(bom);
foreach (var item in items)
{
}
Print("Found " + items.Count); Print("Found " + items.Count);
Print(""); Print("");
@@ -278,16 +283,40 @@ namespace ExportDXF.Forms
if (worker.CancellationPending) if (worker.CancellationPending)
break; break;
var fileName = prefix + item.PartNo + ".dxf"; item.ItemNo = prefix + item.ItemNo;
var fileName = item.ItemNo + ".dxf";
var savepath = Path.Combine(savePath, fileName); var savepath = Path.Combine(savePath, fileName);
var part = item.Component.GetModelDoc2() as PartDoc; 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) if (part == null)
continue; continue;
SavePartToDXF(part, item.Component.ReferencedConfiguration, savepath); SavePartToDXF(part, config, savepath);
Application.DoEvents(); 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) 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() private string UserSelectFolder()
{ {
string path = null; string path = null;
@@ -513,4 +573,6 @@ namespace ExportDXF.Forms
get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); } get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); }
} }
} }
} }

View File

@@ -156,5 +156,28 @@ namespace ExportDXF
return -1; 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;
}
} }
} }

View File

@@ -12,6 +12,10 @@ namespace ExportDXF
public string Description { get; set; } public string Description { get; set; }
public double Thickness { get; set; }
public string Material { get; set; }
public Component2 Component { get; set; } public Component2 Component { get; set; }
} }
} }

Binary file not shown.

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EPPlus" version="4.5.1" targetFramework="net40" />
</packages>