Using QuickBuild Projects in .NET ApplicationsCognex VisionPro

VisionPro supports loading a persisted QuickBuild application in your own .NET application. It assumes that you are familiar with the organization of QuickBuild applications, in particular with the concepts of jobs and queues. To learn more about QuickBuild see Using QuickBuild.

The first section gives you an overview of the the QuickBuild application is organized from the point of view of the VisionPro API. The second section illustrates the process with code examples.

Note: In the QuickBuild API, the term User Queue corresponds to the Posted Items List of the QuickBuild GUI.

How the QuickBuild Application is Organized

As described in Using QuickBuild, each QuickBuild application consists of one or more jobs. The application contains one User Queue that you can configure to provide data from any of the jobs. Typically, the User Queue contains data that you want to display in the user interface of the .NET application you are writing.

Each job has its own Realtime Queue. Like the User Queue, you can configure the Realtime Queue to provide data from any of the vision tools in the job. The Realtime Queue contains data that you want to use to control I/O lines.

To get data out of either the User Queue or a Realtime Queue, you write an event handler that gets invoked when a new queue element is available. To keep the GUI from taking too much processing time and to simplify the threading model, this example uses a timer to specify when to get elements from the User Queue.

Each of the queue element results are stored in a record that you index with the same path you specified when you configured the queue. See Configuring the Posted Items List.

The following table lists the QuickBuild application's elements and their corresponding VisionPro .NET classes. One thing to keep in mind is that general information about a job is stored in a CogJob object, but specific information about a job's state and its queues is stored in a CogJobIndependent object that you access through the job's OwnedIndependent property.

Table 1. QuickBuild GUI Elements and .NET Counterparts
QuickBuild Application ElementCorresponding .NET Object
QuickBuild application documentCogJobManager
JobCogJob and CogJobIndependent
User QueueCogJobManager.UserResultAvailable Event
Realtime QueueCogJobIndependent.RealTimeResultAvailable Event
Using the VisionPro QuickBuild API

This section shows you how to:

  • Load a persisted QuickBuild application
  • Set up the event handlers for the Realtime Queues and the User Queue timer
  • Write the event handlers to extract information from the queues
  • Start and stop the QuickBuild application
Saving the QuickBuild Application

Once you have configured your QuickBuild application, you can save it using the File->Save QuickBuild Application menu command. For best loading performance use the default binary formatter option. The QuickBuild application is saved as a VisionPro persistence (.vpp) file.

Keep in mind that the saved file preserves the state of the QuickBuild application including:

  • Acquisition settings
  • Image file settings
  • User Queue contents
  • Realtime Queue contents

This means that if you move your application to another machine, the acquisition source should be the same in both the development machine and in the deployment machine. Also, if you save your application without clearing the result queues first, your .NET application will take those results from the queues before any newly created results.

Loading the Saved QuickBuild Application

To load a persisted QuickBuild application, use LoadObjectFromFile, casting the result to a CogJobManager object as shown in the following code:

using Cognex.VisionPro.QuickBuild;

CogJobManager _jobManager;

_jobManager = (CogJobManager) CogSerializer.LoadObjectFromFile("C:/qbapp.vpp");

If you want to make sure that your application does not use any saved contents of the result and image queues, you can flush them after you load the QuickBuild application.

_jobManager.UserQueueFlush();
_jobManager.FailureQueueFlush(); 
for (int i = 0; i < _jobManager.JobCount; i++) {
    _jobManager.Job(i).OwnedIndependent.RealTimeQueueFlush();
    _jobManager.Job(i).ImageQueueFlush();
}
Setting Up the Event Handlers

As the QuickBuild application runs, it places the data you specify in either the User Queue or in the Realtime Queue of each job.

To avoid problems that may arise from interaction between the user interface thread and the worker thread in C#, this example uses a timer to determine when results should be posted on the user interface. The sample program ...\Samples\Programming\QuickBuild\EfficientApp shows a way of handling GUI updates that doesn't use a timer.

To extract data from the User Queue, you declare a timer, create an event for it, and start the timer as in the following code. You will need to determine the timer interval empirically for your application. A smaller interval will result in more frequent updates but may impact the overall performance. In this example the timer interval is set to 200 milliseconds.

System.Windows.Forms.Timer timer1;

timer1.Interval = 200;
timer1.Tick += new EventHandler(timer1_Tick);

...

timer1.Start();

The Tick event handler looks like this:

private void timer1_Tick(object sender, EventArgs e) {
    ICogRecord record = _jobManager.UserResult();
    ICogRecords subRecords = record.SubRecords;

    int count = (int) subRecords["Tools.Item[1].CogBarcodeTool.Results.Count"].Content;

    txtResultCount.Text = "Results: " + count;
    txtJobName.Text = (string) subRecords["Tools.Item[1].CogBarcodeTool.Results.Item[0].OwnedDecoded.String"].Content;

    try {
        cogDisplay1.Image = (CogImage8Grey) subRecords["Tools.Item[1].CogBarcodeTool.InputImage"].Content;
    }
    catch (System.Exception ex) {
        Console.WriteLine(ex.Message);
    }
}

The string that you use to index the record (for example "Tools.Item[1].CogBarcodeTool.Results.Count") is the path that you specified when you configured the queue. The job manager also provides the following record names: JobName, UserResultTag, RunStatus to the user results. See Configuring the Posted Items List.

Note: In your application, you should check the values of the records before using them. Results that failed to be collected will be set to null.

To extract data from a job's realtime queue, you set up an event handler for each job.

_jobManager.Job(0).OwnedIndependent.RealTimeResultAvailable += 
    new CogJobIndependent.CogRealTimeResultAvailableEventHandler(myRealTimeResultsHandler);

private void myRealTimeResultsHandler(object sender, CogJobActionEventArgs e) {
    CogJobIndependent theJob = (CogJobIndependent) sender;
    ICogRecord record = theJob.RealTimeResult();
    ICogRecords subRecords = record.SubRecords;
    string decodability = "unavailable";
    Cognex.VisionPro.Barcode.CogBarcodeGradeConstants grade;

    try {
        grade = (Cognex.VisionPro.Barcode.CogBarcodeGradeConstants) subRecords["Tools.Item[1].CogBarcodeTool.Results.Item[0].OwnedGraded.Decodability"].Content;
        decodability = grade.ToString();
    }
    catch (System.Exception ex) {
        Console.WriteLine(ex.Message);
    }

    txtDecodability.Text = decodability;
}
Running the Application

Once you have loaded the QuickBuild application and set up the result handlers, you are ready to run the application. To run it once, use the Run method:

_jobManager.Run();

To run the application continuously, use the RunContinuous method:

_jobManager.RunContinuous();
Stopping the Application

To stop a running application, use the Stop method:

_jobManager.Stop();

Before you exit the enclosing .NET application, shut down the QuickBuild job manager with Shutdown:

_jobManager.Shutdown();