This commit is contained in:
aj
2017-10-16 22:04:48 -04:00
parent 0dbdd4d978
commit 5c77d376a3
10 changed files with 543 additions and 512 deletions

14
ExportDXF/Bend.cs Normal file
View File

@@ -0,0 +1,14 @@
namespace ExportDXF
{
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; }
}
}

View File

@@ -0,0 +1,8 @@
namespace ExportDXF
{
enum BendDirection
{
Up,
Down
}
}

View File

@@ -0,0 +1,9 @@
namespace ExportDXF
{
enum BendOrientation
{
Vertical,
Horizontal,
Unknown
}
}

30
ExportDXF/Bounds.cs Normal file
View File

@@ -0,0 +1,30 @@
namespace ExportDXF
{
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; }
}
}
}

View File

@@ -75,6 +75,14 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bend.cs" />
<Compile Include="BendDirection.cs" />
<Compile Include="BendOrientation.cs" />
<Compile Include="Bounds.cs" />
<Compile Include="Item.cs" />
<Compile Include="IViewFlipDecider.cs" />
<Compile Include="ViewFlipDecider.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Forms\MainForm.cs">
<SubType>Form</SubType>
</Compile>

View File

@@ -6,15 +6,12 @@ using System.ComponentModel;
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;
namespace ExportDXF.Forms
{
public partial class MainForm : Form
public partial class MainForm : Form
{
private SldWorks sldWorks;
private BackgroundWorker worker;
@@ -490,512 +487,4 @@ namespace ExportDXF.Forms
get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); }
}
}
public static class Helper
{
public static Feature GetFeatureByTypeName(this ModelDoc2 model, string featureName)
{
var feature = model.FirstFeature() as Feature;
while (feature != null)
{
if (feature.GetTypeName() == featureName)
return feature;
feature = feature.GetNextFeature() as Feature;
}
return feature;
}
public static List<Feature> GetAllFeaturesByTypeName(this ModelDoc2 model, string featureName)
{
var feature = model.FirstFeature() as Feature;
var list = new List<Feature>();
while (feature != null)
{
if (feature.GetTypeName() == featureName)
list.Add(feature);
feature = feature.GetNextFeature() as Feature;
}
return list;
}
public static bool HasFlatPattern(this ModelDoc2 model)
{
return model.GetBendState() != (int)swSMBendState_e.swSMBendStateNone;
}
public static bool IsSheetMetal(this ModelDoc2 model)
{
if (model is PartDoc == false)
return false;
if (model.HasFlatPattern() == false)
return false;
return true;
}
public static bool IsPart(this ModelDoc2 model)
{
return model is PartDoc;
}
public static bool IsDrawing(this ModelDoc2 model)
{
return model is DrawingDoc;
}
public static bool IsAssembly(this ModelDoc2 model)
{
return model is AssemblyDoc;
}
public static string GetTitle(this Component2 component)
{
var model = component.GetModelDoc2() as ModelDoc2;
return model.GetTitle();
}
public static void AppendText(this RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
public static string ToReadableFormat(this TimeSpan ts)
{
var s = new StringBuilder();
if (ts.TotalHours >= 1)
{
var hrs = ts.Hours + ts.Days * 24.0;
s.Append(string.Format("{0}hrs ", hrs));
s.Append(string.Format("{0}min ", ts.Minutes));
s.Append(string.Format("{0}sec", ts.Seconds));
}
else if (ts.TotalMinutes >= 1)
{
s.Append(string.Format("{0}min ", ts.Minutes));
s.Append(string.Format("{0}sec", ts.Seconds));
}
else
{
s.Append(string.Format("{0} seconds", ts.Seconds));
}
return s.ToString();
}
public static string GetNumWithSuffix(int i)
{
if (i >= 11 && i <= 13)
return i.ToString() + "th";
var j = i % 10;
switch (j)
{
case 1: return i.ToString() + "st";
case 2: return i.ToString() + "nd";
case 3: return i.ToString() + "rd";
default: return i.ToString() + "th";
}
}
public static int IndexOfColumnType(this TableAnnotation table, swTableColumnTypes_e columnType)
{
for (int columnIndex = 0; columnIndex < table.ColumnCount; ++columnIndex)
{
var currentColumnType = (swTableColumnTypes_e)table.GetColumnType(columnIndex);
if (currentColumnType == columnType)
return columnIndex;
}
return -1;
}
public static int IndexOfColumnTitle(this TableAnnotation table, string columnTitle)
{
var lowercaseColumnTitle = columnTitle.ToLower();
for (int columnIndex = 0; columnIndex < table.ColumnCount; ++columnIndex)
{
var currentColumnType = table.GetColumnTitle(columnIndex);
if (currentColumnType.ToLower() == lowercaseColumnTitle)
return columnIndex;
}
return -1;
}
}
public class Item
{
public string Name { get; set; }
public int Quantity { get; set; }
public Component2 Component { get; set; }
}
public interface IViewFlipDecider
{
bool ShouldFlip(SolidWorks.Interop.sldworks.View view);
}
public class ViewFlipDecider : IViewFlipDecider
{
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
{
var orientation = GetOrientation(view);
var bounds = GetBounds(view);
var bends = GetBends(view);
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 (up.Count == 0)
return true;
var bend = ClosestToBounds(bounds, bends);
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);
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 minVBend = null;
double minVBendDist = double.MaxValue;
foreach (var bend in vBends)
{
double distFromLft = Math.Abs(bend.X - bounds.Left);
double distFromRgt = Math.Abs(bounds.Right - bend.X);
double minDist = Math.Min(distFromLft, distFromRgt);
if (minDist < minVBendDist)
{
minVBendDist = minDist;
minVBend = bend;
}
}
Bend minHBend = null;
double minHBendDist = 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)
{
minHBendDist = minDist;
minHBend = bend;
}
}
return minHBendDist < minVBendDist ? minHBend : minVBend;
}
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.X;
index = i;
}
}
return index == -1 ? null : bends[index];
}
private 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)
{
return (view.GetNotes() as Array)?.Cast<Note>();
}
private static Note GetLeftMostNote(SolidWorks.Interop.sldworks.View view)
{
var notes = GetBendNotes(view);
Note leftMostNote = null;
var leftMostValue = double.MaxValue;
foreach (var note in notes)
{
var pt = (note.GetTextPoint() as double[]);
var x = pt[0];
if (x < leftMostValue)
{
leftMostValue = x;
leftMostNote = note;
}
}
return leftMostNote;
}
private static Note GetBottomMostNote(SolidWorks.Interop.sldworks.View view)
{
var notes = GetBendNotes(view);
Note btmMostNote = null;
var btmMostValue = double.MaxValue;
foreach (var note in notes)
{
var pt = (note.GetTextPoint() as double[]);
var y = pt[1];
if (y < btmMostValue)
{
btmMostValue = y;
btmMostNote = note;
}
}
return btmMostNote;
}
private static IEnumerable<double> GetBendAngles(SolidWorks.Interop.sldworks.View view)
{
var angles = new List<double>();
var notes = GetBendNotes(view);
foreach (var note in notes)
{
var angle = RadiansToDegrees(note.Angle);
angles.Add(angle);
}
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)
{
var angles = GetBendAngles(view);
var bends = GetBends(view);
var vertical = 0;
var horizontal = 0;
foreach (var angle in angles)
{
var o = GetAngleOrientation(angle);
switch (o)
{
case BendOrientation.Horizontal:
horizontal++;
break;
case BendOrientation.Vertical:
vertical++;
break;
}
}
if (vertical == 0 && horizontal == 0)
return BendOrientation.Unknown;
return vertical > horizontal ? BendOrientation.Vertical : BendOrientation.Horizontal;
}
private static BendOrientation GetAngleOrientation(double angleInDegrees)
{
if (angleInDegrees < 10 || angleInDegrees > 350)
return BendOrientation.Horizontal;
if (angleInDegrees > 170 && angleInDegrees < 190)
return BendOrientation.Horizontal;
if (angleInDegrees > 80 && angleInDegrees < 100)
return BendOrientation.Vertical;
if (angleInDegrees > 260 && angleInDegrees < 280)
return BendOrientation.Vertical;
return BendOrientation.Unknown;
}
private static double RadiansToDegrees(double angleInRadians)
{
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,
Down
}
enum BendOrientation
{
Vertical,
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; }
}
}
}

