Hi everyone!
I am trying to use the rhino viewport to visualize many images. I found an old @DavidRutten C# component that is almost perfect for me. However, it only repeats on the viewport the first image of a list. I spent a few hours trying to make it work on many images, but I didn´t manage.
How can I do it?
using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using System.IO;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using Rhino.DocObjects;
using Rhino.Collections;
using GH_IO;
using GH_IO.Serialization;
/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
/// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
/// <param name="text">String to print.</param>
private void Print(string text) { /* Implementation hidden. */ }
/// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
/// <param name="format">String format.</param>
/// <param name="args">Formatting parameters.</param>
private void Print(string format, params object[] args) { /* Implementation hidden. */ }
/// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj) { /* Implementation hidden. */ }
/// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion
#region Members
/// <summary>Gets the current Rhino document.</summary>
private readonly RhinoDoc RhinoDocument;
/// <summary>Gets the Grasshopper document that owns this script.</summary>
private readonly GH_Document GrasshopperDocument;
/// <summary>Gets the Grasshopper script component that owns this script.</summary>
private readonly IGH_Component Component;
/// <summary>
/// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
/// Any subsequent call within the same solution will increment the Iteration count.
/// </summary>
private readonly int Iteration;
#endregion
/// <summary>
/// This procedure contains the user code. Input parameters are provided as regular arguments,
/// Output parameters as ref arguments. You don't have to assign output parameters,
/// they will have a default value.
/// </summary>
private void RunScript(System.Object Tx, Point3d P, ref object A)
{
Bitmap texture = Tx as Bitmap;
if (texture == null) return;
if (_bitmap == null)
_bitmap = new Rhino.Display.DisplayBitmap(texture);
if (_points == null)
_points = new List<Point3d>();
_points.Add(P);
}
// <Custom additional code>
private Rhino.Display.DisplayBitmap _bitmap;
private List<Point3d> _points;
/// <summary>
/// This method will be called once every solution, before any calls to RunScript.
/// </summary>
public override void BeforeRunScript()
{
if (_bitmap != null)
{
_bitmap.Dispose();
_bitmap = null;
}
_points = null;
}
//Draw all meshes in this method.
public override void DrawViewportMeshes(IGH_PreviewArgs args)
{
if (_bitmap == null) return;
if (_points == null) return;
if (_points.Count == 0) return;
foreach (Point3d point in _points)
{
Point2d client = args.Viewport.WorldToClient(point);
int x = (int) (client.X);
int y = (int) (client.Y);
args.Display.DrawBitmap(_bitmap, x, y);
}
}
// </Custom additional code>
}
Image.gh (10.4 KB)