Hide Table of Contents

Use Persistent Reference Example (VBA)

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 if the currently selected item has a persistent reference and if that object can be retrieved from its persistent reference.

 

'---------------------------------------------------------

'

' Preconditions:

'       (1) Part, assembly, or drawing is open.

'       (2) Something is selected.

'

' Postconditions: None

'

'-----------------------------------------------------------

Option Explicit

Public Enum swSelectType_e

    swSelNOTHING = 0

    swSelEDGES = 1              '  "EDGE"

    swSelFACES = 2              '  "FACE"

    swSelVERTICES = 3           '  "VERTEX"

    swSelDATUMPLANES = 4        '  "PLANE"

    swSelDATUMAXES = 5          '  "AXIS"

    swSelDATUMPOINTS = 6        '  "DATUMPOINT"

    swSelOLEITEMS = 7           '  "OLEITEM"

    swSelATTRIBUTES = 8         '  "ATTRIBUTE"

    swSelSKETCHES = 9           '  "SKETCH"

    swSelSKETCHSEGS = 10        '  "SKETCHSEGMENT"

    swSelSKETCHPOINTS = 11      '  "SKETCHPOINT"

    swSelDRAWINGVIEWS = 12      '  "DRAWINGVIEW"

    swSelGTOLS = 13             '  "GTOL"

    swSelDIMENSIONS = 14        '  "DIMENSION"

    swSelNOTES = 15             '  "NOTE"

    swSelSECTIONLINES = 16      '  "SECTIONLINE"

    swSelDETAILCIRCLES = 17     '  "DETAILCIRCLE"

    swSelSECTIONTEXT = 18       '  "SECTIONTEXT"

    swSelSHEETS = 19            '  "SHEET"

    swSelCOMPONENTS = 20        '  "COMPONENT"

    swSelMATES = 21             '  "MATE"

    swSelBODYFEATURES = 22      '  "BODYFEATURE"

    swSelREFCURVES = 23         '  "REFCURVE"

    swSelEXTSKETCHSEGS = 24     '  "EXTSKETCHSEGMENT"

    swSelEXTSKETCHPOINTS = 25   '  "EXTSKETCHPOINT"

    swSelHELIX = 26             '  "HELIX" (is this wrong?)

    swSelREFERENCECURVES = 26   '  "REFERENCECURVES"

    swSelREFSURFACES = 27       '  "REFSURFACE"

    swSelCENTERMARKS = 28       '  "CENTERMARKS"

    swSelINCONTEXTFEAT = 29     '  "INCONTEXTFEAT"

    swSelMATEGROUP = 30         '  "MATEGROUP"

    swSelBREAKLINES = 31        '  "BREAKLINE"

    swSelINCONTEXTFEATS = 32    '  "INCONTEXTFEATS"

    swSelMATEGROUPS = 33        '  "MATEGROUPS"

    swSelSKETCHTEXT = 34        '  "SKETCHTEXT"

    swSelSFSYMBOLS = 35         '  "SFSYMBOL"

    swSelDATUMTAGS = 36         '  "DATUMTAG"

    swSelCOMPPATTERN = 37       '  "COMPPATTERN"

    swSelWELDS = 38             '  "WELD"

    swSelCTHREADS = 39          '  "CTHREAD"

    swSelDTMTARGS = 40          '  "DTMTARG"

    swSelPOINTREFS = 41         '  "POINTREF"

    swSelDCABINETS = 42         '  "DCABINET"

    swSelEXPLVIEWS = 43         '  "EXPLODEDVIEWS"

    swSelEXPLSTEPS = 44         '  "EXPLODESTEPS"

    swSelEXPLLINES = 45         '  "EXPLODELINES"

    swSelSILHOUETTES = 46       '  "SILHOUETTE"

    swSelCONFIGURATIONS = 47    '  "CONFIGURATIONS"

    swSelOBJHANDLES = 48

    swSelARROWS = 49            '  "VIEWARROW"

    swSelZONES = 50             '  "ZONES"

    swSelREFEDGES = 51          '  "REFERENCE-EDGE"

    swSelREFFACES = 52

    swSelREFSILHOUETTE = 53

    swSelBOMS = 54              '  "BOM"

    swSelEQNFOLDER = 55         '  "EQNFOLDER"

    swSelSKETCHHATCH = 56       '  "SKETCHHATCH"

    swSelIMPORTFOLDER = 57      '  "IMPORTFOLDER"

    swSelVIEWERHYPERLINK = 58   '  "HYPERLINK"

    swSelMIDPOINTS = 59

    swSelCUSTOMSYMBOLS = 60     '  "CUSTOMSYMBOL"

    swSelCOORDSYS = 61          '  "COORDSYS"

    swSelDATUMLINES = 62        '  "REFLINE"

    swSelROUTECURVES = 63

    swSelBOMTEMPS = 64          '  "BOMTEMP"

    swSelROUTEPOINTS = 65       '  "ROUTEPOINT"

    swSelCONNECTIONPOINTS = 66  '  "CONNECTIONPOINT"

    swSelROUTESWEEPS = 67

    swSelPOSGROUP = 68          '  "POSGROUP"

    swSelBROWSERITEM = 69       '  "BROWSERITEM"

    swSelFABRICATEDROUTE = 70   '  "ROUTEFABRICATED"

    swSelSKETCHPOINTFEAT = 71   '  "SKETCHPOINTFEAT"

    swSelEMPTYSPACE = 72        '  

    swSelCOMPSDONTOVERRIDE = 72

    swSelLIGHTS = 73            '  "LIGHTS"

    swSelWIREBODIES = 74

    swSelSURFACEBODIES = 75     '  "SURFACEBODY"

    swSelSOLIDBODIES = 76       '  "SOLIDBODY"

    swSelFRAMEPOINT = 77        '  "FRAMEPOINT"

    swSelSURFBODIESFIRST = 78

    swSelMANIPULATORS = 79      '  "MANIPULATOR"

    swSelPICTUREBODIES = 80     '  "PICTURE BODY"

    swSelSOLIDBODIESFIRST = 81

    swSelDOWELSYMS = 86         '  "DOWELSYM"

    swSelEXTSKETCHTEXT = 88     '  "EXTSKETCHTEXT"

    swSelBLOCKINST = 93         '  "BLOCKINST"

    swSelFTRFOLDER = 94         '  "FTRFOLDER"

    swSelSKETCHREGION = 95      '  "SKETCHREGION"

    swSelSKETCHCONTOUR = 96     '  "SKETCHCONTOUR"

    swSelBOMFEATURES = 97       '  "BOMFEATURE"

    swSelANNOTATIONTABLES = 98  '  "ANNOTATIONTABLES"

    swSelBLOCKDEF = 99          '  "BLOCKDEF"

    swSelCENTERMARKSYMS = 100   '  "CENTERMARKSYMS"

    swSelCENTERLINES = 103      '  "CENTERLINE"

    swSelHOLETABLEFEATS = 104   '  "HOLETABLE"

    swSelHOLETABLEAXES = 105    '  "HOLETABLEAXIS"

    swSelWELDMENT = 106         '  "WELDMENT"

    swSelSUBWELDFOLDER = 107    '  "SUBWELDMENT"

    swSelEXCLUDEMANIPULATORS = 111

    swSelREVISIONTABLE = 113    '  "REVISIONTABLE"

    swSelBODYFOLDER = 118       '  "BDYFOLDER"

    swSelREVISIONTABLEFEAT = 119

    swSelEVERYTHING = -3

    swSelLOCATIONS = -2

    swSelUNSUPPORTED = -1

