<
Compare Selected Objects and Their Persistent Reference IDs (C#)
This example shows how to determine if two selected entities are the
same entities and if those entities have the same persistent IDs.
Using IModelDocExtension::IsSamePersistentID
and ISldWorks::IsSame
might be helpful when your application is passed two entities that it
didn't select, and your application needs to know if they're the same
entity.
//----------------------------------------------------------------------------
// Preconditions: A part document is open and two
// different entities (e.g., faces, edges, vertices, etc.)
// are selected.
//
// Postconditions: None
//------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
namespace IsSamePersistentIsSame_CSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
SelectionMgr
swSelMgr = default(SelectionMgr);
ModelDocExtension
swModelDocExt = default(ModelDocExtension);
object
selObj1 = null;
object
selObj2 = null;
object
selObjPID1 = null;
object
selObjPID2 = null;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swSelMgr
= (SelectionMgr)swModel.SelectionManager;
swModelDocExt
= swModel.Extension;
//
Get the selected objects
selObj1
= swSelMgr.GetSelectedObject6(1, -1);
selObj2
= swSelMgr.GetSelectedObject6(2, -1);
Debug.Print("Compare
the selected objects:");
switch
(swApp.IsSame(selObj1, selObj2))
{
case
0:
Debug.Print("
Selected objects are not the same.");
break;
case
1:
Debug.Print("
Selected objects are the same.");
break;
case
-1:
Debug.Print("
Unable to determine if selected objects are the same.");
break;
}
Debug.Print("");
Debug.Print("Compare
the same objects:");
switch
(swApp.IsSame(selObj1, selObj1))
{
case
0:
Debug.Print("
Selected objects are not the same.");
break;
case
1:
Debug.Print("
Selected objects are the same.");
break;
case
-1:
Debug.Print("
Unable to determine if selected objects are the same.");
break;
}
Debug.Print("");
//
Get the persistent reference IDs of the
//
selected objects
selObjPID1
= swModelDocExt.GetPersistReference3(selObj1);
selObjPID2
= swModelDocExt.GetPersistReference3(selObj2);
Debug.Print("Compare
the persistent reference IDs of the selected objects:");
switch
(swModelDocExt.IsSamePersistentID(selObjPID1,
selObjPID2))
{
case
0:
Debug.Print("
Selected objects do not have the same persistent reference ID.");
break;
case
1:
Debug.Print("
Selected objects do have the same persistent reference ID.");
break;
case
-1:
Debug.Print("
Unable to determine if the selected objects have the same persistent reference
ID.");
break;
}
Debug.Print("");
Debug.Print("Compare
the persistent reference IDs of the same objects:");
switch
(swModelDocExt.IsSamePersistentID(selObjPID1,
selObjPID1))
{
case
0:
Debug.Print("
Selected objects do not have the same persistent reference ID.");
break;
case
1:
Debug.Print("
Selected objects do have the same persistent reference ID.");
break;
case
-1:
Debug.Print("
Unable to determine if the selected objects have the same persistent reference
ID.");
break;
}
}
public
SldWorks swApp;
}
}