API: Fix a component "only in this configuration"

Hello, I'm resuming a discussion about a problem already mentioned in this one below, but remained unanswered in my last post:

As this post is old, and marked as resolved, it does not attract attention.

The problem is to fix all the components, only in the active configuration.

It seems according to codestack that the API has a gap on this point, and it offers an alternative to invoke the command via windows directly.

For the general idea, it is treated HERE

For the particular application to the case that interests me, here is AN EXAMPLE

I guess

" Sub Main " may or may not perform the " fixcomponent " action on the series of components designated literally (part1-1 to part1-4), depending on whether the action is set to True or false

" Sub fixcomponent " details the procedure that is called in " Sub main " for each component.

First of all, am I wrong?

If not, I'd like to be able to replace the literal list of components with a loop that calls each top-level component in my assembly.

Does anyone know how to write this?

Thank you in advance.

PS: my goal is to create simplified configs of my assemblies. The best of the best would be, from the head assembly, to create a " Simplified " configuration for each sub-assembly, to remove the components that are not very important (typically the screws, the sorting can be done on the basis of the volume, with a threshold to be defined in mm3), and to fix the components. And to make an update if the config already exists, to release the components in this config, then re-fix them.

Thank you in advance

Hello
In the sample code, the True or False parameter is used to apply fixcomponent to the current conf or to all confs.

'**********************
'Copyright(C) 2022 Xarial Pty Limited
'Reference: Fix or float component in active or all configurations using SOLIDWORKS API
'License: License
'**********************

If thisConf Then
cmd = CMD_FixCompInThisConf
Else
cmd = CMD_FixCompInAllConf
End If

To loop in the tree, you must use the analysis functions of the typical tree: Traverse Assembly at Component Level Example (VBA) - 2021 - SOLIDWORKS API Help

2 Likes

Thank you Cyril for your message. In the example you cite, I don't see the "action" made by the macro, which I could replace with my command "fix in this config"

Is it, in 1st, "Sub Main" opens the document "C:\Users[...] \stepped_shaft.sldasm", then we activate the "Traverse components" module? But what happens as an action on each component?..
If that's right, I would have to ask in Sub Main to activate the "Simplified" configuration of the current document. if no "Simplified" configuration, then we don't do anything, end of the macro.

Hello
The API example only shows how to list components from the feature manager.
There is no other processing done, it only displays the path to the file and its record name using debug.print

1 Like

So instead of debug print, I paste what I identified as a command that suits me:
' Windows FloatCompInThisConf command:
SendMessage swApp.Frame(). GetHWnd(), &H111, 51609, 0
' Windows FloatCompInAllConf command:
SendMessage swApp.Frame(). GetHWnd(), &H111, 51608, 0
' Windows FixCompInThisConf command:
SendMessage swApp.Frame(). GetHWnd(), &H111, 51605, 0
' Windows Command: FixCompInAllConf:
SendMessage swApp.Frame(). GetHWnd(), &H111, 51611, 0

And if in Main I want to activate the config called "Simplified", are these few lines enough?

Dim instance As IModelDoc2
Dim ConfigurationName As String
Dim value As Boolean
 
value = instance.ShowConfiguration2(Simplifié)

It seems a bit short to me...

If I ask this question it's because, with mycad Integration, I always get an error when I ask for this action. I don't understand why... and since you have to put something of this "Sub Main", I thought it would be a solution

At first glance I would say yes. On the other hand, from memory, traverse component also lists the files present in repetitions and also other elements (depending on the type of file) so you have to play with this tree scanning procedure to manage the different cases and know when to stop processing or apply something else depending on the need.

Iterate Through All Configurations Example (VBA) - 2021 - SOLIDWORKS API Help

I thought I had progressed at the macro level, but I still have a long way to go. Not able to code the activation of the "Simplified" configuration of the current assembly... It deserves another topic, or can someone pass me a piece of code that works here?

I can provide a functional piece of code to activate a conf if no one goes before but here I'm on something else.

:+1:

Here's the snippet of code, just to enable a specific configuration of the current document

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sConfigName As String
Dim bShowConfig As Boolean

Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
sConfigName = "Simplifié"
bShowConfig = swModel.ShowConfiguration2(sConfigName)

End Sub

Supezr, I'll test that.
But my IT department just blocked me from accessing Codestack.
Could someone stick the code at this address for me?
Thanks again

'**********************
'Copyright(C) 2022 Xarial Pty Limited
'Reference: https://www.codestack.net/solidworks-api/document/assembly/components/fix-float/
'License: https://www.codestack.net/license/
'**********************

#If VBA7 Then
     Private Declare PtrSafe Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#Else
     Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#End If

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    
    Dim swAssy As SldWorks.AssemblyDoc
    
    Set swAssy = swApp.ActiveDoc
    
    FixComponent swAssy.GetComponentByName("Part1-1"), True
    FixComponent swAssy.GetComponentByName("Part1-2"), False
    FloatComponent swAssy.GetComponentByName("Part1-3"), True
    FloatComponent swAssy.GetComponentByName("Part1-4"), False
    
End Sub

Sub FixComponent(comp As SldWorks.Component2, thisConf As Boolean)

    Const CMD_FixCompInThisConf As Long = 51605
    Const CMD_FixCompInAllConf As Long = 51611
    
    If False <> comp.Select4(False, Nothing, False) Then
        
        Const WM_COMMAND As Long = &H111
        Dim cmd As Long
        
        If thisConf Then
            cmd = CMD_FixCompInThisConf
        Else
            cmd = CMD_FixCompInAllConf
        End If
        
        SendMessage swApp.Frame().GetHWnd(), WM_COMMAND, cmd, 0
    Else
        Err.Raise vbError, "", "Failed to select component"
    End If
    
End Sub

Sub FloatComponent(comp As SldWorks.Component2, thisConf As Boolean)
    
    Const CMD_FloatCompInThisConf As Long = 51609
    Const CMD_FloatCompInAllConf As Long = 51608
    
    If False <> comp.Select4(False, Nothing, False) Then
        
        Const WM_COMMAND As Long = &H111
        Dim cmd As Long
        
        If thisConf Then
            cmd = CMD_FloatCompInThisConf
        Else
            cmd = CMD_FloatCompInAllConf
        End If
        
        SendMessage swApp.Frame().GetHWnd(), WM_COMMAND, cmd, 0
    Else
        Err.Raise vbError, "", "Failed to select component"
    End If
    
End Sub

Otherwise tip goes through google translate (Google is never blocked by companies...) to go and see a blocked site, if the site doesn't contain too much java, you'll see the whole thing. (No need to translate, you can leave English in the translation...)

1 Like