Run SolidWorks Design Checker Example (C#)
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 primary interop assembly
// (in
the IDE, right-click the project name, select
// Add Reference,
click the Browse tab, navigate to the
// <SolidWorks_install_dir>\api\redist
folder and
// select
SolidWorks.Interop.dsgnchk.dll).
// 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.
//
//---------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.dsgnchk;
using System;
using System.Diagnostics;
namespace RunDesignCheck4CSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
SWDesignCheck
swDCAddIn = default(SWDesignCheck);
string
StandardFileName = null;
string
ReportFolderName = null;
int
retValue = 0;
bool
AddtoDesignBinder = false;
bool
OverWriteReport = false;
int
warnings = 0;
int
errors = 0;
bool
AutoCorrect = false;
//
Get the SolidWorks Design Checker add-in
swDCAddIn
= (SWDesignCheck)swApp.GetAddInObject("SWDesignChecker.SWDesignCheck");
if
(swDCAddIn == null)
{
Debug.Print("No
SolidWorks Design Checker add-in.");
return;
}
//
Show the Design Binder in the FeatureManager design tree
swApp.SetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swFeatureManagerDesignBinderVisibility,
(int)swAutoHideShowResponse_e.swAutoHideShowResponse_Show);
//
Open the drawing document to check
swApp.OpenDoc6("C:\\Program Files\\SolidWorks
Corp\\SolidWorks\\samples\\tutorial\\advdrawings\\FoodProcessor.slddrw",
(int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"", ref errors, ref 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
= (int)swDCAddIn.RunDesignCheck4(StandardFileName,
ReportFolderName, AddtoDesignBinder, OverWriteReport, AutoCorrect);
switch
(retValue)
{
case
0:
Debug.Print("No
errors.");
break;
case
1:
Debug.Print("Report
already exists.");
break;
case
2:
Debug.Print("Could
not create report directory.");
break;
case
3:
Debug.Print("No
active document.");
break;
case
4:
Debug.Print("Standards
file does not exist.");
break;
}
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}