Refactor methods that handle connecting to solidworks.
This commit is contained in:
@@ -8,6 +8,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace ExportDXF.Forms
|
namespace ExportDXF.Forms
|
||||||
@@ -29,14 +30,50 @@ namespace ExportDXF.Forms
|
|||||||
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
|
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnLoad(EventArgs e)
|
||||||
|
{
|
||||||
|
base.OnLoad(e);
|
||||||
|
|
||||||
|
button1.Enabled = false;
|
||||||
|
|
||||||
|
var task = new Task(ConnectToSolidWorks);
|
||||||
|
|
||||||
|
task.ContinueWith((t) =>
|
||||||
|
{
|
||||||
|
Invoke(new MethodInvoker(() =>
|
||||||
|
{
|
||||||
|
SetActiveDocName();
|
||||||
|
button1.Enabled = true;
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
task.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void button1_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (worker.IsBusy)
|
||||||
|
{
|
||||||
|
worker.CancelAsync();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
worker.RunWorkerAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void button2_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void Worker_DoWork(object sender, DoWorkEventArgs e)
|
private void Worker_DoWork(object sender, DoWorkEventArgs e)
|
||||||
{
|
{
|
||||||
timeStarted = DateTime.Now;
|
timeStarted = DateTime.Now;
|
||||||
|
|
||||||
|
sldWorks.ActiveModelDocChangeNotify -= SldWorks_ActiveModelDocChangeNotify;
|
||||||
|
|
||||||
Invoke(new MethodInvoker(() =>
|
Invoke(new MethodInvoker(() =>
|
||||||
{
|
{
|
||||||
sldWorks.ActiveModelDocChangeNotify -= SldWorks_ActiveModelDocChangeNotify;
|
|
||||||
|
|
||||||
button1.Image = Properties.Resources.stop_alt;
|
button1.Image = Properties.Resources.stop_alt;
|
||||||
|
|
||||||
if (richTextBox1.TextLength != 0)
|
if (richTextBox1.TextLength != 0)
|
||||||
@@ -47,7 +84,8 @@ namespace ExportDXF.Forms
|
|||||||
|
|
||||||
Print("Started at " + DateTime.Now.ToShortTimeString());
|
Print("Started at " + DateTime.Now.ToShortTimeString());
|
||||||
|
|
||||||
CreateDXFTemplates(model);
|
DetermineModelTypeAndExportToDXF(model);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
||||||
@@ -77,44 +115,17 @@ namespace ExportDXF.Forms
|
|||||||
{
|
{
|
||||||
Invoke(new MethodInvoker(() =>
|
Invoke(new MethodInvoker(() =>
|
||||||
{
|
{
|
||||||
SetCurrentDocName();
|
SetActiveDocName();
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SetCurrentDocName();
|
SetActiveDocName();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnLoad(EventArgs e)
|
|
||||||
{
|
|
||||||
base.OnLoad(e);
|
|
||||||
|
|
||||||
new Thread(new ThreadStart(() =>
|
|
||||||
{
|
|
||||||
Invoke(new MethodInvoker(() =>
|
|
||||||
{
|
|
||||||
Enabled = false;
|
|
||||||
sldWorks = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as SldWorks;
|
|
||||||
|
|
||||||
if (sldWorks == null)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Failed to connect to SolidWorks.");
|
|
||||||
Application.Exit();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sldWorks.Visible = true;
|
|
||||||
sldWorks.ActiveModelDocChangeNotify += SldWorks_ActiveModelDocChangeNotify;
|
|
||||||
SetCurrentDocName();
|
|
||||||
Enabled = true;
|
|
||||||
}));
|
|
||||||
}))
|
|
||||||
.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Print(string s)
|
private void Print(string s)
|
||||||
{
|
{
|
||||||
Invoke(new MethodInvoker(() =>
|
Invoke(new MethodInvoker(() =>
|
||||||
@@ -133,25 +144,32 @@ namespace ExportDXF.Forms
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetCurrentDocName()
|
private void ConnectToSolidWorks()
|
||||||
|
{
|
||||||
|
Print("Connecting to SolidWorks, this may take a minute...");
|
||||||
|
sldWorks = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as SldWorks;
|
||||||
|
|
||||||
|
if (sldWorks == null)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Failed to connect to SolidWorks.");
|
||||||
|
Application.Exit();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sldWorks.Visible = true;
|
||||||
|
sldWorks.ActiveModelDocChangeNotify += SldWorks_ActiveModelDocChangeNotify;
|
||||||
|
|
||||||
|
Print("Ready", Color.Green);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetActiveDocName()
|
||||||
{
|
{
|
||||||
var model = sldWorks.ActiveDoc as ModelDoc2;
|
var model = sldWorks.ActiveDoc as ModelDoc2;
|
||||||
|
|
||||||
textBox1.Text = model != null ? model.GetTitle() : "<No Document Open>";
|
textBox1.Text = model != null ? model.GetTitle() : "<No Document Open>";
|
||||||
}
|
}
|
||||||
|
|
||||||
private void button1_Click(object sender, EventArgs e)
|
private void DetermineModelTypeAndExportToDXF(ModelDoc2 model)
|
||||||
{
|
|
||||||
if (worker.IsBusy)
|
|
||||||
{
|
|
||||||
worker.CancelAsync();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
worker.RunWorkerAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateDXFTemplates(ModelDoc2 model)
|
|
||||||
{
|
{
|
||||||
if (model is PartDoc)
|
if (model is PartDoc)
|
||||||
{
|
{
|
||||||
@@ -206,7 +224,7 @@ namespace ExportDXF.Forms
|
|||||||
{
|
{
|
||||||
var prefix = textBox2.Text;
|
var prefix = textBox2.Text;
|
||||||
var model = part as ModelDoc2;
|
var model = part as ModelDoc2;
|
||||||
var dir = GetSaveDir();
|
var dir = UserSelectFolder();
|
||||||
|
|
||||||
if (dir == null)
|
if (dir == null)
|
||||||
{
|
{
|
||||||
@@ -239,7 +257,7 @@ namespace ExportDXF.Forms
|
|||||||
|
|
||||||
private void ExportToDXF(IEnumerable<Item> items)
|
private void ExportToDXF(IEnumerable<Item> items)
|
||||||
{
|
{
|
||||||
var savePath = GetSaveDir();
|
var savePath = UserSelectFolder();
|
||||||
var prefix = textBox2.Text;
|
var prefix = textBox2.Text;
|
||||||
|
|
||||||
if (savePath == null)
|
if (savePath == null)
|
||||||
@@ -328,7 +346,7 @@ namespace ExportDXF.Forms
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetSaveDir()
|
private string UserSelectFolder()
|
||||||
{
|
{
|
||||||
string path = null;
|
string path = null;
|
||||||
|
|
||||||
@@ -379,11 +397,6 @@ namespace ExportDXF.Forms
|
|||||||
return dnCount > upCount;
|
return dnCount > upCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string DrawingTemplatePath
|
|
||||||
{
|
|
||||||
get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); }
|
|
||||||
}
|
|
||||||
|
|
||||||
private DrawingDoc CreateDrawing()
|
private DrawingDoc CreateDrawing()
|
||||||
{
|
{
|
||||||
return sldWorks.NewDocument(
|
return sldWorks.NewDocument(
|
||||||
@@ -498,9 +511,9 @@ namespace ExportDXF.Forms
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void button2_Click(object sender, EventArgs e)
|
private static string DrawingTemplatePath
|
||||||
{
|
{
|
||||||
|
get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
@@ -17,5 +18,7 @@ namespace ExportDXF
|
|||||||
Application.SetCompatibleTextRenderingDefault(false);
|
Application.SetCompatibleTextRenderingDefault(false);
|
||||||
Application.Run(new Forms.MainForm());
|
Application.Run(new Forms.MainForm());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user