I've trying to make a macro that switches the "merge faces" option to "True" in sheet metal flat patterns. However, I keep getting a Run-time error 91. I think I set all of the objects but maybe I'm missing something obvious.
<p>Option Explicit</p><p></p><p>Dim swApp As Object</p><p>Dim swModelDoc As SldWorks.ModelDoc2</p><p>Dim swFlatPattern As SldWorks.FlatPatternFeatureData</p><p>Dim swFeat As SldWorks.Feature</p><p>Dim swModel As SldWorks.ModelDoc2</p><p></p><p>Sub main()</p><p></p><p> Set swApp = Application.SldWorks</p><p> Set swModelDoc = swApp.ActiveDoc</p><p> </p><p> Set swFeat = swModel.FirstFeature</p><p> Set swFlatPattern = swFeat.GetDefinition</p><p> </p><p> swFlatPattern.MergeFace = True</p><p> </p><p>End Sub</p>
EDIT: This is supposed to run on an already-open sheet metal part.
SolidworksApi/macros
There are a couple issues.
1) You are creating swModel and swModelDoc as ModelDoc2 objects. You are only defining swModelDoc but then you try and use swModel (this causes error 91)
2) The first feature of your feature tree is not a sheetmetal part, it is likely the comments folder. You need to loop through the feature tree to find the type you're looking for before you set an object to that specific variable type.
Try this code:
Merge faces set to false, probably can be done cleaner but works.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swFlatPattern As SldWorks.FlatPatternFeatureData
Dim swFeat As SldWorks.Feature
Dim swModel As SldWorks.ModelDoc2
Dim stringType As String
Dim retVal As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeat = swModel.FirstFeature
Do While Not swFeat Is Nothing
stringType = swFeat.GetTypeName2
'Debug.Print stringType
If stringType = "FlatPattern" Then
Exit Do
End If
Set swFeat = swFeat.GetNextFeature
Loop
If Not swFeat Is Nothing Then
Set swFlatPattern = swFeat.GetDefinition
'If swFlatPattern.MergeFace = True Then
swFlatPattern.MergeFace = False
retVal = swFeat.ModifyDefinition(swFlatPattern, swModel, Nothing)
Debug.Print "Merge Faces Changed: " & retVal
Else
Debug.Print "Merge Faces Change Not Needed"
End If
'End If
End Sub
would there be a way to change the code to uncheck merge faces.
1) I guess I derped and didn't realize I basically had a double initialization.
2) For whatever reason, it was not obvious to me that the tree was being looped through. It seemed like there was some sort of specific command to get access to this specific feature, but the loop method makes a lot of sense given some of the examples I saw where the code had to loop through the tree.
Thank you for the clear explanation. Cheers!