Inserting Forces on the Surface of a Part in an Assembly

I created a macro to automatically insert forces from a surface selection set into a part.
In some cases, I need to do the same thing but in the context of an assembly. The selection sets are always in one of the parts of the assembly. The command works but no surface is selected...

I'll put the code used for info:

Public Sub CreateForce200(swSelSet As SldWorks.SelectionSet)
    Dim swApp As Object
    Set swApp = Application.SldWorks
    
    Dim COSMOSWORKSObj As Object
    Dim CWAddinCallBackObj As Object
    
    Set CWAddinCallBackObj = swApp.GetAddInObject("CosmosWorks.CosmosWorks")
    Set COSMOSWORKSObj = CWAddinCallBackObj.CosmosWorks

    Dim ActiveDocObj As Object
    Dim StudyManagerObj As Object
    Dim LoadsAndRestraintsManagerObj As Object
    Dim ErrorCodeObj As Long
    Dim ContactManagerObj As Object
    Set ActiveDocObj = COSMOSWORKSObj.ActiveDoc()
    Set StudyManagerObj = ActiveDocObj.StudyManager()
    Dim StudyObj As Object
    Set StudyObj = StudyManagerObj.GetStudy(0)
    Set LoadsAndRestraintsManagerObj = StudyObj.LoadsAndRestraintsManager()

    Dim vSelItems As Variant
    Dim vSelItemTypes As Variant
    Dim swSelItem As SldWorks.SelectionSetItem
    Dim swFace As SldWorks.Face2
    Dim j As Integer
    Dim errors As Long
    

    vSelItems = swSelSet.GetSelectionSetItems
    vSelItemTypes = swSelSet.GetSelectionSetItemTypes

    Debug.Print "Nom du jeu de Selection: " & swSelSet.GetName
    
    'Contrôle pièce activée
    'Dim swPart As SldWorks.PartDoc
    'Set swPart = swApp.ActivateDoc3(nameCarte2, True, swRebuildOnActivation_e.swUserDecision, errors)
    

    Dim DispArray As Variant
    Dim cnt As Long
    cnt = UBound(vSelItems)
    Dim myArray()
    ReDim Preserve myArray(cnt)

    For j = 0 To cnt
        Set swSelItem = vSelItems(j)
        If vSelItemTypes(j) = swSelectType_e.swSelFACES Then
            Set myArray(j) = swSelItem.GetCorrespondingItem
        End If
    Next
    DispArray = myArray

    Dim CWForceObj As Object
    Dim DistanceValues As Variant
    Dim ForceValues As Variant
    Dim ComponentValues As Variant
    Dim data(6) As Double
    data(0) = 1: data(1) = 1: data(2) = 1: data(3) = 1: data(4) = 1: data(5) = 1
    ComponentValues = data

    Set CWForceObj = LoadsAndRestraintsManagerObj.AddForce3(1, 0, -1, 0, 0, 0, (DistanceValues), (ForceValues), 0, False, 0, 0, 0, 1, (ComponentValues), False, False, (DispArray), Nothing, False, ErrorCodeObj)

    StudyObj.ShowOrHideForce = False

    Set StudyManagerObj = Nothing
    Set ActiveDocObj = Nothing
    Set CWAddinCallBackObj = Nothing
    Set COSMOSWORKSObj = Nothing
End Sub

Hello @fgauvreau ,

My conclusion on the origin of this refusal to create an effort: the addresses of the objects returned by the " Items " of the selection set do not point to the faces of the selection. It seems necessary to use the " GetCorrespondingItem " method of the item to get the correct address.

I would like to add that the number and variety of parameters of the " AddEffort3 " function does not help with exploration...

I propose a working solution in the attached macro, in which I have tried to use CW-typed variables, rather than "object " types, by adding the " Solidworks Simulation 20XX type library " to the list of references.
The parameters of the effort are a little different from those of your initial proposal (effort with direction reference), but the macro works and can be used as a basis for comparison to try to identify malfunctions.

The macro uses two approaches to selecting objects, either with the classic functions of the model's SelectionManager or with the " Items " of the selection set.

Unfortunately, none of them will work if the selection set belongs to a room. Its selection does not give the same behavior as when it belongs to the assembly. Although identified, the entities are not compliant (?) and the " AddEffort3 " function returns an error code.

Macro attachment, as well as the assembly document used for my testing (SW2022). The selection set has 3 flat faces.

Chute.zip (537.0 KB)
CWCreationEffort.swp (71.5 KB)

2 Likes