Bring document foreground (activate document) using SOLIDWORKS API

Edit ArticleEdit Article
Comments

This example demonstrates how to bring the document selected by path to foreground (make active) using ISldWorks::ActivateDoc3 SOLIDWORKS API.

Document can be opened in 2 states (visible or hidden). Hidden document are usually models which are loaded into the memory from the components in the assembly or drawing. In this case when ISldWorks::OpenDoc6 method is called the document will not be brought foreground automatically. Similar scenario applies to closing the document which is loaded as a component: document will be made invisible rather than closed.

  • Run the macro when no files are opened - file will be opened and closed
  • Open assembly and run the macro. In this case ISldWorks::OpenDoc6 API doesn't force the part to be brought foreground, so it is required to force activate it.

Download sample files

Const FILE_NAME As String = "SimpleBox.SLDPRT"

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    
    Dim swModel As SldWorks.ModelDoc2
    
    Dim path As String
    path = swApp.GetCurrentMacroPathFolder() & "\" & FILE_NAME
    
    Set swModel = swApp.GetOpenDocumentByName(path)
    
    Dim wasVisible As Boolean
    
    If Not swModel Is Nothing Then
        wasVisible = swModel.Visible
    End If
    
    Set swModel = swApp.OpenDoc6(path, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)
    
    If Not swModel Is Nothing Then
        swApp.ActivateDoc3 swModel.GetTitle(), False, swRebuildOnActivation_e.swDontRebuildActiveDoc, 0
    End If
    
    MsgBox "Was Visible: " & wasVisible
    
    If False = wasVisible Then
        swApp.CloseDoc swModel.GetTitle
    End If
    
End Sub


Notifications

All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.


Product of Xarial Product of Xarial