Hide Table of Contents

Use Persistent Reference Example (VB.NET)

Persistent references are a means of returning to an item between sessions of SOLIDWORKS. Persistent references are similar to attributes, but are easier to use. In almost all cases, persistent references can, and should, be used in place of attributes.

This example:

  • shows how to use persistent references.
  • is primarily diagnostic code that determines whether the currently selected item has a persistent reference from which an object can be retrieved.

'----------------------------------------------------------------------------
' Preconditions:
' 1. Open a part, assembly, or drawing.
' 2. Select an entity.
'
' Postconditions:
' 1. Gets the selected entity type and its PID.
' 2. Examine the Immediate window.
'----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics

Partial Class SolidWorksMacro

    
Function GetStringFromID(ByVal swApp As SldWorks, ByVal swModel As ModelDoc2, ByVal vPIDarr As Object) As String

        Dim vPID As Object
        For Each vPID In vPIDarr
            Debug.Assert(vbByte = VarType(vPID))
            GetStringFromID = GetStringFromID & Format(vPID,
"###000")
        
Next vPID

    
End Function

    Function GetObjectFromString(ByVal swApp As SldWorks, ByVal swModel As ModelDoc2, ByVal IDstring As String) As Object

        Dim swModExt As ModelDocExtension
        
Dim ByteStream() As Byte
        Dim vPIDarr As Object
        Dim nRetval As Long
        Dim i As Long

        swModExt = swModel.Extension
        
ReDim ByteStream(Len(IDstring) / 3 - 1)

        
For i = 0 To Len(IDstring) - 3 Step 3
            ByteStream(i / 3) =
CByte(Mid(IDstring, i + 1, 3))
        
Next i

        vPIDarr = ByteStream

        GetObjectFromString = swModExt.GetObjectByPersistReference3((vPIDarr), nRetval)

        Debug.Assert(swPersistReferencedObjectStates_e.swPersistReferencedObject_Ok = nRetval)
        Debug.Assert(
Not GetObjectFromString Is Nothing)

    
End Function

    Sub main()

        
Dim swModel As ModelDoc2
        
Dim swModExt As ModelDocExtension
        
Dim swSelMgr As SelectionMgr
        
Dim swSelObj As Object
        Dim swObj As Object
        Dim vPIDarr As Object
        Dim sIDstring As String

        swModel = swApp.ActiveDoc
        swModExt = swModel.Extension
        swSelMgr = swModel.SelectionManager
        swSelObj = swSelMgr.GetSelectedObject6(1, -1) : Debug.Assert(
Not swSelObj Is Nothing)

        vPIDarr = swModExt.GetPersistReference3(swSelObj) : Debug.Assert(
Not IsNothing(vPIDarr))

        Debug.Print(
"SelType = " & swSelMgr.GetSelectedObjectType3(1, -1))

        
If IsNothing(vPIDarr) Then
            Debug.Print("  ModelDocExtension::GetPersistReference3 broken")
            
Exit Sub
        Else
            Debug.Print("  ModelDocExtension::GetPersistReference3 exists")
        
End If

        sIDstring = GetStringFromID(swApp, swModel, vPIDarr)

        Debug.Print(
"  Persist Ref = " & sIDstring)

        swObj = GetObjectFromString(swApp, swModel, sIDstring)

        
If Not Nothing Is swObj Then
            Debug.Assert(swSelObj Is swObj)
            Debug.Print(
"  ModelDocExtension::GetObjectByPersistReference3 exists")
        
Else
            Debug.Print("  ModelDocExtension::GetObjectByPersistReference3 broken")
        
End If

    End Sub


    Public swApp As SldWorks


End Class



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:   Use Persistent Reference Example (VB.NET)
*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.