6 Replies Latest reply on Jul 11, 2016 9:28 PM by Amen Allah Jlili

    Add Equation to selected part

    Yannick Proulx

      Hi Guys,

      I'm trying to add an equation to a part.

      If I run my code when ModelDoc is swapp.activeDoc then everything work fine, my equations are added without any problem.

      If I try to add my equation from an assembly through the selectionManager it's not working.

      I get my modelDoc in an assembly through this way : swcomp2 = swSelMgr.GetSelectedObjectsComponent2(i)

      then convert to modelDoc2 by modelDoc = swcomp2.getmodeldoc2

      i get the swEquationMgr.GetCount to work so the equationMgr is correctly connected.

      but when i Try to add the equation I get -1 which mean equation was not added...


      is it possible that you cannot add an equation from else where that the activeDoc??


      thanks for your time




         Function link_equation(modeldoc As SldWorks.ModelDoc2, dimensionName As String, propertyName As String) As Boolean


              Dim swEquationMgr As SldWorks.EquationMgr = modeldoc.GetEquationMgr

              Dim EquationStr As String = """" & dimensionName & """ = """ & propertyName & """"


              If swEquationMgr Is Nothing Then

                  MsgBox("impossible d'atteindre le EquationMgr de la pièce " & modeldoc.GetPathName)

                  Return False

              End If

              Dim equationResult As Integer = 0


              Dim I As Integer

              Debug.Print("Equation Count : " & swEquationMgr.GetCount) '<------------------ work fine here I get the Equation count

              For i = 0 To swEquationMgr.GetCount - 1

                  If UCase(swEquationMgr.Equation(I)) = UCase(EquationStr) Then

                      Return True 'equation already exist

                  End If




                  If swEquationMgr.GetCount = 0 Then

                      If swEquationMgr.Add2(0, EquationStr, False) = -1 Then '<-------------------return 0 (ok) when modelDoc = activedoc and return -1 in assy

                          Debug.Print("equation not added")

                          Return False

                      End If


                      If swEquationMgr.Add2(-1, EquationStr, False) = -1 Then

                          Debug.Print("equation not added")

                          Return False

                      End If

                  End If

              Return True

          End Function