Reading Tool Information from Tool RecordsCognex VisionPro

This topic describes how to read tool information from tool records. VisionPro tools use records to expose images, graphics, and state information on request. Tool records are organized hierarchically, and each record may contain one or more subrecords which you access through a key. You can use the information contained in vision tool records to configure your inspection processes, understand results, and diagnose problems that occur.

You can find information about records and record keys for each vision tool About Record Keys

Before You Begin

Make sure you have installed VisionPro. Your application needs to use the objects and interfaces defined in the Cognex.VisionPro and Cognex.VisionPro.Blob assemblies and the Cognex Display control.

Add Display Code

Your program needs to be able to display images. The sample code in this topic assumes that your form has a display control named cdDisplay.

Create a CogAcqFifoTool and CogBlobTool

Create module-level CogAcqFifoTool and CogBlobTool objects. You use the CogAcqFifoTool to acquire images from an acquisition FIFO on a frame grabber and the CogBlobTool to perform blob analysis, which yields result graphic records.

' Create a CogAcqFifoTool and CogBlobTool

Private mAcqTool As New CogAcqFifoTool
Private mBlobTool As New CogBlobTool
Acquire an Image, Extract Blobs

To create some interesting tool record information to work with, acquire an image and perform a blob analysis on it. The CogBlobTool uses as input the output of the CogAcqFifoTool. Add this and all following code to the Click event of a Visual Basic command button:

' Acquire an image and extract blobs from it

mAcqTool.Run()
mBlobTool.InputImage = mAcqTool.OutputImage
mBlobTool.Run()
Get the Tool's LastRunRecord

You can obtain a record that reflects the results of the last call to a tool's Run method using the CreateLastRunRecord method of the tool's ICogTool interface. The LastRunRecord contains a hierarchy of subrecords each of which holds cached references to the tool's input images at the time that you called Run. You retrieve the input image record using its key value "InputImage."

' Get the LastRunRecord for the blob tool

Dim Tool As Cognex.VisionPro.ICogTool
Tool = mBlobTool
Dim LastRunRecord As CogRecord
LastRunRecord = Tool.CreateLastRunRecord

' Select the InputImage subrecord

Dim InputImageRecord As CogRecord
InputImageRecord = LastRunRecord.SubRecords("InputImage")
Display the Input Image Record Content

To display the input image subrecord, set the display control's Image property to the InputImage record's Content property. You should also clear the display's static graphics collection.

' Display the image

cdDisplay.Image = InputImageRecord.Content

' Clear previous static graphics

cdDisplay.StaticGraphics.Clear()
Read the Tool Records

You can iteratively read a tool's records and subrecords and select the record content you want to manipulate. In this example, you cycle through the input image subrecords, looking for those that contain a result graphics collection. You then display the content by adding it to the Display control's static graphics.

' Iterate the subrecords of the InputImage record.
' If the record contains a graphics collection,
' add it to the display's static graphic collection

Dim SubRecord As CogRecord
For Each SubRecord In InputImageRecord.SubRecords
  If GetType(Cognex.VisionPro.CogGraphicCollection).IsAssignableFrom(SubRecord.ContentType) Then
    cdDisplay.StaticGraphics.AddList(SubRecord.Content, "recordgraphics")
  End If
Next SubRecord
Code Listing

The following code summarizes the previous steps.

Private mAcqTool As New CogAcqFifoTool
Private mBlobTool As New CogBlobTool

Private Sub Command1_Click()
  mAcqTool.Run()
  mBlobTool.InputImage = mAcqTool.OutputImage
  mBlobTool.Run()

  Dim Tool As Cognex.VisionPro.ICogTool
  Tool = mBlobTool
  Dim LastRunRecord As CogRecord
  LastRunRecord = Tool.CreateLastRunRecord
  Dim InputImageRecord As CogRecord
  InputImageRecord = LastRunRecord.SubRecords("InputImage")

  cdDisplay.Image = InputImageRecord.Content
  cdDisplay.StaticGraphics.Clear()

  Dim SubRecord As CogRecord
  For Each SubRecord In InputImageRecord.SubRecords
    If GetType(Cognex.VisionPro.CogGraphicCollection).IsAssignableFrom(SubRecord.ContentType) Then
      cdDisplay.StaticGraphics.AddList(SubRecord.Content, "recordgraphics")
    End If
  Next SubRecord
End Sub