Hello again, here is my code and the test part as an example
Sub essay01()
ChangeVariableValueInQuaAnd RebuildPart "D1@Ext", "80"
ChangeVariableValueInQuaAndRebuildPart "D1@Esq", "44.6"
End Sub
Sub ChangeVariableValueInQuaAndRebuildPart(NomVar as String, ValVar As String)
Note: an equation is an alphanumeric string and is treated as such with the usual VBA functions.
'Declaration Objects *****************************
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swEquaMgr As SldWorks.EquationMgr
'Assignment of Objects*****************************
Set swApp = Application.SldWorks ' swApp is now an object containing the SW application.
Set swModel = swApp.ActiveDoc ' SwModel is now an object containing the active model (part or assembly) SW.
Set swEqua = swModel.GetEquationMgr ' swEquaMgr is now an object containing the set of equations of the model present in feature manager.
'Declaration of local variables******************
Dim NumEqua As Integer 'NumEqua is the Number of the equation
Dim NbEqua As Integer 'NbEqua is the Number of Equations. If NbEqua=0 then there is an equation, if NbEqua=1 then there are 2 equations, if NbEqua= -1 then there is no equation.
Dim ChScind As Variant 'ChScind is a Split Chain.
Const Title01 As String = "Constat (MC)"
Variable Allocation
NbEqua = swEqua.GetCount - 1
'Procedure ************************************
For NumEqua = 0 TB NbEqua' For NumEqua ranging from 0 to NbEqua (in steps of 1 by default)
ChScind = Split(swEqua.equation(NumEqua), "=") 'ChScind(0) now contains the part of the equation to the left of the "=" sign. ChScind(1) now contains the part of the equation to the right of the "=" sign
ChScind(0) = Replace(ChScind(0), Chr(34), Empty) 'Systematic replacement of the first and last character of Scind(0) (with ascii number 34) by nothing (empty), in other words we remove all the chr(34) ["] of Scind(0) (this is a formatting conditioning the following test).
ChScind(0) = Replace(ChScind(0), Chr(32), Empty) 'Systematic replacement of the characters of Scind(0) (with the ascii number 32) by nothing (empty), in other words we remove all the spaces [" "] of Scind(0) (this is a formatting conditioning the following test).
If ChScind(0) = VarName, Then
swEqua.equation(NumEqua) = Replace(swEqua.equation(NumEqua), ChScind(1), ValVar)
If swModel.EditRebuild3() Then' if the rebuild of the part returns true then
MsgBox "Reconstructed part with new value in equation!", vbInformation, Title01
Else' otherwise
MsgBox "Modified equation but part reconstruction error!", vbInformation, Title01
End If 'end of if
Exit Sub ' leave this Sub
End If
Next NumEqua
If NbEqua = -1 Then
MsgBox "No equation is associated with the model!", vbExclamation, Title01
Else
MsgBox "No equation modification possible!" & Chr$(13) & Chr$(13) & "'" & VarName & "'" & " was not found in any of them.", vbExclamation, Title01
End If
End Sub 'end of sub
A+
part_essai01.sldprt