Bwaggoner
Mechanical
- Apr 9, 2007
- 23
I am testing the following code (from SW). The code does work if I put in the line swDoc.SaveAs I don't want to do because it seems kind-of like bad coding. I don't want to have to saveas the same file. I appears that swDoc.save is a valid option but the references do not change. Any idea of what I'm missing? Thanks.
['Replace References Examples (VB)
'This example shows how to replace external references in an assembly document.
'------------------------------------------------------------------
'
' Preconditions: E:\samples\hinge.SLDASM, hinge.SLDPRT, hingeReplace.SLDPRT,
' pin.SLDPRT, and pinReplace.SLDPRT exist.
'
' Postconditions. If the hinge.SLDASM was saved, hingeReplace.SLDPRT would
' replace hinge.SLDPRT and pinReplace.SLDPRT would
' replace pin.SLDPRT.
'
'------------------------------------------------------------------
Option Explicit
Dim swApp As swdocumentmgr.SwDMApplication
Dim swDocMgr As swdocumentmgr.SwDMApplication
Dim swDoc As swdocumentmgr.SwDMDocument8
Dim swdoc2 As SwDMDocument
Dim classfac As swdocumentmgr.SwDMClassFactory
Dim tapp As swdocumentmgr.SwDMApplication
Dim swconf As swdocumentmgr.SwDMConfiguration6
Dim swconfmgr As swdocumentmgr.SwDMConfigurationMgr
Dim filename As String
Dim e As SwDmDocumentOpenError
Dim src As SwDMSearchOption
Dim brokenRefVar As Variant
Dim varRef As Variant
Dim VarOriArr As Variant
Dim VarNewArr As Variant
Dim i As Long
Dim RefArr As Variant
Dim CongArr As Variant
Dim States As Variant
Sub Main()
Set classfac = CreateObject("SwDocumentMgr.SwDMClassFactory")
' Get SolidWorks Document Manager object
Set tapp = classfac.GetApplication("xxxx") 'Specify your license key
filename = "C:\My Stuff\Ref Test\Original\Assm1.SLDASM"
' Open specified file read-only
Set swDoc = tapp.GetDocument(filename, swDmDocumentAssembly, True, e)
Set src = tapp.GetSearchOptionObject
' Get all external references
varRef = swDoc.GetAllExternalReferences2(src, brokenRefVar)
Debug.Print "All External References:"
For i = 0 To UBound(varRef)
Debug.Print varRef(i)
Next i
Debug.Print ""
'Get changed references
Call swDoc.GetChangedReferences(VarOriArr, VarNewArr)
Debug.Print "All Changed References:"
If (IsEmpty(VarOriArr) = False) Then
For i = 0 To UBound(VarOriArr)
Debug.Print VarOriArr(i) & " ---> " & VarNewArr(i)
Next
End If
Debug.Print ""
' Get name of configuration
Set swconfmgr = swDoc.ConfigurationManager
Set swconf = swconfmgr.GetConfigurationByName("Default")
' Get information about references
Call swconf.GetReferencesInformation(RefArr, CongArr, States)
Debug.Print "Information About References:"
If (IsEmpty(RefArr) = False) Then
For i = 0 To UBound(RefArr)
Debug.Print RefArr(i) & "---> " & CongArr(i) & " --->" & States(i)
Next
End If
Debug.Print ""
' Replace references
Call swDoc.ReplaceReference("C:\My Stuff\Ref Test\Original\Part1.SLDPRT", "C:\My Stuff\Ref Test\Replaced\PartA.SLDPRT")
Call swDoc.ReplaceReference("C:\My Stuff\Ref Test\Original\Part2.SLDPRT", "C:\My Stuff\Ref Test\Replaced\PartB.SLDPRT")
Call swDoc.ReplaceReference("C:\My Stuff\Ref Test\Original\Part3.SLDPRT", "C:\My Stuff\Ref Test\Replaced\PartC.SLDPRT")
' Get changed references
Call swDoc.GetChangedReferences(VarOriArr, VarNewArr)
Debug.Print "Changed References:"
If (IsEmpty(VarOriArr) = False) Then
For i = 0 To UBound(VarOriArr)
Debug.Print VarOriArr(i) & " ---> " & VarNewArr(i)
Next
End If
Debug.Print ""
' Get information about references
Call swconf.GetReferencesInformation(RefArr, CongArr, States)
Debug.Print "Information About References:"
If (IsEmpty(RefArr) = False) Then
For i = 0 To UBound(RefArr)
Debug.Print RefArr(i) & "---> " & CongArr(i) & " --->" & States(i)
Next
End If
swDoc.SaveAs (filename)
swDoc.CloseDoc
End Sub]
['Replace References Examples (VB)
'This example shows how to replace external references in an assembly document.
'------------------------------------------------------------------
'
' Preconditions: E:\samples\hinge.SLDASM, hinge.SLDPRT, hingeReplace.SLDPRT,
' pin.SLDPRT, and pinReplace.SLDPRT exist.
'
' Postconditions. If the hinge.SLDASM was saved, hingeReplace.SLDPRT would
' replace hinge.SLDPRT and pinReplace.SLDPRT would
' replace pin.SLDPRT.
'
'------------------------------------------------------------------
Option Explicit
Dim swApp As swdocumentmgr.SwDMApplication
Dim swDocMgr As swdocumentmgr.SwDMApplication
Dim swDoc As swdocumentmgr.SwDMDocument8
Dim swdoc2 As SwDMDocument
Dim classfac As swdocumentmgr.SwDMClassFactory
Dim tapp As swdocumentmgr.SwDMApplication
Dim swconf As swdocumentmgr.SwDMConfiguration6
Dim swconfmgr As swdocumentmgr.SwDMConfigurationMgr
Dim filename As String
Dim e As SwDmDocumentOpenError
Dim src As SwDMSearchOption
Dim brokenRefVar As Variant
Dim varRef As Variant
Dim VarOriArr As Variant
Dim VarNewArr As Variant
Dim i As Long
Dim RefArr As Variant
Dim CongArr As Variant
Dim States As Variant
Sub Main()
Set classfac = CreateObject("SwDocumentMgr.SwDMClassFactory")
' Get SolidWorks Document Manager object
Set tapp = classfac.GetApplication("xxxx") 'Specify your license key
filename = "C:\My Stuff\Ref Test\Original\Assm1.SLDASM"
' Open specified file read-only
Set swDoc = tapp.GetDocument(filename, swDmDocumentAssembly, True, e)
Set src = tapp.GetSearchOptionObject
' Get all external references
varRef = swDoc.GetAllExternalReferences2(src, brokenRefVar)
Debug.Print "All External References:"
For i = 0 To UBound(varRef)
Debug.Print varRef(i)
Next i
Debug.Print ""
'Get changed references
Call swDoc.GetChangedReferences(VarOriArr, VarNewArr)
Debug.Print "All Changed References:"
If (IsEmpty(VarOriArr) = False) Then
For i = 0 To UBound(VarOriArr)
Debug.Print VarOriArr(i) & " ---> " & VarNewArr(i)
Next
End If
Debug.Print ""
' Get name of configuration
Set swconfmgr = swDoc.ConfigurationManager
Set swconf = swconfmgr.GetConfigurationByName("Default")
' Get information about references
Call swconf.GetReferencesInformation(RefArr, CongArr, States)
Debug.Print "Information About References:"
If (IsEmpty(RefArr) = False) Then
For i = 0 To UBound(RefArr)
Debug.Print RefArr(i) & "---> " & CongArr(i) & " --->" & States(i)
Next
End If
Debug.Print ""
' Replace references
Call swDoc.ReplaceReference("C:\My Stuff\Ref Test\Original\Part1.SLDPRT", "C:\My Stuff\Ref Test\Replaced\PartA.SLDPRT")
Call swDoc.ReplaceReference("C:\My Stuff\Ref Test\Original\Part2.SLDPRT", "C:\My Stuff\Ref Test\Replaced\PartB.SLDPRT")
Call swDoc.ReplaceReference("C:\My Stuff\Ref Test\Original\Part3.SLDPRT", "C:\My Stuff\Ref Test\Replaced\PartC.SLDPRT")
' Get changed references
Call swDoc.GetChangedReferences(VarOriArr, VarNewArr)
Debug.Print "Changed References:"
If (IsEmpty(VarOriArr) = False) Then
For i = 0 To UBound(VarOriArr)
Debug.Print VarOriArr(i) & " ---> " & VarNewArr(i)
Next
End If
Debug.Print ""
' Get information about references
Call swconf.GetReferencesInformation(RefArr, CongArr, States)
Debug.Print "Information About References:"
If (IsEmpty(RefArr) = False) Then
For i = 0 To UBound(RefArr)
Debug.Print RefArr(i) & "---> " & CongArr(i) & " --->" & States(i)
Next
End If
swDoc.SaveAs (filename)
swDoc.CloseDoc
End Sub]