我正在用 C# 构建一个使用 SolidWorks API 的程序。
当我使用以下命令创建 SolidWorks 的新实例时:
sldWrks = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));
应用程序已加载,但加载项未加载。
具体来说,我对该程序感兴趣的插件是 FeatureWorks。
如何让 SolidWorks 在插件处于活动状态时启动?
我是否需要在 SolidWorks 安装文件夹中的某个文本文件中设置选项或者修改注册表项?
你必须通过调用来加载它:
swApp.LoadAddIn(FileName);
以下是我打开 PDM 插件的方法。我确信它会类似于FeatureWorks。这里使用的是 C# Windows 窗体应用程序(因此 MessageBox 调用)。希望这足以让您弄清楚其余的事情。
private SldWorks.SldWorks ConnectToSW()
{
SldWorks.SldWorks swApp = null;
try
{
//try to connect to an existing open instance of SolidWorks:
swApp = (SldWorks.SldWorks)Marshal.GetActiveObject("SldWorks.Application");
}
catch (Exception)
{
//try to open a new instance:
try
{
swApp = new SldWorks.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 null;
}
}
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 swApp;
}