Hi,
I’m attempting to create preview images of individual pieces of geometry. Based on other posts here, I think this should work, but it doesn’t:
using System;
using System.IO;
using System.Linq;
using Rhino;
using Rhino.Commands;
using Rhino.Display;
namespace Dev.Common
{
[
System.Runtime.InteropServices.Guid("e58debff-8f45-4a1c-8b12-32ed2fe44363"),
CommandStyle(Style.ScriptRunner)
]
public class DevCommand : Command
{
public DevCommand()
{
// Rhino only creates one instance of each command class defined in a
// plug-in, so it is safe to store a refence in a static property.
Instance = this;
}
///<summary>The only instance of this command.</summary>
public static DevCommand Instance { get; private set; }
public override string EnglishName => "MyDevCommand";
protected override Result RunCommand(Rhino.RhinoDoc doc, RunMode mode)
{
// Assumes at least one object is selected.
var selectedId = doc.Objects.GetSelectedObjects(false, false).Select(o => o.Id).First();
if (GenerateImage(selectedId, doc))
{
RhinoApp.WriteLine("Created image");
return Result.Success;
} else
{
RhinoApp.WriteLine("Failed creating image");
return Result.Failure;
}
}
private static bool GenerateImage(Guid guid, RhinoDoc doc)
{
try
{
// Create a new RhinoView
RhinoView rhinoView = doc.Views.Add(
"Preview",
DefinedViewportProjection.Perspective,
new System.Drawing.Rectangle(0, 0, 512, 512),
true
);
// Get a tmp file path
var filePath = Path.Combine(Path.GetTempFileName(), ".bmp");
// Zoom Selected
var zoomSuccess = rhinoView.MainViewport.ZoomExtentsSelected();
// Make sure the view is updated..
rhinoView.Redraw();
// Save a preview image
return rhinoView.CreateShadedPreviewImage(filePath, new System.Drawing.Size(512, 512), true, false, false);
} finally
{
// Clean up the view
var views = doc.Views.Where(v => v.MainViewport.Name == "Preview").ToList();
foreach (var v in views)
{
v.Close();
}
}
}
}
}
Can anyone spot what I am doing wrong here? I’ve tried with .jpeg
, .png
, and without extension as well. I’m on Mac, and Rhino 6.
Thanks,
Aske