# Writing the User Scripts

There are two User Scripts utilized in the project: OverlayGreyImage and UpdateDisplay.

- OverlayGreyImage

  The sample script illustrates how to overlay a grey output image over a range image. The script contains two arguments: "overlay" = Boolean and "display" = Object.

      CogImage16Range range = $InputImage.Range;
      CogImage16Grey grey = $InputImage.Grey16;

      if(range != null)
      {
        // Update display
        Cog3DRangeImageGraphic rangeGraph = (overlay == true && grey != null?
          new Cog3DRangeImageGraphic(range, grey) : new Cog3DRangeImageGraphic(range));

        if (displayName.ToLower().Contains("input"))
        {
          $Pages.Page.Display3DInputImage.Clear();
          $Pages.Page.Display3DInputImage.AddGraphics(null, rangeGraph, "InputImage");
          $Pages.Page.Display3DInputImage.Fit(true);
        }
        else
        {
          $Pages.Page.Display3DResultImage.Clear();
          $Pages.Page.Display3DResultImage.AddGraphics(null, rangeGraph, "InputImage");
          $Pages.Page.Display3DResultImage.Fit(true);
        }
      }

- UpdateDisplay

  The sample script below illustrates how to take a 2D shape and transform it into a 3D graphic shape, take a 3D shape and turn it into a 3D graphic shape, and how to add a 3D graphic shape to the VisionPro Display 3D.

  Note: The following code does not include code for retrieving the existing geometric graphics from the display when the OverlayGreyImage value has changed. This will need to be implemented if the display is redrawn, and the existing graphics are to remain. For more information on incorporating how to get a list of graphics already on the display, please see the VisionPro® Online Documentation topic "Adding a 3D Display to Your Application"

      $OverlayGreyImage($Display3D.GreyImageOverlay, "input");

      $Pages.Page.Display3DResultImage.Clear();

      // Update result image display
      if ($Tasks.Task.Task.ToolBlock.ToolBlock.RunStatus.Result == CogToolResultConstants.Accept)
      {
        CogImage16Range range = $InputImage.Range;
        CogImage16Grey grey = $InputImage.Grey16;

        Cog3DRangeImageGraphic rangeGraph = $Display3D.GreyImageOverlay == true ?
          new Cog3DRangeImageGraphic(range, grey) : new Cog3DRangeImageGraphic(range);

        $Pages.Page.Display3DResultImage.AddGraphics(null, rangeGraph, $Display3D.GroupResultGraphics);

        // Now add result graphics
        ICogTransform2D xform2D = grey.GetTransform("#", ".");
        ICog3DTransform xform3D = range.GetTransform3D("Sensor3D", "@");

        Boolean visible;
        UInt16 height;
        double mappedX, mappedY;

        CogOCRMaxLineResult lineResult = $Tasks.Task.Task.ToolBlock.ItemLineResult.Value as CogOCRMaxLineResult;
        if (lineResult != null && lineResult.Count > 0)
        {
          for(int id = 0; id < lineResult.Count; id++)
          {
            CogOCRMaxPositionResult r = lineResult[id];
            //#1 Figure out the XY pixel coordinate from the XY mm value of 2D graphics/shape
            xform2D.MapPoint(r.MarkRect.CenterX, r.MarkRect.CenterY, out mappedX, out mappedY);
            //#2. Get the height value at the XY from the range image
            range.GetPixel((int)mappedX, (int)mappedY, out visible, out height);
            //#3. Now map the XYZ from pixel to mm space
            Cog3DVect3 centerOfResultInMM = xform3D.MapPoint(new Cog3DVect3((int)mappedX, (int)mappedY, height));
            //#4. Create 3D rectangle shape right on top of the image
            Cog3DRectangle rect = new Cog3DRectangle();
            rect.SetOriginVertexXVectorYVector(new Cog3DVect3(-r.MarkRect.SideXLength/2, -r.MarkRect.SideYLength/2, 0),
               new Cog3DVect3(r.MarkRect.SideXLength, 0, 0),
               new Cog3DVect3(0, r.MarkRect.SideYLength, 0));
            //#5. Get rotation info
            Cog3DTransformRotation rotation = new Cog3DTransformRotation(new Cog3DEulerXYZ(0, 0, r.MarkRect.Rotation));
            rect = rect.MapShape(new Cog3DTransformRigid(rotation, centerOfResultInMM)) as Cog3DRectangle;
            //#6. create the 3D rectangle graphic from 3D shape
            if (rect != null)
            {
              Cog3DRectangleGraphic rectGraph = new Cog3DRectangleGraphic(rect);
              rectGraph.Color = CogColorConstants.Cyan;
              rectGraph.DisplayState = Cog3DGraphicDisplayStateConstants.SurfaceWithWireFrame;
              $Pages.Page.Display3DResultImage.AddGraphics(rectGraph, rangeGraph, $Display3D.GroupResultGraphics);
            }
          }
        }

        CogIDResults idResults = $Tasks.Task.Task.ToolBlock.ItemIDResults.Value as CogIDResults;
        if (idResults != null && idResults.Count > 0)
        {
          double boxSize = 5.1; // The DM code is approx. 5.1mm
          double boxHeight = 2;
          for(int id = 0; id < idResults.Count; id++)
          {
            CogIDResult r = idResults[id];
            //#1 Figure out the XY pixel coordinate from the XY mm value
            xform2D.MapPoint(r.CenterX, r.CenterY, out mappedX, out mappedY);
            //#2. Get the height value at the XY from the range image
            range.GetPixel((int)mappedX, (int)mappedY, out visible, out height);
            //#3. Now map the XYZ from pixel to mm space
            Cog3DVect3 centerOfResultInMM = xform3D.MapPoint(new Cog3DVect3((int)mappedX, (int)mappedY, height));
            //#4. Create shape
            Cog3DBox box = new Cog3DBox();
            box.SelectedSpaceName3D = r.BoundsPolygon.SelectedSpaceName;
            box.SetOriginVertexXVectorYVectorZ(new Cog3DVect3(-boxSize/2, -boxSize/2, boxHeight/2),
               new Cog3DVect3(boxSize, 0, 0),
               new Cog3DVect3(0, boxSize, 0), boxHeight);
            //#5. Get rotation info
            Cog3DTransformRotation rotation = new Cog3DTransformRotation(new Cog3DEulerXYZ(0, 0, r.Angle));
            box = box.MapShape(new Cog3DTransformRigid(rotation, centerOfResultInMM)) as Cog3DBox;
            //#6. create the graphic
            if (box != null)
            {
              Cog3DBoxGraphic boxGraph = new Cog3DBoxGraphic(box);
              boxGraph.Opacity = 0.5;
              boxGraph.Color = CogColorConstants.Green;
              boxGraph.DisplayState = Cog3DGraphicDisplayStateConstants.SurfaceWithWireFrame;
              $Pages.Page.Display3DResultImage.AddGraphics(boxGraph, rangeGraph, $Display3D.GroupResultGraphics);
            }
          }
        }
        $Pages.Page.Display3DResultImage.Fit(true);
      }
      else
      {
        $System.Log.Write("ToolBlock failed: " + $Tasks.Task.Task.ToolBlock.ToolBlock.RunStatus.Message);
      }