Update file prefix when active document changes.

This commit is contained in:
aj
2018-04-26 20:45:52 -04:00
parent 3e59eebd11
commit f9fca98fe6
4 changed files with 198 additions and 133 deletions

49
ExportDXF/DrawingInfo.cs Normal file
View File

@@ -0,0 +1,49 @@
using System.Text.RegularExpressions;
namespace ExportDXF
{
public class DrawingInfo
{
public string JobNo { get; set; }
public string DrawingNo { get; set; }
public string Source { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", JobNo, DrawingNo);
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
return obj.ToString() == ToString();
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public static DrawingInfo Parse(string input)
{
const string pattern = @"(?<jobNo>[34]\d{3})\s?(?<dwgNo>[ABEP]\d+)";
var match = Regex.Match(input, pattern);
if (match.Success == false)
return null;
var dwg = new DrawingInfo();
dwg.JobNo = match.Groups["jobNo"].Value;
dwg.DrawingNo = match.Groups["dwgNo"].Value;
dwg.Source = input;
return dwg;
}
}
}

View File

@@ -87,6 +87,7 @@
<Compile Include="BendDirection.cs" /> <Compile Include="BendDirection.cs" />
<Compile Include="BendOrientation.cs" /> <Compile Include="BendOrientation.cs" />
<Compile Include="Bounds.cs" /> <Compile Include="Bounds.cs" />
<Compile Include="DrawingInfo.cs" />
<Compile Include="Item.cs" /> <Compile Include="Item.cs" />
<Compile Include="IViewFlipDecider.cs" /> <Compile Include="IViewFlipDecider.cs" />
<Compile Include="ViewFlipDecider.cs" /> <Compile Include="ViewFlipDecider.cs" />

View File

@@ -28,24 +28,25 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.textBox1 = new System.Windows.Forms.TextBox(); this.activeDocTitleBox = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox(); this.prefixTextBox = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout(); this.SuspendLayout();
// //
// textBox1 // textBox1
// //
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) this.activeDocTitleBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.BackColor = System.Drawing.Color.White; this.activeDocTitleBox.BackColor = System.Drawing.Color.White;
this.textBox1.Location = new System.Drawing.Point(130, 13); this.activeDocTitleBox.Location = new System.Drawing.Point(130, 13);
this.textBox1.Name = "textBox1"; this.activeDocTitleBox.Name = "textBox1";
this.textBox1.ReadOnly = true; this.activeDocTitleBox.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(400, 25); this.activeDocTitleBox.Size = new System.Drawing.Size(400, 25);
this.textBox1.TabIndex = 2; this.activeDocTitleBox.TabIndex = 2;
this.activeDocTitleBox.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
// //
// richTextBox1 // richTextBox1
// //
@@ -80,12 +81,12 @@
// //
// textBox2 // textBox2
// //
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) this.prefixTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.Location = new System.Drawing.Point(130, 44); this.prefixTextBox.Location = new System.Drawing.Point(130, 44);
this.textBox2.Name = "textBox2"; this.prefixTextBox.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(400, 25); this.prefixTextBox.Size = new System.Drawing.Size(400, 25);
this.textBox2.TabIndex = 2; this.prefixTextBox.TabIndex = 2;
// //
// button1 // button1
// //
@@ -107,8 +108,8 @@
this.Controls.Add(this.label2); this.Controls.Add(this.label2);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Controls.Add(this.richTextBox1); this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.textBox2); this.Controls.Add(this.prefixTextBox);
this.Controls.Add(this.textBox1); this.Controls.Add(this.activeDocTitleBox);
this.Controls.Add(this.button1); this.Controls.Add(this.button1);
this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
@@ -123,11 +124,11 @@
#endregion #endregion
private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox activeDocTitleBox;
private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox prefixTextBox;
} }
} }

View File

