Capture specific view to TIF file on macOS

Hello,
I need to capture from code a specific viewport (even if this viewport is currently hidden), on Rhino for macOS.
I have tried many ways so far, but none of them seemed to work.

1 - RhinoCommon API ViewCapture.CaptureToBitmap()

Although this works like a charm on Rhino for Windows, I have issues exporting the correct viewport on Mac.

I don’t know if the problem is related to setting the correct ActiveViewport PRIOR to capturing the scene, but what I obtain is only and always the current active viewport.
What I do on Windows is:

//Create a temporary view for the capture
var view = m_doc.Views.Add("bbview", DefinedViewportProjection.Top, new Rectangle(new System.Drawing.Point(0, 0), size), true);

//Setting the size again will snug up the camera.
view.Size = size;

view.ActiveViewport.ZoomBoundingBox(viewBounds);

// 600dpi to dot per mm
var dpmm = 600.0 / 25.4;

var captureWidth = (int)(width / scale * dpmm);
var captureHeight = (int)(height / scale * dpmm);
      
// This performs the actual job (the result can either be a capture bitmap or the result of a scripted command).
var viewCapture = new ViewCapture() {
   DrawAxes = false,
   DrawGrid = false,
   DrawGridAxes = false,
   TransparentBackground = true,
   Width = captureWidth,
   Height = captureHeight
};
              
var bmp = viewCapture.CaptureToBitmap(view);
      
//close the temp view
view.Close();

If I do the same on Mac, the captured image is not taken from the “top” view, but rather from the current one (e.g. perspective).

2 - Scripted -_ViewCapture command

I also tried the scripted version.
To try to mimic the above code, I was doing something like:

// Change the current active viewport to the one I want.
var viewportChanged = RhinoApp.RunScript("-_SetActiveViewport Top", echo: true);

// Capture...
var captured = RhinoApp.RunScript("-_ViewCaptureToFile ... <some options>", echo: true);

But the captured boolean result is always false and the view is not captured.
Any idea?

3 - Scripted -_Print

I managed to export a given view with “Rhino PDF" printer, with the exact layout I want.
The problem is that I need a *.tif file as a result, rather than a PDF.

I can provide as printer name ”Image File (PNG, BMP, TIF, …)” and set the other options, but when I send the final Go option to specify the target filename, Rhino shows the following dialog:

I don’t know what else I can try.
Is there any workaround that possibly comes to your mind?

Thank you.

Any updates on this @andy? :slight_smile:

Hi @p.stragliotto,

Can you post the code to a test command, that I can run here, that reproduces the problem?

You are using Rhino 8? System info might be helpful.

Thanks,

– Dale

Sorry for the late reply, here below is some test code that can be run in the C# Script editor:

// #! csharp
using System;
using Rhino.Display;
using System.Drawing;

var doc = Rhino.RhinoDoc.ActiveDoc;

//By default, Zoom commands have a 1.1 padding around them.
//We can temporarily override this padding by setting it to 0
//Make sure we set it back when finished.
Rhino.ApplicationSettings.ViewSettings.ZoomExtentsParallelViewBorder = 0;

//Disable view drawing while we work
doc.Views.RedrawEnabled = false;

var viewBounds = doc.Objects.BoundingBox;

var width  = viewBounds.Diagonal.X;
var height = viewBounds.Diagonal.Y;

var size = new Size((int)width, (int)height);

//Create a temporary view for the capture
var view = doc.Views.Add("bbview", DefinedViewportProjection.Top, new Rectangle(new System.Drawing.Point(0, 0), size), true);

//Setting the size again will snug up the camera.
view.Size = size;

view.ActiveViewport.ZoomBoundingBox(viewBounds);

// 600dpi to dot per mm
var dpmm = 600.0 / 25.4;

var captureWidth = (int)(width / dpmm);
var captureHeight = (int)(height / dpmm);

// This performs the capture.
var viewCapture = new ViewCapture() {
    DrawAxes = false,
    DrawGrid = false,
    DrawGridAxes = false,
    TransparentBackground = true,
    Width = captureWidth,
    Height = captureHeight
};
        
var bmp = viewCapture.CaptureToBitmap(view);

//close the temp view
view.Close();

//Reset the zoom padding
Rhino.ApplicationSettings.ViewSettings.ZoomExtentsParallelViewBorder = 1.1;

doc.Views.RedrawEnabled = true;

if (bmp == null) {
    Rhino.RhinoApp.WriteLine("View capture FAILED");
} else {
    Rhino.RhinoApp.WriteLine("View capture SUCCEEDED");
}

Tested on:

  • Mac Studio 2022 (processor Apple M1 Max)
  • macOS Tahoe 26.5.1
  • Rhino 8.32 (8.32.26160.13002)

This will consistently fail to create a valid bitmap with viewCapture.CaptureToBitmap(view).
Same code successfully creates a captured bitmap on Windows.

Thanks for your help :slight_smile:

@rajaa - can you help with this?

Same hardware (Mac Studio 2022 - Apple M1 Max)
Rhino Version 8.36
macOS Golden Gate 27.0.1

Still viewCapture.CaptureToBitmap() fails.
Any news?

@dale @rajaa

@p.stragliotto - does the ViewCaptureToCiipboard/File commands work?

– Dale

Yes they do.

Hi @p.stragliotto,

Your test script computes the capture size as width / dpmm, which produces a width or height of 0 for most models, and CaptureToBitmap returns null when that happens. Try width * dpmm and height * dpmm.

Also, give the temporary view a fixed pixel size (e.g. 800×600) rather than using the model dimensions as pixels.

If it still fails after that, let us know.

– Dale

That was failing again - the size computation was actually wrong - my bad;
but the size was never zero (if that was the problem, the image capture would have failed on Windows as well).

After several tries and with the help of AI analysis I found out that the problem was the following:

// A temporary view (Views.Add) does not work on both platforms: on macOS it is not a real
// window (it is never floating and has a 0x0 size) and CaptureToBitmap always returns null for
// it. On Windows it is a real window, clamped and scaled by the OS, which is why the frustum
// must not be derived from the window (ZoomBoundingBox letterboxed the graphic inside the
// bitmap, differently per machine).

So basically, the issue was caused by adding a custom floating viewport.
But the following works:

  1. store current view settings
  2. customise the view by setting camera, frustum, etc.
  3. capture to bitmap
  4. restore original view settings