Search code examples
vbasolidworks

How can I activate a model window if it is already open? Solidworks VBA


I am trying to find a way to automatically open the model the drives Drawing View 1. I have the following code which opens the model if it is not open already, but if it is already open, does not activate/show the window with the model open. I would like my code to open the model if it's not already, or switch the window to show the model if it's already open.

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2

Sub openpart()

    Set swApp = Application.SldWorks
    
    Set swModel = swApp.ActiveDoc
    
    Set Part = swApp.ActiveDoc
    boolstatus = Part.ActivateView("Drawing View1")
    boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
    
    If Not swModel Is Nothing Then
    
        Dim swSelMgr As SldWorks.SelectionMgr
        
        Set swSelMgr = swModel.SelectionManager
        
        Dim swView As SldWorks.View
        
        Set swView = swSelMgr.GetSelectedObject6(1, -1)
        
        If Not swView Is Nothing Then
        
            Dim swRefDoc As SldWorks.ModelDoc2
            Set swRefDoc = swView.ReferencedDocument
            
            If swRefDoc Is Nothing Then
                Err.Raise vbError, "", "Drawing view model is not loaded"
            End If
            
            swRefDoc.ShowConfiguration2 swView.ReferencedConfiguration
            
            Dim swConf As SldWorks.Configuration
            Set swConf = swRefDoc.GetConfigurationByName(swView.ReferencedConfiguration)
            
            swConf.ApplyDisplayState swView.DisplayState
            
            swRefDoc.Visible = True
            
            
            
        Else
            Err.Raise vbError, "", "Select drawing view"
        End If
        
    Else
        Err.Raise vbError, "", "No active documents"
    End If
    
End Sub

Solution

  • To activate an open document, use ActivateDoc3 Method (ISldWorks) as documented here.

    I've changed your code to the following:

    Dim Part As Object
    Dim boolstatus As Boolean
    Dim longstatus As Long, longwarnings As Long
    Dim swApp As Object
    Dim swModel As SldWorks.ModelDoc2
    Dim swModelAtivated As SldWorks.ModelDoc2
    Dim Errors As Long
    
    Sub openpart()
        Set swApp = Application.SldWorks
        Set swModel = swApp.ActiveDoc
        Set Part = swApp.ActiveDoc
        boolstatus = Part.ActivateView("Drawing View1")
        boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
        
        If Not swModel Is Nothing Then
            Dim swSelMgr As SldWorks.SelectionMgr
            Set swSelMgr = swModel.SelectionManager
            Dim swView As SldWorks.View
            Set swView = swSelMgr.GetSelectedObject6(1, -1)
            Dim docName As String
    
            If Not swView Is Nothing Then
                Dim swRefDoc As SldWorks.ModelDoc2
                Set swRefDoc = swView.ReferencedDocument
    
                If swRefDoc Is Nothing Then
                    Err.Raise vbError, "", "Drawing view model is not loaded"
                End If
    
                swRefDoc.ShowConfiguration2 swView.ReferencedConfiguration
    
                Dim swConf As SldWorks.Configuration
                Set swConf = swRefDoc.GetConfigurationByName(swView.ReferencedConfiguration)
                swConf.ApplyDisplayState swView.DisplayState
                swRefDoc.Visible = True
    
                docName = swRefDoc.GetTitle
    
                Set swModelAtivated = swApp.ActivateDoc3(docName, False, swRebuildOnActivation_e.swUserDecision, Errors)
            Else
                Err.Raise vbError, "", "Select drawing view"
            End If
        Else
            Err.Raise vbError, "", "No active documents"
        End If
    End Sub
    

    I've tested it with a drawing that has only a view named Drawing View1. This view refers to a part file. It always activated the file.

    Go through the Remarks on that link to make sure that you don't have problems when the View refers to a part file that has same name of an assembly file or vice versa.