VP01 1 Scripting
VP01 1 Scripting
1
Scripting Agenda
• What is VisionPro Scripting
– What can Scripting be used for
– Using the Script Editor
– Scripting Access Points
• ToolGroup Scripting Examples:
– Example 1 – Convert Radians to Degrees
• Creating and using Script Terminal
• Overriding GroupRun
• Saving a “custom tool” template for re-use
– Example 2 – Save only Failed Images
• Saving images to hard drive dynamically
– Example 3 – Center of Bounding Box
• Exposing API not available w/ standard terminals
– Example 4 – Custom Behavior
• Easily control the RunStatus(Accept/Reject) of a CogJob
• Add inputs that are accesible in an AppWizard App
– Example 5 – Custom Code & Graphics
• Demo of Multiple Targets sample job
• Finer Points on Scripting
2
Reasons for Scripting
• If QuickBuild and AppWizard do not quite accomplish what you
need
• Extend the abilities of QuickBuild and AppWizard
• No DevStudio (Quick, “Easy”)
Scripting Uses
• Create a “Custom Tool” to perform an action that there is no VisionPro tool for.
– Add two numbers
– Perform mathematical, logical, or string manipulation
• Expose parts of the API that are not available via Tool Terminals.
– CogBlobResult.BoundingBox()
• Easily allow an AppWizard application to have additional control over your inspection routine
– Add a flag to retrain a PMAlignPatten
– Switch between different modes in the same job
• Stitch acquired images together
• Conditionally run tools or change the execution order
• Situations in which you need to do the same thing many times (avoid Jobs with hundreds of
tools and terminals)
– MultiTarget Sample
• Evaluate a complicated RunStatus expression of an inspection much easier than using the
ResultsAnalysisTool
• Save particular images to file
• Write to a log File
• Change or add the Graphics related to a particular inspection
• Creative uses to solve problems the designers of VisionPro never anticipated
3
Two Kinds of Scripting
• CogJob Scripting
– CogJob Scripting access to CogJob properties (the AcqFifo for
instance)
– CogJob scripts will tend to deal with setup and execution of the image
acquisition process.
• CogToolGroup Scripting
– Remember that each Job contains a CogToolGroup, ToolGroup
Scripting allows access to properties of a ToolGroup and to override
some of the methods that we have already talked about in a
ToolGroup
– Scripts here will deal mainly with Tools, and how they are run
Initialize :
Called when the job script is
initialized
AcqFifoConstruction :
Called after constructing the AcqFifo,
can be used to modify the initial
acquisition properties
PreAcquisition :
Called before an acquisition is started
only when using Manual and Semi-
Automatic trigger models
PostAcquisition :
Called when acquisition has completed
(applies to all trigger models)
4
Opening Script Editor for Job
10
5
Simple Scripting Using ToolBlocks
• Choosing the simple
scripting option
automates most of the
setup work involved
with scripting
• Inputs and outputs are
created in the GUI
• This allows you to
focus on the
application code
instead of the setup
code
11
12
6
ToolBlock Benefits
• Easy to use Inputs and Outputs
– Drag & Drop Terminals
– Assign any type, enter values manually
– No need to connect to Terminal
13
ToolBlock benefits
• Increase understanding of application
– Break application into functional blocks
– Expose inside of blocks through Terminals
Find and count application Organized with ToolGroup Organized with
Block
14
7
Scripting in ToolBlocks
• Simple Script for both C# and VB.NET
– Minimize coding effort
• Upon script creation all references of Vision Tools used in ToolBlock
are loaded and included
• Auto generated code simplfies access to ToolBlock terminals and VisionTools
• No overloaded functions such as Initialize(), ModifyLastRunRecord()
• No ToolLoop Run function
15
Scripting in ToolBlocks
• Simple Script example
for a ToolBlock
containing a Blob Tool
• New buttons
– Remove script in case
script is no longer
needed
– Refresh from Tool to
recreate auto
generated code in
case new Vision
Tools or terminals
have been added
16
8
Scripting in ToolBlock
• Advanced Script
– Comparable to ToolGroup Script
• Automatically includes VisionTool References and includes them
17
18
9
Example 1 – Convert to Degrees – Step 1
• Declare input and
output terminals in the
Inputs/Outputs tab
• Input
– Radians as
System.Double
• Output
– Degrees as
System.Double
19
Add this.
20
10
Example 1 – Convert to Degrees – Step 3
• Connect the
Terminals and add
the Output to Posted
Items (remember that
this is now available
in the App Wizard)
21
11
Example 2 –Saving only Failed Images
23
Adding/Removing References
1
2
24
12
Example 2 – Save Failed Images – Step 2
Add this.
25
• Connect the
Terminals and note
the images that are
saved when results
are not “Accept”
26
13
ToolGroup Advanced Scripting Access Points
UserScript : CogToolGroupBaseScript
GroupRun :
Used to control the order of
execution of the tools in the tool
group
ModifyCurrentRunRecord :
Used to modify the current run
record after the tool has completed
ModifyLastRunRecord :
Used to modify the last run record
after the tool has completed
Initialize :
Run When the Script is created, used
for script setup, adding terminals, and
subscribing to Events
27
28
14
ToolGroup Scripting Template Access Points
• When you open the ToolGroup script editor you are given an
empty template to get you started
29
GroupRun
'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
'Run each tool in the tool group using the RunTool function
For toolIdx As Int32 = 0 To ToolGroup.Tools.Count – 1
ToolGroup.RunTool(ToolGroup.Tools(toolIdx), message, result)
Next
'Returning False indicates we ran the tools in script, and they should not be
'run by VisionPro
Return False
End Function
15
Records Overrides
#Region "When the Current Run Record is Created"
Overrides Sub ModifyCurrentRunRecord(ByVal currentRecord As
Cognex.VisionPro.ICogRecord)
End Sub
#End Region
End Sub
#End Region
31
Initialize
#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)
End Sub
#End Region
32
16
Example 3 –Center of Bounding Box
• This example was an actual customer problem that was solved with scripting
We have been measuring the distance between
Blobs using the CenterOfMass of the Blobs
This customer had a need to measure the distances
between the center of the pixel aligned bounding box
(the rectangle shown below)
33
34
17
Example 3 –Center of Bounding Box – Step 1
• Remember to initialize input terminals for the two blob results
and the X and Y for each of the calculated positions for the
bounding boxes (four values)
35
• Even after adding the reference to the DLL, you must import
the Cognex.VisionPro.Blob namespace before you can use
CogBlobResult variable type
36
18
Example 3 –Center of Bounding Box – Step 3
37
38
19
Example 4 – Custom Behavior
• Replace Data Analysis with a script because it’s easier and we can expose the terminals to
the App Wizard
• Add a new CogToolGroup and in its ToolGroup script add three terminals for the Distance,
a RangeHigh and a RangeLow.
• The script should return a reject result if the distance does not fall within the range we
specify
MyBase.toolGroup.GetScriptTerminalData("Distance", distance)
MyBase.toolGroup.GetScriptTerminalData("RangeHigh", rangehigh)
MyBase.toolGroup.GetScriptTerminalData("RangeLow", rangelow)
Return False
End Function
39
40
20
Example 4 – Custom Behavior
• Run the App Wizard and add an Input for RangeHigh and RangeLow
• The values for the Terminals are stored in the ScriptData property of the CogToolGroup
• The Path For Range High is:
Tools.Item["ResultsAnalysis_Script"].(Cognex.VisionPro.ToolGroup.CogToolGroup).ScriptData
.Item["RangeHigh"].(System.Double)
41
42
21
Example 5 – Custom Code & Graphics
• This section will reconstruct the MultiTarget app that some of
you may have seen previously.
• This will teach you about:
– Customizing the execution Order of the Tools.
– Working with the LastRunRecord script point to customize the
Records/Graphics.
• Start a new job, I’ve saved a tool template called Synthetic
Image to your ToolBox, add one to your job and run it a few
times.
43
Example 5 Cont.
44
22
Example 5 – Custom Code & Graphics
• You should see something like this
45
46
23
Example 5 – Custom Code & Graphics
• Add a CogBlobTool and a CogCaliperTool
47
48
24
Example 5 – Custom Code & Graphics
• Set Up a Caliper Tool to find the Edges of the black region in the center of a
rectangle.
• Make sure your scan and search Directions are correct and that you get a result
49
50
25
Example 5 – Custom Code & Graphics
Group Run Override
Overrides Function GroupRun(ByRef message As String, _
ByRef result As CogToolResultConstants) _
As Boolean
'Get references to the blob and caliper tool from the ToolGroup
Dim blobTool As CogBlobTool = ToolGroup.Tools("Blob")
Dim caliperTool As CogCaliperTool = ToolGroup.Tools("Caliper")
Dim caliperRegion As CogRectangleAffine = caliperTool.Region
'Run the caliper tool once for each target found using the blob tool.
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)
Next
End Function
51
= New Code
Dim BadObjectFound As Boolean = False
'Run the caliper tool once for each target found using the blob tool.
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)
If caliperTool.Results.Count = 0 Then
BadObjectFound = True
End If
Next
'Set the result of our inspection to Reject if one or more of the objects were bad
If BadObjectFound Then
result = CogToolResultConstants.Reject
End If
'Returning False indicates we ran the tools in script, and they should not be
'run by VisionPro
Return False
52
26
Example 5 – Custom Code & Graphics
• Define a labels collection to hold some Graphics
53
'Run the caliper tool once for each target found using the blob tool.
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)
'If the caliper got a result, this inspection passes. We then create an appropriate graphic label
Dim myLabel As CogGraphicLabel = New CogGraphicLabel
myLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineCenter
If caliperTool.Results.Count > 0 Then
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
54
27
Example 5 – Custom Code & Graphics
• Use the ModifyLastRunRecord override to add the labels we
created to the LastRun record
'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
lastRecord.SubRecords.Remove("CogInputImageTool1.OutputImage")
55
56
28
Invalid Scripts
• A script is considered invalid if
– There is an error compiling the script
– An exception is thrown from the initialize routine
• Invalid scripts will not be used. VisionPro will behave as if you
had not written the script.
• If a tool group script is invalid, you will not lose the terminals
defined in the script.
• You can save a job or application with invalid scripts, and
reload it later to continue development.
57
Scripting Guidelines
• Your script effectively becomes QuickBuild code. Any errors in your script
can cause QuickBuild to become unstable.
• Save frequently. QuickBuild is your development environment for scripting.
You will lose your unsaved work when (not if) a bug in your script causes
QuickBuild to become unstable.
• Like any program environment, many problems will result in unexpected
behavior instead of an error message. For example, there is nothing to stop
you from writing and running an infinite loop.
58
29
Summary
• ToolGroup and ToolBlock scripting provide additional
functionality within QuickBuild including:
– Custom tool creation
– Drawing custom graphics
– Creating custom application behavior
59
30