Create Library Feature Data Object and Library Feature With References (VB.NET)
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 defined and initialized.
' 2. Library feature created on selected face on the model.
' 3. References set for library feature to position
' it on the model.
'-------------------------------------------
Imports
SolidWorks.Interop.sldworks
Imports
SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Imports
System.Runtime.InteropServices
Partial
Class SolidWorksMacro
Public
Sub Main()
Dim swFeat
As Feature
Dim swModel
As ModelDoc2
Dim swFeatMgr
As FeatureManager
Dim swLibFeat
As LibraryFeatureData
Dim swSelMgr
As SelectionMgr
Dim boolstatus
As
Boolean
Dim
selectedObjects() As
Object
Dim
Refs As
Object =
Nothing
Dim
RefTypes As
Object =
Nothing
Dim
RefType As
Object
Dim
RefCount As
Integer
Dim
LibRefs() As
DispatchWrapper
swModel = swApp.ActiveDoc
swSelMgr = swModel.SelectionManager
swFeatMgr = swModel.FeatureManager
' Create library feature
swLibFeat = swFeatMgr.CreateDefinition(swFeatureNameID_e.swFmLibraryFeature)
' Initialize newly created library feature
using the specified library part
boolstatus = swLibFeat.Initialize("C:\MyLibraryParts\SlotWithRefs.SLDLFP")
' Get the type of
references required for the library feature
RefCount = swLibFeat.GetReferencesCount
Refs = swLibFeat.GetReferences2(swLibFeatureData_e.swLibFeatureData_FeatureRespect,
RefTypes)
If
Not IsNothing(RefTypes)
Then
Debug.Print("Types
of references required: ")
For
Each refType
In RefTypes
Debug.Print(vbTab + CStr(refType))
Next
End
If
' 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,
Nothing, 0)
' Create the library feature
swFeat = swFeatMgr.CreateFeature(swLibFeat)
' Access the library feature to position it
on the part
swLibFeat = Nothing
swLibFeat = swFeat.GetDefinition
boolstatus = swLibFeat.AccessSelections(swModel,
Nothing)
' Select the point and plane where to
position the library feature
boolstatus = swModel.Extension.SelectByID2("Point1@Location
Point", "EXTSKETCHPOINT",
0, 0, 0, True, 0,
Nothing, 0)
boolstatus = swModel.Extension.SelectByID2("Plane1",
"PLANE", 0, 0, 0,
True, 0,
Nothing, 0)
Dim selCount
As
Integer
selCount = swSelMgr.GetSelectedObjectCount2(-1)
ReDim
selectedObjects(selCount)
Dim i
As
Integer
For
i = 0 To (selCount - 1)
selectedObjects(i) = swSelMgr.GetSelectedObject6(i + 1, -1)
Next i
ReDim
Preserve selectedObjects(selCount
- 1)
' Convert the .NET array to IDispatch
LibRefs =
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, Nothing)
swModel.ClearSelection2(True)
End
Sub
Function
ObjectArrayToDispatchWrapperArray(ByVal
Objects As
Object())
As DispatchWrapper()
Dim ArraySize
As
Integer
ArraySize = Objects.GetUpperBound(0)
Dim d(ArraySize)
As DispatchWrapper
Dim ArrayIndex
As
Integer
For
ArrayIndex = 0 To ArraySize
d(ArrayIndex) = New
DispatchWrapper(Objects(ArrayIndex))
Next
Return
d
End
Function
'''
<summary>
''' The SldWorks swApp
variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End
Class