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 do 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

Thank you very much for your answer and the time spent.
I admit to being " a little " below your level in programming...
I couldn't use your files because I'm currently using the 2019 version.

The macro works if the selection sets are at the assembly level.

Manually, you have to select the selection set that is in the room and create a new selection set.
But how do you transcribe this into code???

We have to edit and change the references to use those of 2019 instead of 2022.

Hello

The Solidworks API Help does not mention any functions that can create a selection set (ISselectionSetFolder). Neither in a part, nor in an assembly.
A possible solution would be to select the game entities included in the room, and keep them in an array, to apply effort to them.

Disadvantage: the selection set is not kept at the level of the assembly construction tree for possible later use, allowing the entities to be easily found.

To conclude, I have a hard time seeing the interest of this approach compared to the functions of SW Simulation which allow multiple selections when creating efforts.

I have a macro that groups part surfaces according to certain criteria in selection sets.
It's normal to have trouble understanding the point of such a request if you only have a few forces to insert... But imagine you have several hundred... :slight_smile:

Hello @fgauvreau ,

Hundreds!? Quick, an image to illustrate and appreciate the thing... :smile:

That being said, I understand better your interest in selection sets.

Contrary to what I mentioned earlier, it is possible to create/delete selection sets in VBA. The functions are in the IModelDocExtension interface of the document. They are called SaveSelection and DeleteSelection2.

All that remains is to imagine a small management module that would allow you to select " SetSelection " belonging to parts, then insert them into the assembly, one by one, or grouped together...