Hi all
I'm new to the forum and I hope I'm in the right place to ask my question. I work under Solidworks 2018 and I would like to control configurations according to a variable.
I have a main assembly in which I don't want to have more than one configuration. This master assembly contains subassemblies and parts that have multiple configurations.
I have created equations in my main assembly that allow me to drive sketch dimensions belonging to the subassemblies and parts contained in the main assembly. All the equations are driven by 2 properties (Height and Length) that I modify manually according to my needs.
In addition to sketch dimensions, I need to control the configurations contained in the subassemblies and parts. I would like to do this by equations, because I'm not good at macro. However, I haven't found it and I don't know if it's possible.
Ex: If I enter a value less than or equal to 10 for the length and a value less than or equal to 2 for the height, I would like to display configuration A of subassembly 1 and configuration D of part 3 in the main assembly, or if I enter a value greater than 10 for length and a value greater than 2 for height, I would like to display configuration B of subassembly 1 and configuration A of part 3 in the main assembly.
Can someone tell me if this is possible and if so how?
Thanks in advance
Kind regards
Hello, You can use this macro that will ask you to enter the length and height, then change the values and change the configurations of assembly1 and part3
Option Explicit
Dim swModel As SldWorks.ModelDoc2
Sub main()
Dim swApp As SldWorks.SldWorks
Dim maLongueur As Double
Dim maHauteur As Double
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
maLongueur = InputBox("Entrer la longueur")
maHauteur = InputBox("Entrer la hauteur")
' change les variables de longueur et largeur
SetEquationValue "Longueur", maLongueur
SetEquationValue "Largeur", maHauteur
If maLongueur <= 10 And maHauteur <= 2 Then
' change la configuration de l'assemblage Assemblage1 à ConfigA
ChangeConfig "Assemblage1", "ConfigA"
' change la configuration de la pièce piece3 à ConfigD
ChangeConfig "piece3", "ConfigD"
Else
' change la configuration de l'assemblage Assemblage1 à ConfigB
ChangeConfig "Assemblage1", "ConfigB"
' change la configuration de la pièce piece3 à ConfigA
ChangeConfig "piece3", "ConfigA"
End If
swModel.ForceRebuild3 True
End Sub
Sub SetEquationValue(eqName As String, eqValue As Double)
Dim swEqMgr As SldWorks.EquationMgr
Set swEqMgr = swModel.GetEquationMgr
Dim i As Integer
For i = 0 To swEqMgr.GetCount - 1
Debug.Print swEqMgr.Equation(i)
Dim Name As String
Name = Split(swEqMgr.Equation(i), """")(1)
Debug.Print Name
If UCase(eqName) = UCase(Name) Then
swEqMgr.Equation(i) = """" & eqName & """=" & eqValue
Exit Sub
End If
Next
End Sub
Sub ChangeConfig(CompName As String, ConfigName As String)
Dim swConf As SldWorks.Configuration
Set swConf = swModel.GetActiveConfiguration
TraverseComponent swConf.GetRootComponent3(True), CompName, ConfigName
End Sub
Sub TraverseComponent(swParentComp As SldWorks.Component2, CompName As String, ConfigName As String)
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim i As Long
vChildComp = swParentComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
If InStr(swChildComp.Name2, CompName) > 0 Then
'Debug.Print swChildComp.Name2 & " <" & swChildComp.ReferencedConfiguration & ">"
swChildComp.ReferencedConfiguration = ConfigName
Exit Sub
End If
TraverseComponent swChildComp, CompName, ConfigName
Next
End Sub
1 Like
I can't edit my answer but the line: SetEquationValue "Width", myHeight
should be: SetEquationValue "Height", myHeight
Of course, you'll have to replace this name with the name of your height variable anyway (same for the length)
Apart from using a macro, the other possibility is to use a part family and to integrate the logic into the Excel file. Example
Hello JeromeP,
Thank you for your answer. I'm going to test your solution.
It's going to be new for me, I've already used small macros in Excel, but a first in Solidworks.
Kind regards