core

#### Step 3: Add VB Script to REFERENCE_ID_COLOR.LBL

In this step, using the Label Template Editor, you will add VB script code to the new label template, REFERENCE_ID_COLOR.LBL, to set the background color based on the dimension color.

1. Select File | Reporting | Edit | Label Template, and open REFERENCE_ID_COLOR.LBL. The label opens. It contains a GridControlObject with one row and one column. If you double-click on the cell, you will see an expression inside which reads:

=REF_ID:1

This means the label will display the ID of the reference feature.

2. Right-click on the editor to display the Properties dialog box.

3. Select GridControlObject1 from the list of objects.

4. In the Properties dialog box, expand Events.

5. Click [None] to the right of the EventReportData property. The VBS Mini-Editor appears. You may need to expand the VBS Mini Editor to be wider so that you can see the entire Sub statement toward the top.

You can see that the ReportData object is passed in. This is where PC-DMIS extract information about passed in dimensions. For additional information on the ReportData object, see the "ReportData Object" topic in the PC-DMIS 2019 R1 Object Library documentation. Also, if you need to reference the GridControlObject, you can use "This" as a shortcut to the object.

6. Insert this code into the VBS Mini-Editor:

Dim Count As Integer

Dim I As Integer

Dim MaxIndex As Integer

Dim MaxDev As Double

Dim CurrentDev As Variant

Dim Dev As Variant

Dim PTol As Variant

Dim MTol As Variant

Dim iReturn As Boolean

' Initialize Max deviation and Max Index

MaxDev = 0.0

MaxIndex = 1

' Get the number of axes for this dimension

' 132 is the Dtype AXIS

Count = ReportData.GetCount(132)

' Loop through to find the largest deviation

' When the loop is complete, MaxIndex is the index to the

' largest deviation

For I = 1 to Count

' 340 is the Dtype DIM_DEVIATION

CurrentDev = ABS(ReportData.GetValue(340, I))

If CurrentDev > MaxDev Then

MaxDev = CurrentDev

MaxIndex = I

End If

Next I

' Using MaxIndex, acquire that axes deviation, + Tol and - Tol

' 167 is Dtype F_PLUS_TOL; 168 is Dtype F_MINUS_TOL

Dev = ReportData.GetValue(340, MaxIndex)

PTol = ReportData.GetValue(167, MaxIndex)

MTol = ReportData.GetValue(168, MaxIndex)

' Use this information to adjust the background color of the grid cell

iReturn = This.SetCellBackgroundColor(0, 0, ReportData.GetTolColor(Dev,PTol,MTol))

Explanation of Code:

This code uses the numeric values for Dtypes. The variable MaxDev is initialized to zero and when a larger deviation is found, it is placed in MaxDev. After the For/Next loop finishes, MaxDev holds the maximum deviation for this dimension and MaxIndex holds the index to that axis. The statement ReportData.GetCount gets the number of axes for this dimension. The For/Next loop loops through each axis.

ReportData.GetTolColor gets the tolerance color for the specified deviation and tolerance values. We use MaxIndex to get the deviation and tolerance values for the axis with the largest deviation. GetTolColor returns the color values as a Long.

We use "This" to reference the GridControlObject1 object. SetCellBackgroundColor sets the background color for a specific cell. Arguments 1 and 2 are the cell’s row and column numbers. The first cell in a row and column of a grid control is row / column zero. The third argument is the color that you wish to be the background color for this cell. It is also a Long value.

7. Once you have added the code, click OK. The Mini-Editor will check for any syntax errors. If you have an error in your code, a message is displayed at this time. If there aren't any errors, the VBS Mini-Editor closes.

8. Select File | Save to saves your new copy of REFERENCE_ID_COLOR.LBL.

9. Select File | Close. This closes the Label Template Editor.