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

@@ -33,7 +33,6 @@
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.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout(); this.SuspendLayout();
// //
@@ -42,10 +41,10 @@
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) this.textBox1.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.textBox1.BackColor = System.Drawing.Color.White;
this.textBox1.Location = new System.Drawing.Point(130, 17); this.textBox1.Location = new System.Drawing.Point(130, 13);
this.textBox1.Name = "textBox1"; this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true; this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(348, 25); this.textBox1.Size = new System.Drawing.Size(400, 25);
this.textBox1.TabIndex = 2; this.textBox1.TabIndex = 2;
// //
// richTextBox1 // richTextBox1
@@ -54,17 +53,17 @@
| 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, 83); this.richTextBox1.Location = new System.Drawing.Point(12, 76);
this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true; this.richTextBox1.ReadOnly = true;
this.richTextBox1.Size = new System.Drawing.Size(570, 259); this.richTextBox1.Size = new System.Drawing.Size(570, 266);
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, 20); this.label1.Location = new System.Drawing.Point(12, 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(112, 17);
this.label1.TabIndex = 4; this.label1.TabIndex = 4;
@@ -73,7 +72,7 @@
// label2 // label2
// //
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(23, 51); 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;
@@ -83,32 +82,19 @@
// //
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) this.textBox2.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, 48); this.textBox2.Location = new System.Drawing.Point(130, 44);
this.textBox2.Name = "textBox2"; this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(348, 25); this.textBox2.Size = new System.Drawing.Size(400, 25);
this.textBox2.TabIndex = 2; 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 // 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(484, 13); this.button1.Location = new System.Drawing.Point(536, 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, 60); 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);
@@ -123,7 +109,6 @@
this.Controls.Add(this.richTextBox1); this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1); this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
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);
@@ -143,7 +128,6 @@
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 textBox2;
private System.Windows.Forms.Button button2;
} }
} }

View File

@@ -7,6 +7,7 @@ using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@@ -319,7 +320,6 @@ namespace ExportDXF.Forms
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);
@@ -660,27 +660,144 @@ namespace ExportDXF.Forms
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view) public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
{ {
var orientation = GetOrientation(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) if (down.Count == 0)
{
case BendOrientation.Horizontal:
note = GetBottomMostNote(view);
break;
case BendOrientation.Vertical:
note = GetLeftMostNote(view);
break;
default:
return false; return false;
if (up.Count == 0)
return true;
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();
if (hBends.Count == vBends.Count)
{
var r1 = bounds.Width / hBends.Count;
var r2 = bounds.Height / vBends.Count;
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;
}
} }
var bendDir = GetBendDirection(note); return down.Count > up.Count;
var shouldFlip = bendDir == BendDirection.Down; }
return shouldFlip; 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) private static BendDirection GetBendDirection(Note note)
@@ -753,10 +870,50 @@ namespace ExportDXF.Forms
return angles; return angles;
} }
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) private static BendOrientation GetOrientation(SolidWorks.Interop.sldworks.View view)
{ {
var angles = GetBendAngles(view); var angles = GetBendAngles(view);
var bends = GetBends(view);
var vertical = 0; var vertical = 0;
var horizontal = 0; var horizontal = 0;
@@ -801,10 +958,22 @@ namespace ExportDXF.Forms
private static double RadiansToDegrees(double angleInRadians) 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 enum BendDirection
{ {
Up, Up,
@@ -817,4 +986,38 @@ namespace ExportDXF.Forms
Horizontal, Horizontal,
Unknown 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.