Set Tolerances and Compare Geometry (C#)
//----------------------------------------------------------------------------------------
// This example shows how to set the tolerances and then
compare the volume and geometry
// of two versions of the same part and save the volume
comparison results as a solid part
// using the SOLIDWORKS Utilities API.
//
// Preconditions:
//
// 1. Add the SOLIDWORKS Utilities as an add-in
// (in
SOLIDWORKS, click Tools > Add-Ins
> SOLIDWORKS Utilities).
// 2. Add the SOLIDWORKS Utilities interop assembly as
a reference
// (right-click
on the project in Project Explorer, Add Reference,
// Browse
to <SOLIDWORKS_install_dir>\api\redist\CLR2,
// select
Solidworks.Interop.gtswutilities.dll).
// 3. The specified files exist.
// 4. C:\test\
exists.
// 5. Change the namespace of this example to <Your_project_name>.csproj.
//
// Postconditions:
//
// 1. Geometry comparison report, C:\test\Report\gtReportIndex.htm,
is created.
// 2. Position and angular tolerance statuses display
in the Immediate Window.
// 3. Face and volume comparison statuses display in the
Immediate Window.
// 4. Volume comparison part, C:\test\volumeComparison.sldprt,
is created.
//
// NOTE: Do not save the parts as they are used in a SOLIDWORKS
tutorial.
//---------------------------------------------------------
using SOLIDWORKS.Interop.sldworks;
using SOLIDWORKS.Interop.swconst;
using SOLIDWORKS.Interop.gtswutilities;
using System;
using System.Diagnostics;
namespace SetTolerancesAndCompareGeometry_CSharp.csproj
{
partial
class SOLIDWORKSMacro
{
PartDoc
Part;
public
void Main()
{
Part
= (PartDoc)swApp.ActiveDoc;
gtcocswUtilities
swUtil = default(gtcocswUtilities);
gtcocswCompareGeometry
swUtilCompGeom = default(gtcocswCompareGeometry);
gtcocswUtilOptions
OUtils = default(gtcocswUtilOptions);
string
file1 = null;
string
file2 = null;
string
reportname = null;
string
savevol = null;
int
errorcode = 0;
double
posTol = 0;
double
angTol = 0;
bool
AddToDesignBinder = false;
bool
OverwriteReport = false;
savevol
= "C:\\test\\volumeComparison";
reportname
= "C:\\test\\Report";
AddToDesignBinder
= false;
OverwriteReport
= true;
//
Get pointer to SOLIDWORKS Utilities interface
swUtil
= (gtcocswUtilities)swApp.GetAddInObject("Utilities.UtilitiesApp");
//
Get pointer to SOLIDWORKS Compare Geometry tool
swUtilCompGeom
= (gtcocswCompareGeometry)swUtil.GetToolInterface((int)gtSwTools_e.gtSwToolGeomDiff,
out errorcode);
//
Get SOLIDWORKS Utilities options
OUtils
= swUtil.options;
posTol
= 0.0001;
//
Meters
angTol
= 1E-06;
//
Radians
//
Set position tolerance
errorcode
= OUtils.SetPositionTolerance(posTol);
Debug.Print("Position
tolerance is set." + " gtError_e: " + errorcode);
//
Set angular tolerance
errorcode
= OUtils.SetAngularTolerance(angTol);
Debug.Print("Angular
tolerance is set." + " gtError_e: " + errorcode);
Debug.Print("");
int
volDiffStatus = 0;
int
faceDiffStatus = 0;
file1
= "C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS\\samples\\tutorial\\swutilities\\bracket_a.sldprt";
file2
= "C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS\\samples\\tutorial\\swutilities\\bracket_b.sldprt";
//
Compare the geometry of the faces and volumes and save results to a report
errorcode
= swUtilCompGeom.CompareGeometry3(file1,
"", file2, "", (int)gtGdfOperationOption_e.gtGdfFaceAndVolumeCompare,
(int)gtResultOptions_e.gtResultSaveReport, reportname, AddToDesignBinder,
OverwriteReport, ref volDiffStatus,
ref
faceDiffStatus);
if
(!(errorcode == (int)gtError_e.gtNOErr))
{
Debug.Print("Error
comparing geometries. Inspect gtError_e = " + errorcode + "
in the API help.");
}
diffStatus("Volume
comparison", volDiffStatus);
diffStatus("Face
comparison", faceDiffStatus);
//
Save volume comparison results as a solid part
errorcode
= swUtilCompGeom.SaveCompareVolumeResults(savevol);
//
Perform any necessary cleanup
errorcode
= swUtilCompGeom.Close();
}
public
void diffStatus(string name, int diffCode)
{
Debug.Print(name);
switch
(diffCode)
{
case
(int)gtVolDiffStatusOptionType_e.gtSuccess:
Debug.Print("Succeeded");
break;
case
(int)gtVolDiffStatusOptionType_e.gtNotPerformed:
Debug.Print("Not
performed");
break;
case
(int)gtVolDiffStatusOptionType_e.gtCanceled:
Debug.Print("Canceled");
break;
case
(int)gtVolDiffStatusOptionType_e.gtFailed:
Debug.Print("Failed");
break;
case
(int)gtVolDiffStatusOptionType_e.gtIdenticalParts:
Debug.Print("Identical
parts");
break;
case
(int)gtVolDiffStatusOptionType_e.gtDifferentParts:
Debug.Print("Different
parts");
break;
case
(int)gtVolDiffStatusOptionType_e.gtNoSolidBody:
Debug.Print("No
solid body found");
break;
case
(int)gtVolDiffStatusOptionType_e.gtAlreadySaved:
Debug.Print("Already
saved");
break;
}
Debug.Print("");
}
public
SldWorks swApp;
}
}