Anyone has an example or a tip?
Regards
Jorn Bjarning
' Open an assembly
Dim swApp As Object
Dim Part As Object
Dim SelectionManager As Object
Dim Feature As Object
Dim FeatureData As Object
Set SwApp = Application.Sldworks
Set Part = SwApp.Activedoc
Set SelectionManager = Part.SelectionManager
'1. Find the linear component pattern in tree. Select it with your
mouse.
Set Feature = SelectionManager.GetSelectedObject5(1)
2. Get FeatureData object for Feature.
Set FeatureData = Feature.GetDefinition
3. Make an array of which components are patterned.
'Select new component in tree
Dim newComponents(0) As Variant
newComponents(0) = selectionmanager.GetSelectedObject5(1)
4. Edit the feature
FeatureData.AccessSelections Part, Nothing
FeatureData.SeedComponentArray = newComponents
Feature.ModifyDefinition FeatureData, Part, Nothing
Pretty straightforward.
I have tried your example with addition of 2 lines where I select a
pattern and the component to add to the pattern (while testing). I get
the error: "Run-time error '438': Object doesn't support this property
or method" in the line "newComponents(0) =
SelectionManager.GetSelectedObject5(1)".
How come?
Public Sub Main()
' Open an assembly
Dim swApp As Object
Dim Part As Object
Dim SelectionManager As Object
Dim Feature As Object
Dim FeatureData As Object
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelectionManager = Part.SelectionManager
'Select the linear component pattern in tree.
Part.Extension.SelectByID2 "LocalLPattern1", "COMPPATTERN", 0, 0,
0, False, 0, Nothing, 0
Set Feature = SelectionManager.GetSelectedObject5(1)
'Get FeatureData object for Feature.
Set FeatureData = Feature.GetDefinition
'Make an array of which components are patterned. Select component
to add to pattern
Part.Extension.SelectByID2 "Part3-1@Assem1", "COMPONENT", 0, 0, 0,
False, 0, Nothing, 0
Dim newComponents(0) As Variant
newComponents(0) = SelectionManager.GetSelectedObject5(1)
'Edit the feature
FeatureData.AccessSelections Part, Nothing
FeatureData.SeedComponentArray = newComponents
Feature.ModifyDefinition FeatureData, Part, Nothing
End Sub
Then because you have an object array you need to store it in a variant
instead.
Dim vNewComponents as Variant
vNewComponents = newComponents
Then in your modify call:
FeatureData.SeedComponentArray = newComponents
Unforunately I was messing around with this a bit and it looks like the
api is broken in sw2006 at least. I can't add any new components to a
pattern. Sorry for the bad news!
You also have to call
DerivedPatternFeatureData.ModifyDefinition to release the selections while
applying the change.
The () is for COM solutions. And as I stated above it works when I
remove a component from the array (by reducing the array size). As I
see it I am either missing something when adding a component to the
pattern or the API is broken here.
Thanks for your input anyway Corey. Any new ideas/comments?
Case closed... Thank you all for your input.