4 Replies Latest reply on Jan 30, 2018 11:06 PM by fuba

    VB.NET Variant Manager Problem

    thomas.feigenspan

      When I try to access Variant Manager via Automation to retrieve Variant Information I'm experiencing a strange behaviour under VX2.1. In the first run everything finishes successfully, but when I try a second time it fails...

       

      This is a sample script:

      VB.NET

      Private Sub VariantManager()

           Dim vmDocument As MGCVARIANTGUI.VMDocument

           Dim vmAddin As Object = PcbApplication.Addins("Variant Manager")

           If vmAddin Is Nothing Then Exit Sub

           vmAddin.Visible = True

           vmDocument = VmAddin.Control.VariantGUIApplication.VMDocument

           For i As Integer = 1 To vmDocument.Variants.Count

                With vmDocument.Variants.Item(i)

                     MsgBox( .Name & ", " & .Number & ", " & .Description )

                End With

           Next

      End Sub

      VBS

      Dim vmDocument, vmAddin

      Set vmAddin = Addins( "Variant Manager" )

      vmAddin.Visible = True

      Set vmDocument = vmAddin.Control.VariantGUIApplication.VMDocument

      For i = 1 To vmDocument.Variants.Count

          With vmDocument.Variants.Item(i)

              MsgBox( .Name & ", " & .Number & ", " & .Description )

          End With

      Next

       

      The VBS runs fine every time, the VB.NET fails at the second attempt (hangs at "For i As Integer = 1 To vmDocument.Variants.Count"). Then I have to restart Xpedition to make it work again, and it fails in the second try.

       

      Does anyone else have the same issue? Mentor doesn't help me, because they don't provide Automation VB.NET support.

        • 1. Re: VB.NET Variant Manager Problem
          Patrick.Cashman

          I copied and pasted your .net code directly into my test project.  I had to change the application variable but otherwise left it unchanged.  I ran it in a loop (from another subroutine, attached to a button) 20 times and it worked each time.

           

          I recommend that you check your references to MGCPCB and the MGCVARIANTGUI.  Make sure they are the latest versions. You may have a reference from an earlier version of Xpedition.

          • 2. Re: VB.NET Variant Manager Problem
            thomas.feigenspan

            Hi Patrick!

             

            Thanks a lot for looking into that. Thats very strange. I tested on different machines, Windows 7/10, NET Framework 2.0/4.5... every time the same problem. I've checked the references as you advised, re-added them to be sure, but they seem to be correct (COM version 41 with VX.2.1).

             

            Below is the code that seems to work. I'm not that much into programming, so I don't know what exactly does the trick.

             

            Private Sub GetVariantInfo()

                Dim vmApp As MGCVARIANTGUI.Application = Nothing

                Dim vmDoc As MGCVARIANTGUI.VMDocument = Nothing

                vmApp = VmgrApplication( PcbApplicationObject )

                If IsNothing( vmApp ) Then Exit Sub

                vmDoc = VmApp.VMDocument

                VmgrShow( PcbApplicationObject )

                For i As Integer = 1 To vmDoc.Variants.Count

                    With vmDoc.Variants.Item(i)

                        MsgBox( .Name & ", " & .Number & ", " & .Description )

                    End With

                Next

                VmgrHide( PcbApplicationObject )

            End Sub

             

            Private Sub VmgrShow( ByRef vxApp As MGCPCB.Application )

                Dim vmgr As Object = VmgrAddin( "Variant Manager", vxApp )

                If IsNothing( vmgr ) Then Exit Sub

                vmgr.Visible = True

            End Sub

             

            Private Sub VmgrHide( ByRef vxApp As MGCPCB.Application )

                Dim vmgr As Object = VmgrAddin( "Variant Manager", vxApp )

                If IsNothing( vmgr ) Then Exit Sub

                vmgr.Visible = False

            End Sub

             

            Private Function VmgrApplication( ByRef vxApp As MGCPCB.Application ) As MGCVARIANTGUI.Application

                Dim addin As Object = Nothing

                Dim vmGui As MGCVARIANTGUI.Application = Nothing

                addin = VmgrAddin( "Variant Manager", vxApp )

                If IsNothing( addin ) Then Return Nothing

                vmGui = CType( addin.Control.VariantGUIApplication, MGCVARIANTGUI.Application )

                If IsNothing( vmGui ) Then Return Nothing

                Return vmGui

            End Function

             

            Private Function VmgrAddin( AddinName As String, ByRef vxApp As MGCPCB.Application ) As Object

                For Each addin As Object In vxApp.Addins

                    If addin.Name = AddinName Then Return addin

                Next

                Return Nothing

            End Function

            • 3. Re: VB.NET Variant Manager Problem
              Patrick.Cashman

              Nothing about how you are doing it jumps out as a problem.  I'll share the code of how I do this; perhaps you can give it a try.

               

              First I declare the relevant variables globally because I use the VM in several different subroutines:

               

              Public vmdoc as MGCVARIANTGUI.VMDocument

              Public vmapp as MGCVARIANTGUI.Application

              Public addinObj

              Public Addins

               

              Then I use a routine like this to load the VM:

               

              Public Sub VM_load()

                   Addins = pcbapp.Addins

                   addinObj = Addins.item("Variant Manager")

                   addinObj.visible = True

                   vmapp = addinObj.Control.VariantGUIApplication

                   vmdoc = vmapp.VMDocument

               

                   'from here down is not needed to load the VM; it's just here so you verify what happens when it loads.

                   Debug.Print(vbCrLf & "Variants:")

                   For Each var In vmdoc.Variants

                        Debug.Print(var.Name)

                   Next

               

                   VM_Unload()

              End Sub

               

              You could take the extra stuff out and then it would just be this:

               

              Public Sub VM_load()

                   Addins = pcbapp.Addins

                   addinObj = Addins.item("Variant Manager")

                   addinObj.visible = True

                   vmapp = addinObj.Control.VariantGUIApplication

                   vmdoc = vmapp.VMDocument

              End Sub

               

              Here is the unload subroutine:

               

              Public Sub VM_Unload()

                   addinObj.visible = False

                   addinObj.Active = False

                   addinObj = Nothing

                   Addins = nothing

                   vmapp = Nothing

                   vmdoc = Nothing

              End Sub

               

              You might want to add some validation checks to be sure it loads properly, but otherwise it can stay very simple.

              • 4. Re: VB.NET Variant Manager Problem
                fuba

                Hello Thomas,

                not sure if your problem still persits, but I found that one in the Help Manual.

                Maybe you should "simplify/flatten" your code.

                 

                VarManWarning.PNG