Hide Table of Contents

Compare Selected Objects and Their Persistent Reference IDs Example (C#)

This example shows how to determine if two selected entities are the same entities and if those entities have the same persistent IDs.

NOTE: 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: 
// 1. Open a part document is open and select two
//    different entities (e.g., face, edge, vertex, etc.).
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Compares the selected entities.
// 2. Examine the code and Immediate window.
//------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
 
namespace Macro1CSharp.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;
    }
}


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Compare Selected Objects and Their Persistent Reference IDs Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2018 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.