Create Library Feature Data Object and Library Feature With References (C#)
This example shows how to create a library feature with references in order
to position the library feature on a model. First you create the library
feature; then you access it to set references.
//-------------------------------------------
// Preconditions:
// 1. Model and library part exist.
// 2. Model is open.
// 3. Plane1 and Point1@Location Point sketch point features exist.
//
// Postconditions:
// 1. Library feature is defined and initialized.
// 2. Library feature is created on selected face on the model.
// 3. References are set for library feature to position
// it on the model.
//-------------------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using
System.Runtime.InteropServices;
namespace
CreateLibraryFeaturesWithReferencesCSharp.csproj
{
partial
class
SolidWorksMacro
{
public
void Main()
{
Feature swFeat;
ModelDoc2 swModel;
FeatureManager swFeatMgr;
LibraryFeatureData swLibFeat;
SelectionMgr swSelMgr;
bool boolstatus
= false;
object[] Refs =
null;
object RefTypes
= null;
int RefCount =
0;
object
selectedObject = null;
int k = 0;
int i = 0;
swModel = (ModelDoc2)swApp.ActiveDoc;
swSelMgr = (SelectionMgr)swModel.SelectionManager;
swFeatMgr = (FeatureManager)swModel.FeatureManager;
// Create library feature
swLibFeat = (LibraryFeatureData)swFeatMgr.CreateDefinition((int)swFeatureNameID_e.swFmLibraryFeature);
// Initialize newly created library
feature using the specified library part
boolstatus = swLibFeat.Initialize("C:\\MyLibraryParts\\SlotWithRefs.SLDLFP");
//modify this path
// Get the
type of references required for the library feature
RefCount = swLibFeat.GetReferencesCount();
Refs = (object[])swLibFeat.GetReferences2((int)swLibFeatureData_e.swLibFeatureData_FeatureRespect,
out RefTypes);
if ((RefTypes
!= null))
{
Debug.Print("Types
of references required: ");
int[]
RefType = (int[])RefTypes;
for (k =
RefType.GetLowerBound(0); k <= RefType.GetUpperBound(0); k++)
{
Debug.Print("
" + RefType[k].ToString());
}
}
// Set the name of the active library
feature configuration
swLibFeat.ConfigurationName =
"Default";
// Select the face where to create the
library feature
boolstatus = swModel.Extension.SelectByID2("",
"FACE",
-0.008955455851577, 0.01051592924882, 0.00400000000019,
false, 0,
null, 0);
// Create the library feature
swFeat = (Feature)swFeatMgr.CreateFeature(swLibFeat);
// Access the library feature to position
it on the part
swLibFeat =
null;
swLibFeat = (LibraryFeatureData)swFeat.GetDefinition();
boolstatus = swLibFeat.AccessSelections(swModel,
null);
// Select the point and plane where to
position the library feature
boolstatus = swModel.Extension.SelectByID2("Point1@Location
Point", "EXTSKETCHPOINT",
0, 0, 0, true, 0,
null, 0);
boolstatus = swModel.Extension.SelectByID2("Plane1",
"PLANE", 0, 0, 0,
true, 0,
null, 0);
int selCount =
0;
selCount = swSelMgr.GetSelectedObjectCount2(-1);
object[]
selectedObjects = new
object[selCount];
for (i = 0; i <
selCount; i++)
{
selectedObject = (object)swSelMgr.GetSelectedObject6(i
+ 1, -1);
selectedObjects[i] = selectedObject;
}
// Convert the .NET array to IDispatch
LibRefs = (DispatchWrapper[])ObjectArrayToDispatchWrapperArray((selectedObjects));
// Set the references for locating the
library feature on the part
swLibFeat.SetReferences(LibRefs);
// Update the definition of the library
feature
boolstatus = swFeat.ModifyDefinition(swLibFeat,
swModel, null);
swModel.ClearSelection2(true);
}
public
DispatchWrapper[]
ObjectArrayToDispatchWrapperArray(object[]
Objects)
{
int ArraySize =
0;
ArraySize = Objects.GetUpperBound(0);
DispatchWrapper[]
d = new
DispatchWrapper[ArraySize + 1];
int ArrayIndex
= 0;
for (ArrayIndex
= 0; ArrayIndex <= ArraySize; ArrayIndex++)
{
d[ArrayIndex] = new
DispatchWrapper(Objects[ArrayIndex]);
}
return d;
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
public
DispatchWrapper[]
LibRefs;
}
}