DrawViewportMeshes could not trigger correctly

Hi Everyone, I written an component named TextDot to create textdot object in grasshopper. recently.
And there have an problem about the component . the problem is that. when i create an textdot object using the component and it could not invoke the DrawViewportMeshes and could not see the preview content in rhino viewport. but when I drag another TextDot Component into grasshopper canvas , something strange has happen, the component that could not draw preview content before become can draw preview content .

the TextDot source code is attach at below

TextDot.cs (3.6 KB)

You are not overriding the clipping box, which Grasshopper preview needs to make sure your previews are fully within the z-buffer.

Does it work if your dot is ‘inside’ some pre-existing geometry in Rhino?

Hi @DavidRutten, It Works When I override Clippingbox properties. Thanks

And could you show me some more detail about the clippingbox and how the clippingobx affect the Grasshopper preview?

using System;
using System.Drawing;
using System.Collections.Generic;

using Rhino.Geometry;
using Grasshopper.Kernel;

namespace CSCECDEC.Okavango.Dimension
{
  public class TextDotObj : GH_Component
  {
    public TextDotObj() : base("TextDot", "TextDot", "Display a collection of text dots", "Display", "Custom") { }

    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
      pManager.AddPointParameter("Point", "P", "Dot location", GH_ParamAccess.item, Point3d.Origin);
      pManager.AddTextParameter("Text", "T", "Dot text", GH_ParamAccess.item);
      pManager.AddColourParameter("Colour", "C", "Dot colour", GH_ParamAccess.item, Color.Crimson);
    }
    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
      pManager.AddGenericParameter("TextDot", "D", "Text dot", GH_ParamAccess.item);
    }

    public override Guid ComponentGuid
    {
      get { return new Guid("24a24263-416b-48c6-a199-680d50e3ffaa"); }
    }

    protected override void BeforeSolveInstance()
    {
      _dots.Clear();
      _fill.Clear();
      _edge.Clear();
    }
    protected override void AfterSolveInstance()
    {
      _box = BoundingBox.Empty;
      foreach (var dot in _dots)
        _box.Union(dot.Point);
    }

    protected override void SolveInstance(IGH_DataAccess access)
    {
      var point = Point3d.Unset;
      var text = string.Empty;
      var colour = Color.Transparent;

      if (!access.GetData(0, ref point)) return;
      if (!access.GetData(1, ref text)) return;
      if (!access.GetData(2, ref colour)) return;

      if (!point.IsValid) return;
      if (string.IsNullOrEmpty(text)) return;
      if (colour.A == 0) return;

      var dot = new TextDot(text, point);
      _dots.Add(dot);
      _fill.Add(colour);
      _edge.Add(colour.GetBrightness() < 0.3 ? Color.White : Color.Black);

      access.SetData(0, dot);
    }

    public override void BakeGeometry(Rhino.RhinoDoc doc, List<Guid> obj_ids)
    {
      for (int i = 0; i < _dots.Count; i++)
      {
        var attributes = doc.CreateDefaultAttributes();
        attributes.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject;
        attributes.ObjectColor = _fill[i];
        obj_ids.Add(doc.Objects.AddTextDot(_dots[i], attributes));
      }
    }

    public override BoundingBox ClippingBox
    {
      get { return _box; }
    }
    public override void DrawViewportWires(IGH_PreviewArgs args)
    {
      // I prefer drawing in wires as dots are usually not transparent.
      // Wires are drawn first so any transparent meshes in front of the dots won't obscure them.

      if (_dots.Count == 0)
        return;

      if (Attributes.Selected)
      {
        var fill = args.WireColour_Selected;
        var edge = fill.GetBrightness() < 0.3 ? Color.White : Color.Black;
        for (int i = 0; i < _dots.Count; i++)
          args.Display.DrawDot(_dots[i], fill, edge, edge);
      }
      else
      {
        for (int i = 0; i < _dots.Count; i++)
          args.Display.DrawDot(_dots[i], _fill[i], _edge[i], _edge[i]);
      }
    }

    private readonly List<TextDot> _dots = new List<TextDot>();
    private readonly List<Color> _fill = new List<Color>();
    private readonly List<Color> _edge = new List<Color>();
    private /*....*/ BoundingBox _box;
  }
}
3 Likes

Thanks ,@David It is Very nice of You, Thanks Again