Select component's face in assembly

I’m trying to show the flat pattern’s fixed face for any selected sheetmetal part in the assembly.

I already tried the method swEntity.Select3 the return value is True but nothing is selected.

1 Like

You need to post some code with an example assembly.

Got it!

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swComp As SldWorks.Component2
Dim swEnt As SldWorks.Entity

Sub Main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

Dim Sheetmetal_Flatpattern_Name As String

If swModel.GetType = swDocPART Then
        
    Sheetmetal_Flatpattern_Name = Get_Flatpattern_Name(swModel)
Else
    
    Set swComp = swModel.SelectionManager.GetSelectedObjectsComponent2(1)
    
    Sheetmetal_Flatpattern_Name = Get_Flatpattern_Name(swComp.GetModelDoc2)
       
    Set swEnt = swComp.GetCorrespondingEntity(swEnt)
End If

swEnt.Select False
End Sub

Private Function Get_Flatpattern_Name(ByVal Ref_Model As SldWorks.ModelDoc2) As String

Dim Ref_Feat As SldWorks.Feature
Dim Ref_SheetMetal As SldWorks.SheetMetalFeatureData
Dim Ref_FlatPattern As SldWorks.FlatPatternFeatureData
Dim Ref_Face As SldWorks.Face2

Set Ref_Feat = Ref_Model.FirstFeature

Do While Not Ref_Feat Is Nothing

    If Ref_Feat.GetTypeName2 = "FlatPattern" Then
        Get_Flatpattern_Name = Ref_Feat.name
        Exit Do
      End If

    Set Ref_Feat = Ref_Feat.GetNextFeature
Loop

Set Ref_FlatPattern = Ref_Feat.GetDefinition

Ref_FlatPattern.AccessSelections Ref_Model, Nothing

Set Ref_Face = Ref_FlatPattern.FixedFace2

Ref_FlatPattern.ReleaseSelectionAccess

Set swEnt = Ref_Face
Set swEnt = swEnt.GetSafeEntity
End Function

This was the missing line in the code:
Set swEnt = swComp.GetCorrespondingEntity(swEnt)

1 Like

It seems like you’re attempting to write a macro in SolidWorks to select the fixed face of a sheet metal part. Here’s a basic example of how you could achieve this:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swFlatPatternFeature As SldWorks.FlatPatternFeature
Dim swSelMgr As SldWorks.SelectionMgr

Sub Main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
        If swModel.GetType = swDocPART Then
            Set swPart = swModel
            If swPart.IsSheetMetal Then
                Set swFlatPatternFeature = swPart.FeatureByName("Flat-Pattern1") ' Change "Flat-Pattern1" to match your flat pattern feature name
                If Not swFlatPatternFeature Is Nothing Then
                    swFlatPatternFeature.Select2 True, 0 ' Select the flat pattern feature
                    Set swSelMgr = swModel.SelectionManager
                    Dim selectedFace As SldWorks.Face2
                    Set selectedFace = swSelMgr.GetSelectedObject6(1, -1) ' Get the selected face
                    If Not selectedFace Is Nothing Then
                        MsgBox "Selected face: " & selectedFace.Name
                    Else
                        MsgBox "No face selected"
                    End If
                Else
                    MsgBox "Flat pattern feature not found"
                End If
            Else
                MsgBox "Selected part is not a sheet metal part"
            End If
        Else
            MsgBox "Selected document is not a part"
        End If
    Else
        MsgBox "No active document"
    End If
End Sub