Set and Get Sheet Metal Part's Persistent Reference IDs Example (C#)
This example shows how to set and get persistent reference IDs on a
sheet metal part.
The entities in flattened and unflattened (folded) states of sheet metal
in SOLIDWORKS do not have the same properties, making it difficult to
track entities across states of sheet metal.
Together these methods provide a way to track entities across sheet
metal states.
//------------------------------------------
// Preconditions:
// 1. Specified part document exists.
// 2. Open the Immediate window.
// 3. Run the macro.
//
// Postconditions:
// 1. Opens specified part document.
// 2. Unsuppresses the Flat-Pattern1 feature.
// 3. Gets the PIDs of the selected face on the
// Flat-Pattern1 feature.
// 4. Prints the PIDs of the selected face to the
// the Immediate window.
// 5. Suppresses the Flat-Pattern1 feature.
// 6. Uses the persistent reference IDs to
// retrieve and highlight the array of objects
// that comprise the previously selected face in the
// folded state of the part.
//
// NOTE: Because this part document is used elsewhere,
// do not save any changes when closing it.
//--------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
namespace EditSuppress2.csproj
{
partial class SolidWorksMacro
{
ModelDoc2 swModel;
ModelDocExtension swModelDocExt;
SelectionMgr swSelectionMgr;
Face2 swFace;
SelectData swSelectData;
byte[] pid;
object selObj;
object[] swObjList;
string fileName;
int errors;
int warnings;
bool boolstatus;
int i;
int j;
public void Main()
{
// Open the specified sheet metal part
fileName = "C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS\\samples\\design portfolio\\sheet_metal_bracket.sldprt";
swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
// Get flat-pattern feature
swModelDocExt = (ModelDocExtension)swModel.Extension;
boolstatus = swModelDocExt.SelectByID2("Flat-Pattern1", "BODYFEATURE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
// Unsuppress (unfold) flat-pattern feature
boolstatus = swModel.EditUnsuppress2();
// Select the top face on the flattened sheet metal part
boolstatus = swModelDocExt.SelectByID2("", "FACE", -0.00101040556784, 0.08553215906153, 0.001499999999965, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
swSelectionMgr = (SelectionMgr)swModel.SelectionManager;
swFace = (Face2)swSelectionMgr.GetSelectedObject6(1, -1);
// Get the persistent reference IDs for the
// the selected face on the flattened sheet
// metal part
pid = (byte[])swModelDocExt.GetFlattenSheetMetalPersistReference(swFace);
// Print the PID values to the Immediate window
for (j = pid.GetLowerBound(0); j <= pid.GetUpperBound(0); j++)
{
Debug.Print("PID = " + pid[j]);
}
// Suppress (fold) flat-pattern feature
boolstatus = swModelDocExt.SelectByID2("Flat-Pattern1", "BODYFEATURE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
swModel.EditSuppress2();
// Use the persistent reference IDs to
// retrieve an array of objects that comprise
// the entity in the folded state of the part
swObjList = (object[])swModelDocExt.GetSheetMetalObjectsByPersistReference((pid), out errors);
if ((swObjList != null))
{
swModel.ClearSelection2(true);
for (i = swObjList.GetLowerBound(0); i <= swObjList.GetUpperBound(0); i++)
{
selObj = swObjList[i];
if (selObj == null)
{
Debug.Print("Persistent reference ID conversion error.");
return;
}
else
{
SelectObject(selObj, true);
}
}
}
Debug.Print("The entities that comprise the previously selected entity in the folded state are selected.");
}
private void SelectObject(object selObj, bool append)
{
if (selObj is Entity)
{
Entity selObj1;
selObj1 = (Entity)selObj;
swSelectData = (SelectData)swSelectionMgr.CreateSelectData();
selObj1.Select4(append, swSelectData);
}
else
{
Debug.Print("Need selection handle.");
}
}
/// <summary>
/// The SldWorks swApp variable is pre-assigned for you.
/// </summary>
public SldWorks swApp;
}
}