Document Mgr API
Document Mgr API
(OP)
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]






RE: Document Mgr API
RE: Document Mgr API
If the assembly is opened as read-only how would I tell or how would I tell it to open it for editing?
RE: Document Mgr API
CODE
Set swDoc = tapp.GetDocument(filename, swDmDocumentAssembly, True, e)
You'll need to understand all the arguments of GetDocument.
RE: Document Mgr API
The True argument = read only
False = read-write
Who would have thunk it.
RE: Document Mgr API
The below code is part of a much bigger macro. Basically what I'm trying to accomplish is changing a file reference. What we've done is 1) Read the children from an assembly. 2) Look in directory (x) and see if any of the same named files reside there. 3) If there are then change the references to directory (x).
The code seems to work. After I run the function I can see that the time stamp of the assembly has changed. So, I would think that the refs have also been changed to the new location. If I open the assembly in SW then check the refs I find out that they have not been change.
Any ideas?
CODE
Dim swApp As SwDocumentMgr.SwDMApplication
Dim swDocMgr As SwDocumentMgr.SwDMApplication
Dim swDoc As SwDocumentMgr.SwDMDocument8
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
Debug.Print "ReplaceRefs Function"
For t = 1 To 60000
If Cells(t, 1).Value = "Parent" Then
parentassm = Cells(t, 2).Value
Debug.Print parentassm
For r = 0 To 100
If Cells(t + r, 2).Value = "@@" Then
Exit For
End If
Next r
RefCounter = r
Debug.Print r
'Debug.Print Cells(r).Value
Debug.Print "RefCounter = " & RefCounter
StartRefRow = t + 1
Debug.Print Cells(t + 1).Value
EndRefRow = t + r
Debug.Print "StartRefRow = " & StartRefRow
Debug.Print "EndRefRow = " & EndRefRow
Debug.Print "-"
For u = StartRefRow To EndRefRow
Debug.Print Cells(u, 3).Value
Debug.Print Cells(u, 4).Value
Debug.Print "-"
If Cells(u, 3).Value = "File Name Match" Then
Set classfac = CreateObject("swdocumentmgr.swdmclassfactory")
Set tapp = classfac.GetApplication("KEY")
Set swDoc = tapp.GetDocument(parentassm, swDmDocumentAssembly, True, e)
Call swDoc.ReplaceReference(Cells(u, 2), Cells(u, 4))
End If
Next u
t = t + EndRefRow
swDoc.save 'As (parentassm)
End If
If Cells(t, 2).Value = "" Then Exit For
Next t
swDoc.save 'As (parentassm)
swDoc.CloseDoc
End Function
RE: Document Mgr API
Also check under File --> Find References. This list is tends to be accurate even if feature tree is not updated.
Also you can use SW Explorer to see if references have changed.
http://www.EsoxRepublic.com-SolidWorks API VB programming help
RE: Document Mgr API
ModelDoc2.EditRebuild3 'Stoplight or [Ctrl]+B
ModelDoc2.ForceRebuild '[Ctrl]+Q
Ken
RE: Document Mgr API
I'm beginning to believe that it is not possible to rereference an assembly to parts in a different location with the same name. It can be done in Solidworks using the open dialog and selecting references. I've done that.
The first code I posted works if the file names are different. If they are the same then the references do not change. It doesn't seem to matter if you use a save or a saveas.
I'm out of ideas.
RE: Document Mgr API
http://www.EsoxRepublic.com-SolidWorks API VB programming help
RE: Document Mgr API
That would be great I'm curious to see what you did.
RE: Document Mgr API
Perhaps you can make a double-switch? Switch to a temporary copy of your file with a different name, then switch to your intended target.
http://www.EsoxRepublic.com-SolidWorks API VB programming help
RE: Document Mgr API
What happens if you run your macro (using the same filename with different path), then rename the original files prior to opening the assembly in SW?
RE: Document Mgr API
In C:\My Stuff\Ref Test\Original I have: Assm1, Part1, Part2, & Part3. Also, I have new folder
In C:\My Stuff\Ref Test\Replaced I have: Part1, Part2, & Part3.
If I run my macro then I move the part files in the Original folder to folder C:\My Stuff\Ref Test\Original\new folder
Then open Assm1 in the Original folder everything works fine.
Not exactly what I would like but progress none the less. I guess I could then delete those files that I moved into the new folder.