Loading the QuickBuild ApplicationCognex VisionPro
What You'll Be Doing in This Step

In this step you'll build a Visual Basic application that loads a saved QuickBuild application. The important things to notice in this step are:

  • Specifying the assemblies your application uses
  • Defining class variables you'll use to refer to the QuickBuild application
  • Using the form Load and Closing methods to load and close the QuickBuild application
Creating the Application

Open Visual Studio and create a new Visual Basic Windows Application.

Note: This example uses Visual Basic, but you can write your application in C# as well.

For this topic, the name of the application is UsingQB.

Setting Up the Environment

In order to use VisionPro classes in your application you must reference the assemblies that define the classes you'll be using. For all VisionPro projects that use a QuickBuild application as their basis, you will need to reference the following assemblies:

  • Cognex.VisionPro
  • Cognex.VisionPro.Core
  • Cognex.VisionPro.QuickBuild.Core

Importing Namespaces

To make it easier to use the VisionPro classes in your application, you'll import the namespaces that you'll be using. Doing this makes your code easier to read and faster to enter.

Open the code for Form1.vb and add the lines in bold:


Imports Cognex.VisionPro
Imports Cognex.VisionPro.QuickBuild


Public Class Form1

End Class
Declare Object Variables

You'll need to declare some variables to maintain references to the saved QuickBuild application that you load and to the jobs it contains.

Declare the following three variables in the Form1 class:

Public Class Form1

  Private myJobManager As CogJobManager
  Private myJob As CogJob
  Private myIndependentJob As CogJobIndependent

End Class
Loading the QuickBuild Application

When you save a QuickBuild application, it is stored as a a VisionPro persistence (.vpp) file. This file contains all of the information needed to recreate the QuickBuild application exactly as you left it when you saved it in QuickBuild. In most cases, the most convenient time to load the saved application is in the form's Load method.

Select the Form1.vb [Design] tab, and double-click the form to create the Form1_Load method that runs when the form is loaded.

When you save a QuickBuild application, VisionPro saves it as a CogJobManager object. The LoadObjectFromFile method loads a saved VisionPro object from a file, but it returns it as a generic object. Use CType to cast the object to the appropriate type.

Note: The location of the example QuickBuild application is accurate if you installed VisionPro in the default location. If you installed it somewhere else you'll need to change the path name.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        myJobManager = CType(CogSerializer.LoadObjectFromFile("C:\Program Files\Cognex\VisionPro\Samples\Programming\QuickBuild\advancedAppOne.vpp"), CogJobManager)
        myJob = myJobManager.Job(0)
        myIndependentJob = myJob.OwnedIndependent

        myJobManager.UserQueueFlush()
        myJobManager.FailureQueueFlush()
        myJob.ImageQueueFlush()
        myIndependentJob.RealTimeQueueFlush()

    End Sub

A QuickBuild application can have more than one job, but in this example, the application uses only one job. By calling myJobManager.Job(0) you get a reference to the first, and only job, in this QuickBuild application. The next line myJob.OwnedIndependent gets a reference to a CogJobIndependent object which provides access to more information about the job.

The last step of loading a saved QuickBuild application is to make sure that all the queues are empty. Remember that loading a VisionPro persistence file recreates the saved object exactly as it was when it was saved. It's a good idea to flush all the queues just in case there was data stored in one of them.

Closing Gracefully

The CogJobManager object that represents your QuickBuild application creates several threads while it is running. It is important to shut down the CogJobManager.

The Form1_FormClosing method gets called as the form closes. This is where you should call Shutdown.

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    myJobManager.Shutdown()
End Sub
Running the Program

At this point, your QuickBuild-based Visual Basic applications does nothing but load the QuickBuild application and waits until you close the window to exit.

In the next step, you'll add user interface elements to the program.