Hide Table of Contents

Compare Selected Objects and Their Persistent Reference IDs Example (VBA)

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 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.
'----------------------------------------------------------
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim selObj1 As Object
Dim selObj2 As Object
Dim selObjPID1 As Variant
Dim selObjPID2 As Variant
 

Sub main()


        Set swApp = Application.SldWorks
        Set swModel = swApp.ActiveDoc
        Set swSelMgr = swModel.SelectionManager
        Set swModelDocExt = swModel.Extension


        ' Get the selected objects
        Set selObj1 = swSelMgr.GetSelectedObject6(1, -1)
        Set selObj2 = swSelMgr.GetSelectedObject6(2, -1)


        Debug.Print ("Compare the selected objects:")
        Select Case swApp.IsSame(selObj1, selObj2)
            Case 0
                Debug.Print ("  Selected objects are not the same.")
            Case 1
                Debug.Print ("  Selected objects are the same.")
            Case -1
                Debug.Print ("  Unable to determine if selected objects are the same.")
        End Select


        Debug.Print ("")


        Debug.Print ("Compare the same objects:")
        Select Case swApp.IsSame(selObj1, selObj1)
            Case 0
                Debug.Print ("  Selected objects are not the same.")
            Case 1
                Debug.Print ("  Selected objects are the same.")
            Case -1
                Debug.Print ("  Unable to determine if selected objects are the same.")
        End Select


        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:")
        Select Case swModelDocExt.IsSamePersistentID(selObjPID1, selObjPID2)
            Case 0
                Debug.Print ("  Selected objects do not have the same persistent reference ID.")
            Case 1
                Debug.Print ("  Selected objects do have the same persistent reference ID.")
            Case -1
                Debug.Print ("  Unable to determine if the selected objects have the same persistent reference ID.")
        End Select


        Debug.Print ("")


        Debug.Print ("Compare the persistent reference IDs of the same objects:")
        Select Case swModelDocExt.IsSamePersistentID(selObjPID1, selObjPID1)
            Case 0
                Debug.Print ("  Selected objects do not have the same persistent reference ID.")
            Case 1
                Debug.Print ("  Selected objects do have the same persistent reference ID.")
            Case -1
                Debug.Print ("  Unable to determine if the selected objects have the same persistent reference ID.")
        End Select


End Sub



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 (VBA)
*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) 2017 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.