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. Specified file exists.
' 2. Run the macro.
'
' Postconditions: The specified assembly file and its
' its
components files are saved as PDF files
to the c:\temp folder.
'------------------------------
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:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\blade
shaft.sldasm"
Output
= "c:\temp\"
Debug.Print("---
Save files as PDF ---")
SaveToPDF(Document,
Output)
swApp.CloseAllDocuments(True)
Debug.Print("---
Done ---")
'
Show SolidWorks frame and SolidWorks
pFrame.KeepInvisible = False
swApp.Visible = True
Exit
Sub
Fail:
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 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
i As Long
'
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
Else
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 + ".
")
Else
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