using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using SolidWorksTools;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Timers;
using System.Threading;
using System.IO;
using System.Reflection;
namespace Callback_Test
{
//Declare a new delegate specific to updating the text of a label
delegate void UpdateLabels(string text);
[ComVisible(true)]
[ProgId(SWTASKPANE_PROGID)]
public partial class KoppelingTaskpane : UserControl
{
System.Threading.Thread thread;
delegate void TextBoxDelegate(string message);
public SldWorks mSWApplication;
public int mCookie;
public CommandManager mCommandManager;
public ModelDoc2 swModel;
public ModelDoc2 modDoc;
public ModelDoc2 swModel2;
SolidWorks.Interop.sldworks.View swView = default(SolidWorks.Interop.sldworks.View);
public SketchManager swSketchMgr;
public Sketch swSketch;
public SldWorks swApp;
bool pathName;
string val = "";
string valout = "";
CustomPropertyManager swCustProp;
public AssemblyDoc swAssembly;
public BackgroundWorker backgroundWorker1;
private TaskpaneView mTaskpaneView;
public KoppelingTaskpane mTaskpaneHost;
public int stop = 0;
public SwAddin addin;
PartDoc doc;
public const string SWTASKPANE_PROGID = "Robin.KoppelingTaskpane2_AddInRobin";
public KoppelingTaskpane()
{
InitializeComponent();
}
public void Init(SldWorks app)
{
mSWApplication = app;
}
string propName;
string Configuration;
string oldValue;
string NewValue;
int valueType;
public void button1_Click(object sender, EventArgs e)
{
PropertyChange(propName, Configuration, oldValue, NewValue, valueType);
}
public int myValue;
public void CreatingNewThread()
{
int myValue = 40; //some value to pass as parameter to the other Thread
Test other = new Test(this); //passing param to constructor
thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(other.SomethingToDo));
thread.Start(myValue);
}
public void UpdatingTextBox(string msg)
{
if (this.textBox1.InvokeRequired)
this.textBox1.Invoke(new TextBoxDelegate(UpdatingTextBox), new object[] { msg });
else
this.textBox1.Text = msg;
}
public void button2_Click(object sender, EventArgs e)
{
try
{
doc = (PartDoc)modDoc;
doc.ChangeCustomPropertyNotify += new DPartDocEvents_ChangeCustomPropertyNotifyEventHandler(PropertyChange);
}
catch
{
MessageBox.Show("jshdfjh\n" + e);
}
}
public void SelectAllinDocument(ModelDoc2 swModel3, ModelDocExtension swModelDocExt, SelectionMgr swSelMgr2)
{
int selCount;
// Select all edges in a part, all components in an assembly,
// or all entities in a drawing
swModelDocExt.SelectAll();
// Get and print the number of selections
selCount = 0;
selCount = swSelMgr2.GetSelectedObjectCount2(-1);
switch (swModel3.GetType())
{
case (int)swDocumentTypes_e.swDocPART:
Debug.Print("Number of edges selected in part = " + selCount);
break;
case (int)swDocumentTypes_e.swDocASSEMBLY:
Debug.Print("Number of components selected in assembly = " + selCount);
break;
case (int)swDocumentTypes_e.swDocDRAWING:
Debug.Print("Number of entities selected in drawing = " + selCount);
break;
default:
Debug.Print("Unknown type of document.");
break;
}
}
public void label1_Click(object sender, EventArgs e)
{
}
public void label2_Click(object sender, EventArgs e)
{
}
private void KoppelingTaskpane_Load(object sender, EventArgs e)
{
}
public void help2()
{
this.backgroundWorker1.RunWorkerAsync();
InitializeComponent();
}
public void help()
{
MessageBox.Show("Property gewijzigd!");
Thread.Sleep(5000);
//Stop de backgroundworker
this.backgroundWorker1.CancelAsync();
}
public void UpdateLabel(String str)
{
string Testtest;
Testtest = label1.ToString();
MessageBox.Show("str = " + Testtest);
label1.Text = str;
MessageBox.Show("str = " + str);
}
void UpdateLabelText(Label lbl, string text)
{
}
//This method is invoked by our delegate and actually updates the label //text
void UpdateLabelTextByDelegate(Label lbl, string text)
{
lbl.Text = text;
}
public int PropertyChange(string propName, string Configuration, string oldValue, string NewValue, int valueType)
{
MessageBox.Show("", "!?");
//MessageBox.Show("Button 2 geklikt. Properties moet nu worden geupdate", "Fout");
try
{
// Code die een exceptie kan opleveren
MessageBox.Show("" + swApp, "0");
swApp = (SldWorks)Activator.CreateInstance(
Type.GetTypeFromProgID("SldWorks.Application"));
MessageBox.Show("" + swApp, "1");
swModel2 = swApp.IActiveDoc2;
MessageBox.Show("" + swModel2, "2");
swCustProp = swModel2.Extension.get_CustomPropertyManager("");
MessageBox.Show("" + swCustProp, "3");
pathName = swCustProp.Get4("PartNo", false, out val, out valout);
MessageBox.Show("" + pathName, "4");
//PropID = swCustProp.Set("PartNo", "Test");
//if (val != "" & valout != "")
//{
MessageBox.Show("" + val, "5");
label1.Text = val;
MessageBox.Show("" + valout, "6");
label2.Text = valout;
//MessageBox.Show(
// "Value: " + val +
// "\nEvaluated value: " + valout +
// "\nUp-to-date data: " + pathName);
//}
//else
//{
// if (Melding == 0)
// {
// MessageBox.Show("Property 'PartNo' bestaat niet");
// Melding++;
// }
//}
MessageBox.Show("" + textBox1.ToString(), "7");
textBox1.Text = "het lukt tot hier";
MessageBox.Show("" + textBox1.ToString(), "8");
return 1;
}
catch (System.Exception e)
{
//Verwerk een systeemexceptie
//label1.BeginInvoke(delegate { label1.Text = "No Document"; });
MessageBox.Show("Er is geen document geopend: \n" + e);
return 0;
}
}
private void backgroundWorker1_RunWorkerCompleted(
object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Cancelled) MessageBox.Show("Cancelled!");
else if (e.Error != null) MessageBox.Show("Error!");
else MessageBox.Show("Het resultaat is: " + e.Result);
this.textBox1.Text =
"This text was set safely by BackgroundWorker.";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 2000; i++)
{
if (stop != 0) backgroundWorker1.CancelAsync();
else if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
backgroundWorker1.ReportProgress(0, i);
Thread.Sleep(50); //We vertragen het werk om een mooier resultaat te kunnen tonen
}
e.Result = 12345; //Enkel ter illustratie
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.label1.Text = ("" + backgroundWorker1.CancellationPending);
progressBar1.Value = ((int)e.UserState) % 100;
this.textBox1.Text = ("" + e.UserState);
}
private void button3_Click(object sender, EventArgs e)
{
swApp = (SldWorks)Activator.CreateInstance(
Type.GetTypeFromProgID("SldWorks.Application"));
swModel2 = swApp.IActiveDoc2;
swCustProp = swModel2.Extension.get_CustomPropertyManager("");
//pathName = swCustProp.Get4("PartNo", false, out val, out valout);
int henk = swCustProp.Set("PartNo", "Test");
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}