Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations SDETERS 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
23
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
 

Part and Inventory Search

Sponsor