Macro improvement change unfolded side on sheet metal

Hello
I'm looking to perfect a macro that is used very regularly.
This macro is very functional on all recent sheet metal parts but does not work on older parts (without sheet metal backrest)
If anyone has an idea to make it functional for the 2 attached sheets.

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim selManager As SldWorks.SelectionMgr


Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set selManager = swModel.SelectionManager
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'code
    
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim swFace1 As Face2
    Do While swFace1 Is Nothing
        Set swFace1 = selManager.GetSelectedObject6(1, -1)
        DoEvents
    Loop
    
    set_fixed_face get_flat_feature(swFace1.GetBody), swFace1
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'code

    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Sub
Public Function get_flat_feature(bod As Body2) As Feature
    Dim featurmgr As FeatureManager
    Set featurmgr = swModel.FeatureManager
    Dim flatpaternfolder As FlatPatternFolder
    Set flatpaternfolder = featurmgr.GetFlatPatternFolder()
    Dim flatfeatures As Variant
    flatfeatures = flatpaternfolder.GetFlatPatterns()
    Dim sFlatPatternFeatureData As FlatPatternFeatureData
    Dim face As Face2
    Dim feat As Variant
    For Each feat In flatfeatures
        Set sFlatPatternFeatureData = feat.GetDefinition()
        Set face = sFlatPatternFeatureData.FixedFace2
        If face.GetBody.Name = bod.Name Then
            Set get_flat_feature = feat
            Exit Function
        End If
    Next
End Function

Public Sub set_fixed_face(feat As Feature, face As Face2)
    Dim sFlatPatternFeatureData As FlatPatternFeatureData
    Set sFlatPatternFeatureData = feat.GetDefinition()
    sFlatPatternFeatureData.AccessSelections swModel, Nothing
    sFlatPatternFeatureData.FixedFace2 = face
    feat.ModifyDefinition sFlatPatternFeatureData, swModel, Nothing
End Sub

The original subject or @Lynkoa15 had found me the current solution functional at 92% remains the 8% of older part.
New.SLDPRT (245.2 KB)
Old.SLDPRT (578.4 KB)
In the documents that do not have the unfolded State file:
image
No matter how hard I look, I can't make my selection of feat to then apply the face to him.

No idea if it helps, but impossible to unfold by hand at home (SW 2024 SP01) without going through the " Unfold " function, usually we prefer the " Flatten " function but not functional in this case. Even converting your file to my current version.

1 Like

Hello @sbadenis
Indeed the function get_flat_feature() directly targets the unfolded functions folder (in order to optimize performance) however this folder does not seem to exist for these old parts, I suggest you to go through all the functions using this function,

Public Function get_flat_feature2(bod As Body2) As Feature
    Dim featurmgr As FeatureManager
    Set featurmgr = swModel.FeatureManager
    
    Dim sFlatPatternFeatureData As FlatPatternFeatureData
    Dim face As Face2
    
    Dim feat As Feature
    Set feat = swModel.FirstFeature
    While Not feat Is Nothing
        If feat.GetTypeName2() = "FlatPattern" Then
            Set sFlatPatternFeatureData = feat.GetDefinition()
            Set face = sFlatPatternFeatureData.FixedFace2
            If face.GetBody.Name = bod.Name Then
                Set get_flat_feature2 = feat
                Exit Function
            End If
        End If
        
        Set feat = feat.GetNextFeature
    Wend
End Function

Roman, try undoing the deletion of the folds (sometimes sw doesn't do it automatically by selecting unfolded state)
Capture (SW22 tested)

2 Likes

It works, hats off!

2 Likes

Yes, my macro in debug mode had removed these functions. Nothing too serious.

Thank you @Lynkoa15 I had tried something similar but without succeeding in making it work.
For the time being, it is now perfectly functional.
My automatic drawing for sheet metal parts now includes one more string to its bow.
Thank you!

1 Like