0% found this document useful (0 votes)
222 views7 pages

VisionPro ToolBlock Scripting Guide

The document provides examples of using ToolBlock scripting in VisionPro Advanced. It includes 4 examples written in both VB and C# that demonstrate common scripting tasks like: 1) Converting radians to degrees, 2) Saving failed images, 3) Getting blob bounding box coordinates, and 4) Implementing custom pass/fail behavior based on measurement ranges. The examples are intended to help users learn ToolBlock scripting functionality.

Uploaded by

Jorge Huerta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views7 pages

VisionPro ToolBlock Scripting Guide

The document provides examples of using ToolBlock scripting in VisionPro Advanced. It includes 4 examples written in both VB and C# that demonstrate common scripting tasks like: 1) Converting radians to degrees, 2) Saving failed images, 3) Getting blob bounding box coordinates, and 4) Implementing custom pass/fail behavior based on measurement ranges. The examples are intended to help users learn ToolBlock scripting functionality.

Uploaded by

Jorge Huerta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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-

Common questions

Powered by AI

In both VB and C# scripts, after computing the degrees from radians, the scripts use 'SetScriptTerminalData' method to update the 'Degrees' terminal with the newly computed value, ensuring that the conversion is accessible to the VisionPro system .

In the scripting examples, the conversion from radians to degrees is achieved by multiplying the radians value by 180 and then dividing by π (pi), as seen in both the Visual Basic and C# implementations .

The scripts handle rejected images by checking the 'ResultStatus'. If it is not acceptable ('CogToolResultConstants.Accept'), an image is saved to a predefined directory ('c:\SavedImages\') with an incrementing filename. This logic ensures that only images which fail the specified criteria are saved .

The 'DefineScriptTerminal' method establishes the parameters for data exchange between the script and VisionPro. It sets input and output pathways ensuring that data, such as 'Radians' and 'Degrees', is correctly directed and manipulated, which is essential for the script's operation .

Conditional structures in 'GroupRun' check bounds of width measurements against specified distances. They help dictate script flow by determining whether output aligns with 'Accept' or 'Reject' categories. This control mechanism ensures output accuracy and relevance against predefined thresholds .

The syntax difference includes declaration and method structure. VB uses a combination of 'Function' and 'End Function' for method definition while C# uses 'public override bool'. VB uses 'As' for variable types, whereas C# uses explicit type declaration before variable names. These distinctions highlight differences in language architecture and readability .

The custom behavior implementation evaluates if a detected object's width is within specified 'MinDistance' and 'MaxDistance' limits. If outside these bounds, the script returns a 'Reject' result, along with a message detailing the out-of-range status. Conversely, if within limits, it returns an 'Accept' result, affirming inclusion within specified distances .

The 'ModifyCurrentRunRecord' method is overridable and allows modifications to the current run record during script execution. Although it is empty in the provided examples, its function is crucial for applications needing dynamic adjustments or data persistence during runtime .

The 'Initialize' method is crucial for setting up the script environment by defining necessary script terminals for data transmission. It ensures that the script starts with the requisite settings, thus facilitating proper interaction between the script and the VisionPro framework .

The 'GetBoundingBox' method is used in scenarios where it is important to determine the spatial attributes of blobs detected in an image. It calculates and provides the center coordinates (X, Y) of the bounding box of each blob, facilitating precise image analysis or object tracking .

You might also like