C# SolidWorks PDM加载项对文件的访问
首先,我不习惯这样编程,但我正在尝试。我很沮丧,因为我无法找到解决问题的方法,我希望有人能给我一个好的资源来使用,因为我认为SolidWorks的帮助和文档在很多方面都是缺乏的。我刚刚开始了解这一点,唯一的问题是如何访问IEdmFile5对象 我试图做的是创建一个插件,它将做一件事,那就是将URL链接作为变量添加到文件中。我当然会添加代码,每当文件被移动或其名称发生更改时,都会更新此URL。现在,我已经创建了一个加载项,它只有一个右键单击菜单,因此我可以一次更改一个或多个文件的URL。(当前需要创建/更新现有文件的URL。) 除了一件事之外,我已经写了所有做这件事的代码。也就是说,将URL保存到变量。菜单命令没有文件的路径,只有ID。让我困惑的是我右键点击了文件。它应该有我需要的关于这个文件的信息,或者一种获取它的方法。也许有,但我就是找不到 这是代码C# SolidWorks PDM加载项对文件的访问,c#,solidworks,solidworksapi,C#,Solidworks,Solidworksapi,首先,我不习惯这样编程,但我正在尝试。我很沮丧,因为我无法找到解决问题的方法,我希望有人能给我一个好的资源来使用,因为我认为SolidWorks的帮助和文档在很多方面都是缺乏的。我刚刚开始了解这一点,唯一的问题是如何访问IEdmFile5对象 我试图做的是创建一个插件,它将做一件事,那就是将URL链接作为变量添加到文件中。我当然会添加代码,每当文件被移动或其名称发生更改时,都会更新此URL。现在,我已经创建了一个加载项,它只有一个右键单击菜单,因此我可以一次更改一个或多个文件的URL。(当前需要
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用EdmLib;
命名空间类文件URL
{
[Guid(“A0A3EA71-06F1-4399-AAEA-256DBDC28652”)]
[ComVisible(true)]
公共类类别1:IEdmAddIn5
{
私有只读字符串vaultName=“kern_vault test”;
私有IEdmVault5 vault=null;
public void GetAddInInfo(参考EdmAddInInfo poInfo、IEDMVault 5 poVault、IEDMCMDMMGR5 POCMDMMGR)
{
尝试
{
//指定要在外接程序的“属性”对话框中显示的信息
poInfo.mbsAddInName=“文件URL生成器加载项”;
poInfo.mbsCompany=“Kern激光系统”;
poInfo.mbsdesdescription=“创建文件的URL。”;
poInfo.mlAddInVersion=1;
//指定SolidWorks PDM Professional所需的最低版本
poInfo.mlRequiredVersionMajor=6;
poInfo.mlRequiredVersionMinor=4;
//注册菜单命令
poCmdMgr.AddCmd(1,“生成文件URL”,(int)edmmenuuflags.EdmMenu_Nothing);
//设置vault对象。
保险库=poVault;
//所有需要的命令挂钩。
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostAdd);
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostMove);
poCmdMgr.AddHook(EdmCmdType.EdmCmd_PostMoveFolder);
}
捕获(System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show(“HRESULT=0x”+ex.ErrorCode.ToString(“X”)+ex.Message);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
void IEdmAddIn5.OnCmd(参考EdmCmd poCmd,参考数组ppoData)
{
尝试
{
//声明变量。
int index=ppoData.GetLowerBound(0);//获取数组的下限。
int last=ppoData.GetUpperBound(0);//获取数组的上限。
EdmCmdData[]tmpArr=(EdmCmdData[])ppoData;//创建临时数组将具有完全访问权限。
//确保我们已登录到保险库。
如果(!vault.IsLoggedIn)
{
//以当前用户身份登录到选定的vault
vault.LoginAuto(vaultName,poCmd.mlParentWnd);
}
//处理命令。
开关(poCmd.meCmdType)
{
案例EdmCmdType.EdmCmd_菜单:
//声明变量。
String message=“是否要更新或创建所选文件的文件URL?(仅适用于.SLDPRT和.SLDASM文件)。”;
String title=“更新/创建文件URL”;
MessageBoxButtons=MessageBoxButtons.YesNo;
对话结果;
如果(poCmd.mlCmdID==1)
{
//显示消息框。
结果=MessageBox.Show(消息、标题、按钮);
//确定对文件所做的操作。
if(result==DialogResult.Yes)
{
ProcessFileList(索引、最后一个、tmpArr);
//完成后显示完成框。
MessageBox.Show(“完成”,标题);
}
}
打破
案例EdmCmdType.EdmCmd\u PostAdd:
打破
案例EdmCmdType.EdmCmd_PostMove:
打破
案例EdmCmdType.EdmCmd_PostMoveFolder:
打破
违约:
打破
}
}
捕获(System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show(“HRESULT=0x”+ex.ErrorCode.ToString(“X”)+ex.Message);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
//
私有void ProcessFileList(int-lowerBound、int-upperBound、edmcmdata[]文件)
{
//声明变量。
IEdmEnumeratorVariable5 linkVariable=默认值(IEdmEnumeratorVariable5);
IEdmFolder5 retFolder=默认值(IEdmFolder5);
IEdm