VisionPro Advanced
Section 1: Scripting
Objectives:
Use scripting to complete application specific tasks
Review ToolBlock scripting and its advantages over ToolGroup scripting
Compare ToolGroup solution to ToolBlock scripting solution
Solutions for ToolBlock and ToolGroup Scripting
ToolBlock Example 1 – Converting from Radians to Degrees
Public Overrides Function GroupRun(ByRef message As String, ByRef result As
CogToolResultConstants) As Boolean
'Convert radians to degrees
Me.Outputs.Degrees = (Me.Inputs.Radians * 180) / 3.1415927
Return false
End Function
ToolBlock Example 2 – Save Failed Images
'Declaring variable for running count
Dim running As Integer
Public Overrides Function GroupRun(ByRef message As String, ByRef result As
CogToolResultConstants) As Boolean
'Declaring variables for filename
Dim fname As String
If (Me.Inputs.RunStatus <> Cognex.VisionPro.CogToolResultConstants.Accept) Then
'Build the filename
fname = System.String.Format("c:\SavedImages\badimage{0:d}.bmp", running)
'Perform save on the image
Me.Inputs.Image.ToBitmap.Save(fname)
'Increment counter
running = running + 1
End If
Section 1 Lab Exercise
-1-
Return false
End Function
ToolGroup Example 3 – Blob Bounding Box Center
Imports System
Imports Cognex.VisionPro
Imports Cognex.VisionPro.ToolGroup
Imports Cognex.VisionPro.Blob
Public Class UserScript
Inherits CogToolGroupBaseScript
'Declare variables for the 2 BlobResults and x & y outputs of the 2 bounding box blob
results
Private blobresult0 as New CogBlobResult
Private blobresult1 as New CogBlobResult
Private blob0x As Double
Private blob0y as Double
Private blob1x As Double
Private blob1y as Double
'The GroupRun function is called when the tool group is run. The default
'implementation provided here is equivalent to the normal behavior of the
'tool group. Modifying this function will allow you to change the behavior
'when the tool group is run.
Overrides Function GroupRun(ByRef message As String, _
ByRef result As CogToolResultConstants) _
As Boolean
'Capture blob results from inputs and place them in the CogBlobResults
blobresult0 =
CType(MyBase.toolGroup.GetScriptTerminalData("BlobResult0"),CogBlobResult)
blobresult1 =
CType(MyBase.toolGroup.GetScriptTerminalData("BlobResult1"),CogBlobResult)
'Extract the bounding box x & y coordinates and place them into the outputs
blob0x = blobresult0.GetBoundingBox(CogBlobAxisConstants.PixelAligned).CenterX
blob0y = blobresult0.GetBoundingBox(CogBlobAxisConstants.PixelAligned).CenterY
blob1x = blobresult1.GetBoundingBox(CogBlobAxisConstants.PixelAligned).CenterX
blob1y = blobresult1.GetBoundingBox(CogBlobAxisConstants.PixelAligned).CenterY
Section 1 Lab Exercise
-2-
MyBAse.toolGroup.SetScriptTerminalData("Blob0X", blob0x)
MyBAse.toolGroup.SetScriptTerminalData("Blob0Y", blob0y)
MyBAse.toolGroup.SetScriptTerminalData("Blob1X", blob1x)
MyBAse.toolGroup.SetScriptTerminalData("Blob1Y", blob1y)
'Returning False indicates we ran the tools in script, and they should not be
'run by VisionPro
Return False
End Function
#Region "When the Current Run Record is Created"
Overrides Sub ModifyCurrentRunRecord(ByVal currentRecord As
Cognex.VisionPro.ICogRecord)
End Sub
#End Region
#Region "When the Last Run Record is Created"
'Allows you to add or modify the contents of the last run record when it is
'created. For example, you might add custom graphics to the run record here.
Overrides Sub ModifyLastRunRecord(ByVal lastRecord As
Cognex.VisionPro.ICogRecord)
End Sub
#End Region
#Region "When the Script is Initialized"
'Perform any initialization required by your script here
Overrides Sub Initialize(ByVal host As CogToolGroup)
'DO NOT REMOVE ‐ Call the base class implementation first ‐ DO NOT REMOVE
MyBase.Initialize(host)
'Initialize blob result inputs and x & y outputs of the 2 bounding box blob results
MyBase.toolGroup.DefineScriptTerminal(blobresult0, "BlobResult0", True)
MyBase.toolGroup.DefineScriptTerminal(blobresult1, "BlobResult1", True)
MyBase.toolGroup.DefineScriptTerminal(blob0x, "Blob0X", False)
MyBase.toolGroup.DefineScriptTerminal(blob0y, "Blob0Y", False)
MyBase.toolGroup.DefineScriptTerminal(blob1x, "Blob1X", False)
MyBase.toolGroup.DefineScriptTerminal(blob1y, "Blob1Y", False)
End Sub
#End Region
Section 1 Lab Exercise
-3-
End Class
Section 1 Lab Exercise
-4-
ToolGroup Example 4 – Implementing Custom Behavior
Imports System
Imports Cognex.VisionPro
Imports Cognex.VisionPro.ToolGroup
Public Class UserScript
Inherits CogToolGroupBaseScript
'Declare variables
Private rangehigh as Double
Private rangelow as Double
Private distance as Double
'The GroupRun function is called when the tool group is run. The default
'implementation provided here is equivalent to the normal behavior of the
'tool group. Modifying this function will allow you to change the behavior
'when the tool group is run.
Overrides Function GroupRun(ByRef message As String, _
ByRef result As CogToolResultConstants) _
As Boolean
'Bring the inputs into the variables
MyBase.toolGroup.GetScriptTerminalData("RangeHigh", rangehigh)
MyBase.toolGroup.GetScriptTerminalData("RangeLow", rangelow)
MyBase.toolGroup.GetScriptTerminalData("Distance", distance)
'Perform tolerance check
if (distance < rangelow Or distance > rangehigh)
message = ("The distance (" & distance.ToString() & ") fell outside of the valid
range")
result = Cognex.VisionPro.CogToolResultConstants.Reject
end if
'Returning False indicates we ran the tools in script, and they should not be
'run by VisionPro
Return False
End Function
#Region "When the Current Run Record is Created"
Overrides Sub ModifyCurrentRunRecord(ByVal currentRecord As
Cognex.VisionPro.ICogRecord)
End Sub
Section 1 Lab Exercise
-5-
#End Region
#Region "When the Last Run Record is Created"
'Allows you to add or modify the contents of the last run record when it is
'created. For example, you might add custom graphics to the run record here.
Overrides Sub ModifyLastRunRecord(ByVal lastRecord As
Cognex.VisionPro.ICogRecord)
End Sub
#End Region
#Region "When the Script is Initialized"
'Perform any initialization required by your script here
Overrides Sub Initialize(ByVal host As CogToolGroup)
'DO NOT REMOVE ‐ Call the base class implementation first ‐ DO NOT REMOVE
MyBase.Initialize(host)
'Initialize 3 inputs
MyBase.toolGroup.DefineScriptTerminal(rangehigh, "RangeHigh", True)
MyBase.toolGroup.DefineScriptTerminal(rangelow, "RangeLow", True)
MyBase.toolGroup.DefineScriptTerminal(distance, "Distance", True)
End Sub
#End Region
End Class
Section 1 Lab Exercise
-6-
ToolGroup Example 5 – Dynamic Execution and Custom Graphics
' This CogToolGroup script provides customized inspection behavior for the tool group.
' It illustrates the following techniques:
' * Using one tool (Blob) to locate an arbitrary number of objects, then running another
' tool (Caliper) once to inspect each instance of the object.
' * Setting the tools group's result based on the result of inspecting all the objects.
' * Adding custom inspection graphics to the tool group's run record.
Imports System
Imports System.Collections
Imports Cognex.VisionPro
Imports Cognex.VisionPro.ToolGroup
Imports Cognex.VisionPro.Blob
Imports Cognex.VisionPro.Caliper
Public Class UserScript
Inherits CogToolGroupBaseScript
'An array to hold a series of labels for the tool group's run record.
Dim labels As ArrayList = new ArrayList()
#Region "When the tool group is run"
'We provide our own logic for running tools in place of VisionPro's normal strategy of
'running each tool once.
Overrides Function GroupRun(ByRef message As String, _
ByRef result As CogToolResultConstants) _
As Boolean
'We're inspecting an unknown number of objects, but need to produce a single
pass/fail
'result for the entire group. In this example, we'll simply reject the whole batch of
'objects in an image if any of them are bad.
Dim badObjectFound As Boolean = False
'Reset any labels from previous runs
labels = new ArrayList()
'Run the Synthetic Image tool to generate an image for inspection. For more details
on
'the generation of the image, see the script associated with the Synthetic Image tool
group.
toolGroup.RunTool(ToolGroup.Tools("Synthetic Image"), message, result)
Section 1 Lab Exercise
-7-
'Get references to the blob and caliper tool
Dim blobTool As CogBlobTool = ToolGroup.Tools("Blob")
Dim caliperTool As CogCaliperTool = ToolGroup.Tools("Caliper")
Dim caliperRegion As CogRectangleAffine = caliperTool.Region
'Run the blob tool and get a reference to the results. Note that the run parameters
'for the blob tool were established using the Blob tool's GUI.
toolGroup.RunTool(blobTool, message, result)
Dim blobResults As CogBlobResultCollection = blobTool.Results.GetBlobs()
'Run the caliper tool once for each target found using the blob tool. Note that most
'of the run parameters for the caliper tool were established using the Caliper tool's
GUI.
For Each blob As CogBlobResult In blobResults
'Use the blob center of mass to set the region where we run caliper for each target.
caliperRegion.CenterX = blob.CenterOfMassX
caliperRegion.CenterY = blob.CenterOfMassY
toolGroup.RunTool(caliperTool, message, result)
'The caliper tool was configured to find a 5 pixel high bar in the center of the region.
'If we got a result, the inspection passes. Based on the result of the inspection, we
'create an appropriate label for each target to add to the run record.
Dim myLabel As CogGraphicLabel = new CogGraphicLabel()
myLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineCenter
If caliperTool.Results.Count > 0
myLabel.SetXYText (blob.CenterOfMassX, blob.CenterOfMassY, "Good")
myLabel.Color = CogColorConstants.Green
Else
myLabel.SetXYText (blob.CenterOfMassX, blob.CenterOfMassY, "Bad")
myLabel.Color = CogColorConstants.Red
BadObjectFound = True
End If
labels.Add(myLabel)
Next
'Set the result of our inspection to Reject if one or more of the objects were bad
If BadObjectFound
result = CogToolResultConstants.Reject
End If
'Returning False indicates we ran the tools in script, and they should not be
'run by VisionPro
Section 1 Lab Exercise
-8-
Return False
End Function
#End Region
#Region "When the Last Run Record is Created"
'Add the labels in our array list as part of the inspection record for the synthetic image.
Overrides Sub ModifyLastRunRecord(ByVal lastRecord As
Cognex.VisionPro.ICogRecord)
For Each label As CogGraphicLabel in labels
toolGroup.AddGraphicToRunRecord(label, lastRecord, "Synthetic
Image.OutputImage", "script")
Next
End Sub
#End Region
End Class
Section 1 Lab Exercise
-9-