Replace Component (VB.NET)
This example shows how to replace a component with a different component.
'---------------------------------------------------------------------------
' Preconditions: Specified file exists.
'
' Postconditions: The first instance of the shaft
washer component is
' replaced
with a lock washer.
'
' NOTE: The
replacement does not fit properly with the model.
'--------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Imports SwDocumentMgr
Partial Class SolidWorksMacro
Public
Sub main()
'
Opening the file in SolidWorks Document Manager API, not SolidWorks.
'
This code is packaged in a SolidWorks macro for your convenience.
Dim
swCf As SwDMClassFactory
swCf
= New SwDMClassFactory
Dim
swDocMgr As SwDocumentMgr.SwDMApplication
swDocMgr
= swCf.GetApplication("your_license_key")
Dim
swDoc12 As SwDMDocument12
Dim
res As SwDocumentMgr.SwDmDocumentOpenError
Dim
dt As SwDmDocumentType
dt
= SwDmDocumentType.swDmDocumentAssembly
Dim
filename As String
Debug.Print("Opening
the assembly...")
filename
= "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\98food
processor.sldasm"
swDoc12
= swDocMgr.GetDocument(filename,
dt, False, res)
If
swDoc12 Is Nothing Or (res <> SwDmDocumentOpenError.swDmDocumentOpenErrorNone)
Then
Debug.Print("Error
opening file....")
Exit
Sub
End
If
Debug.Print("Getting
the active configuration...")
Dim
activeConfig As SwDMConfiguration8
Dim
configMgr As SwDocumentMgr.SwDMConfigurationMgr
configMgr
= swDoc12.ConfigurationManager
configMgr.GetConfigurationNames()
activeConfig
= configMgr.GetConfigurationByName(configMgr.GetActiveConfigurationName)
If
activeConfig Is Nothing Then
Debug.Print("Error
getting the active configuration...")
Exit
Sub
End
If
Debug.Print("Getting
the components of the active configuration...")
Dim
vComponents As Object
vComponents
= activeConfig.GetComponents
Dim
swDmComponent As SwDMComponent6
Dim
i As Integer
For
i = 0 To UBound(vComponents)
swDmComponent
= vComponents(i)
'
Replace first instance of shaft washer component with lock washer
If
swDmComponent.Name = "shaft
washer" Then
Debug.Print("Replacing
shaft washer with lock washer... " & swDmComponent.Replace("C:\Program
Files\SolidWorks Corp\SolidWorks\samples\tutorial\smartcomponents\lockwasher.sldprt",
"Default", False))
Exit
For
End
If
Next
swDoc12.Save()
swDoc12.CloseDoc()
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class