160
ExportDXF/Helper.cs Normal file
View File

@@ -0,0 +1,160 @@
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ExportDXF
{
public static class Helper
{
public static Feature GetFeatureByTypeName(this ModelDoc2 model, string featureName)
{
var feature = model.FirstFeature() as Feature;
while (feature != null)
{
if (feature.GetTypeName() == featureName)
return feature;
feature = feature.GetNextFeature() as Feature;
}
return feature;
}
public static List<Feature> GetAllFeaturesByTypeName(this ModelDoc2 model, string featureName)
{
var feature = model.FirstFeature() as Feature;
var list = new List<Feature>();
while (feature != null)
{
if (feature.GetTypeName() == featureName)
list.Add(feature);
feature = feature.GetNextFeature() as Feature;
}
return list;
}
public static bool HasFlatPattern(this ModelDoc2 model)
{
return model.GetBendState() != (int)swSMBendState_e.swSMBendStateNone;
}
public static bool IsSheetMetal(this ModelDoc2 model)
{
if (model is PartDoc == false)
return false;
if (model.HasFlatPattern() == false)
return false;
return true;
}
public static bool IsPart(this ModelDoc2 model)
{
return model is PartDoc;
}
public static bool IsDrawing(this ModelDoc2 model)
{
return model is DrawingDoc;
}
public static bool IsAssembly(this ModelDoc2 model)
{
return model is AssemblyDoc;
}
public static string GetTitle(this Component2 component)
{
var model = component.GetModelDoc2() as ModelDoc2;
return model.GetTitle();
}
public static void AppendText(this RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
public static string ToReadableFormat(this TimeSpan ts)
{
var s = new StringBuilder();
if (ts.TotalHours >= 1)
{
var hrs = ts.Hours + ts.Days * 24.0;
s.Append(string.Format("{0}hrs ", hrs));
s.Append(string.Format("{0}min ", ts.Minutes));
s.Append(string.Format("{0}sec", ts.Seconds));
}
else if (ts.TotalMinutes >= 1)
{
s.Append(string.Format("{0}min ", ts.Minutes));
s.Append(string.Format("{0}sec", ts.Seconds));
}
else
{
s.Append(string.Format("{0} seconds", ts.Seconds));
}
return s.ToString();
}
public static string GetNumWithSuffix(int i)
{
if (i >= 11 && i <= 13)
return i.ToString() + "th";
var j = i % 10;
switch (j)
{
case 1: return i.ToString() + "st";
case 2: return i.ToString() + "nd";
case 3: return i.ToString() + "rd";
default: return i.ToString() + "th";
}
}
public static int IndexOfColumnType(this TableAnnotation table, swTableColumnTypes_e columnType)
{
for (int columnIndex = 0; columnIndex < table.ColumnCount; ++columnIndex)
{
var currentColumnType = (swTableColumnTypes_e)table.GetColumnType(columnIndex);
if (currentColumnType == columnType)
return columnIndex;
}
return -1;
}
public static int IndexOfColumnTitle(this TableAnnotation table, string columnTitle)
{
var lowercaseColumnTitle = columnTitle.ToLower();
for (int columnIndex = 0; columnIndex < table.ColumnCount; ++columnIndex)
{
var currentColumnType = table.GetColumnTitle(columnIndex);
if (currentColumnType.ToLower() == lowercaseColumnTitle)
return columnIndex;
}
return -1;
}
}
}

View File

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

13
ExportDXF/Item.cs Normal file
View File

@@ -0,0 +1,13 @@
using SolidWorks.Interop.sldworks;
namespace ExportDXF
{
public class Item
{
public string Name { get; set; }
public int Quantity { get; set; }
public Component2 Component { get; set; }
}
}

View File

@@ -0,0 +1,293 @@
using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ExportDXF
{
public class ViewFlipDecider : IViewFlipDecider
{
public bool ShouldFlip(SolidWorks.Interop.sldworks.View view)
{
var orientation = GetOrientation(view);
var bounds = GetBounds(view);
var bends = GetBends(view);
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 (up.Count == 0)
return true;
var bend = ClosestToBounds(bounds, bends);
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);
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 minVBend = null;
double minVBendDist = double.MaxValue;
foreach (var bend in vBends)
{
double distFromLft = Math.Abs(bend.X - bounds.Left);
double distFromRgt = Math.Abs(bounds.Right - bend.X);
double minDist = Math.Min(distFromLft, distFromRgt);
if (minDist < minVBendDist)
{
minVBendDist = minDist;
minVBend = bend;
}
}
Bend minHBend = null;
double minHBendDist = 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)
{
minHBendDist = minDist;
minHBend = bend;
}
}
return minHBendDist < minVBendDist ? minHBend : minVBend;
}
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.X;
index = i;
}
}
return index == -1 ? null : bends[index];
}
private 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)
{
return (view.GetNotes() as Array)?.Cast<Note>();
}
private static Note GetLeftMostNote(SolidWorks.Interop.sldworks.View view)
{
var notes = GetBendNotes(view);
Note leftMostNote = null;
var leftMostValue = double.MaxValue;
foreach (var note in notes)
{
var pt = (note.GetTextPoint() as double[]);
var x = pt[0];
if (x < leftMostValue)
{
leftMostValue = x;
leftMostNote = note;
}
}
return leftMostNote;
}
private static Note GetBottomMostNote(SolidWorks.Interop.sldworks.View view)
{
var notes = GetBendNotes(view);
Note btmMostNote = null;
var btmMostValue = double.MaxValue;
foreach (var note in notes)
{
var pt = (note.GetTextPoint() as double[]);
var y = pt[1];
if (y < btmMostValue)
{
btmMostValue = y;
btmMostNote = note;
}
}
return btmMostNote;
}
private static IEnumerable<double> GetBendAngles(SolidWorks.Interop.sldworks.View view)
{
var angles = new List<double>();
var notes = GetBendNotes(view);
foreach (var note in notes)
{
var angle = RadiansToDegrees(note.Angle);
angles.Add(angle);
}
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)
{
var angles = GetBendAngles(view);
var bends = GetBends(view);
var vertical = 0;
var horizontal = 0;
foreach (var angle in angles)
{
var o = GetAngleOrientation(angle);
switch (o)
{
case BendOrientation.Horizontal:
horizontal++;
break;
case BendOrientation.Vertical:
vertical++;
break;
}
}
if (vertical == 0 && horizontal == 0)
return BendOrientation.Unknown;
return vertical > horizontal ? BendOrientation.Vertical : BendOrientation.Horizontal;
}
private static BendOrientation GetAngleOrientation(double angleInDegrees)
{
if (angleInDegrees < 10 || angleInDegrees > 350)
return BendOrientation.Horizontal;
if (angleInDegrees > 170 && angleInDegrees < 190)
return BendOrientation.Horizontal;
if (angleInDegrees > 80 && angleInDegrees < 100)
return BendOrientation.Vertical;
if (angleInDegrees > 260 && angleInDegrees < 280)
return BendOrientation.Vertical;
return BendOrientation.Unknown;
}
private static double RadiansToDegrees(double angleInRadians)
{
return Math.Round(angleInRadians * 180.0 / Math.PI, 8);
}
}
}