2 Replies Latest reply on Aug 20, 2018 2:27 PM by Keaton Warn

    How to change MergeFace?

    Keaton Warn

      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.

       

      Option Explicit
      
      Dim swApp           As Object
      Dim swModelDoc      As SldWorks.ModelDoc2
      Dim swFlatPattern   As SldWorks.FlatPatternFeatureData
      Dim swFeat          As SldWorks.Feature
      Dim swModel         As SldWorks.ModelDoc2
      
      Sub main()
      
          Set swApp = Application.SldWorks
          Set swModelDoc = swApp.ActiveDoc
          
          Set swFeat = swModel.FirstFeature
          Set swFlatPattern = swFeat.GetDefinition
          
          swFlatPattern.MergeFace = True
          
      End Sub
      

       

      EDIT: This is supposed to run on an already-open sheet metal part.

        • Re: How to change MergeFace?
          Alex Burnett

          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:

          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 = False Then
                      swFlatPattern.MergeFace = True
                      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
          
            • Re: How to change MergeFace?
              Keaton Warn

              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!