Hi,
I’m trying to get the assembly pattern’s children components but unsuccessfully.
The code snippet below gets the children successfully (generic method):
private void GetChildren()
{
Feature selectedObject = (Feature)swModel.ISelectionManager.GetSelectedObject5(1);
object[] components = (object[])selectedObject.GetChildren();
}
Here is the complemented code that’s got error on conversion:
private void GetChildren()
{
Feature selectedObject = (Feature)swModel.ISelectionManager.GetSelectedObject5(1);
object[] components = (object[])selectedObject.GetChildren();
foreach (object item in components)
{
Component2 component = (Component2)item; // conversion error
}
}
The specific assembly’s pattern interfaces do not support any method for getting the children components. Here’s a snippet to gain access to a linear pattern assembly feature:
Feature selectedObject = (Feature)swModel.ISelectionManager.GetSelectedObject5(1);
LocalLinearPatternFeatureData pattern = (LocalLinearPatternFeatureData)selectedObject.GetDefinition();
Forget C# for a second and spin up a new VBA macro.
- Select a patterned component instance;
- Run a macro that prints the type of the selected objects types;
- Inspect the type of the selected objects from the docs, which will tell you the API objects you can inefer from the ISelectionMgr.GetSelectedObject.
By selecting a patterned component instance will return as component type from ISelectionMgr.GetSelectedObject method.
So, if you get the array from IFeature::GetChildren
method for the selected pattern feature then it will return an array size of patterned component instances but every array index is an object type. Then if you try to convert that object to component then you will get error. That’s the point.
On the other hand I have been searching for another method to get the relation between the pattern feature and the patterned component instance.
Not too sure I understand your post. Ideal thing is to post some VBA code with an assembly that has some virtual patterned components for anyone to be replicate your issue.
IFeature.GetChildren does not return components, it returns an array of subfeatures.
1 Like
Here’s a VBA code snippet for testing:
// Preconditions:
// 1. Open or make an assembly then pattern any component.
// 2. Select the newly created pattern feature.
// 3. Open the imediate window and examine the debug prints.
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.IModelDoc2
Dim swFeature As SldWorks.IFeature
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeature = swModel.ISelectionManager.GetSelectedObject5(1)
Dim swSubFeature As SldWorks.IFeature
Set swSubFeature = swFeature.IGetFirstSubFeature
Dim swParentFeature As Variant
While Not swSubFeature Is Nothing
swParentFeature = swSubFeature.GetParents
Debug.Print "Component instance: " & swSubFeature.Name
Debug.Print "Feature name: " & swParentFeature(0).Name
Debug.Print "Seed component: " & swParentFeature(1).Name
Set swSubFeature = swSubFeature.IGetNextSubFeature
Wend
End Sub
It’s possible to get the relation between the patterned component and its feature.
Here’s a code snippet based on the assembly’s components.
I’ve figured out the patterned component instance is threated as a feature.
Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
Dim swAssembly As SldWorks.IAssemblyDoc
Dim swFeature As SldWorks.IFeature
Set swApp = Application.SldWorks
Set swAssembly = swApp.ActiveDoc
Dim components As Variant
components = swAssembly.GetComponents(True)
Dim component As SldWorks.IComponent2
Dim i As Integer
For i = 0 To UBound(components)
Set component = components(i)
If component.IsPatternInstance Then
Dim featureName As String
featureName = GetFeatureName(component.Name)
Debug.Print "Component name: " & component.Name, "Pattern feature name: " & featureName
End If
Next
End Sub
Private Function GetFeatureName(ByVal componentName As String) As String
Dim swModel As SldWorks.IModelDoc2
Set swModel = swApp.ActiveDoc
Dim features As Variant
features = swModel.FeatureManager.GetFeatures(True)
Dim feature As Variant
For Each feature In features
If feature.GetTypeName2 = "ReferencePattern" And feature.Name = componentName Then
Dim parentFeature As Variant
parentFeature = feature.GetParents
GetFeatureName = parentFeature(0).Name
Exit For
End If
Next
End Function