vb.netcognex

Cognex Insight SetComment Not Persisting


I have a job that stores cell locations as comments in particular cells, but I'm running into a situation where the CvsInsight::SetComment method is not persisting.

I'm displaying a form as a dialog wherein the user can change the cell locations that are stored in the comment cells and when the user clicks the save button I'm creating a new instance of a custom class, setting the properties to the new cell locations (set by the user), setting the DialogResult as OK, and then closing the form. Then in the form where I called ShowDialog, I call the SetComment method for each property in the custom class on their respective cell.

This is what I'm doing in the dialog's Save button:

Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
    ' Check if the name, username, password, pass, fail, total, reset, and results are all set
    Dim invalidFields As List(Of String) = New List(Of String)()
    For Each pair In _requiredFields
        If (String.IsNullOrWhiteSpace(DirectCast(Controls.Find(pair.Key, True).FirstOrDefault, TextBox).Text)) Then
            invalidFields.Add(pair.Value)
        End If
    Next

    If (invalidFields.Any()) Then
        MessageBox.Show($"The following required fields are missing a value: {String.Join(", ", invalidFields)}", "Invalid Form", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return
    End If

    ' Set the returned object's values, set the dialog result, and then close the dialog
    CameraSettings = New CameraSettings() With {
        .FailCell = FailCellLocation.Text,
        .FocusCell = FocusCell.Text,
        .IsVisible = Visibility.Checked,
        .PassCell = PassCellLocation.Text,
        .ResetCell = ResetCellLocation.Text,
        .ResultsCell = ResultsCellLocation.Text,
        .TotalCell = TotalCellLocation.Text
    }
    DialogResult = DialogResult.OK
    Close()
End Sub

And this is what I'm doing in the form that opens the dialog:

Private Sub Settings_Click(sender As Object, e As EventArgs) Handles Settings.Click
    Using cameraSettingsDialog As frmCameraSetting = New frmCameraSetting(InsightDisplay.InSight)
        With cameraSettingsDialog
            If (.ShowDialog = DialogResult.OK) Then
                InsightDisplay.InSight.SetComment(New CvsCellLocation(_focusCell), New CvsCellComment(.CameraSettings.FocusCell))
                InsightDisplay.InSight.SetComment(New CvsCellLocation(_passCell), New CvsCellComment(.CameraSettings.PassCell))
                InsightDisplay.InSight.SetComment(New CvsCellLocation(_failCell), New CvsCellComment(.CameraSettings.FailCell))
                InsightDisplay.InSight.SetComment(New CvsCellLocation(_totalCell), New CvsCellComment(.CameraSettings.TotalCell))
                InsightDisplay.InSight.SetComment(New CvsCellLocation(_resultCell), New CvsCellComment(.CameraSettings.ResultsCell))
                InsightDisplay.InSight.SetComment(New CvsCellLocation(_resetCell), New CvsCellComment(.CameraSettings.ResetCell))

                GetSettingCells()
            End If
        End With
    End Using
End Sub

What is happening is that the code executes without throwing any exceptions, but the comment is not set. What's frustrating is that I'm not able to debug because the CvsInsightDisplay's Insight gets set to null anytime I try to access the results in the middle of setting the comment. However, I can verify that the CameraSettings' properties are what I'd expect them to be because if I setup a Console.WriteLine to print the various properties they're right.

Looking through the SDK, I cannot find any documentation as to why it wouldn't set the value without throwing an exception.


Solution

  • For those of you who run into the same issue, the problem resolved around the fact that I was trying to set a cell that was past the maximum row in the job. To fix the issue, I had to change the cells in which I was setting the comments for to ones with a lower row index.

    Unfortunately, Cognex does not have this behavior documented in any of their documentation.