Hide Table of Contents

Use Persistent Reference Example (C#)

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.
//----------------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
namespace UsePersistentReference_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
public string GetStringFromID(SldWorks swApp, ModelDoc2 swModel, byte[] vPIDarr)
        {
            
string functionReturnValue = null;

            
foreach ( int vPID in vPIDarr) {
                functionReturnValue = functionReturnValue + vPID.ToString(
"###000");
            }
            
return functionReturnValue;

        }

        
public object GetObjectFromString(SldWorks swApp, ModelDoc2 swModel, string IDstring)
        {
            
object functionReturnValue = null;
            
ModelDocExtension swModExt = default(ModelDocExtension);
            
byte[] ByteStream = new byte[IDstring.Length/3];
            
object vPIDarr = null;
            
int nRetval = 0;
            
int i = 0;

            swModExt = swModel.Extension;
            
for (i = 0; i <= IDstring.Length - 3; i += 3)
            {
                
int j;
                j = i / 3;
                ByteStream[j] =
Convert.ToByte(IDstring.Substring(i, 3));
            }

            vPIDarr = ByteStream;

            functionReturnValue = swModExt.GetObjectByPersistReference3((vPIDarr),
out nRetval);

            
Debug.Assert((int)swPersistReferencedObjectStates_e.swPersistReferencedObject_Ok == nRetval);
            
Debug.Assert((functionReturnValue != null));
            
return functionReturnValue;

        }


        
public void Main()
        {
            
ModelDoc2 swModel = default(ModelDoc2);
            
ModelDocExtension swModExt = default(ModelDocExtension);
            
SelectionMgr swSelMgr = default(SelectionMgr);
            
object swSelObj = null;
            
object swObj = null;
            
byte[] vPIDarr = null;
            
string sIDstring = null;

            swModel = (
ModelDoc2)swApp.ActiveDoc;
            swModExt = swModel.Extension;
            swSelMgr = (
SelectionMgr)swModel.SelectionManager;
            swSelObj = swSelMgr.GetSelectedObject6(1, -1);
            
Debug.Assert((swSelObj != null));

            vPIDarr = (
byte[])swModExt.GetPersistReference3(swSelObj);
            
Debug.Assert((vPIDarr != null));

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

            
if ((vPIDarr == null))
            {
                
Debug.Print("  ModelDocExtension::GetPersistReference3 broken");
                
return;
            }
            
else
            {
                
Debug.Print("  ModelDocExtension::GetPersistReference3 exists");
            }

            sIDstring = GetStringFromID(swApp, swModel, vPIDarr);

            
Debug.Print("  Persist Ref = " + sIDstring);

            swObj = GetObjectFromString(swApp, swModel, sIDstring);

            
if ((null != swObj))
            {
                
Debug.Assert(object.ReferenceEquals(swSelObj, swObj));
                
Debug.Print("  ModelDocExtension::GetObjectByPersistReference3 exists");
            }
            
else
            {
                
Debug.Print("  ModelDocExtension::GetObjectByPersistReference3 broken");
            }

        }


        
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:   Use Persistent Reference 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.