Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Document Mgr API 1

Status
Not open for further replies.

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]

 
Replies continue below

Recommended for you

What happens when you try to use Save instead of SaveAs? According to the comments, the assembly document is opened read-only. While I haven't confirmed, it looks like the code also reflects read-only. If so, then Save should not work. My guess is that the document manager read-only behavior is similar to SW - If you open a file as read-only in SW then try to save, it actually brings up the SaveAs dialog. You can then SaveAs with the original filename. Have you tried not opening the assembly document as read-only?
 
The refs don't get changed when I use Save.

If the assembly is opened as read-only how would I tell or how would I tell it to open it for editing?
 
Investigate this section:

Code:
' Open specified file read-only

    Set swDoc = tapp.GetDocument(filename, swDmDocumentAssembly, True, e)

You'll need to understand all the arguments of GetDocument.
 
handleman your a lifesaver. . .

The True argument = read only
False = read-write

Who would have thunk it.
 
Since this is regarding the same topic I thought I would add to this post.

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:
Function ReplaceRefs()
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
 
Be sure to ctrl-Q your assembly. I noticed sometimes the feature tree info does not update until after rebuild after using SW Explorer to change refs.

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.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
If you didn't know...

ModelDoc2.EditRebuild3 'Stoplight or [Ctrl]+B
ModelDoc2.ForceRebuild '[Ctrl]+Q


Ken
 
The rebuilds are not necessary.

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.
 
It can be done. I just wrote a macro that does it. To be published soon.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
TheTick.

That would be great I'm curious to see what you did.
 
In my case, it is component substitution when opening a drawing or assembly in SW. To make same-name substitution work, I have to open the component file before I redirect the reference.

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.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Bwaggoner,
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?
 
Here's what I've done.

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor