User selection of viewFlipDecider

This commit is contained in:
aj
2018-06-09 22:37:10 -04:00
parent d2de737ffb
commit 3a313d2409
6 changed files with 355 additions and 256 deletions

View File

@@ -0,0 +1,24 @@
using System.Windows.Forms;
namespace ExportDXF
{
public class AskViewFlipDecider : IViewFlipDecider
{
public string Name => "Ask to flip";
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
{
var bends = ViewHelper.GetBends(view);
if (bends.Count == 0)
return false;
return MessageBox.Show("Flip view?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
}
public override string ToString()
{
return Name;
}
}
}

View File

@@ -88,6 +88,7 @@
<Compile Include="BendOrientation.cs" /> <Compile Include="BendOrientation.cs" />
<Compile Include="Bounds.cs" /> <Compile Include="Bounds.cs" />
<Compile Include="DrawingInfo.cs" /> <Compile Include="DrawingInfo.cs" />
<Compile Include="AskViewFlipDecider.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,96 +28,118 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.activeDocTitleBox = 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.prefixTextBox = 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.label3 = new System.Windows.Forms.Label();
// this.comboBox1 = new System.Windows.Forms.ComboBox();
// textBox1 this.SuspendLayout();
// //
this.activeDocTitleBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) // activeDocTitleBox
//
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.activeDocTitleBox.BackColor = System.Drawing.Color.White; this.activeDocTitleBox.BackColor = System.Drawing.Color.White;
this.activeDocTitleBox.Location = new System.Drawing.Point(130, 13); this.activeDocTitleBox.Location = new System.Drawing.Point(130, 13);
this.activeDocTitleBox.Name = "textBox1"; this.activeDocTitleBox.Name = "activeDocTitleBox";
this.activeDocTitleBox.ReadOnly = true; this.activeDocTitleBox.ReadOnly = true;
this.activeDocTitleBox.Size = new System.Drawing.Size(400, 25); this.activeDocTitleBox.Size = new System.Drawing.Size(424, 25);
this.activeDocTitleBox.TabIndex = 2; this.activeDocTitleBox.TabIndex = 2;
this.activeDocTitleBox.TextChanged += new System.EventHandler(this.textBox1_TextChanged); this.activeDocTitleBox.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
// //
// richTextBox1 // richTextBox1
// //
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.richTextBox1.BackColor = System.Drawing.Color.White; this.richTextBox1.BackColor = System.Drawing.Color.White;
this.richTextBox1.Location = new System.Drawing.Point(12, 76); this.richTextBox1.Location = new System.Drawing.Point(12, 119);
this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true; this.richTextBox1.ReadOnly = true;
this.richTextBox1.Size = new System.Drawing.Size(570, 266); this.richTextBox1.Size = new System.Drawing.Size(594, 260);
this.richTextBox1.TabIndex = 3; this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = ""; this.richTextBox1.Text = "";
// //
// label1 // label1
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 16); this.label1.Location = new System.Drawing.Point(13, 16);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 17); this.label1.Size = new System.Drawing.Size(111, 17);
this.label1.TabIndex = 4; this.label1.TabIndex = 4;
this.label1.Text = "Active Document :"; this.label1.Text = "Active document :";
// //
// label2 // label2
// //
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(23, 47); this.label2.Location = new System.Drawing.Point(23, 47);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(101, 17); this.label2.Size = new System.Drawing.Size(101, 17);
this.label2.TabIndex = 4; this.label2.TabIndex = 4;
this.label2.Text = "Prefix files with :"; this.label2.Text = "Prefix files with :";
// //
// textBox2 // prefixTextBox
// //
this.prefixTextBox.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.prefixTextBox.Location = new System.Drawing.Point(130, 44); this.prefixTextBox.Location = new System.Drawing.Point(130, 44);
this.prefixTextBox.Name = "textBox2"; this.prefixTextBox.Name = "prefixTextBox";
this.prefixTextBox.Size = new System.Drawing.Size(400, 25); this.prefixTextBox.Size = new System.Drawing.Size(424, 25);
this.prefixTextBox.TabIndex = 2; this.prefixTextBox.TabIndex = 2;
// //
// button1 // button1
// //
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Image = global::ExportDXF.Properties.Resources.play; this.button1.Image = global::ExportDXF.Properties.Resources.play;
this.button1.Location = new System.Drawing.Point(536, 13); this.button1.Location = new System.Drawing.Point(560, 13);
this.button1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.button1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.button1.Name = "button1"; this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(46, 56); this.button1.Size = new System.Drawing.Size(46, 56);
this.button1.TabIndex = 0; this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = true; this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click); this.button1.Click += new System.EventHandler(this.button1_Click);
// //
// MainForm // label3
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); this.label3.AutoSize = true;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.label3.Location = new System.Drawing.Point(12, 78);
this.ClientSize = new System.Drawing.Size(594, 354); this.label3.Name = "label3";
this.Controls.Add(this.label2); this.label3.Size = new System.Drawing.Size(112, 17);
this.Controls.Add(this.label1); this.label3.TabIndex = 4;
this.Controls.Add(this.richTextBox1); this.label3.Text = "View flip decider :";
this.Controls.Add(this.prefixTextBox); //
this.Controls.Add(this.activeDocTitleBox); // comboBox1
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.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.comboBox1.FormattingEnabled = true;
this.MaximizeBox = false; this.comboBox1.Location = new System.Drawing.Point(130, 75);
this.Name = "MainForm"; this.comboBox1.Name = "comboBox1";
this.Text = "ExportDXF"; this.comboBox1.Size = new System.Drawing.Size(353, 25);
this.ResumeLayout(false); this.comboBox1.TabIndex = 5;
this.PerformLayout(); //
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(618, 391);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.prefixTextBox);
this.Controls.Add(this.activeDocTitleBox);
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.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.MaximizeBox = false;
this.Name = "MainForm";
this.Text = "ExportDXF";
this.ResumeLayout(false);
this.PerformLayout();
} }
@@ -129,6 +151,8 @@
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 prefixTextBox; private System.Windows.Forms.TextBox prefixTextBox;
} private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox comboBox1;
}
} }

