Calling Add-ins (VB.NET)
This
sample shows how to implement
IEdmAddIn5::GetAddInInfo and
IEdmAddIn5::OnCmd to create a Visual Basic add-in
that is called when the user clicks a button in a file data card. The add-in
opens a dialog box in which the user browses for the file whose data card is
displayed. The add-in copies the path of the selected file to a text field in the
file's data card.
NOTE:
Because SOLIDWORKS Enterprise PDM cannot force a reload of
add-ins if they are written in .NET, all client machines must be restarted to ensure that the latest version of the add-in is used.
-
Follow
Creating Menu Commands (VB.NET) to create a basic add-in.
-
Register a hook
to notify your add-in when a user clicks a button in a file data card.
Implement IEdmAddIn5::GetAddInInfo as follows:
Public Sub GetAddInInfo(ByRef
poInfo As EdmLib.EdmAddInInfo, ByVal poVault As EdmLib.IEdmVault5,
ByVal poCmdMgr As EdmLib.IEdmCmdMgr5) Implements EdmLib.IEdmAddIn5.GetAddInInfo
'Specify
information to display in the add-in's Properties dialog box
poInfo.mbsAddInName = "My serial number generator"
poInfo.mbsCompany = "The name of my company"
poInfo.mbsDescription = "Implements serial numbers"
poInfo.mlAddInVersion = 1
poInfo.mlRequiredVersionMajor = 5
poInfo.mlRequiredVersionMinor = 2
'Notify the add-in when a
file data card
button is clicked
poCmdMgr.AddHook(EdmCmdType.EdmCmd_CardButton)
End Sub
-
Implement IEdmAddIn5::OnCmd as follows:
Public Sub OnCmd(ByRef poCmd As EdmLib.EdmCmd, ByRef ppoData
As System.Array) Implements EdmLib.IEdmAddIn5.OnCmd
On Error GoTo ErrHand
'Respond only to a specific button command
'The
button command to respond to begins with "MyButton:" and ends with the name
of the
'variable to update in the card
If Left(poCmd.mbsComment, 9) = "MyButton:" Then
'Get the name of the variable to update.
Dim VarName As String
VarName = Right(poCmd.mbsComment, Len(poCmd.mbsComment) - 9)
'Let the user
select the file whose path will be copied to the card variable
Dim vault As EdmVault5
vault = poCmd.mpoVault
Dim PathList As IEdmStrLst5
PathList = vault.BrowseForFile(poCmd.mlParentWnd, EdmBrowseFlag.EdmBws_ForOpen
+ EdmBrowseFlag.EdmBws_PermitVaultFiles,
"", "", "", "", "Select File for
" + VarName)
If Not PathList Is Nothing Then
Dim path As String
path = PathList.GetNext(PathList.GetHeadPosition)
'Store the path in the
card variable
Dim vars As IEdmEnumeratorVariable5
vars = poCmd.mpoExtra
Dim VariantPath As Object
VariantPath = path
vars.SetVar(VarName, "", VariantPath)
End If
End If
Exit Sub
'Handle
errors
ErrHand:
Dim ErrVault As EdmVault5
ErrVault = poCmd.mpoVault
Dim ErrName As String
Dim ErrDesc As String
ErrVault.GetErrorString(Err.Number, ErrName, ErrDesc)
ErrVault.MsgBox(poCmd.mlParentWnd, "Error in my card
button." + vbLf + ErrDesc)
End Sub
The second argument to
OnCmd, ppoData,
is an array of
EdmCmdData structures. There is one element in the array when
it is called from the file data card.
See EdmCmdData for information about the structure.
-
Click Build > Build
Solution to build the add-in.
- Install
the add-in through the SOLIDWORKS Enterprise PDM
Administration tool:
- Open the SOLIDWORKS
Enterprise PDM Administration tool.
- Expand the vault where
you want to install this add-in and log in as Admin.
- Right-click Add-ins and click New
Add-in.
- Browse to
project_path\project_name\project_name\bin\Debug,
click project_name.dll and Interop.EdmLib.dll.
- Click Open.
- Click OK.
- Click OK.
-
Click Cards > File Cards.
-
Double-click Text Card.
-
Add
a button to the card.
-
Click the button.
-
In Caption, type Browse....
-
In
Command
type, select Run Add-in.
-
In Name of add-in, type
MyButton:Title.
-
Save the card and exit the
Card Editor.
-
Open
Windows Explorer on the vault and select a checked-out text
file.
-
Click
Browse
in the file's data card.
-
The
Select File for Title dialog box pops up.
-
Browse to and select the
checked-out text file.
-
Click Open
to copy the path of the selected file to the Title field of the
file's data card.
Remarks
In this example, the value of a variable
is set using
IEdmEnumeratorVariable5::SetVar. You
can also read values using
IEdmEnumeratorVariable5::GetVar.
Using a button handler like this
add-in, you can also:
-
Retrieve
the number of configurations, layouts, or both, in the file by inspecting the
EdmCmdData::mpoExtra
variable, which contains
IEdmStrLst5
of file interfaces.
-
Switch the active configuration.
-
Set focus to a certain
control using the members of
EdmCmdData.
-
Close the card automatically after
the button handler
returns by setting the
EdmCmdData::mlLongData1
variable to one of the
EdmCardFlag constants.