Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

VisionPro Advanced

Section 3: Implementing Custom Graphics

Objectives:

 Use an interactive graphic to set the working region of a tool


 Get the position of interactive graphics when the graphic changes

Instructions:

Exercise

Add code to the vision inspection portion of your application to draw the Edge
graphics from the Caliper tool in the previous lab.

Solution

Up top:

Private WithEvents myCaliperTool As CogCaliperTool

'Declare CogRectangleAffine in which to load certain tool regions


for interactive setting purposes
Private WithEvents myRect As CogRectangleAffine

Add references:

Cognex.VisonPro.Caliper

Section 3 Lab Exercise


-1-
Add namespace:

Imports Cognex.VisionPro.Caliper

To bottom of Form_Load():

'Creating Caliper object


myCaliperTool = New CogCaliperTool
myCaliperTool = myTG.Tools("CogCaliperTool1")

'Create new CogRectangleAffine instance


myRect = New CogRectangleAffine

When Edit Caliper button is clicked:

Private Sub btnEditCaliper_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles btnEditCaliper.Click

'Disable drawing until all graphics are added


CogRecordDisplay1.DrawingEnabled = False

'Create new CogRecetangleAffine instance


myRect = New CogRectangleAffine

'Assign the new CogRectablgeAffine the region of the Caliper


tool from the application loaded above
'This means that the new interactive graphic will appear
exactly where the caliper tool is already inspecting
myRect = myCaliperTool.Region

'Set the color


myRect.Color = CogColorConstants.Yellow

'Set to interactive
myRect.Interactive = True

'Set the space in which it will be interpreted (this is the


string name assigned within the coordiante space tool; watch for
default duplicate names)
myRect.SelectedSpaceName = "Fixture"

'Set what will be changable when interacting with the graphic


myRect.GraphicDOFEnable = CogRectangleAffineDOFConstants.All

'Set line style


myRect.LineStyle = CogGraphicLineStyleConstants.Solid

'Add the graphic to the display


CogRecordDisplay1.InteractiveGraphics.Add(myRect, "Group1",
False)

'Now that we are done adding, enable drawing on the display


CogRecordDisplay1.DrawingEnabled = True

Section 3 Lab Exercise


-2-
When Interactive Graphic is changed:

Private Sub myRect_DraggingStopped(ByVal sender As Object,


ByVal e As Cognex.VisionPro.CogDraggingEventArgs) Handles
myRect.DraggingStopped

'Grab the transferred arguments and set them to the properties


'of the CogRectangleAffine
myRect = e.DragGraphic

'Set the Caliper tool inspection region to the region


'designated by the interactive graphic that has just changed
myCaliperTool.Region = myRect

'Update the status label to confirm successful execution


stlStatus.Text = "Caliper region modified."
End Sub

Section 3 Lab Exercise


-3-
Complete Solution

Imports Cognex.VisionPro
Imports Cognex.VisionPro.QuickBuild
Imports Cognex.VisionPro.ToolGroup
Imports Cognex.VisionPro.PMAlign
Imports Cognex.VisionPro.Caliper

Public Class Form1

Private WithEvents myJobManager As CogJobManager


Private myJob As CogJob
Private myIndependentJob As CogJobIndependent

Private myTG As CogToolGroup


Private WithEvents myPMTool As CogPMAlignTool

Private WithEvents myCaliperTool As CogCaliperTool


Private WithEvents myRect As CogRectangleAffine

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As


System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

'Explictly dispose of all VisionPro controls


CogDisplayStatusBarV21.Dispose()
CogRecordDisplay1.Dispose()

'Closing the CogJobManager gracefully


myJobManager.Shutdown()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

'Load the QuickBuild application