View File

@@ -12,7 +12,7 @@ using System.Windows.Forms;
namespace ExportDXF.Forms namespace ExportDXF.Forms
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
private SldWorks sldWorks; private SldWorks sldWorks;
private BackgroundWorker worker; private BackgroundWorker worker;
@@ -29,7 +29,16 @@ namespace ExportDXF.Forms
worker.DoWork += Worker_DoWork; worker.DoWork += Worker_DoWork;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted; worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
viewFlipDecider = new AskViewFlipDecider(); var type = typeof(IViewFlipDecider);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && p.IsClass)
.ToList();
comboBox1.DataSource = GetItems();
comboBox1.DisplayMember = "Name";
//viewFlipDecider = new AskViewFlipDecider();
} }
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
@@ -52,6 +61,29 @@ namespace ExportDXF.Forms
task.Start(); task.Start();
} }
private List<Item2> GetItems()
{
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => typeof(IViewFlipDecider).IsAssignableFrom(p) && p.IsClass)
.ToList();
var items = new List<Item2>();
foreach (var type in types)
{
var obj = (IViewFlipDecider)Activator.CreateInstance(type);
items.Add(new Item2
{
Name = obj.Name,
ViewFlipDecider = obj
});
}
return items;
}
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
if (worker.IsBusy) if (worker.IsBusy)
@@ -77,12 +109,15 @@ namespace ExportDXF.Forms
Invoke(new MethodInvoker(() => Invoke(new MethodInvoker(() =>
{ {
var item = comboBox1.SelectedItem as Item2;
viewFlipDecider = item.ViewFlipDecider;
button1.Image = Properties.Resources.stop_alt; button1.Image = Properties.Resources.stop_alt;
if (richTextBox1.TextLength != 0) if (richTextBox1.TextLength != 0)
richTextBox1.AppendText("\n\n"); richTextBox1.AppendText("\n\n");
})); }));
var model = sldWorks.ActiveDoc as ModelDoc2; var model = sldWorks.ActiveDoc as ModelDoc2;
Print("Started at " + DateTime.Now.ToShortTimeString()); Print("Started at " + DateTime.Now.ToShortTimeString());
@@ -169,10 +204,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(); activeDocTitleBox.Text = model == null ? "<No Document Open>" : model.GetTitle();
} }
private void DetermineModelTypeAndExportToDXF(ModelDoc2 model) private void DetermineModelTypeAndExportToDXF(ModelDoc2 model)
{ {
if (model is PartDoc) if (model is PartDoc)
{ {
@@ -300,10 +335,10 @@ namespace ExportDXF.Forms
{ {
item.Thickness = sheetMetalData.Thickness.FromSldWorks(); item.Thickness = sheetMetalData.Thickness.FromSldWorks();
item.KFactor = sheetMetalData.KFactor; item.KFactor = sheetMetalData.KFactor;
item.BendRadius = sheetMetalData.BendRadius.FromSldWorks(); item.BendRadius = sheetMetalData.BendRadius.FromSldWorks();
} }
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)
@@ -313,7 +348,7 @@ namespace ExportDXF.Forms
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);
@@ -322,9 +357,9 @@ namespace ExportDXF.Forms
try try
{ {
var drawingInfo = DrawingInfo.Parse(prefix); var drawingInfo = DrawingInfo.Parse(prefix);
var bomName = drawingInfo != null ? string.Format("{0} {1} BOM", drawingInfo.JobNo, drawingInfo.DrawingNo) : "BOM"; var bomName = drawingInfo != null ? string.Format("{0} {1} BOM", drawingInfo.JobNo, drawingInfo.DrawingNo) : "BOM";
var bomFile = Path.Combine(savePath, bomName + ".xlsx"); var bomFile = Path.Combine(savePath, bomName + ".xlsx");
CreateBOMExcelFile(bomFile, items.ToList()); CreateBOMExcelFile(bomFile, items.ToList());
} }
catch (Exception ex) catch (Exception ex)
@@ -363,11 +398,11 @@ namespace ExportDXF.Forms
if (templateDrawing == null) if (templateDrawing == null)
templateDrawing = CreateDrawing(); templateDrawing = CreateDrawing();
var sheet = templateDrawing.IGetCurrentSheet(); var sheet = templateDrawing.IGetCurrentSheet();
var modelName = Path.GetFileNameWithoutExtension(partModel.GetPathName()); var modelName = Path.GetFileNameWithoutExtension(partModel.GetPathName());
sheet.SetName(modelName); sheet.SetName(modelName);
Print(partModel.GetTitle() + " - Creating flat pattern."); Print(partModel.GetTitle() + " - Creating flat pattern.");
SolidWorks.Interop.sldworks.View view; SolidWorks.Interop.sldworks.View view;
view = templateDrawing.CreateFlatPatternViewFromModelView3(partModel.GetPathName(), partConfiguration, 0, 0, 0, false, false); view = templateDrawing.CreateFlatPatternViewFromModelView3(partModel.GetPathName(), partConfiguration, 0, 0, 0, false, false);
@@ -430,11 +465,11 @@ namespace ExportDXF.Forms
if (item.KFactor > 0) if (item.KFactor > 0)
partsSheet.Cells[row, 7].Value = item.KFactor; partsSheet.Cells[row, 7].Value = item.KFactor;
if (item.BendRadius > 0) if (item.BendRadius > 0)
partsSheet.Cells[row, 8].Value = item.BendRadius; partsSheet.Cells[row, 8].Value = item.BendRadius;
} }
partsSheet.Column(1).AutoFit(); partsSheet.Column(1).AutoFit();
workbook.Calculate(); workbook.Calculate();
pkg.Save(); pkg.Save();
@@ -603,29 +638,33 @@ 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) private void textBox1_TextChanged(object sender, EventArgs e)
{ {
var model = sldWorks.ActiveDoc as ModelDoc2; var model = sldWorks.ActiveDoc as ModelDoc2;
var isDrawing = model is DrawingDoc; var isDrawing = model is DrawingDoc;
if (!isDrawing) if (!isDrawing)
return; return;
var drawingInfo = DrawingInfo.Parse(activeDocTitleBox.Text); var drawingInfo = DrawingInfo.Parse(activeDocTitleBox.Text);
if (drawingInfo == null) if (drawingInfo == null)
return; return;
prefixTextBox.Text = string.Format("{0} {1} PT", drawingInfo.JobNo, drawingInfo.DrawingNo); prefixTextBox.Text = string.Format("{0} {1} PT", drawingInfo.JobNo, drawingInfo.DrawingNo);
prefixTextBox.SelectionStart = prefixTextBox.Text.Length; prefixTextBox.SelectionStart = prefixTextBox.Text.Length;
} }
} }
public class AskViewFlipDecider : IViewFlipDecider public class Item2
{ {
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view) public string Name { get; set; }
{ public IViewFlipDecider ViewFlipDecider { get; set; }
return MessageBox.Show("Flip view?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; }
} }
}
namespace Helpers
{
} }

