Hide Table of Contents

Keep SOLIDWORKS Invisible While Activating Documents Example (VB.NET)

This example shows how to keep SOLIDWORKS invisible while activating SOLIDWORKS documents, including assembly component files, and saving those documents as PDF files.

' Preconditions:
' 1. Verify that the specified assembly file exists.
' 2. Verify that c:\temp exists.
' 3. Open the Immediate window.
' Postconditions:
' 1. Saves the specified assembly file and its
'    component files as PDF files to the c:\temp.
' 2. Examine the Immediate window and c:\temp.
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics

Partial Class SolidWorksMacro

    Public swModel As ModelDoc2
    Public swExtension As ModelDocExtension

    Public Sub main()

        Dim pFrame As Frame
        Dim Document As String
        Dim Output As String

        On Error GoTo Fail

        ' Allow SOLIDWORKS to run in the background
        ' and be invisible
        swApp.UserControl = False

        ' If the following property is true, then the
        ' SOLIDWORKS frame will be visible on a call to
        ' ISldWorks::ActivateDoc2; so, set it to false
        swApp.Visible = False

        ' Keep SOLIDWORKS frame invisible when
        ' ISldWorks::ActivateDoc2 is called
        pFrame = swApp.Frame
        pFrame.KeepInvisible = True

        Document = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\advdrawings\blade shaft.sldasm"
        Output = "c:\temp\"

        Debug.Print("--- Save files as PDF ---")
        SaveToPDF(Document, Output)
        Debug.Print("--- Done ---")

        ' Show SOLIDWORKS frame and SOLIDWORKS
        pFrame.KeepInvisible = False
        swApp.Visible = True
        Exit Sub
        Debug.Print("Execution failed with error " & Err.Number & ": '" & Err.Description & "'")

    End Sub

    Private Sub SaveToPDF(ByVal docFileName As String, ByVal outputpath As String)
        Dim swAssembly As AssemblyDoc
        Dim doctype As Integer
        Dim errors As Integer
        Dim warnings As Integer
        Dim modelpath As String = ""
        Dim modelFileName As String = ""
        Dim convFileName As String = ""
        Dim Success As Boolean
        Dim vComponents As Object
        Dim i As Integer

        ' Determine the type of SOLIDWORKS file based on
        ' its filename extension
        If LCase(Right(docFileName, 7)) = ".sldprt" Then
            doctype = swDocumentTypes_e.swDocPART
        ElseIf LCase(Right(docFileName, 7)) = ".sldasm" Then
            doctype = swDocumentTypes_e.swDocASSEMBLY
        ElseIf LCase(Right(docFileName, 7)) = ".slddrw" Then
            doctype = swDocumentTypes_e.swDocDRAWING
            doctype = swDocumentTypes_e.swDocNONE
        End If

        ' Open document
        swModel = swApp.OpenDoc6(docFileName, doctype, swOpenDocOptions_e.swOpenDocOptions_Silent Or swOpenDocOptions_e.swOpenDocOptions_ReadOnly, "", errors, warnings)
        If swModel Is Nothing Then
            Debug.Print("Failed to open document " + modelpath + ". Errors: " & errors)
        End If

        ' Activate the document, which should remain invisible
        ' due to earlier call to IFrame::KeepInvisible
        swModel = swApp.ActivateDoc2(docFileName, True, errors)

        ' Build destination filename
        modelpath = swModel.GetPathName()
        modelFileName = Mid(modelpath, InStrRev(modelpath, "\") + 1)
        modelFileName = Left(modelFileName, InStrRev(modelFileName, ".") - 1)
        convFileName = outputpath + modelFileName + ".pdf"

        ' Save document as PDF
        swExtension = swModel.Extension
        Success = swExtension.SaveAs(convFileName, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, errors, warnings)
        If Success Then
            Debug.Print("Document, " + modelpath + ", saved as " + convFileName + ". ")
            Debug.Print("Document not saved: ")
            Debug.Print("  Errors: " & errors + modelpath + " as " + convFileName + ". ")
        End If

        ' Process all components
        If doctype = swDocumentTypes_e.swDocASSEMBLY Then
            swAssembly = swModel
            vComponents = swAssembly.GetComponents(True)
            For i = 0 To UBound(vComponents)
                Dim swComponent As Component2
                swComponent = vComponents(i)
                SaveToPDF(swComponent.GetPathName(), outputpath)
            Next i
        End If

    End Sub


    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks


End Class

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Keep SOLIDWORKS Invisible While Activating Documents Example (VB.NET)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2023 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.