User selection of viewFlipDecider
This commit is contained in:
24
ExportDXF/AskViewFlipDecider.cs
Normal file
24
ExportDXF/AskViewFlipDecider.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,7 @@
|
||||
<Compile Include="BendOrientation.cs" />
|
||||
<Compile Include="Bounds.cs" />
|
||||
<Compile Include="DrawingInfo.cs" />
|
||||
<Compile Include="AskViewFlipDecider.cs" />
|
||||
<Compile Include="Item.cs" />
|
||||
<Compile Include="IViewFlipDecider.cs" />
|
||||
<Compile Include="ViewFlipDecider.cs" />
|
||||
|
||||
198
ExportDXF/Forms/MainForm.Designer.cs
generated
198
ExportDXF/Forms/MainForm.Designer.cs
generated
@@ -28,96 +28,118 @@
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.activeDocTitleBox = new System.Windows.Forms.TextBox();
|
||||
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.label2 = new System.Windows.Forms.Label();
|
||||
this.prefixTextBox = new System.Windows.Forms.TextBox();
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// textBox1
|
||||
//
|
||||
this.activeDocTitleBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
this.activeDocTitleBox = new System.Windows.Forms.TextBox();
|
||||
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.label2 = new System.Windows.Forms.Label();
|
||||
this.prefixTextBox = new System.Windows.Forms.TextBox();
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.label3 = new System.Windows.Forms.Label();
|
||||
this.comboBox1 = new System.Windows.Forms.ComboBox();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// activeDocTitleBox
|
||||
//
|
||||
this.activeDocTitleBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.activeDocTitleBox.BackColor = System.Drawing.Color.White;
|
||||
this.activeDocTitleBox.Location = new System.Drawing.Point(130, 13);
|
||||
this.activeDocTitleBox.Name = "textBox1";
|
||||
this.activeDocTitleBox.ReadOnly = true;
|
||||
this.activeDocTitleBox.Size = new System.Drawing.Size(400, 25);
|
||||
this.activeDocTitleBox.TabIndex = 2;
|
||||
this.activeDocTitleBox.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
|
||||
//
|
||||
// richTextBox1
|
||||
//
|
||||
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
this.activeDocTitleBox.BackColor = System.Drawing.Color.White;
|
||||
this.activeDocTitleBox.Location = new System.Drawing.Point(130, 13);
|
||||
this.activeDocTitleBox.Name = "activeDocTitleBox";
|
||||
this.activeDocTitleBox.ReadOnly = true;
|
||||
this.activeDocTitleBox.Size = new System.Drawing.Size(424, 25);
|
||||
this.activeDocTitleBox.TabIndex = 2;
|
||||
this.activeDocTitleBox.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
|
||||
//
|
||||
// richTextBox1
|
||||
//
|
||||
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.Right)));
|
||||
this.richTextBox1.BackColor = System.Drawing.Color.White;
|
||||
this.richTextBox1.Location = new System.Drawing.Point(12, 76);
|
||||
this.richTextBox1.Name = "richTextBox1";
|
||||
this.richTextBox1.ReadOnly = true;
|
||||
this.richTextBox1.Size = new System.Drawing.Size(570, 266);
|
||||
this.richTextBox1.TabIndex = 3;
|
||||
this.richTextBox1.Text = "";
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(12, 16);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(112, 17);
|
||||
this.label1.TabIndex = 4;
|
||||
this.label1.Text = "Active Document :";
|
||||
//
|
||||
// label2
|
||||
//
|
||||
this.label2.AutoSize = true;
|
||||
this.label2.Location = new System.Drawing.Point(23, 47);
|
||||
this.label2.Name = "label2";
|
||||
this.label2.Size = new System.Drawing.Size(101, 17);
|
||||
this.label2.TabIndex = 4;
|
||||
this.label2.Text = "Prefix files with :";
|
||||
//
|
||||
// textBox2
|
||||
//
|
||||
this.prefixTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
this.richTextBox1.BackColor = System.Drawing.Color.White;
|
||||
this.richTextBox1.Location = new System.Drawing.Point(12, 119);
|
||||
this.richTextBox1.Name = "richTextBox1";
|
||||
this.richTextBox1.ReadOnly = true;
|
||||
this.richTextBox1.Size = new System.Drawing.Size(594, 260);
|
||||
this.richTextBox1.TabIndex = 3;
|
||||
this.richTextBox1.Text = "";
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(13, 16);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(111, 17);
|
||||
this.label1.TabIndex = 4;
|
||||
this.label1.Text = "Active document :";
|
||||
//
|
||||
// label2
|
||||
//
|
||||
this.label2.AutoSize = true;
|
||||
this.label2.Location = new System.Drawing.Point(23, 47);
|
||||
this.label2.Name = "label2";
|
||||
this.label2.Size = new System.Drawing.Size(101, 17);
|
||||
this.label2.TabIndex = 4;
|
||||
this.label2.Text = "Prefix files with :";
|
||||
//
|
||||
// prefixTextBox
|
||||
//
|
||||
this.prefixTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.prefixTextBox.Location = new System.Drawing.Point(130, 44);
|
||||
this.prefixTextBox.Name = "textBox2";
|
||||
this.prefixTextBox.Size = new System.Drawing.Size(400, 25);
|
||||
this.prefixTextBox.TabIndex = 2;
|
||||
//
|
||||
// button1
|
||||
//
|
||||
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.Location = new System.Drawing.Point(536, 13);
|
||||
this.button1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
|
||||
this.button1.Name = "button1";
|
||||
this.button1.Size = new System.Drawing.Size(46, 56);
|
||||
this.button1.TabIndex = 0;
|
||||
this.button1.UseVisualStyleBackColor = true;
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(594, 354);
|
||||
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();
|
||||
this.prefixTextBox.Location = new System.Drawing.Point(130, 44);
|
||||
this.prefixTextBox.Name = "prefixTextBox";
|
||||
this.prefixTextBox.Size = new System.Drawing.Size(424, 25);
|
||||
this.prefixTextBox.TabIndex = 2;
|
||||
//
|
||||
// button1
|
||||
//
|
||||
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.Location = new System.Drawing.Point(560, 13);
|
||||
this.button1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
|
||||
this.button1.Name = "button1";
|
||||
this.button1.Size = new System.Drawing.Size(46, 56);
|
||||
this.button1.TabIndex = 0;
|
||||
this.button1.UseVisualStyleBackColor = true;
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
//
|
||||
// label3
|
||||
//
|
||||
this.label3.AutoSize = true;
|
||||
this.label3.Location = new System.Drawing.Point(12, 78);
|
||||
this.label3.Name = "label3";
|
||||
this.label3.Size = new System.Drawing.Size(112, 17);
|
||||
this.label3.TabIndex = 4;
|
||||
this.label3.Text = "View flip decider :";
|
||||
//
|
||||
// comboBox1
|
||||
//
|
||||
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
this.comboBox1.FormattingEnabled = true;
|
||||
this.comboBox1.Location = new System.Drawing.Point(130, 75);
|
||||
this.comboBox1.Name = "comboBox1";
|
||||
this.comboBox1.Size = new System.Drawing.Size(353, 25);
|
||||
this.comboBox1.TabIndex = 5;
|
||||
//
|
||||
// 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 label2;
|
||||
private System.Windows.Forms.TextBox prefixTextBox;
|
||||
}
|
||||
private System.Windows.Forms.Label label3;
|
||||
private System.Windows.Forms.ComboBox comboBox1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ using System.Windows.Forms;
|
||||
|
||||
namespace ExportDXF.Forms
|
||||
{
|
||||
public partial class MainForm : Form
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
private SldWorks sldWorks;
|
||||
private BackgroundWorker worker;
|
||||
@@ -29,7 +29,16 @@ namespace ExportDXF.Forms
|
||||
worker.DoWork += Worker_DoWork;
|
||||
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)
|
||||
@@ -52,6 +61,29 @@ namespace ExportDXF.Forms
|
||||
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)
|
||||
{
|
||||
if (worker.IsBusy)
|
||||
@@ -77,12 +109,15 @@ namespace ExportDXF.Forms
|
||||
|
||||
Invoke(new MethodInvoker(() =>
|
||||
{
|
||||
var item = comboBox1.SelectedItem as Item2;
|
||||
viewFlipDecider = item.ViewFlipDecider;
|
||||
|
||||
button1.Image = Properties.Resources.stop_alt;
|
||||
|
||||
if (richTextBox1.TextLength != 0)
|
||||
richTextBox1.AppendText("\n\n");
|
||||
}));
|
||||
|
||||
|
||||
var model = sldWorks.ActiveDoc as ModelDoc2;
|
||||
|
||||
Print("Started at " + DateTime.Now.ToShortTimeString());
|
||||
@@ -169,10 +204,10 @@ namespace ExportDXF.Forms
|
||||
private void SetActiveDocName()
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -300,10 +335,10 @@ namespace ExportDXF.Forms
|
||||
{
|
||||
item.Thickness = sheetMetalData.Thickness.FromSldWorks();
|
||||
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");
|
||||
|
||||
if (item.Description == null)
|
||||
@@ -313,7 +348,7 @@ namespace ExportDXF.Forms
|
||||
|
||||
item.Material = part.GetMaterialPropertyName2(config, out db);
|
||||
|
||||
if (part == null)
|
||||
if (part == null)
|
||||
continue;
|
||||
|
||||
SavePartToDXF(part, config, savepath);
|
||||
@@ -322,9 +357,9 @@ namespace ExportDXF.Forms
|
||||
|
||||
try
|
||||
{
|
||||
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");
|
||||
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());
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -363,11 +398,11 @@ namespace ExportDXF.Forms
|
||||
|
||||
if (templateDrawing == null)
|
||||
templateDrawing = CreateDrawing();
|
||||
|
||||
|
||||
var sheet = templateDrawing.IGetCurrentSheet();
|
||||
var modelName = Path.GetFileNameWithoutExtension(partModel.GetPathName());
|
||||
sheet.SetName(modelName);
|
||||
|
||||
|
||||
Print(partModel.GetTitle() + " - Creating flat pattern.");
|
||||
SolidWorks.Interop.sldworks.View view;
|
||||
view = templateDrawing.CreateFlatPatternViewFromModelView3(partModel.GetPathName(), partConfiguration, 0, 0, 0, false, false);
|
||||
@@ -430,11 +465,11 @@ namespace ExportDXF.Forms
|
||||
if (item.KFactor > 0)
|
||||
partsSheet.Cells[row, 7].Value = item.KFactor;
|
||||
|
||||
if (item.BendRadius > 0)
|
||||
partsSheet.Cells[row, 8].Value = item.BendRadius;
|
||||
if (item.BendRadius > 0)
|
||||
partsSheet.Cells[row, 8].Value = item.BendRadius;
|
||||
}
|
||||
|
||||
partsSheet.Column(1).AutoFit();
|
||||
partsSheet.Column(1).AutoFit();
|
||||
|
||||
workbook.Calculate();
|
||||
pkg.Save();
|
||||
@@ -603,29 +638,33 @@ namespace ExportDXF.Forms
|
||||
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;
|
||||
private void textBox1_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
var model = sldWorks.ActiveDoc as ModelDoc2;
|
||||
var isDrawing = model is DrawingDoc;
|
||||
|
||||
if (!isDrawing)
|
||||
return;
|
||||
if (!isDrawing)
|
||||
return;
|
||||
|
||||
var drawingInfo = DrawingInfo.Parse(activeDocTitleBox.Text);
|
||||
var drawingInfo = DrawingInfo.Parse(activeDocTitleBox.Text);
|
||||
|
||||
if (drawingInfo == null)
|
||||
return;
|
||||
if (drawingInfo == null)
|
||||
return;
|
||||
|
||||
prefixTextBox.Text = string.Format("{0} {1} PT", drawingInfo.JobNo, drawingInfo.DrawingNo);
|
||||
prefixTextBox.SelectionStart = prefixTextBox.Text.Length;
|
||||
}
|
||||
}
|
||||
prefixTextBox.Text = string.Format("{0} {1} PT", drawingInfo.JobNo, drawingInfo.DrawingNo);
|
||||
prefixTextBox.SelectionStart = prefixTextBox.Text.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public class AskViewFlipDecider : IViewFlipDecider
|
||||
{
|
||||
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
return MessageBox.Show("Flip view?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
|
||||
}
|
||||
}
|
||||
public class Item2
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public IViewFlipDecider ViewFlipDecider { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
namespace Helpers
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
namespace ExportDXF
|
||||
{
|
||||
public interface IViewFlipDecider
|
||||
public interface IViewFlipDecider
|
||||
{
|
||||
bool ShouldFlip(SolidWorks.Interop.sldworks.View view);
|
||||
|
||||
string Name { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,147 +1,156 @@
|
||||
using SolidWorks.Interop.sldworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ExportDXF
|
||||
{
|
||||
public class ViewFlipDecider : IViewFlipDecider
|
||||
[DisplayName("Automatic")]
|
||||
public class ViewFlipDecider : IViewFlipDecider
|
||||
{
|
||||
public string Name => "Automatic";
|
||||
|
||||
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
var orientation = GetOrientation(view);
|
||||
var bounds = GetBounds(view);
|
||||
var bends = GetBends(view);
|
||||
var orientation = ViewHelper.GetOrientation(view);
|
||||
var bounds = ViewHelper.GetBounds(view);
|
||||
var bends = ViewHelper.GetBends(view);
|
||||
|
||||
var up = bends.Where(b => b.Direction == BendDirection.Up).ToList();
|
||||
var down = bends.Where(b => b.Direction == BendDirection.Down).ToList();
|
||||
var up = bends.Where(b => b.Direction == BendDirection.Up).ToList();
|
||||
var down = bends.Where(b => b.Direction == BendDirection.Down).ToList();
|
||||
|
||||
if (down.Count == 0)
|
||||
return false;
|
||||
if (down.Count == 0)
|
||||
return false;
|
||||
|
||||
if (up.Count == 0)
|
||||
return true;
|
||||
if (up.Count == 0)
|
||||
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);
|
||||
var height = Math.Abs(minY) + Math.Abs(maxY);
|
||||
internal static class ViewHelper
|
||||
{
|
||||
public static Bounds GetBounds(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
var outline = view.GetOutline() as double[];
|
||||
|
||||
return new Bounds
|
||||
{
|
||||
X = minX,
|
||||
Y = minY,
|
||||
Width = width,
|
||||
Height = height
|
||||
};
|
||||
}
|
||||
var minX = outline[0] / 0.0254;
|
||||
var minY = outline[1] / 0.0254;
|
||||
var maxX = outline[2] / 0.0254;
|
||||
var maxY = outline[3] / 0.0254;
|
||||
|
||||
private static Bend ClosestToBounds(Bounds bounds, IList<Bend> bends)
|
||||
{
|
||||
var hBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Horizontal).ToList();
|
||||
var vBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Vertical).ToList();
|
||||
var width = Math.Abs(minX) + Math.Abs(maxX);
|
||||
var height = Math.Abs(minY) + Math.Abs(maxY);
|
||||
|
||||
Bend minVBend = null;
|
||||
double minVBendDist = double.MaxValue;
|
||||
return new Bounds
|
||||
{
|
||||
X = minX,
|
||||
Y = minY,
|
||||
Width = width,
|
||||
Height = height
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var bend in vBends)
|
||||
{
|
||||
double distFromLft = Math.Abs(bend.X - bounds.Left);
|
||||
double distFromRgt = Math.Abs(bounds.Right - bend.X);
|
||||
public static Bend ClosestToBounds(Bounds bounds, IList<Bend> bends)
|
||||
{
|
||||
var hBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Horizontal).ToList();
|
||||
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)
|
||||
{
|
||||
minVBendDist = minDist;
|
||||
minVBend = bend;
|
||||
}
|
||||
}
|
||||
foreach (var bend in vBends)
|
||||
{
|
||||
double distFromLft = Math.Abs(bend.X - bounds.Left);
|
||||
double distFromRgt = Math.Abs(bounds.Right - bend.X);
|
||||
|
||||
Bend minHBend = null;
|
||||
double minHBendDist = double.MaxValue;
|
||||
double minDist = Math.Min(distFromLft, distFromRgt);
|
||||
|
||||
foreach (var bend in hBends)
|
||||
{
|
||||
double distFromBtm = Math.Abs(bend.Y - bounds.Bottom);
|
||||
double distFromTop = Math.Abs(bounds.Top - bend.Y);
|
||||
if (minDist < minVBendDist)
|
||||
{
|
||||
minVBendDist = minDist;
|
||||
minVBend = bend;
|
||||
}
|
||||
}
|
||||
|
||||
double minDist = Math.Min(distFromBtm, distFromTop);
|
||||
Bend minHBend = null;
|
||||
double minHBendDist = double.MaxValue;
|
||||
|
||||
if (minDist < minHBendDist)
|
||||
{
|
||||
minHBendDist = minDist;
|
||||
minHBend = bend;
|
||||
}
|
||||
}
|
||||
foreach (var bend in hBends)
|
||||
{
|
||||
double distFromBtm = Math.Abs(bend.Y - bounds.Bottom);
|
||||
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)
|
||||
{
|
||||
double dist = double.MaxValue;
|
||||
int index = -1;
|
||||
if (minDist < minHBendDist)
|
||||
{
|
||||
minHBendDist = minDist;
|
||||
minHBend = bend;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < bends.Count; i++)
|
||||
{
|
||||
var bend = bends[i];
|
||||
return minHBendDist < minVBendDist ? minHBend : minVBend;
|
||||
}
|
||||
|
||||
if (bend.Y < dist)
|
||||
{
|
||||
dist = bend.Y;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
public static Bend SmallestYCoordinate(IList<Bend> bends)
|
||||
{
|
||||
double dist = double.MaxValue;
|
||||
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)
|
||||
{
|
||||
double dist = double.MaxValue;
|
||||
int index = -1;
|
||||
if (bend.Y < dist)
|
||||
{
|
||||
dist = bend.Y;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < bends.Count; i++)
|
||||
{
|
||||
var bend = bends[i];
|
||||
return index == -1 ? null : bends[index];
|
||||
}
|
||||
|
||||
if (bend.X < dist)
|
||||
{
|
||||
dist = bend.X;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
public static Bend SmallestXCoordinate(IList<Bend> bends)
|
||||
{
|
||||
double dist = double.MaxValue;
|
||||
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();
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
private static Note GetLeftMostNote(SolidWorks.Interop.sldworks.View view)
|
||||
public static Note GetLeftMostNote(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
var notes = GetBendNotes(view);
|
||||
|
||||
@@ -163,7 +172,7 @@ namespace ExportDXF
|
||||
return leftMostNote;
|
||||
}
|
||||
|
||||
private static Note GetBottomMostNote(SolidWorks.Interop.sldworks.View view)
|
||||
public static Note GetBottomMostNote(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
var notes = GetBendNotes(view);
|
||||
|
||||
@@ -185,12 +194,12 @@ namespace ExportDXF
|
||||
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 notes = GetBendNotes(view);
|
||||
|
||||
foreach (var note in notes)
|
||||
foreach (var note in notes)
|
||||
{
|
||||
var angle = RadiansToDegrees(note.Angle);
|
||||
angles.Add(angle);
|
||||
@@ -199,49 +208,49 @@ namespace ExportDXF
|
||||
return angles;
|
||||
}
|
||||
|
||||
private static List<Bend> GetBends(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
var bends = new List<Bend>();
|
||||
var notes = GetBendNotes(view);
|
||||
public static List<Bend> GetBends(SolidWorks.Interop.sldworks.View view)
|
||||
{
|
||||
var bends = new List<Bend>();
|
||||
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)
|
||||
{
|
||||
var pos = note.GetTextPoint2() as double[];
|
||||
foreach (var note in notes)
|
||||
{
|
||||
var pos = note.GetTextPoint2() as double[];
|
||||
|
||||
var x = pos[0] / 0.0254;
|
||||
var y = pos[1] / 0.0254;
|
||||
var x = pos[0] / 0.0254;
|
||||
var y = pos[1] / 0.0254;
|
||||
|
||||
var text = note.GetText();
|
||||
var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
|
||||
var text = note.GetText();
|
||||
var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
|
||||
|
||||
if (!match.Success)
|
||||
continue;
|
||||
if (!match.Success)
|
||||
continue;
|
||||
|
||||
var angle = double.Parse(match.Groups["ANGLE"].Value);
|
||||
var direection = match.Groups["DIRECTION"].Value;
|
||||
var angle = double.Parse(match.Groups["ANGLE"].Value);
|
||||
var direection = match.Groups["DIRECTION"].Value;
|
||||
|
||||
var bend = new Bend
|
||||
{
|
||||
ParallelBendAngle = RadiansToDegrees(note.Angle),
|
||||
Angle = angle,
|
||||
Direction = direection == "UP" ? BendDirection.Up : BendDirection.Down,
|
||||
X = x,
|
||||
Y = y
|
||||
};
|
||||
var bend = new Bend
|
||||
{
|
||||
ParallelBendAngle = RadiansToDegrees(note.Angle),
|
||||
Angle = angle,
|
||||
Direction = direection == "UP" ? BendDirection.Up : BendDirection.Down,
|
||||
X = x,
|
||||
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 bends = GetBends(view);
|
||||
var bends = GetBends(view);
|
||||
|
||||
var vertical = 0;
|
||||
var horizontal = 0;
|
||||
@@ -268,7 +277,7 @@ namespace ExportDXF
|
||||
return vertical > horizontal ? BendOrientation.Vertical : BendOrientation.Horizontal;
|
||||
}
|
||||
|
||||
private static BendOrientation GetAngleOrientation(double angleInDegrees)
|
||||
public static BendOrientation GetAngleOrientation(double angleInDegrees)
|
||||
{
|
||||
if (angleInDegrees < 10 || angleInDegrees > 350)
|
||||
return BendOrientation.Horizontal;
|
||||
@@ -285,7 +294,7 @@ namespace ExportDXF
|
||||
return BendOrientation.Unknown;
|
||||
}
|
||||
|
||||
private static double RadiansToDegrees(double angleInRadians)
|
||||
public static double RadiansToDegrees(double angleInRadians)
|
||||
{
|
||||
return Math.Round(angleInRadians * 180.0 / Math.PI, 8);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user