Skip to content

Instantly share code, notes, and snippets.

@sbaer
Last active October 20, 2020 18:55
Show Gist options
  • Save sbaer/92b02021c454b15104a8989f77f38753 to your computer and use it in GitHub Desktop.
Save sbaer/92b02021c454b15104a8989f77f38753 to your computer and use it in GitHub Desktop.
Hack in V6 to draw an inactive Grasshopper document
using System;
using System.Collections.Generic;
using System.Drawing;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino;
using Rhino.Commands;
namespace InactiveGhDocDisplay
{
public class InactiveGhDocDisplayCommand : Command
{
bool _conduitsEnabled = false;
public override string EnglishName => "InactiveGhDocDisplay";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
_conduitsEnabled = !_conduitsEnabled; // toggle on/off
if (_conduitsEnabled)
{
Rhino.Display.DisplayPipeline.CalculateBoundingBoxZoomExtents += DisplayPipeline_CalculateBoundingBoxZoomExtents;
Rhino.Display.DisplayPipeline.CalculateBoundingBox += DisplayPipeline_CalculateBoundingBox;
Rhino.Display.DisplayPipeline.PreDrawTransparentObjects += DisplayPipeline_PreDrawTransparentObjects;
}
else
{
Rhino.Display.DisplayPipeline.CalculateBoundingBoxZoomExtents -= DisplayPipeline_CalculateBoundingBoxZoomExtents;
Rhino.Display.DisplayPipeline.CalculateBoundingBox -= DisplayPipeline_CalculateBoundingBox;
Rhino.Display.DisplayPipeline.PreDrawTransparentObjects -= DisplayPipeline_PreDrawTransparentObjects;
}
RhinoApp.WriteLine($"Inactive GH Doc drawing enabled = {_conduitsEnabled}");
doc.Views.Redraw();
return Result.Success;
}
GH_Document GetInactiveDoc(bool buildCache)
{
int docCount = Grasshopper.Instances.DocumentServer.DocumentCount;
if (docCount < 2)
return null;
var ghDoc = Grasshopper.Instances.DocumentServer[1];
if( ghDoc != null && buildCache)
{
var field = ghDoc.GetType().GetField("m_drawList", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (field.GetValue(ghDoc) == null)
{
var createPreviewCaches = ghDoc.GetType().GetMethod("CreatePreviewCaches", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if( createPreviewCaches != null)
{
createPreviewCaches.Invoke(ghDoc, null);
}
}
}
return ghDoc;
}
private void DisplayPipeline_PreDrawTransparentObjects(object sender, Rhino.Display.DrawEventArgs e)
{
var ghDoc = GetInactiveDoc(false);
if (null == ghDoc || !ghDoc.PreviewBoundingBox.IsValid)
return;
var field = ghDoc.GetType().GetField("m_drawList", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var drawList = field.GetValue(ghDoc) as List<IGH_PreviewObject>;
if (null == drawList)
return;
var col0 = Color.FromArgb(ghDoc.PreviewColour.R, ghDoc.PreviewColour.G, ghDoc.PreviewColour.B);
var col1 = Color.FromArgb(ghDoc.PreviewColourSelected.R, ghDoc.PreviewColourSelected.G, ghDoc.PreviewColourSelected.B);
var mat0 = GH_Material.CreateStandardMaterial(ghDoc.PreviewColour);
var mat1 = GH_Material.CreateStandardMaterial(ghDoc.PreviewColourSelected);
int wireThickness = e.Display.DefaultCurveThickness;
var constructors = typeof(GH_PreviewArgs).GetConstructors(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var args0 = constructors[0].Invoke(new object[] {ghDoc, e.Display, e.Viewport, wireThickness, col0, col1, mat0, mat1, ghDoc.PreviewCurrentMeshParameters() }) as GH_PreviewArgs;
// Draw all objects
foreach(var item in drawList)
{
if (item.Hidden)
continue;
item.DrawViewportWires(args0);
}
foreach (var item in drawList)
{
if (item.Hidden)
continue;
item.DrawViewportMeshes(args0);
}
}
private void DisplayPipeline_CalculateBoundingBox(object sender, Rhino.Display.CalculateBoundingBoxEventArgs e)
{
var ghDoc = GetInactiveDoc(true);
if (null == ghDoc)
return;
e.IncludeBoundingBox(ghDoc.PreviewBoundingBox);
}
private void DisplayPipeline_CalculateBoundingBoxZoomExtents(object sender, Rhino.Display.CalculateBoundingBoxEventArgs e)
{
var ghDoc = GetInactiveDoc(true);
if (null == ghDoc)
return;
e.IncludeBoundingBox(ghDoc.PreviewBoundingBox);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment