index
此宏使用SOLIDWORKS API计算装配中所选组件的数量。组件可以在特征管理树或图形区域中选择。
如果仅选择组件的实体(例如面或边),宏也会计算组件的数量,使用ISelectionMgr SOLIDWORKS API接口。
{ width=320 }
Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Sub main()
Set swApp = Application.SldWorks
Set swAssy = swApp.ActiveDoc
If Not swAssy Is Nothing Then
Dim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swAssy.SelectionManager
Dim swCompsColl As Collection
Set swCompsColl = New Collection
Dim i As Integer
For i = 0 To swSelMgr.GetSelectedObjectCount2(-1)
Dim swComp As SldWorks.Component2
Set swComp = swSelMgr.GetSelectedObjectsComponent2(i)
If Not swComp Is Nothing Then
If Not Contains(swCompsColl, swComp) Then '仅获取唯一的组件
swCompsColl.Add swComp
End If
End If
Next
Dim swFrame As SldWorks.Frame
Set swFrame = swApp.Frame
swFrame.SetStatusBarText "所选组件数量为 " & swCompsColl.Count() & " 个"
Else
MsgBox "请打开装配"
End If
End Sub
Function Contains(coll As Collection, item As Object) As Boolean
Dim i As Integer
For i = 1 To coll.Count
If coll.item(i) Is item Then
Contains = True
Exit Function
End If
Next
Contains = False
End Function