一.如何在插件模板里创建UI
在插件模板里有 UI Methods里可以添加命令按钮,修改UI
二. 添加按钮
cmdGroup = iCmdMgr.CreateCommandGroup2(mainCmdGroupID, Title, ToolTip, "", -1, ignorePrevious, cmdGroupErr)
If cmdGroup Is Nothing Or thisAssembly Is Nothing Then
Throw New NullReferenceException()
End If
'用来修改UI图标
cmdGroup.LargeIconList = iBmp.CreateFileFromResourceBitmap("SwVBAddin1.ToolbarLarge.bmp", thisAssembly)
cmdGroup.SmallIconList = iBmp.CreateFileFromResourceBitmap("SwVBAddin1.ToolbarSmall.bmp", thisAssembly)
cmdGroup.LargeMainIcon = iBmp.CreateFileFromResourceBitmap("SwVBAddin1.MainIconLarge.bmp", thisAssembly)
cmdGroup.SmallMainIcon = iBmp.CreateFileFromResourceBitmap("SwVBAddin1.MainIconSmall.bmp", thisAssembly)
Dim menuToolbarOption As Integer = swCommandItemType_e.swMenuItem Or swCommandItemType_e.swToolbarItem
cmdIndex0 = cmdGroup.AddCommandItem2("油箱参数化", -1, "输入参数", "油箱参数化", 0, "YouXiang", "", mainItemID1, menuToolbarOption)
cmdIndex1 = cmdGroup.AddCommandItem2("自动工程图", -1, "生成工程图", "自动工程图", 1, "Drawing_YX", "", mainItemID2, menuToolbarOption)
cmdIndex2 = cmdGroup.AddCommandItem2("链接工程图", -1, "链接已存在的工程图", "工程图", 2, "Drawing_LJ", "", mainItemID2, menuToolbarOption)
cmdIndex3 = cmdGroup.AddCommandItem2("油箱板", -1, "自动属性", "邮箱板参数化", 3, "YouXiangBan", "", mainItemID2, menuToolbarOption)
cmdGroup.HasToolbar = True
cmdGroup.HasMenu = True
cmdGroup.Activate()
在cmdIndex 里可以添加按钮,但要注意要在前面代码定义cmdIndex
Dim cmdIndex0 As Integer, cmdIndex1 As Integer, cmdIndex2 As Integer, cmdIndex3 As Integer
在这里主要用到了AddCommandItem2()
Function AddCommandItem2(Name As String, Position As Integer, HintString As String, ToolTip As String, ImageListIndex As Integer, CallbackFunction As String, EnableMethod As String, UserID As Integer, MenuTBOption As Integer) As Integer
SolidWorks.Interop.sldworks.ICommandGroup 的成员
三.排列按钮
solidworks是先添加UI按钮,再进行图标大小,类型的排列。
solidworks通过添加按钮块,然后在每一个按钮块里定义添加按钮,从而形成按钮。
For Each docType As Integer In docTypes
Dim cmdTab As ICommandTab = iCmdMgr.GetCommandTab(docType, Title)
Dim bResult As Boolean
If Not cmdTab Is Nothing And Not getDataResult Or ignorePrevious Then 'if tab exists, but we have ignored the registry info, re-create the tab. Otherwise the ids won't matchup and the tab will be blank
Dim res As Boolean = iCmdMgr.RemoveCommandTab(cmdTab)
cmdTab = Nothing
End If
If cmdTab Is Nothing Then
cmdTab = iCmdMgr.AddCommandTab(docType, Title)
'添加第一个按钮块
Dim cmdBox As CommandTabBox = cmdTab.AddCommandTabBox
Dim cmdIDs(1) As Integer
Dim TextType(1) As Integer
cmdIDs(0) = cmdGroup.CommandID(cmdIndex0)
TextType(0) = swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow
bResult = cmdBox.AddCommands(cmdIDs, TextType)
'第二个
Dim cmdBox1 As CommandTabBox = cmdTab.AddCommandTabBox()
ReDim cmdIDs(1)
ReDim TextType(1)
cmdIDs(0) = cmdGroup.CommandID(cmdIndex1)
TextType(0) = swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow
bResult = cmdBox1.AddCommands(cmdIDs, TextType)
'第三个
Dim cmdBox2 As CommandTabBox = cmdTab.AddCommandTabBox()
ReDim cmdIDs(1)
ReDim TextType(1)
cmdIDs(0) = cmdGroup.CommandID(cmdIndex2)
TextType(0) = swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow
bResult = cmdBox2.AddCommands(cmdIDs, TextType)
'第四个
Dim cmdBox3 As CommandTabBox = cmdTab.AddCommandTabBox()
ReDim cmdIDs(1)
ReDim TextType(1)
cmdIDs(0) = cmdGroup.CommandID(cmdIndex3)
TextType(0) = swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow
bResult = cmdBox3.AddCommands(cmdIDs, TextType)
'第五个
Dim cmdBox4 As CommandTabBox = cmdTab.AddCommandTabBox()
ReDim cmdIDs(1)
ReDim TextType(1)
cmdIDs(0) = flyGroup.CmdID
TextType(0) = swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow
bResult = cmdBox4.AddCommands(cmdIDs, TextType)
'用来分割按钮,灰色分割线
'cmdTab.AddSeparator(cmdBox3, cmdIDs(0))