首先我不习惯这种方式编程,但我正在尝试。我很沮丧,因为我找不到我的问题的解决方案,我希望有人能给我一个很好的资源来使用,因为我认为 SolidWorks 的帮助和文档在很多方面都缺乏。我刚刚开始学习这一点,我唯一的问题是访问 IEdmFile5 对象。
我想要做的是创建一个插件,它会做一件事,它是将 URL 链接作为变量添加到文件中。我当然会添加代码,以便在文件移动或名称更改时更新此 URL。目前,我创建了一个只有右键菜单的插件,因此我可以一次更改一个或多个文件的 URL。(当务之急是为现有文件创建/更新 URL。)
除了一件事之外,我已经编写了所有执行此操作的代码。即,将 URL 保存到变量中。菜单命令没有文件路径,只有 ID。令我困惑的是我右键单击该文件。它应该有我需要的关于文件的信息,或者获取它的方法。也许确实如此,但我似乎无法找到它。
这是代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using EdmLib;
namespace ClassFileURL
{
[Guid("A0A3EA71-06F1-4369-AAEA-256DBDC28652")]
[ComVisible(true)]
public class Class1 : IEdmAddIn5
{
private readonly String vaultName = "kern_vault test";
private IEdmVault5 vault = null;
public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
{
try
{
//Specify information to display in the add-in's Properties dialog box
poInfo.mbsAddInName = "File URL Generator Add-in";
poInfo.mbsCompany = "Kern Laser Systems";
poInfo.mbsDescription = "Creates a URL of the file.";
poInfo.mlAddInVersion = 1;
//Specify the minimum required version of SolidWorks PDM Professional
poInfo.mlRequiredVersionMajor = 6;
poInfo.mlRequiredVersionMinor = 4;
// Register a menu command
poCmdMgr.AddCmd(1, "Generate File URL", (int)EdmMenuFlags.EdmMenu_Nothing);
// Set the vault object.
vault = poVault;
// All all the needed command hooks.
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostAdd);
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostMove);
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostMoveFolder);
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void IEdmAddIn5.OnCmd(ref EdmCmd poCmd, ref Array ppoData)
{
try
{
// Declare variables.
int index = ppoData.GetLowerBound(0); // Get the lower bound of the array.
int last = ppoData.GetUpperBound(0); // Get the upper bound of the array.
EdmCmdData[] tmpArr = (EdmCmdData[])ppoData; // Create a temporary array will full access.
// Make sure we are logged into the vault.
if (!vault.IsLoggedIn)
{
//Log into selected vault as the current user
vault.LoginAuto(vaultName, poCmd.mlParentWnd);
}
// Handle the command.
switch (poCmd.meCmdType)
{
case EdmCmdType.EdmCmd_Menu:
// Declare variables.
String message = "Do you want to update or create the file URL for the selected files? (Aplies only to .SLDPRT and .SLDASM files.)";
String title = "Update/Create file URL";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
if (poCmd.mlCmdID == 1)
{
// Show the message box.
result = MessageBox.Show(message, title, buttons);
// Determine what is done to the files.
if (result == DialogResult.Yes)
{
ProcessFileList(index, last, tmpArr);
// Show done box when finished.
MessageBox.Show("Done", title);
}
}
break;
case EdmCmdType.EdmCmd_PostAdd:
break;
case EdmCmdType.EdmCmd_PostMove:
break;
case EdmCmdType.EdmCmd_PostMoveFolder:
break;
default:
break;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//
private void ProcessFileList(int lowerBound, int upperBound, EdmCmdData[] files)
{
// Declare variables.
IEdmEnumeratorVariable5 linkVariable = default(IEdmEnumeratorVariable5);
IEdmFolder5 retFolder = default(IEdmFolder5);
IEdmFile5 aFile = default(IEdmFile5);
String extension1 = ".sldprt";
String extension2 = ".sldasm";
String url = null;
//Append the paths of all files to a message string
while (lowerBound <= upperBound)
{
// Get the data.
int fileID = files[lowerBound].mlObjectID1;
int folderID = files[lowerBound].mlObjectID2;
int parentFolderID = files[lowerBound].mlObjectID3;
String fileName = files[lowerBound].mbsStrData1.ToLower();
// Process only the desired files.
if ((fileName.EndsWith(extension1)) || (fileName.EndsWith(extension2)))
{
// Check the file ID for 0.
if (fileID == 0)
{
url = CreateURL(folderID, fileID);
// work on this. for files in folder
}
// Check the folder ID for 0.
if (folderID == 0)
{
url = CreateURL(parentFolderID, fileID);
}
// Get the file object.
//this is where I get hung up, I have no path to the file.
aFile = vault.GetFileFromPath(**path to file**, out retFolder);
linkVariable = aFile.GetEnumeratorVariable(**path to file **);
// Set the File Link of the selected file.
linkVariable.SetVar("File Link", "", url, true);
linkVariable.Flush();
}
lowerBound++;
}
}
//
private String CreateURL(int folderID, int fileID)
{
// Declare variables.
String action = "open"; // Could be one of these: open, view, explore, get, lock, properties, or history.
int EdmObject_File = 1;
String url = String.Format("conisio://{0}/{1}?projectid={2}&documentid={3}&objecttype={4}", vaultName, action, folderID, fileID, EdmObject_File);
// Show the url.
MessageBox.Show(url, "The file URL");
return url;
}
}
}
格式不正确。这是一个 COM 对象,需要 EDMLib dll 才能编译。任何帮助,将不胜感激。谢谢。