myJobManager =
CType(CogSerializer.LoadObjectFromFile("C:\Users\mperez.NATICK‐
NT\Desktop\VB_App.vpp"), CogJobManager)

'Initialize variables
myJob = myJobManager.Job("DemoPlate")
myIndependentJob = myJob.OwnedIndependent

'Flush queues
myJobManager.UserQueueFlush()
myJobManager.FailureQueueFlush()
myJob.ImageQueueFlush()
myIndependentJob.RealTimeQueueFlush()

' Connect the status bar


CogDisplayStatusBarV21.Display = CogRecordDisplay1

'Creating ToolGroup object


myTG = New CogToolGroup
myTG = myJob.VisionTool

Section 3 Lab Exercise


-4-
'Creating PMTool object
myPMTool = New CogPMAlignTool
myPMTool = myTG.Tools("CogPMAlignTool1")

'Creating Caliper object


myCaliperTool = New CogCaliperTool
myCaliperTool = myTG.Tools("CogCaliperTool1")

'Create new CogRectangleAffine instance


myRect = New CogRectangleAffine

End Sub

Private Sub btnRunOnce_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnRunOnce.Click

'Disable buttons
btnRunOnce.Enabled = False
chkContinuous.Enabled = False
ControlBox = False
chkShowTrain.Enabled = False

'Attempt to run the application


Try
myJobManager.Run()
Catch ex As Exception
'If there is a problem show the error message
MessageBox.Show(ex.Message)
End Try

End Sub

'Delegate whose signature matches CJM events.


Delegate Sub myJobManagerDelegate(ByVal sender As Object, ByVal e As
CogJobManagerActionEventArgs)

Private Sub myJobManager_Stopped(ByVal sender As Object, ByVal e As


Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs) Handles
myJobManager.Stopped

If InvokeRequired Then

' Create a pointer to this function


Dim myDel As New myJobManagerDelegate(AddressOf myJobManager_Stopped)

' Call this same function on the correct thread


Dim eventArgs() As Object = {sender, e}
Invoke(myDel, eventArgs)
Return

End If

'Enable buttons
btnRunOnce.Enabled = True
chkContinuous.Enabled = True

Section 3 Lab Exercise


-5-
ControlBox = True
chkShowTrain.Enabled = True

End Sub

Private Sub chkContinuous_CheckedChanged(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles chkContinuous.CheckedChanged
If (chkContinuous.Checked) Then
btnRunOnce.Enabled = False
ControlBox = False
chkShowTrain.Enabled = False

Try
myJobManager.RunContinuous()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
btnRunOnce.Enabled = True
ControlBox = True
chkShowTrain.Enabled = True

Try
myJobManager.Stop()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If

End Sub

Private Sub myJobManager_UserResultAvailable(ByVal sender As Object, ByVal e


As Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs) Handles
myJobManager.UserResultAvailable

If InvokeRequired Then
' Create a pointer to this function
Dim myDel As New myJobManagerDelegate(AddressOf
myJobManager_UserResultAvailable)
' Call this same function on the correct thread
Dim eventArgs() As Object = {sender, e}
Invoke(myDel, eventArgs)
Return
End If

'Declare and assign local ICogRecord variable


Dim topRecord As Cognex.VisionPro.ICogRecord = _
myJobManager.UserResult
'Populate status label with application information
stlStatus.Text = _
topRecord.SubRecords("UserResultTag").Content & ": " _
& topRecord.SubRecords("JobName").Content & " ‐‐> " _
& topRecord.SubRecords("RunStatus").Content.ToString

Dim tmpRecord As Cognex.VisionPro.ICogRecord


' Assume the required record is present and get it
tmpRecord = topRecord.SubRecords("ShowLastRunRecordForUserQueue")
tmpRecord = tmpRecord.SubRecords("LastRun")

Section 3 Lab Exercise


-6-
tmpRecord = tmpRecord.SubRecords("CogFixtureTool1.OutputImage")
CogRecordDisplay1.Record = tmpRecord
CogRecordDisplay1.Fit(True)

End Sub

Private Sub chkShowTrain_CheckedChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles chkShowTrain.CheckedChanged
If (chkShowTrain.Checked) Then
btnRunOnce.Enabled = False
chkContinuous.Enabled = False
btnRetrain.Enabled = True

Dim tmpRecord As Cognex.VisionPro.ICogRecord

'Extract the "TrainIMage" record and replace it with the "InputImage“


If myPMTool.InputImage IsNot Nothing Then
'Make the last acquired image the image to be used for training
myPMTool.Pattern.TrainImage = myPMTool.InputImage
tmpRecord = myPMTool.CreateCurrentRecord()
tmpRecord = tmpRecord.SubRecords.Item("TrainImage")
CogRecordDisplay1.Record = tmpRecord
CogRecordDisplay1.Fit(True)
stlStatus.Text = "Set the region/origin and click Retrain to train
a new pattern."

Else
stlStatus.Text = "There is no input image to load as a train
image."
End If

Else
btnRunOnce.Enabled = True
chkContinuous.Enabled = True
btnRetrain.Enabled = False

'Release use of the CogRecordDisplay


CogRecordDisplay1.Record = Nothing
stlStatus.Text = "Ready"

End If

End Sub

Private Sub btnRetrain_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnRetrain.Click
'Training the new pattern
Try
myPMTool.Pattern.Train()
Catch ex As Exception
'If there is a problem show the error message
MessageBox.Show(ex.Message)
End Try

If myPMTool.Pattern IsNot Nothing Then


stlStatus.Text = "Pattern trained successfully."
Else

Section 3 Lab Exercise


-7-
stlStatus.Text = "Pattern NOT trained successfully."
End If

End Sub

Private Sub btnEditCaliper_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnEditCaliper.Click

'Disable drawing until all graphics are added


CogRecordDisplay1.DrawingEnabled = False

'Assign the new CogRectablgeAffine the region of the Caliper tool from the
application loaded above
'This means that the new interactive graphic will appear exactly where the
caliper tool is already inspecting
myRect = myCaliperTool.Region

'Set the color


myRect.Color = CogColorConstants.Yellow

'Set to interactive
myRect.Interactive = True

'Set the space in which it will be interpreted (this is the string name
assigned within the coordiante space tool; watch for default duplicate names)
myRect.SelectedSpaceName = "Fixture"

'Set what will be changable when interacting with the graphic


myRect.GraphicDOFEnable = CogRectangleAffineDOFConstants.All

'Set line style


myRect.LineStyle = CogGraphicLineStyleConstants.Solid

'Add the graphic to the display


CogRecordDisplay1.InteractiveGraphics.Add(myRect, "Group1", False)

'Now that we are done adding, enable drawing on the display


CogRecordDisplay1.DrawingEnabled = True

End Sub

Private Sub myRect_DraggingStopped(ByVal sender As Object, ByVal e As


Cognex.VisionPro.CogDraggingEventArgs) Handles myRect.DraggingStopped

'Grab the transferred arguments and set them to the properties 'of the
CogRectangleAffine
myRect = e.DragGraphic

'Set the Caliper tool inspection region to the region 'designated by the
interactive graphic that has just changed
myCaliperTool.Region = myRect

'Update the status label to confirm successful execution


stlStatus.Text = "Caliper region modified."

End Sub
End Class

Section 3 Lab Exercise


-8-

You might also like