I am very new to working with the SolidWorks API and VB. I recently wrote a VB script based on sample code found on the API knowledge base to:
- Launch a background application of SolidWorks
- Load an assembly which is linked to a design table (keeping it invisible)
- Update the model based on the design table entries
- Resave the model as a step file
- Close the model and exit SolidWorks
For the most part, the code works. However, when the part linked to the Excel file is opened, MS Excel is launched and the Excel design table and SolidWorks application windows become visible, even though I have the Frame.KeepInvisible = True option specified. Is there a way to keep the MS Excel Window from opening, allowing it to run in the background? Also, I want to keep SolidWorks invisible while the model is updating to reflect changes in the design table. I have provided the code for reference. It is relatively short. I appreciate any help you can offer. Thanks!
Option Explicit On
Imports System
Module Module1
'-----------------------------
' Preconditions: Specified file exists.
'
' Postconditions: The specified assembly file is exported in the STEP format
'
'------------------------------
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExtension As SldWorks.ModelDocExtension
Dim swAssembly As SldWorks.AssemblyDoc
Dim doctype As Long
Dim errors As Long
Dim warnings As Long
Dim modelpath As String
Dim modelFileName As String
Dim convFileName As String
Dim Success As Boolean
Dim vComponents As Object
Dim outputpath As String
Sub SaveToSTEP(ByVal inputFileName As String, ByVal outputFileName As String)
' Determine the type of SolidWorks file based on
' its filename extension
If LCase(Right(inputFileName, 7)) = ".sldprt" Then
doctype = SwConst.swDocumentTypes_e.swDocPART
ElseIf LCase(Right(inputFileName, 7)) = ".sldasm" Then
doctype = SwConst.swDocumentTypes_e.swDocASSEMBLY
ElseIf LCase(Right(inputFileName, 7)) = ".slddrw" Then
doctype = SwConst.swDocumentTypes_e.swDocDRAWING
Else
doctype = SwConst.swDocumentTypes_e.swDocNONE
End If
' Open part document linked to design table
swModel = swApp.OpenDoc6("C:\LDI Multi-Body Part.SLDPRT", SwConst.swDocumentTypes_e.swDocPART, 256, "", errors, warnings)
swApp.SetUserPreferenceIntegerValue(3, 0)
swModel.ForceRebuild3(False)
swApp.Visible = False
swApp.Frame.KeepInvisible = True
swModel = swApp.OpenDoc6(inputFileName, doctype, 256, "", 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(inputFileName, True, errors)
' Save document as STEP
swApp.SetUserPreferenceIntegerValue(SwConst.swUserPreferenceIntegerValue_e.swStepAP, 214)
swModel.Extension.SaveAs("C:\CE13 Combustor Geometry Files\MultiPoint\" + outputFileName + ".step", SwConst.swSaveAsVersion_e.swSaveAsCurrentVersion, SwConst.swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, errors, warnings)
swModel = Nothing
End Sub
Sub main(ByVal args() As String)
Dim Output As String = args(0)
Dim Document As String
On Error GoTo Fail
' Get SolidWorks
swApp = CreateObject("SldWorks.Application")
' 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
swApp.Frame.KeepInvisible = True
Document = "C:\LDI Combustor Test Section.SLDASM"
SaveToSTEP(Document, Output)
swApp.CloseAllDocuments(True)
swApp.ExitApp()
Debug.Print("--- Done ---")
swApp.ExitApp()
swApp = Nothing
Exit Sub
Fail:
Debug.Print("Execution failed with error " & Err.Number & ": '" & Err.Description & "'")
End Sub
End Module
SolidworksApi macros