Run SolidWorks Design Checker Example (VBA)
This example shows how to check the active document using the SolidWorks
Design Checker API.
'--------------------------------------------
'
' Preconditions:
' (1)
SolidWorks Design Checker add-in is not
' loaded.
' (2)
Model document is open and active.
' (3)
Specified SolidWorks Design Checker
' requirements
file exists.
'
' Postconditions:
' (1)
SolidWorks Design Checker add-in is loaded.
' (2)
SolidWorks Design Checker checks the active
' model
document as per the requirements file
' adds
the report to the Design Binder.
'
' Possible values for retval are:
' 0
= dsgnchkNOErr
' 1
= dsgnchkReportAlreadyExists
' 2
= dsgnchkCouldNotCreateReportDirectory
' 3
= dsgnchkNoActivedocument
' 4
= dsgnchkStandardFileDoesNotExist
'
'---------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
Dim
swModel As
SldWorks.ModelDoc2
Dim
swDCAddIn As
DesignCheckerLib.SWDesignCheck
Dim
strDllFileName As
String
Dim
strExecutablePath As
String
Dim
lStatus As
Long
Dim
StandardFileName As
String
Dim
ReportFolderName As
String
Dim
retval As
Long
Dim
AddtoDesignBinder As
Boolean
Dim
OverWriteReport As
Boolean
Dim
CanViewReportOnSave As
Boolean
'
Get the SolidWorks application
Set
swApp = Application.SldWorks
'
'
Load the add-in
'
'
Compose the name of the DLL to load by getting
'
the path where the SolidWorks executable exists
'
relative to where the SolidWorks Design Checker DLL exists
strExecutablePath
= swApp.GetExecutablePath
strDllFileName
= strExecutablePath & "\dsgnchk\dsgnchku.dll"
'
Load the DLL
lStatus
= swApp.LoadAddIn(strDllFileName)
If
lStatus <> 0 Then
'
DLL was not loaded
Select
Case lStatus
Case
-1:
Debug.Print
strDllFileName & " not loaded; unknown error."
Case
1:
Debug.Print
strDllFileName & " not loaded; not an error."
Case
2:
Debug.Print
strDllFileName & " not loaded; already loaded. "
Debug.Print
"Unload the add-in in SolidWorks and then rerun the macro."
End
Select
Exit
Sub
Else
'
DLL was loaded
'
Get the SolidWorks Design Checker add-in
Set
swDCAddIn = swApp.GetAddInObject("SWDesignChecker.SWDesignCheck")
If
swDCAddIn Is Nothing Then
Debug.Print
"No SolidWorks Design Checker add-in."
Exit
Sub
End
If
End
If
'
SolidWorks Design Checker add-in is loaded
'
Use the add-in in SolidWorks 2006 and later
'
Substitute the path and name of your requirements file
'
and your name for the report
StandardFileName
= "C:\test\aw_hook.swstd" ' Requirements file
ReportFolderName
= "aw_hook" ' Filename for report
AddtoDesignBinder
= True ' Add report to Design Binder
OverWriteReport
= True ' Overwrite any existing report
CanViewReportOnSave
= True ' Display report and save it
'
Run SolidWorks Design Checker on the active model document
retval
= swDCAddIn.RunDesignCheck2(StandardFileName,
ReportFolderName, AddtoDesignBinder, OverWriteReport, CanViewReportOnSave)
Debug.Print
retval
End Sub