Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

ReplaceViewModel not working

koubaleite

Mechanical
Nov 27, 2024
32
Hello,

I’m attempting to replace the view model of a drawing for a part file that has been renamed. I’ve been following a similar approach outlined in the API documentation, but I’m unsure if the issue lies with my instances variable. The ReplaceModelView function isn't working as expected, and there’s no error message, which is why I suspect the problem might be with how I’m handling the instances variable.

Any help or guidance would be greatly appreciated.

Thank you!

Code:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDrawing As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swDrawingComponent As SldWorks.DrawingComponent
Dim views(0) As SldWorks.View
Dim instances(0) As SldWorks.Component2
Dim status As Boolean
Sub ReplaceDrawingModels()
    Dim folderPath As String
    Dim fileName As String
    Dim partPath As String
    Dim drawingPath As String
    Dim fileSystem As Object
    Dim file As Object
    
    ' Initialize SolidWorks
    Set swApp = Application.SldWorks
    
    ' Ask user to select a folder
    folderPath = BrowseForFolder("Select Folder Containing SLDPRT and SLDDRW Files")
    If folderPath = "" Then Exit Sub
    folderPath = folderPath & "\"
    
    ' Initialize file system object
    Set fileSystem = CreateObject("Scripting.FileSystemObject")
    
    ' Loop through all .SLDDRW files in the folder
    For Each file In fileSystem.GetFolder(folderPath).Files
        If LCase(fileSystem.GetExtensionName(file.Name)) = "slddrw" Then
            drawingPath = folderPath & file.Name
            fileName = fileSystem.GetBaseName(file.Name)
            partPath = folderPath & fileName & ".sldprt"
            
            ' Check if corresponding part file exists
            If fileSystem.FileExists(partPath) Then
                ' Open the drawing
                Set swModel = swApp.OpenDoc6(drawingPath, swDocDRAWING, swOpenDocOptions_Silent, "", 0, 0)
                If Not swModel Is Nothing Then
                    Set swDrawing = swModel
                    
                    ' Get the first view and skip the sheet view
                    Set swView = swDrawing.GetFirstView
                    If Not swView Is Nothing Then Set swView = swView.GetNextView ' Move to the first actual drawing view
                    
                    ' Loop through views and replace model
                    Do While Not swView Is Nothing
                        ' Select the drawing view
                        Set views(0) = swView
                        ' Select the instance of the model to replace (the current view's component)
                        Set swDrawingComponent = swView.RootDrawingComponent
                        If Not swDrawingComponent Is Nothing Then
                            Set instances(0) = swDrawingComponent.Component
                            
                            ' Replace model in the view
                            status = swDrawing.ReplaceViewModel(partPath, views, instances)
                        End If
                        
                        ' Move to the next view
                        Set swView = swView.GetNextView
                    Loop
                    
                    ' Save and close the drawing
                    swModel.Save
                    swApp.CloseDoc swModel.GetTitle
                End If
            Else
                MsgBox "Part file '" & partPath & "' not found.", vbExclamation
            End If
        End If
    Next file
    
    ' Clean up
    MsgBox "Drawing models updated successfully!", vbInformation
End Sub
 
Replies continue below

Recommended for you

Did you tried a force rebuild?

Else try the ReplaceReferencedDocument method (and this would need all the files to be closed)
 
Did you tried a force rebuild?

Else try the ReplaceReferencedDocument method (and this would need all the files to be closed)
Yes I did tried with .ForceRebuild3 True but it is still not working.

I also tried with RepalceReferencedDocument but the problem is that I cannot access the old document path (just the name) as it requieres both old and new document path
 
I’m attempting to replace the view model of a drawing for a part file that has been renamed. I’ve been following a similar approach outlined in the API documentation, but I’m unsure if the issue lies with my instances variable. The ReplaceModelView function isn't working as expected, and there’s no error message, which is why I suspect the problem might be with how I’m handling the instances variable.

Any help or guidance would be greatly appreciated.

Thank you!
Hey! I ran into something similar before—double-check that your `instances` variable is actually getting populated with the correct view names from the drawing. If it's empty or not matching the current view names, `ReplaceModelView` won’t do anything but also won’t throw an error. Try printing out or logging the instance names first to make sure they line up. Hope that helps!
 
Hey! I ran into something similar before—double-check that your `instances` variable is actually getting populated with the correct view names from the drawing. If it's empty or not matching the current view names, `ReplaceModelView` won’t do anything but also won’t throw an error. Try printing out or logging the instance names first to make sure they line up. Hope that helps!
I verified but seems like it was not the issue. I am pretty sure the naming is correct
 
Did you tried a force rebuild?

Else try the ReplaceReferencedDocument method (and this would need all the files to be closed)
I found a trick to get the old document path, now the document is referenced correctly but drawings views are still empty
 
I forgot to precise that the documents are all parts and drawing no assemblies
 
Yes I did tried with .ForceRebuild3 True but it is still not working.

I also tried with RepalceReferencedDocument but the problem is that I cannot access the old document path (just the name) as it requieres both old and new document path
I tested your codes, and they worked OK for me. So something not correct with your workflow. Can you check the macro references, remove any references that says utilities, and then try again?

The only change I made was to hard code the path, since you had not shared the codes for the browse folder.
 
I tested your codes, and they worked OK for me. So something not correct with your workflow. Can you check the macro references, remove any references that says utilities, and then try again?

The only change I made was to hard code the path, since you had not shared the codes for the browse folder.
Oh I have just unchecked the utility reference and and run again the macro but still the same problem ! If it's working for you maybe there is an option in the settings that causes this. I have not identified which one. I have noticed that the macro works well for one drawing in particular but for others, the drawing views are hiddens and empty for some reason.
 
Check if "Update component names when documents are replaced" is selected under Tools > Options> External References. If not, then select it, and try again.
 
Check if "Update component names when documents are replaced" is selected under Tools > Options> External References. If not, then select it, and try again.
Hi, the option "Update component names when documents are replaced" was already selected.
 
Well, will have to verify/debug this on your machine then. Are you open for screen sharing?
I would love to thanks but I am on a work computer so it's not allowed unfortunately.
 
If the default configuration names are different, there may sometimes possibly be a problem.
 

Part and Inventory Search

Sponsor