I need to have a value of total of different parts and sub-assemblies on an assembly.
I know the value is easy to find from the Assembly Expert, but I haven't found anywhere how to extract the value.
Anyone knows how to extract that from the Assembly Expert and use it into a macro?
Thanks
SolidworksApi macros
Check if this post helps:
Thanks Deepak!
I wanted to use a method of going throught all parts at first, and found this thread
Where you said it was easier to use the Assembly Expert! So I thought it might be easy just to extract the existing value.
Anyway, I was able to modify a macro and came up with this:
----
----
----
Sub TotalQty()
Dim swModel As ModelDoc2
Dim swAssy As AssemblyDoc
Dim vComponents As Variant
Dim i As Integer
Dim swComponent As Component2
Dim swAssembly As AssemblyDoc
Dim TotalQty As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
TotalQty = 0
vComponents = swAssy.GetComponents(True)
For i = 0 To UBound(vComponents)
Set swComponent = vComponents(i)
Set swModel = swComponent.GetModelDoc2
Debug.Print swComponent.Name2
swComponent.Select4 False, Nothing, False
TotalQty = TotalQty + 1
If swModel.GetType = swDocASSEMBLY Then
Set swAssembly = swModel
TransverseComponents swAssembly
End If
Next i
MsgBox (TotalQty)
End Sub
----
----
----
The only probleme I get is that it can count the same component twice (or as many time it is used)
I mentioned that outside of API.
I've read a macro here on this forum which should be able to solve the issue. Let me find that.
Thanks a lot!
My only idea as solutions was to build a matrix and check if the part was already counted in, but I don't know of to do that yet!
I have this:
---
---
---
Sub TotalQty()
Dim swModel As ModelDoc2
Dim swAssy As AssemblyDoc
Dim vComponents As Variant
Dim i As Integer
Dim swComponent As Component2
Dim swAssembly As AssemblyDoc
Dim TotalQty As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
TotalQty = 0
vComponents = swAssy.GetComponents(True)
For i = 0 To UBound(vComponents)
Set swComponent = vComponents(i)
Set swModel = swComponent.GetModelDoc2
Debug.Print swComponent.Name2
swComponent.Select4 False, Nothing, False
'
If Right(swComponent.Name2, 2) = "-1" Then 'Check instance of component
TotalQty = TotalQty + 1 'Count in
End If
If swModel.GetType = swDocASSEMBLY Then
Set swAssembly = swModel
TransverseComponents swAssembly
End If
Next i
TotalQty = TotalQty + 1 'Add 1 to count in the main assembly
MsgBox (TotalQty)
End Sub
---
---
---
It kinda works, but the main problem is that the way I count in or not it based on the instance name. Only part ending with "-1" will be count in. But I delete the first instance, and keep only 2 and higher, it will never be count in...
Still not able to find the macro, searching for it.