# ToggleRectangle Script

The ToggleRectangle script will be called by the Checked and Unchecked events of the CheckBox on the Page. This script will initialize the interactive rectangle on the VisionPro Display, and adds or removes it from the display.

1. Right-click the User Scripts node under Scripting in the Project Explorer and select Add Script.

2. Enter ToggleRectangle as script name.

3. Enter the following script:

       CogRectangleAffine aRect;
       CogDisplay disp;

       if($myRect == null)
       {
       	aRect = new CogRectangleAffine();
       	aRect.CenterX = 100;
       	aRect.CenterY = 100;
       	aRect.SetOriginCornerXCornerY(50, 50, 100, 50, 50, 100);
       	aRect.Visible = true;
       	aRect.Color = CogColorConstants.Red;
       	aRect.GraphicDOFEnable = CogRectangleAffineDOFConstants.All;
       	aRect.Interactive = true;
       	aRect.SelectedSpaceName = ".";
       	$myRect = aRect;
       	$rectCenterX = aRect.CenterX;
       	$rectCenterY = aRect.CenterY;
       	$rectAngle = aRect.Rotation;
       	$rectSkew = aRect.Skew;
       }
       else
       	aRect = (CogRectangleAffine)$myRect;

       disp = (Cognex.VisionPro.Display.CogDisplay)$System.GetInternalObject("Pages.Page.ImageDisplay");

       if ($Pages.Page.Checkbox.State == true)
       	disp.InteractiveGraphics.Add(aRect, "Rect", false);
       else
       	disp.InteractiveGraphics.Remove("Rect");

       $Tasks.CopyRegion.Run();

4. In Properties, add using statements to the Code property.

5. Press the drop-down button, and in the blank text field, enter Cognex.VisionPro and Cognex.VisionPro.Display.

6. Press the browse button of the References property, using the Script References dialog, from the Discovered References pane, add Cognex.VisionPro.dll, Cognex.VisionPro.Core.dll, Cognex.VisionPro.Display.Controls.dll and System.Windows.Forms.dll to the Local References, and press the Accept button.

7. Right-click on the Show Rectangle checkbox on Page, and select Scripts | On Checked. Enter "$ToggleRectangle();" in the editor. Do the same for Scripts | On Unchecked as well.

8. Press the Compile button to ensure the script correctly compiles.