Hello, everyone.
Now I'd like to create small SolidWorks Add-in with C#.
So I did it but it does not work.
Belows are my code.
[Guid("BB75C0FD-3720-45E5-8B80-2ACB34900F7E")]
[ComVisible(true)]
public class MyAddin:ISwAddin
{
SldWorks swApp = null;
public bool ConnectToSW(object ThisSW, int Cookie)
{
MessageBox.Show("My C# Add-In Loaded!", "My C# Add-In", MessageBoxButtons.OK, MessageBoxIcon.Information);
try
{
//try to connect to an existing open instance of SolidWorks:
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
}
catch (Exception)
{
//try to open a new instance:
try
{
swApp = new SldWorks();
}
catch (Exception ex)
{
MessageBox.Show("Can't connect to SolidWorks and can't open a new SolidWorks instance.\\n" + ex.Message, "Error connecting to SolidWorks", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
}
swApp.Visible = true;
//now also try to make sure PDM add-in is enabled:
try
{
string appPath = swApp.GetExecutablePath();
appPath = appPath.Substring(0, appPath.LastIndexOf('\\\\')) + "\\\\SOLIDWORKS PDM"; //e.g. C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS -> C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS PDM
string pdmAddinPath1 = \\\$"{appPath}\\\\PDMSW.dll";
string pdmAddinPath2 = \\\$"{appPath}\\\\epdmlib.dll";
//try the first addin dll
if (!File.Exists(pdmAddinPath1))
{
DialogResult dlgRes = MessageBox.Show(\\\$"Can't load the PDM add-in:\\n\\n{pdmAddinPath1}\\n\\nPlease manually turn on the PDM Add-in in SolidWorks and click OK to continue", "Enable PDM Manually", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dlgRes == DialogResult.Cancel) throw new Exception();
}
try
{
swApp.LoadAddIn(pdmAddinPath1);
}
catch (Exception)
{
throw;
}
// try the second addin dll
if (!File.Exists(pdmAddinPath2))
{
DialogResult dlgRes = MessageBox.Show(\\\$"Can't load the PDM add-in:\\n\\n{pdmAddinPath2}\\n\\nPlease manually turn on the PDM Add-in in SolidWorks and click OK to continue", "Enable PDM Manually", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dlgRes == DialogResult.Cancel) throw new Exception();
}
Cursor.Current = Cursors.WaitCursor;
try
{
swApp.LoadAddIn(pdmAddinPath2);
}
catch (Exception)
{
throw;
}
}
catch (Exception ex)
{
MessageBox.Show(\\\$"Couldn't load the PDM addin. Nothing about this is going to work right.\\n\\n{ex.Message}", "PDM Add-in not found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//return null;
}
return false;
}
public bool DisconnectFromSW()
{
swApp.SendMsgToUser("My C# Add-In Unloaded!");
Marshal.ReleaseComObject(swApp);
swApp = null;
return true;
}
}
Hope someone help me. Thank you in advance.
