AnsweredAssumed Answered

Recorded pattern creation macro fails

Question asked by Chris McRae on May 6, 2020
Latest reply on May 7, 2020 by Chris McRae

Hi,

 

I recorded a macro to create a curve-driven body pattern. That worked, but replaying the macro doesn't actually create any new pattern.

 

On inspection, the code seems ok. Can anyone show me how to automate curve-driven pattern creation?

 

See the attached model and the code below.

 

To be clear, note that the code was generated by recording a macro while manually creating the "CrvPattern1" feature. I tried rolling back the model to just above that pattern feature and running the macro - it does not create a new pattern. I also tried rolling to the end and then deleting the "CrvPattern1" feature before running the macro - still didn't work.

 

Here is the code -

 

' ******************************************************************************
' C:\Users\me\AppData\Local\Temp\swx6488\Macro1.swb - macro recorded on 05/06/20 by me
' ******************************************************************************
Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("3DSketch8", "SKETCH", 0, 0, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByRay(1.60201298865559E-03, -0.016458764315928, 4.10462218350744E-02, 0.543337693150041, -0.525670105752014, -0.654564810481787, 4.64346677787231E-04, 2, True, 1024, 0)
boolstatus = Part.Extension.SelectByID2("Boss-Extrude1", "SOLIDBODY", 0, 0, 0, True, 256, Nothing, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("3DSketch8", "SKETCH", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByRay(1.60201298865559E-03, -0.016458764315928, 4.10462218350744E-02, 0.543337693150041, -0.525670105752014, -0.654564810481787, 4.64346677787231E-04, 2, True, 1024, 0)
boolstatus = Part.Extension.SelectByID2("Boss-Extrude1", "SOLIDBODY", 0, 0, 0, True, 256, Nothing, 0)
Dim swFeat As Object
Dim swFeatMgr As Object

Set swFeatMgr = Part.FeatureManager
Dim swFeatData As Object
Set swFeatData = swFeatMgr.CreateDefinition(swFeatureNameID_e.swFmCurvePattern)
swFeatData.D1AlignmentMethod = 0
swFeatData.D1CurveMethod = 0
swFeatData.D1InstanceCount = 7
swFeatData.D1IsEqualSpaced = True
swFeatData.D1ReverseDirection = False
swFeatData.D1Spacing = 0.05
swFeatData.D2InstanceCount = 1
swFeatData.D2IsEqualSpaced = False
swFeatData.D2PatternSeedOnly = False
swFeatData.D2ReverseDirection = False
swFeatData.D2Spacing = 0.05
swFeatData.Dir2Specified = False
Set swFeat = swFeatMgr.CreateFeature(swFeatData)
End Sub

I tried adding some debug code to verify the selection, which seems fine. The code below was inserted before line 24 of the macro listed above.

 

Debug.Print "total select object count: " & swSelMgr.GetSelectedObjectCount2(-1)
    Dim k As Long
    For k = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        Debug.Print "select object type and mark for selection " & k & ": " & swSelMgr.GetSelectedObjectType3(k, -1) & " - " & swSelMgr.GetSelectedObjectMark(k)
    Next

I also tried using the other elements of the pattern definition (according to the docs), that didn't work for me either. To do this, I inserted the code block below after the CreateDefinition line in the macro above.

 

    swFeatData.BodyPattern = True
'    swFeatData.PatternElement = 2 ' swBodiesToPattern
''
'    Dim bodies(1) As Body2
'    Set bodies(0) = seed_body
'    swFeatData.PatternBodyArray = bodies

 

Thanks for your help!

Outcomes