VisionPro - VisionPro Advanced – Section 1
Scripting Lab
Approximate Duration: 45 minutes
Objectives:
Use scripting to complete application specific tasks
Review ToolBlock scripting
Solutions for ToolBlock Scripting
Show Example 1 with ToolGroup to show ease of use of ToolBlock
Solution (in VB)
Imports System
Imports [Link]
Imports [Link]
Public Class UserScript
Inherits CogToolGroupBaseScript
Private radians as double
Private degrees as double
Overrides Function GroupRun(ByRef message As String, _
ByRef result As CogToolResultConstants) _
As Boolean
[Link]("Radians", radians)
degrees = (radians * 180) / 3.14159
'[Link]([Link]())
[Link]("Degrees", degrees)
'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"
Section 1 Lab Exercise
-1-
Overrides Sub ModifyCurrentRunRecord(ByVal currentRecord As
[Link])
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
[Link])
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
[Link](host)
[Link](radians,"Radians", True)
[Link](degrees, "Degrees", False)
End Sub
#End Region
End Class
Using C#:
using System;
using [Link];
using [Link];
public class UserScript : CogToolGroupBaseScript
{
// Declare variables
private double radians = 0.0;
private double degrees = 0.0;
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
Section 1 Lab Exercise
-2-
object val = new object();
// Bring in radian data
[Link]("Radians", ref val);
radians = (double) val;
// Do calculations
degrees = radians * 180.0 / 3.145;
// Place degree value on terminal
[Link]("Degrees", degrees);
// Returning False indicates we ran the tools in script, and they should not be
// run by VisionPro
return false;
}
#region "When the Current Run Record is Created"
public override void ModifyCurrentRunRecord([Link]
currentRecord)
{
}
#endregion
#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.
public override void ModifyLastRunRecord([Link] lastRecord)
{
}
#endregion
#region "When the Script is Initialized"
// Perform any initialization required by your script here
public override void Initialize(CogToolGroup host)
{
// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
[Link](host);
// Create terminals for passing data
[Link](radians, "Radians", true);
[Link](degrees, "Degrees", false);
}
Section 1 Lab Exercise
-3-
#endregion
ToolBlock Example 1 – Converting from Radians to Degrees (using VB)
Public Overrides Function GroupRun(ByRef message As String, ByRef result As
CogToolResultConstants) As Boolean
'Convert radians to degrees
[Link] = ([Link] * 180) / [Link]
Return false
End Function
Using C#:
public override bool GroupRun(ref string message, ref CogToolResultConstants
result)
{
// Perform calculation
[Link] = [Link] * 180 / [Link];
return false;
}
ToolBlock Example 2 – Save Failed Images (using VB)
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 ([Link] <> [Link]) Then
'Build the filename
fname = [Link]("c:\SavedImages\badimage{0:d}.bmp", running)
'Perform save on the image
[Link](fname)
Section 1 Lab Exercise
-4-
'Increment counter
running = running + 1
End If
Return False
End Function
Using C#:
public class CogToolBlockSimpleScript : CogToolBlockAdvancedScript
{
// Declare variable for incrementing
private int running = 0;
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
// Code to save only bad images
string filename = [Link];
if ([Link] != [Link])
{
filename = [Link](@"c:\SavedImages\BadImage{0:d}.bmp", running);
[Link]().Save(filename);
running++;
}
return false;
}
Section 1 Lab Exercise
-5-
ToolBlock Example 3 – Blob Bounding Box Center (using VB)
Public Overrides Function GroupRun(ByRef message As String, ByRef result As
CogToolResultConstants) As Boolean
[Link].blob0x =
[Link].Results_GetBlobs_Item_0_.GetBoundingBox([Link]
ned).CenterX
[Link].blob0y =
[Link].Results_GetBlobs_Item_0_.GetBoundingBox([Link]
ned).CenterY
[Link].blob1x =
[Link].Results_GetBlobs_Item_1_.GetBoundingBox([Link]
ned).CenterX
[Link].blob1y =
[Link].Results_GetBlobs_Item_1_.GetBoundingBox([Link]
ned).CenterY
Return false
End Function
Using C#:
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
[Link].Blob0X =
[Link]([Link]).CenterX;
[Link].Blob0Y =
[Link]([Link]).CenterY;
[Link].Blob1X =
[Link]([Link]).CenterX;
[Link].Blob1Y =
[Link]([Link]).CenterY;
return false;
}
Section 1 Lab Exercise
-6-
ToolBlock Example 4 – Implementing Custom Behavior (using VB)
Public Overrides Function GroupRun(ByRef message As String, ByRef result As
CogToolResultConstants) As Boolean
If [Link].Results_Item_0_Width > [Link] Or
[Link].Results_Item_0_Width < [Link] Then
result = [Link]
message = "The distance is outside of " + [Link] + " and " +
[Link] + " mm."
Else
result = [Link]
message = "The distance is inside of " + [Link] + " and " +
[Link] + " mm."
End If
Return false
End Function
Using C#:
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
// Check if width is in range
if ([Link] > [Link] || [Link] <
[Link])
{
result = [Link];
message = "The distance is outside of " + [Link]() + " and "
+ [Link]() + " mm.";
}
return false;
}
Section 1 Lab Exercise
-7-