Macro Force Insertion from a Selection Set

Hello

I'm working on Solidworks 2019 prenium... " Simulation " supplement

I want to make a macro that inserts a force in automatic. The idea...

  • Retrieve an already created selection set containing multiple faces
  • insert a force of 1N on each face of the selection set
  • " hide " the force as there can be more than 1000 faces in the selection game

I managed to make a force per side by putting the control in a loop but suddenly the simulation report becomes very heavy and unreadable.

Manually, when I create the simulation study, here is my method:

  • I select one of the faces of the selection set
  • I insert a force of 1N
  • I " hide " the force
  • I edit the force
    - I delete the previously selected face
    - I select the selection set (all sides of the set fit into a single force"

The idea will also be to do the same thing with the fixed support areas in a 2nd step...

Help! Thank you in advance to all the programming pros!

Hello
Here, the one who best masters the force is @OBI_WAN ! But I don't think he will help you, young padawan.

(sorry, I had to do it)

3 Likes

Hello;

Working on an assembly or part? And what type? (Beam(s),Low thickness, mechanically welded... other)

It would be interesting to have an idea of the geometry (1000 faces ???) on which you apply your force of 1N (1Newton, it's not much... Wouldn't it be easier to play with gravity, or with the masses?
… My curiosity won't mind a little screenshot ... or better the *.sld file... (Solidworks 2019 (that's old-fashioned: 2019) has the advantage to be able to be exploited by a large number of the forumists present here... :yum:

Kind regards.

1 Like

Thanks for the interest in the topic!
The static study is done either in a part file or on a part in an assembly.
In all cases, the part is a volume (extrusion) of about 1.6mm thick.
I can provide a sld file in private message if needed.

1000 faces, 1.6mm thick... Is it a piece of the type that is grilled, knurled, embossed?

The bets are off!
no it's the electronic card type

Arf I only know that... That explains the 1.6mm, I could have thought of it but for there to be 1000 faces, either it has a damn original shape, full of holes and cutouts, or you include the components but there it changes everything to the simulation.

I guess the fixed support points you're talking about are the areas where the PCB is held and fixed.

Is the purpose of the simulation to simulate a deformation of the PCB, if for example it is in a flexible case, or to simulate its deformation if an object presses on it?

@Sylk I metrise the Force and its extraordinary abilities, such as telekinesis, clairvoyance, and mind manipulation. I have been trained in the art of self-control and meditation.
but with solidworks we are in another world, only Dassault has control over it... in fact no I think that even we don't :rofl: :rofl: know :rofl:

6 Likes

FYI, here is the code I use to insert the force at each surface in the loop:

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub insert_force(DispatchObj1 As Object)

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
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()

DispArray = Array(DispatchObj1)             'Récupération de la surface

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)

' Redraw
Part.GraphicsRedraw2
Set StudyManagerObj = Nothing
Set ActiveDocObj = Nothing
Set CWAddinCallBackObj = Nothing
Set COSMOSWORKSObj = Nothing
End Sub

But how do you " group " several surfaces into a " batch " that would be called by the insert command???
Unless it's not the right logic??

Be careful for the code use the tag:
image
Otherwise the translation of bing does damage:
image
For the rest no ideas for your worries.

2 Likes

You can try " Selection list " but I don't know if you can apply it with " Simulation ":
https://help.solidworks.com/2019/english/api/sldworksapi/Selection_Lists_VB.htm
or

[Off-topic-ON]
you forgot the time travel @OBI_WAN ...
[Off-topic-OFF]

2 Likes

Maybe instead go through a " for", " until  " or " while " loop to collect/affect each face?

Or better, I'll let the macro pros answer. It's been too long since I've coded a line...

Hello fgauvreau,
Try this. It will run through the selection games and create a hidden force for everyone (though without the step of creating it just on the first side. I may not have understood the reason for this step)

Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
    Dim swModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swSelectionSetFolder As SldWorks.SelectionSetFolder
    Dim vSels As Variant
    Dim vSel As Variant
    Dim swSelSet As SldWorks.SelectionSet
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swFeat = swModel.FirstFeature

    While Not swFeat Is Nothing
        If swFeat.Name = "Selection Sets" Then
            Set swSelectionSetFolder = swFeat.GetSpecificFeature2
            vSels = swSelectionSetFolder.GetSelectionSets
            For Each vSel In vSels
                Set swSelSet = vSel
                Debug.Print "Nom du jeu de Selection: " & swSelSet.GetName
                swModel.ClearSelection2 True
                CreateForce swSelSet
            Next
        End If
        Set swFeat = swFeat.GetNextFeature
    Wend
End Sub

Sub CreateForce(swSelSet As SldWorks.SelectionSet)
    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

    vSelItems = swSelSet.GetSelectionSetItems
    vSelItemTypes = swSelSet.GetSelectionSetItemTypes

    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
3 Likes

Thank you very much!
That's exactly what I needed...
I was able to integrate the code into my macro by managing the value of the force according to the selection game and creating fixed supports on the same principle.

StudyObj.ShowOrHideForce = FalseTexte préformaté
nickel to lighten the treatment!

1 Like