Run SolidWorks Design Checker Example (VBA)
This example shows how to check the active document using the SolidWorks
Design Checker API.
'--------------------------------------------
'
' Preconditions:
' 1.
Load the SolidWorks Design Checker add-in
' (click
Tools > Add-ins > SolidWorks Design
Checker).
' 2.
Create c:\test if needed.
' 3.
Create a SolidWorks Design Checker standards file using
' the
SolidWorks Design Checker tutorial
' and
save the standards file to c:\test
(click
' Tools > Design
Checker > Build Checks > Design Checker
' Tutorial
to open the tutorial and perform the steps
' through
"Building the Design Requirements-2").
' 4.
Reference the SolidWorks Design Checker type library
' (in
the IDE, click Tools > References
> SolidWorks Design
' Checker
version Type
Library).
' 5.
Open the Immediate window.
' 6.
Run the macro.
'
' Postconditions:
' 1.
The Design Binder folder is shown in the FeatureManager design tree.
' 2.
SolidWorks Design Checker checks the active
' model
document as per the standards file, adds the
' Post-Correction
Result.drp and Pre-Correction Result.drp
' reports
to the Design Binder and the SWCDReport.xml reports to the
' c:\test\Food Processor_mm_dd_yyyy_hr_min\Post-Correction
Result and
' c:\test\Food Processor_mm_dd_yyyy_hr_min\Pre-Correction
Result folders.
' 3.
To verify that these reports were added
' to
the Design Binder, examine the Design Binder in the
' FeatureManager
design tree. To verify that the folder and reports
' were
created, examine c:\test.
'
' NOTE: Because this drawing document is used by a SolidWorks
' tutorial,
do not save any changes when closing the
' document.
'
'---------------------------------------------
Option Explicit
Sub main()
Dim
swApp As SldWorks.SldWorks
Dim
swDCAddIn As
DesignCheckerLib.SWDesignCheck
Dim
StandardFileName As String
Dim
ReportFolderName As String
Dim
retValue As Long
Dim
AddtoDesignBinder As Boolean
Dim
OverWriteReport As Boolean
Dim
warnings As Long
Dim
errors As Long
Dim
AutoCorrect As Boolean
'
Get the SolidWorks application
Set
swApp = Application.SldWorks
'
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
'
Show the Design Binder in the FeatureManager design tree
swApp.SetUserPreferenceIntegerValue swFeatureManagerDesignBinderVisibility,
swAutoHideShowResponse_Show
'
Open the drawing document to check
swApp.OpenDoc6 "C:\Program Files\SolidWorks
Corp\SolidWorks\samples\tutorial\advdrawings\FoodProcessor.slddrw",
swDocDRAWING, swOpenDocOptions_Silent, "", errors, warnings
'
Requirements file
StandardFileName
= "c:\test\tutorial.swstd"
'
Filename for report
ReportFolderName
= "c:\test\Food Processor"
'
Add report to Design Binder
AddtoDesignBinder
= True
'
Overwrite any existing report
OverWriteReport
= True
'
Autocorrect all failures
AutoCorrect
= True
'
Run SolidWorks Design Checker on the active drawing document
retValue
= swDCAddIn.RunDesignCheck4(StandardFileName,
ReportFolderName, AddtoDesignBinder, OverWriteReport, AutoCorrect)
Select
Case retValue
Case
0
Debug.Print
"No errors."
Case
1
Debug.Print
"Report already exists."
Case
2
Debug.Print
"Could not create report directory."
Case
3
Debug.Print
"No active document."
Case
4
Debug.Print
"Standards file does not exist."
End
Select
End Sub