Private Declare PtrSafe Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Boolean
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As LongPtr
hInstance As LongPtr
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As LongPtr
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
try_:
On Error GoTo catch_
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
If Not swModel Is Nothing Then
If swModel.GetType() = swDocumentTypes_e.swDocASSEMBLY Then
Dim swAssy As SldWorks.AssemblyDoc
Set swAssy = swModel
Dim vComps As Variant
vComps = GetSelectedComponents(swModel.SelectionManager)
If Not IsEmpty(vComps) Then
Dim i As Integer
Dim path As String
path = vComps(0).GetPathName()
For i = 1 To UBound(vComps)
If LCase(vComps(i).GetPathName()) <> LCase(path) Then
Err.Raise vbError, "", "Only identical components are supported"
End If
Next
Dim ext As String
ext = Right(path, Len(path) - InStrRev(path, ".") + 1)
Dim filter As String
Dim fileType As String
If LCase(ext) = ".sldprt" Then
fileType = "SOLIDWORKS Parts"
ElseIf LCase(ext) = ".sldasm" Then
fileType = "SOLIDWORKS Assemblies"
Else
Err.Raise vbError, "", "Unknown error"
End If
Dim replaceFilePath As String
replaceFilePath = BrowseForFileSave("Select replacement file path", filter, path)
If replaceFilePath <> "" Then
If False = swAssy.MakeIndependent(replaceFilePath) Then
Err.Raise vbError, "", "Failed to make components independent"
End If
MakeDrawingIndependent path, replaceFilePath
End If
Else
Err.Raise vbError, "", "Select components"
End If
Else
Err.Raise vbError, "", "Only assembly documents are supported"
End If
Else
Err.Raise vbError, "", "No model found"
End If
Dim destDrwFilePathAttr As VbFileAttribute
destDrwFilePathAttr = GetAttr(destDrwFilePath)
If destDrwFilePathAttr And vbReadOnly Then
Debug.Print "Removing the read-only flag from the destination drawing: " & destDrwFilePath
SetAttr destDrwFilePath, destDrwFilePathAttr Xor vbReadOnly
End If
If False = swApp.ReplaceReferencedDocument(destDrwFilePath, srcFilePath, destFilePath) Then
Err.Raise vbError, "", "Failed to replace referenced drawing document"
End If
End If
End Sub
Function GetSelectedComponents(selMgr As SldWorks.SelectionMgr) As Variant
Dim isInit As Boolean
isInit = False
Dim swComps() As SldWorks.Component2
Dim i As Integer
For i = 1 To selMgr.GetSelectedObjectCount2(-1)
Dim swComp As SldWorks.Component2
Set swComp = selMgr.GetSelectedObjectsComponent4(i, -1)
If Not swComp Is Nothing Then
If Not isInit Then
ReDim swComps(0)
Set swComps(0) = swComp
isInit = True
Else
If Not Contains(swComps, swComp) Then
ReDim Preserve swComps(UBound(swComps) + 1)
Set swComps(UBound(swComps)) = swComp
End If
End If
End If
Next
If isInit Then
GetSelectedComponents = swComps
Else
GetSelectedComponents = Empty
End If
End Function
Function BrowseForFileSave(title As String, filters As String, initFilePath As String) As String
Dim ofn As OPENFILENAME
Const FILE_PATH_BUFFER_SIZE As Integer = 260
Dim initFileName As String
initFileName = Right(initFilePath, Len(initFilePath) - InStrRev(initFilePath, "\"))