View File

@@ -1,7 +1,9 @@
namespace ExportDXF namespace ExportDXF
{ {
public interface IViewFlipDecider public interface IViewFlipDecider
{ {
bool ShouldFlip(SolidWorks.Interop.sldworks.View view); bool ShouldFlip(SolidWorks.Interop.sldworks.View view);
string Name { get; }
} }
} }

View File

@@ -1,147 +1,156 @@
using SolidWorks.Interop.sldworks; using SolidWorks.Interop.sldworks;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace ExportDXF namespace ExportDXF
{ {
public class ViewFlipDecider : IViewFlipDecider [DisplayName("Automatic")]
public class ViewFlipDecider : IViewFlipDecider
{ {
public string Name => "Automatic";
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view) public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
{ {
var orientation = GetOrientation(view); var orientation = ViewHelper.GetOrientation(view);
var bounds = GetBounds(view); var bounds = ViewHelper.GetBounds(view);
var bends = GetBends(view); var bends = ViewHelper.GetBends(view);
var up = bends.Where(b => b.Direction == BendDirection.Up).ToList(); var up = bends.Where(b => b.Direction == BendDirection.Up).ToList();
var down = bends.Where(b => b.Direction == BendDirection.Down).ToList(); var down = bends.Where(b => b.Direction == BendDirection.Down).ToList();
if (down.Count == 0) if (down.Count == 0)
return false; return false;
if (up.Count == 0) if (up.Count == 0)
return true; return true;
var bend = ClosestToBounds(bounds, bends); var bend = ViewHelper.ClosestToBounds(bounds, bends);
return bend.Direction == BendDirection.Down; return bend.Direction == BendDirection.Down;
} }
private static Bounds GetBounds(SolidWorks.Interop.sldworks.View view)
{
var outline = view.GetOutline() as double[];
var minX = outline[0] / 0.0254; }
var minY = outline[1] / 0.0254;
var maxX = outline[2] / 0.0254;
var maxY = outline[3] / 0.0254;
var width = Math.Abs(minX) + Math.Abs(maxX); internal static class ViewHelper
var height = Math.Abs(minY) + Math.Abs(maxY); {
public static Bounds GetBounds(SolidWorks.Interop.sldworks.View view)
{
var outline = view.GetOutline() as double[];
return new Bounds var minX = outline[0] / 0.0254;
{ var minY = outline[1] / 0.0254;
X = minX, var maxX = outline[2] / 0.0254;
Y = minY, var maxY = outline[3] / 0.0254;
Width = width,
Height = height
};
}
private static Bend ClosestToBounds(Bounds bounds, IList<Bend> bends) var width = Math.Abs(minX) + Math.Abs(maxX);
{ var height = Math.Abs(minY) + Math.Abs(maxY);
var hBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Horizontal).ToList();
var vBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Vertical).ToList();
Bend minVBend = null; return new Bounds
double minVBendDist = double.MaxValue; {
X = minX,
Y = minY,
Width = width,
Height = height
};
}
foreach (var bend in vBends) public static Bend ClosestToBounds(Bounds bounds, IList<Bend> bends)
{ {
double distFromLft = Math.Abs(bend.X - bounds.Left); var hBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Horizontal).ToList();
double distFromRgt = Math.Abs(bounds.Right - bend.X); var vBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Vertical).ToList();
double minDist = Math.Min(distFromLft, distFromRgt); Bend minVBend = null;
double minVBendDist = double.MaxValue;
if (minDist < minVBendDist) foreach (var bend in vBends)
{ {
minVBendDist = minDist; double distFromLft = Math.Abs(bend.X - bounds.Left);
minVBend = bend; double distFromRgt = Math.Abs(bounds.Right - bend.X);
}
}
Bend minHBend = null; double minDist = Math.Min(distFromLft, distFromRgt);
double minHBendDist = double.MaxValue;
foreach (var bend in hBends) if (minDist < minVBendDist)
{ {
double distFromBtm = Math.Abs(bend.Y - bounds.Bottom); minVBendDist = minDist;
double distFromTop = Math.Abs(bounds.Top - bend.Y); minVBend = bend;
}
}
double minDist = Math.Min(distFromBtm, distFromTop); Bend minHBend = null;
double minHBendDist = double.MaxValue;
if (minDist < minHBendDist) foreach (var bend in hBends)
{ {
minHBendDist = minDist; double distFromBtm = Math.Abs(bend.Y - bounds.Bottom);
minHBend = bend; double distFromTop = Math.Abs(bounds.Top - bend.Y);
}
}
return minHBendDist < minVBendDist ? minHBend : minVBend; double minDist = Math.Min(distFromBtm, distFromTop);
}
private static Bend SmallestYCoordinate(IList<Bend> bends) if (minDist < minHBendDist)
{ {
double dist = double.MaxValue; minHBendDist = minDist;
int index = -1; minHBend = bend;
}
}
for (int i = 0; i < bends.Count; i++) return minHBendDist < minVBendDist ? minHBend : minVBend;
{ }
var bend = bends[i];
if (bend.Y < dist) public static Bend SmallestYCoordinate(IList<Bend> bends)
{ {
dist = bend.Y; double dist = double.MaxValue;
index = i; int index = -1;
}
}
return index == -1 ? null : bends[index]; for (int i = 0; i < bends.Count; i++)
} {
var bend = bends[i];
private static Bend SmallestXCoordinate(IList<Bend> bends) if (bend.Y < dist)
{ {
double dist = double.MaxValue; dist = bend.Y;
int index = -1; index = i;
}
}
for (int i = 0; i < bends.Count; i++) return index == -1 ? null : bends[index];
{ }
var bend = bends[i];
if (bend.X < dist) public static Bend SmallestXCoordinate(IList<Bend> bends)
{ {
dist = bend.X; double dist = double.MaxValue;
index = i; int index = -1;
}
}
return index == -1 ? null : bends[index]; for (int i = 0; i < bends.Count; i++)
} {
var bend = bends[i];
private static BendDirection GetBendDirection(Note note) if (bend.X < dist)
{
dist = bend.X;
index = i;
}
}
return index == -1 ? null : bends[index];
}
public static BendDirection GetBendDirection(Note note)
{ {
var txt = note.GetText(); var txt = note.GetText();
return txt.ToUpper().Contains("UP") ? BendDirection.Up : BendDirection.Down; return txt.ToUpper().Contains("UP") ? BendDirection.Up : BendDirection.Down;
} }
private static IEnumerable<Note> GetBendNotes(SolidWorks.Interop.sldworks.View view) public static IEnumerable<Note> GetBendNotes(SolidWorks.Interop.sldworks.View view)
{ {
return (view.GetNotes() as Array)?.Cast<Note>(); return (view.GetNotes() as Array)?.Cast<Note>();
} }
private static Note GetLeftMostNote(SolidWorks.Interop.sldworks.View view) public static Note GetLeftMostNote(SolidWorks.Interop.sldworks.View view)
{ {
var notes = GetBendNotes(view); var notes = GetBendNotes(view);
@@ -163,7 +172,7 @@ namespace ExportDXF
return leftMostNote; return leftMostNote;
} }
private static Note GetBottomMostNote(SolidWorks.Interop.sldworks.View view) public static Note GetBottomMostNote(SolidWorks.Interop.sldworks.View view)
{ {
var notes = GetBendNotes(view); var notes = GetBendNotes(view);
@@ -185,12 +194,12 @@ namespace ExportDXF
return btmMostNote; return btmMostNote;
} }
private static IEnumerable<double> GetBendAngles(SolidWorks.Interop.sldworks.View view) public static IEnumerable<double> GetBendAngles(SolidWorks.Interop.sldworks.View view)
{ {
var angles = new List<double>(); var angles = new List<double>();
var notes = GetBendNotes(view); var notes = GetBendNotes(view);
foreach (var note in notes) foreach (var note in notes)
{ {
var angle = RadiansToDegrees(note.Angle); var angle = RadiansToDegrees(note.Angle);
angles.Add(angle); angles.Add(angle);
@@ -199,49 +208,49 @@ namespace ExportDXF
return angles; return angles;
} }
private static List<Bend> GetBends(SolidWorks.Interop.sldworks.View view) public static List<Bend> GetBends(SolidWorks.Interop.sldworks.View view)
{ {
var bends = new List<Bend>(); var bends = new List<Bend>();
var notes = GetBendNotes(view); var notes = GetBendNotes(view);
const string pattern = @"(?<DIRECTION>(UP|DOWN))\s*(?<ANGLE>(\d+))°"; const string pattern = @"(?<DIRECTION>(UP|DOWN))\s*(?<ANGLE>(\d+))°";
foreach (var note in notes) foreach (var note in notes)
{ {
var pos = note.GetTextPoint2() as double[]; var pos = note.GetTextPoint2() as double[];
var x = pos[0] / 0.0254; var x = pos[0] / 0.0254;
var y = pos[1] / 0.0254; var y = pos[1] / 0.0254;
var text = note.GetText(); var text = note.GetText();
var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase); var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
if (!match.Success) if (!match.Success)
continue; continue;
var angle = double.Parse(match.Groups["ANGLE"].Value); var angle = double.Parse(match.Groups["ANGLE"].Value);
var direection = match.Groups["DIRECTION"].Value; var direection = match.Groups["DIRECTION"].Value;
var bend = new Bend var bend = new Bend
{ {
ParallelBendAngle = RadiansToDegrees(note.Angle), ParallelBendAngle = RadiansToDegrees(note.Angle),
Angle = angle, Angle = angle,
Direction = direection == "UP" ? BendDirection.Up : BendDirection.Down, Direction = direection == "UP" ? BendDirection.Up : BendDirection.Down,
X = x, X = x,
Y = y Y = y
}; };
bends.Add(bend); bends.Add(bend);
} }
return bends; return bends;
} }
private static BendOrientation GetOrientation(SolidWorks.Interop.sldworks.View view) public static BendOrientation GetOrientation(SolidWorks.Interop.sldworks.View view)
{ {
var angles = GetBendAngles(view); var angles = GetBendAngles(view);
var bends = GetBends(view); var bends = GetBends(view);
var vertical = 0; var vertical = 0;
var horizontal = 0; var horizontal = 0;
@@ -268,7 +277,7 @@ namespace ExportDXF
return vertical > horizontal ? BendOrientation.Vertical : BendOrientation.Horizontal; return vertical > horizontal ? BendOrientation.Vertical : BendOrientation.Horizontal;
} }
private static BendOrientation GetAngleOrientation(double angleInDegrees) public static BendOrientation GetAngleOrientation(double angleInDegrees)
{ {
if (angleInDegrees < 10 || angleInDegrees > 350) if (angleInDegrees < 10 || angleInDegrees > 350)
return BendOrientation.Horizontal; return BendOrientation.Horizontal;
@@ -285,7 +294,7 @@ namespace ExportDXF
return BendOrientation.Unknown; return BendOrientation.Unknown;
} }
private static double RadiansToDegrees(double angleInRadians) public static double RadiansToDegrees(double angleInRadians)
{ {
return Math.Round(angleInRadians * 180.0 / Math.PI, 8); return Math.Round(angleInRadians * 180.0 / Math.PI, 8);
} }