@@ -169,11 +169,10 @@ namespace ExportDXF.Forms
private void SetActiveDocName() private void SetActiveDocName()
{ {
var model = sldWorks.ActiveDoc as ModelDoc2; var model = sldWorks.ActiveDoc as ModelDoc2;
activeDocTitleBox.Text = model == null ? "<No Document Open>" : model.GetTitle();
}
textBox1.Text = model != null ? model.GetTitle() : "<No Document Open>"; private void DetermineModelTypeAndExportToDXF(ModelDoc2 model)
}
private void DetermineModelTypeAndExportToDXF(ModelDoc2 model)
{ {
if (model is PartDoc) if (model is PartDoc)
{ {
@@ -206,7 +205,7 @@ namespace ExportDXF.Forms
Print("Found " + bomTables.Count); Print("Found " + bomTables.Count);
Print(""); Print("");
var items = new List<Item>(); var items = new List<Item>();
foreach (var bom in bomTables) foreach (var bom in bomTables)
{ {
@@ -215,18 +214,18 @@ namespace ExportDXF.Forms
Print(bom.BomFeature.Name); Print(bom.BomFeature.Name);
Print("Fetching components..."); Print("Fetching components...");
Print("Found " + items.Count); Print("Found " + items.Count);
items.AddRange(GetItems(bom)); items.AddRange(GetItems(bom));
} }
Print("Found " + items.Count + " total"); Print("Found " + items.Count + " total");
ExportToDXF(items); ExportToDXF(items);
} }
private void ExportToDXF(PartDoc part) private void ExportToDXF(PartDoc part)
{ {
var prefix = textBox2.Text; var prefix = prefixTextBox.Text;
var model = part as ModelDoc2; var model = part as ModelDoc2;
var dir = UserSelectFolder(); var dir = UserSelectFolder();
@@ -262,9 +261,9 @@ namespace ExportDXF.Forms
private void ExportToDXF(IEnumerable<Item> items) private void ExportToDXF(IEnumerable<Item> items)
{ {
var savePath = UserSelectFolder(); var savePath = UserSelectFolder();
var prefix = textBox2.Text; var prefix = prefixTextBox.Text;
if (savePath == null) if (savePath == null)
{ {
Print("Canceled\n", Color.Red); Print("Canceled\n", Color.Red);
return; return;
@@ -279,65 +278,65 @@ namespace ExportDXF.Forms
if (worker.CancellationPending) if (worker.CancellationPending)
break; break;
item.ItemNo = prefix + item.ItemNo; item.ItemNo = prefix + item.ItemNo;
var fileName = item.ItemNo + ".dxf"; var fileName = item.ItemNo + ".dxf";
var savepath = Path.Combine(savePath, fileName); var savepath = Path.Combine(savePath, fileName);
var model = item.Component.GetModelDoc2() as ModelDoc2; var model = item.Component.GetModelDoc2() as ModelDoc2;
var part = model as PartDoc; var part = model as PartDoc;
if (part == null) if (part == null)
{ {
Print(item.ItemNo + " - skipped, not a part document"); Print(item.ItemNo + " - skipped, not a part document");
continue; continue;
} }
var config = item.Component.ReferencedConfiguration; var config = item.Component.ReferencedConfiguration;
var sheetMetal = model.GetFeatureByTypeName("SheetMetal"); var sheetMetal = model.GetFeatureByTypeName("SheetMetal");
var sheetMetalData = sheetMetal?.GetDefinition() as SheetMetalFeatureData; var sheetMetalData = sheetMetal?.GetDefinition() as SheetMetalFeatureData;
if (sheetMetalData != null) if (sheetMetalData != null)
{ {
item.Thickness = sheetMetalData.Thickness.FromSldWorks(); item.Thickness = sheetMetalData.Thickness.FromSldWorks();
item.KFactor = sheetMetalData.KFactor; item.KFactor = sheetMetalData.KFactor;
} }
if (item.Description == null) if (item.Description == null)
item.Description = model.Extension.CustomPropertyManager[config].Get("Description"); item.Description = model.Extension.CustomPropertyManager[config].Get("Description");
if (item.Description == null) if (item.Description == null)
item.Description = model.Extension.CustomPropertyManager[""].Get("Description"); item.Description = model.Extension.CustomPropertyManager[""].Get("Description");
var db = string.Empty; var db = string.Empty;
item.Material = part.GetMaterialPropertyName2(config, out db); item.Material = part.GetMaterialPropertyName2(config, out db);
if (part == null) if (part == null)
continue; continue;
SavePartToDXF(part, config, savepath); SavePartToDXF(part, config, savepath);
Application.DoEvents(); Application.DoEvents();
} }
try try
{ {
var bomFile = Path.Combine(savePath, "BOM.xlsx"); var bomFile = Path.Combine(savePath, "BOM.xlsx");
CreateBOMExcelFile(bomFile, items.ToList()); CreateBOMExcelFile(bomFile, items.ToList());
} }
catch (Exception ex) catch (Exception ex)
{ {
Print(ex.Message, Color.Red); Print(ex.Message, Color.Red);
} }
} }
private string ChangePathExtension(string fullpath, string newExtension) private string ChangePathExtension(string fullpath, string newExtension)
{ {
var dir = Path.GetDirectoryName(fullpath); var dir = Path.GetDirectoryName(fullpath);
var name = Path.GetFileNameWithoutExtension(fullpath); var name = Path.GetFileNameWithoutExtension(fullpath);
return Path.Combine(dir, name + newExtension); return Path.Combine(dir, name + newExtension);
} }
private bool SavePartToDXF(PartDoc part, string savePath) private bool SavePartToDXF(PartDoc part, string savePath)
{ {
@@ -397,42 +396,42 @@ namespace ExportDXF.Forms
} }
} }
private void CreateBOMExcelFile(string filepath, IList<Item> items) private void CreateBOMExcelFile(string filepath, IList<Item> items)
{ {
var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "BomTemplate.xlsx"); var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "BomTemplate.xlsx");
File.Copy(templatePath, filepath, true); File.Copy(templatePath, filepath, true);
var newFile = new FileInfo(filepath); var newFile = new FileInfo(filepath);
using (var pkg = new ExcelPackage(newFile)) using (var pkg = new ExcelPackage(newFile))
{ {
var workbook = pkg.Workbook; var workbook = pkg.Workbook;
var partsSheet = workbook.Worksheets["Parts"]; var partsSheet = workbook.Worksheets["Parts"];
for (int i = 0; i < items.Count; i++) for (int i = 0; i < items.Count; i++)
{ {
var item = items[i]; var item = items[i];
var row = i + 2; var row = i + 2;
partsSheet.Cells[row, 1].Value = item.ItemNo; partsSheet.Cells[row, 1].Value = item.ItemNo;
partsSheet.Cells[row, 2].Value = item.Quantity; partsSheet.Cells[row, 2].Value = item.Quantity;
partsSheet.Cells[row, 3].Value = item.Description; partsSheet.Cells[row, 3].Value = item.Description;
partsSheet.Cells[row, 4].Value = item.PartNo; partsSheet.Cells[row, 4].Value = item.PartNo;
if (item.Thickness > 0) if (item.Thickness > 0)
partsSheet.Cells[row, 5].Value = item.Thickness; partsSheet.Cells[row, 5].Value = item.Thickness;
partsSheet.Cells[row, 6].Value = item.Material; partsSheet.Cells[row, 6].Value = item.Material;
if (item.KFactor > 0) if (item.KFactor > 0)
partsSheet.Cells[row, 7].Value = item.KFactor; partsSheet.Cells[row, 7].Value = item.KFactor;
} }
workbook.Calculate(); workbook.Calculate();
pkg.Save(); pkg.Save();
} }
} }
private string UserSelectFolder() private string UserSelectFolder()
{ {
@@ -449,7 +448,7 @@ namespace ExportDXF.Forms
return path; return path;
} }
private bool ShouldFlipView(SolidWorks.Interop.sldworks.View view) private bool ShouldFlipView(SolidWorks.Interop.sldworks.View view)
{ {
return viewFlipDecider.ShouldFlip(view); return viewFlipDecider.ShouldFlip(view);
} }
@@ -491,28 +490,28 @@ namespace ExportDXF.Forms
Print("Item numbers are in the " + Helper.GetNumWithSuffix(itemNoColumnIndex + 1) + " column."); Print("Item numbers are in the " + Helper.GetNumWithSuffix(itemNoColumnIndex + 1) + " column.");
} }
var qtyColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_Quantity); var qtyColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_Quantity);
if (qtyColumnIndex == -1) if (qtyColumnIndex == -1)
{ {
Print("Error: Quantity column not found."); Print("Error: Quantity column not found.");
return null; return null;
} }
var descriptionColumnIndex = table.IndexOfColumnTitle("Description"); var descriptionColumnIndex = table.IndexOfColumnTitle("Description");
if (descriptionColumnIndex == -1) if (descriptionColumnIndex == -1)
{ {
Print("Error: Description column not found."); Print("Error: Description column not found.");
return null; return null;
} }
var partNoColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_PartNumber); var partNoColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_PartNumber);
if (partNoColumnIndex == -1) if (partNoColumnIndex == -1)
{ {
Print("Error: Part number column not found."); Print("Error: Part number column not found.");
return null; return null;
} }
var isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly; var isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly;
for (int rowIndex = 0; rowIndex < table.RowCount; rowIndex++) for (int rowIndex = 0; rowIndex < table.RowCount; rowIndex++)
{ {
@@ -535,7 +534,7 @@ namespace ExportDXF.Forms
if (distinctComponents.Count() > 1) if (distinctComponents.Count() > 1)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
//foreach (var comp in distinctComponents) //foreach (var comp in distinctComponents)
//{ //{
@@ -548,12 +547,12 @@ namespace ExportDXF.Forms
} }
else else
{ {
items.Add(new Item items.Add(new Item
{ {
PartNo = table.DisplayedText[rowIndex, partNoColumnIndex], PartNo = table.DisplayedText[rowIndex, partNoColumnIndex],
Quantity = int.Parse(table.DisplayedText[rowIndex, qtyColumnIndex]), Quantity = int.Parse(table.DisplayedText[rowIndex, qtyColumnIndex]),
Description = table.DisplayedText[rowIndex, descriptionColumnIndex], Description = table.DisplayedText[rowIndex, descriptionColumnIndex],
ItemNo = table.DisplayedText[rowIndex, itemNoColumnIndex].PadLeft(2, '0'), ItemNo = table.DisplayedText[rowIndex, itemNoColumnIndex].PadLeft(2, '0'),
Component = distinctComponents.First() Component = distinctComponents.First()
}); });
} }
@@ -579,18 +578,18 @@ namespace ExportDXF.Forms
{ {
var component = group.First(); var component = group.First();
var model = component.GetModelDoc2() as ModelDoc2; var model = component.GetModelDoc2() as ModelDoc2;
if (model == null) if (model == null)
continue; continue;
var name = component.ReferencedConfiguration.ToLower() == "default" ? var name = component.ReferencedConfiguration.ToLower() == "default" ?
component.GetTitle() : component.GetTitle() :
string.Format("{0} [{1}]", component.GetTitle(), component.ReferencedConfiguration); string.Format("{0} [{1}]", component.GetTitle(), component.ReferencedConfiguration);
list.Add(new Item list.Add(new Item
{ {
ItemNo = name, ItemNo = name,
PartNo = name, PartNo = name,
Quantity = group.Count(), Quantity = group.Count(),
Component = component Component = component
@@ -604,7 +603,22 @@ namespace ExportDXF.Forms
{ {
get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); } get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); }
} }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
var model = sldWorks.ActiveDoc as ModelDoc2;
var isDrawing = model is DrawingDoc;
if (!isDrawing)
return;
var drawingInfo = DrawingInfo.Parse(activeDocTitleBox.Text);
if (drawingInfo == null)
return;
prefixTextBox.Text = string.Format("{0} {1} PT", drawingInfo.JobNo, drawingInfo.DrawingNo);
prefixTextBox.SelectionStart = prefixTextBox.Text.Length;
}
}
} }