Page 1 of 1

Document Manager Replace external references

Posted: Thu Sep 28, 2023 2:25 pm
by ATANAS
Hi,
Has anybody succeeded in replacing external references FOR ASSEMBLY AND PART using document manager?

The below code works for drawing where GetAllExternalReferences4 is called. But according to the SW documentation this function should be called on drawings only and for part and assembly files GetExternalFeatureReferences2 should be used.
the code works with no errors but when i open the assembly file that is supposed to pint to a new referenced file location it still looks for the file at the old location.

Cheers,
Atanas

Public Sub UpdateReferences(strFromFile As String, strToFile As String)
Dim swDmApp As SwDocumentMgr.SwDMApplication4
Dim swDmDoc As SwDocumentMgr.SwDMDocument21
Dim searchOpts As SwDocumentMgr.SwDMSearchOption

Dim vDependArr As Variant
Dim vDepend As Variant

Dim i As Long

Dim brokenRef As SwDmReferenceStatus
Dim isVirtual As Boolean
Dim timeStamp As Double
Set swDmApp = ConnectToDm

For i = 0 To UBound(arFiles)

If LCase(Right(arFiles(i), Len(arFiles(i)) - InStrRev(arFiles(i), "."))) = "slddrw" Then 'the document is drawing
Set searchOpts = swDmApp.GetSearchOptionObject

searchOpts.SearchFilters = SwDmSearchFilters.SwDmSearchExternalReference
Set swDmDoc = OpenDocument(swDmApp, arFiles(i), False)

On Error Resume Next
vDependArr = swDmDoc.GetAllExternalReferences4(searchOpts, vBrokenRefs, vIsVirtuals, vTimeStamps)
If Err.Number > 0 Then MsgBox Err.Number
On Error GoTo 0
Else ' not a drawing- assembly or part
Dim l As Long
Dim dmExtRefOption As SwDMExternalReferenceOption2
Set swDmDoc = OpenDocument(swDmApp, arFiles(i), False)

Set dmExtRefOption = swDmApp.GetExternalReferenceOptionObject2
Set searchOpts = swDmApp.GetSearchOptionObject
searchOpts.SearchFilters = SwDmSearchFilters.SwDmSearchForAssembly

dmExtRefOption.SearchOption = searchOpts


On Error Resume Next
l = swDmDoc.GetExternalFeatureReferences2(dmExtRefOption)
vDependArr = dmExtRefOption.ExternalReferences
If Err.Number > 0 Then MsgBox Err.Number
On Error GoTo 0
End If


For Each vDepend In vDependArr
If InStr(vDepend, strFromFile) > 0 Then Exit For
Next vDepend


swDmDoc.ReplaceReference vDepend, strToFile
swDmDoc.Save
swDmDoc.CloseDoc
Next i
End Sub

Re: Document Manager Replace external references

Posted: Fri Sep 29, 2023 1:34 am
by andrmollo
Hi!

Have you looked at codestack?
https://www.codestack.net/solidworks-do ... eferences/

Following that example I was able to do it for assembly and parts.

Re: Document Manager Replace external references

Posted: Fri Sep 29, 2023 11:21 am
by ATANAS
It worked!
I came across this example but dismissed it as I thought it would work only on drawing files.
I should have known better than to dismiss code offered by Artem.
Thank you both!