'   swSelEVERYTHING = 4294967293

'   swSelLOCATIONS = 4294967294

'   swSelUNSUPPORTED = 4294967295

End Enum

Public Enum swPersistReferencedObjectStates_e

    swPersistReferencedObject_Ok = 0

    swPersistReferencedObject_Invalid = &H1

    swPersistReferencedObject_Suppressed = &H2

    swPersistReferencedObject_Deleted = &H4

End Enum

Function GetStringFromID ( swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, vPIDarr As Variant )As String

    Dim vPID                        As Variant

    For Each vPID In vPIDarr

        Debug.Assert vbByte = VarType(vPID)

        GetStringFromID = GetStringFromID & Format(vPID, "###000")

    Next vPID

End Function

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

    Dim swModExt                    As SldWorks.ModelDocExtension

    Dim ByteStream()                As Byte

    Dim vPIDarr                     As Variant

    Dim nRetval                     As Long

    Dim i                           As Long

    

    Set 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

    

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

    Debug.Assert swPersistReferencedObject_Ok = nRetval

    Debug.Assert Not GetObjectFromString Is Nothing

    

End Function

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swModExt                    As SldWorks.ModelDocExtension

    Dim swSelMgr                    As SldWorks.SelectionMgr

    Dim swSelObj                    As Object

    Dim swObj                       As Object

    Dim nRetval                     As Long

    Dim vPIDarr                     As Variant

    Dim sIDstring                   As String

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swModExt = swModel.Extension

    Set swSelMgr = swModel.SelectionManager

    Set swSelObj = swSelMgr.GetSelectedObject5(1): Debug.Assert Not swSelObj Is Nothing

    

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

    

    Debug.Print "SelType = " & swSelMgr.GetSelectedObjectType(1)

    

    If IsEmpty(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

    

    Set 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

'---------------------------------------



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 (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) 2011 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.