跳到主要内容

使用SOLIDWORKS API获取装配件组件的实例ID

该示例使用SOLIDWORKS API从组件的名称中提取组件实例索引。

组件属性对话框中的组件实例ID选项{ width=400 }

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2

Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then

Set swSelMgr = swModel.SelectionManager

Set swComp = swSelMgr.GetSelectedObjectsComponent3(1, -1)

If Not swComp Is Nothing Then

Dim instId As Integer
Dim compName As String
compName = swComp.Name2
instId = CInt(Right(compName, Len(compName) - InStrRev(compName, "-")))

MsgBox "所选组件的实例ID为 " & instId

Else

MsgBox "请选择组件"

End If

Else

MsgBox "请打开装配"

End If

End Sub