Compare Geometry Example (C#)
//---------------------------------------------------------------------------------
// This example shows how to use the SOLIDWORKS Utilities
API to compare geometries in two part documents.
//
// Preconditions:
//
// 1. 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).
// 2. Change the namespace of this example to <Your_project_name>.csproj.
// 2. The specified files exist.
// 3. C:\test\
exists.
//
// Postconditions:
//
// 1. Geometry comparison report, C:\test\Report\gtReportIndex.htm,
is created.
// 2. Face and volume comparison statuses display in the
Immediate Window.
// 3. Volume comparison part, C:\test\volumeComparison.sldprt,
is created.
//
// NOTE: Do not save any changes.
//---------------------------------------------------------
using SOLIDWORKS.Interop.sldworks;
using SOLIDWORKS.Interop.swconst;
using SOLIDWORKS.Interop.gtswutilities;
using System;
using System.Diagnostics;
namespace CompareGeometry_CSharp.csproj
{
partial
class SOLIDWORKSMacro
{
public
void Main()
{
gtcocswUtilities
swUtil = default(gtcocswUtilities);
gtcocswCompareGeometry
swUtilCompGeom = default(gtcocswCompareGeometry);
gtError_e
longStatus = default(gtError_e);
bool
bAddToBinder = false;
bool
bOverwrite = false;
int
errorCode = 0;
//
Get the SOLIDWORKS Utilities tool interface
swUtil
= (gtcocswUtilities)swApp.GetAddInObject("Utilities.UtilitiesApp");
//
Get the CompareGeometry tool
swUtilCompGeom
= (gtcocswCompareGeometry)swUtil.GetToolInterface(2,
out errorCode);
if
(!(errorCode == (int)gtError_e.gtNOErr))
{
Debug.Print("Error
getting compare geometry tool. Inspect gtError_e = " + errorCode
+ " in the API help.");
}
//
Compare the volumes and faces of the specified part documents
//
Save the results to a file in the specified path
bAddToBinder
= false;
bOverwrite
= true;
string
file1 = null;
string
file2 = null;
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";
longStatus
= (gtError_e)swUtilCompGeom.CompareGeometry3(file1,
"", file2, "", (int)gtGdfOperationOption_e.gtGdfFaceAndVolumeCompare,
(int)gtResultOptions_e.gtResultSaveReport, "C:\\test\\Report",
bAddToBinder, bOverwrite, ref volDiffStatus,
ref
faceDiffStatus);
if
(!(longStatus == gtError_e.gtNOErr))
{
Debug.Print("Error
comparing geometries. Inspect gtError_e = " + longStatus + "
in the API help.");
}
diffStatus("Volume
comparison", volDiffStatus);
diffStatus("Face
comparison", faceDiffStatus);
//Save
the volume comparison results as a solid part
longStatus
= (gtError_e)swUtilCompGeom.SaveCompareVolumeResults("c:\\test\\volumeComparison");
//
Perform any necessary clean up
longStatus
= (gtError_e)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;
}
}