Files
Get IFeature object from component
Code
Module:
'Demonstrates how to get the IFeature object from an IComponent2 object.
'Precondition: component is selected.
'Note: If the component isn't fully loaded into memory, this method will _
return Nothing.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObject6(1, -1)
Dim swFeat As SldWorks.Feature
Set swFeat = GetComponentsFeature(swApp, swComp)
If Not swFeat Is Nothing Then
swApp.SendMsgToUser "Feature object obtained."
'Re-select, just to prove that correct feature was obtained.
swFeat.Select False
End If
End Sub
Function GetComponentsFeature(swApp As SldWorks.SldWorks, swComp As SldWorks.Component2) As Feature
Dim swParentComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim swFeat As SldWorks.Feature
Dim vSplit As Variant
vSplit = Split(swComp.Name2, "/")
Set swParentComp = swComp.GetParent
If swParentComp Is Nothing Then
Set swCompModel = swApp.ActiveDoc
Else
Set swCompModel = swParentComp.GetModelDoc2
If swCompModel Is Nothing Then
Set GetComponentsFeature = Nothing
Exit Function
End If
End If
Set swFeat = swCompModel.FirstFeature
Do While Not swFeat Is Nothing
If swFeat.Name = vSplit(UBound(vSplit)) Then
Set GetComponentsFeature = swFeat
Exit Function
End If
Set swFeat = swFeat.GetNextFeature
Loop
Set GetComponentsFeature = Nothing
End Function