Evan Riskedahl

Exclude components from BOM

Discussion created by Evan Riskedahl on Oct 7, 2020
Latest reply on Oct 7, 2020 by Deepak Gupta

I'm pretty new to the SW API so my macro is a bit of a mess. 

I'm trying to create a macro that can run on my Assembly and set any component that ends with "-PH" to exclude it from the BOM. 

So far I've managed to create the macro and it runs, however it doesn't always work and I'm not sure why. 


Bellow is the macro so far. 


Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Assembly As ModelDoc2
Dim myAsy As AssemblyDoc
Dim myCmps
Dim i As Integer
Dim myCmp As Component2

' Look through the components of an active assembly
' and determine if the hardware part ending with "-PH"
' needs to be excluded from the BOM
' can be run from main() or Exclude_From_BOM()
' it is preferred to run main()
Sub main()


End Sub

Sub Exclude_From_BOM()

Dim TempString As String
Dim myComponent As String

Set swApp = Application.SldWorks
Set Assembly = swApp.ActiveDoc
Set myAsy = Assembly

Dim value As Integer

myCmps = myAsy.GetComponents(False)

For i = 0 To UBound(myCmps)
'Do some shit
Set myCmp = myCmps(i)
TempString = CStr(myCmp.GetPathName)

'Get the component name
myComponent = Get_Component_Name(CStr(myCmp.GetPathName))

'Now check to see if it is a -PH part and
'exclude it from BOM if it is
If InStr(UCase(myComponent), "-PH") > 1 Then
'Set -PH to exclude from BOM
value = myCmp.SetExcludeFromBOM2(True, 2, 2)
TempString = "Exclude from BOM"
End If
'End doing some shit
Next i

End Sub

'Gets the component name and returns the
'component name as a string
'input the file path of the component and
'return the component's name as a string
Function Get_Component_Name(myStr As String)
'Create a temp array to split up the file path of the component
Dim tempArray() As String

'Component array to split away the file extension
Dim CompArray() As String

'Some Strings (Obsolete)
Dim Component_Name As String
Dim TempString As String

tempArray = Split(myStr, "\")

CompArray() = Split(tempArray(UBound(tempArray)), ".")

Get_Component_Name = CompArray(LBound(CompArray))

End Function