Improved should flip decision.

This commit is contained in:
aj
2017-10-16 20:37:46 -04:00
parent 29677eb705
commit def7c380ee
7 changed files with 309 additions and 122 deletions

View File

@@ -28,110 +28,95 @@
/// </summary>
private void InitializeComponent()
{
this.textBox1 = 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.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
this.textBox1 = 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.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.BackColor = System.Drawing.Color.White;
this.textBox1.Location = new System.Drawing.Point(130, 17);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(348, 25);
this.textBox1.TabIndex = 2;
//
// richTextBox1
//
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
this.textBox1.BackColor = System.Drawing.Color.White;
this.textBox1.Location = new System.Drawing.Point(130, 13);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(400, 25);
this.textBox1.TabIndex = 2;
//
// 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, 83);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true;
this.richTextBox1.Size = new System.Drawing.Size(570, 259);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 20);
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, 51);
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.textBox2.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, 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.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.Location = new System.Drawing.Point(130, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(348, 25);
this.textBox2.TabIndex = 2;
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Image = global::ExportDXF.Properties.Resources.edit_alt;
this.button2.Location = new System.Drawing.Point(536, 13);
this.button2.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(46, 60);
this.button2.TabIndex = 0;
this.button2.UseVisualStyleBackColor = true;
this.button2.Visible = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// 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(484, 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, 60);
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.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
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.textBox2.Location = new System.Drawing.Point(130, 44);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(400, 25);
this.textBox2.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.textBox2);
this.Controls.Add(this.textBox1);
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();
}
@@ -143,7 +128,6 @@
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -318,7 +319,6 @@ namespace ExportDXF.Forms
var sheet = templateDrawing.IGetCurrentSheet();
var modelName = Path.GetFileNameWithoutExtension(partModel.GetPathName());
sheet.SetName(modelName);
Print(partModel.GetTitle() + " - Creating flat pattern.");
SolidWorks.Interop.sldworks.View view;
@@ -660,30 +660,147 @@ namespace ExportDXF.Forms
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
{
var orientation = GetOrientation(view);
var bounds = GetBounds(view);
var bends = GetBends(view);
Note note;
var up = bends.Where(b => b.Direction == BendDirection.Up).ToList();
var down = bends.Where(b => b.Direction == BendDirection.Down).ToList();
switch (orientation)
{
case BendOrientation.Horizontal:
note = GetBottomMostNote(view);
break;
if (down.Count == 0)
return false;
case BendOrientation.Vertical:
note = GetLeftMostNote(view);
break;
if (up.Count == 0)
return true;
default:
return false;
}
if (down.Count == up.Count)
{
var hBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Horizontal).ToList();
var vBends = bends.Where(b => GetAngleOrientation(b.ParallelBendAngle) == BendOrientation.Vertical).ToList();
var bendDir = GetBendDirection(note);
var shouldFlip = bendDir == BendDirection.Down;
if (hBends.Count == vBends.Count)
{
var r1 = bounds.Width / hBends.Count;
var r2 = bounds.Height / vBends.Count;
return shouldFlip;
return r2 > r1;
}
else if (hBends.Count > vBends.Count)
{
var x = SmallestYCoordinate(hBends);
return x.Direction == BendDirection.Down;
}
else
{
var x = SmallestXCoordinate(vBends);
return x.Direction == BendDirection.Down;
}
}
return down.Count > up.Count;
}
private static BendDirection GetBendDirection(Note note)
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);
return new Bounds
{
X = minX,
Y = minY,
Width = width,
Height = height
};
}
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();
Bend minHBend = null;
double minHBendDist = double.MaxValue;
foreach (var bend in hBends)
{
double distFromLft = Math.Abs(bend.X - bounds.Left);
double distFromRgt = Math.Abs(bounds.Right - bend.X);
double minDist = Math.Min(distFromLft, distFromRgt);
if (minDist < minHBendDist)
{
minHBendDist = minDist;
minHBend = bend;
}
}
Bend minVBend = null;
double minVBendDist = double.MaxValue;
foreach (var bend in hBends)
{
double distFromBtm = Math.Abs(bend.Y - bounds.Bottom);
double distFromTop = Math.Abs(bounds.Top - bend.Y);
double minDist = Math.Min(distFromBtm, distFromTop);
if (minDist < minHBendDist)
{
minVBendDist = minDist;
minVBend = bend;
}
}
return minVBendDist < minHBendDist ? minVBend : minHBend;
}
private static Bend SmallestYCoordinate(IList<Bend> bends)
{
double dist = double.MaxValue;
int index = -1;
for (int i = 0; i < bends.Count; i++)
{
var bend = bends[i];
if (bend.Y < dist)
{
dist = bend.Y;
index = i;
}
}
return index == -1 ? null : bends[index];
}
private static Bend SmallestXCoordinate(IList<Bend> bends)
{
double dist = double.MaxValue;
int index = -1;
for (int i = 0; i < bends.Count; i++)
{
var bend = bends[i];
if (bend.X < dist)
{
dist = bend.Y;
index = i;
}
}
return index == -1 ? null : bends[index];
}
private static BendDirection GetBendDirection(Note note)
{
var txt = note.GetText();
@@ -744,7 +861,7 @@ namespace ExportDXF.Forms
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);
@@ -753,10 +870,50 @@ namespace ExportDXF.Forms
return angles;
}
private static BendOrientation GetOrientation(SolidWorks.Interop.sldworks.View view)
private 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+))°";
foreach (var note in notes)
{
var pos = note.GetTextPoint2() as double[];
var x = pos[0] / 0.0254;
var y = pos[1] / 0.0254;
var text = note.GetText();
var match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
if (!match.Success)
continue;
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
};
bends.Add(bend);
}
return bends;
}
private static BendOrientation GetOrientation(SolidWorks.Interop.sldworks.View view)
{
var angles = GetBendAngles(view);
var bends = GetBends(view);
var vertical = 0;
var horizontal = 0;
@@ -801,10 +958,22 @@ namespace ExportDXF.Forms
private static double RadiansToDegrees(double angleInRadians)
{
return angleInRadians * 180.0 / Math.PI;
return Math.Round(angleInRadians * 180.0 / Math.PI, 8);
}
}
class Bend
{
public BendDirection Direction { get; set; }
public double ParallelBendAngle { get; set; }
public double Angle { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
enum BendDirection
{
Up,
@@ -817,4 +986,38 @@ namespace ExportDXF.Forms
Horizontal,
Unknown
}
class Size
{
public double Width { get; set; }
public double Height { get; set; }
}
class Bounds
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public double Left
{
get { return X; }
}
public double Right
{
get { return X + Width; }
}
public double Bottom
{
get { return Y; }
}
public double Top
{
get { return Y + Height; }
}
}
}

Binary file not shown.

BIN
TestDocs/Assem1.SLDASM Normal file

Binary file not shown.

BIN
TestDocs/Part1.SLDPRT Normal file

Binary file not shown.

BIN
TestDocs/Part2.SLDPRT Normal file

Binary file not shown.