Rhinocommon Print Window Size to PDF

Hi,

  1. I have rectangles on the 3d model top view.
  2. These rectnagles are A4 paper size.
  3. I need to automatically print that A4 paper window using the corners from the rectangle.

Now getting the corners isnt hard, nor identifying the rectangles.
I am having trouble printing.

I have :
-coordinate p1,p2 where p(x) is (x,y,z)

I need to :
-Use rhinocommon to print the rectangle defined by the p1,p2 points

How can I print that area in ironpython rhinocommon? I can’t find anything useful, I find viewport to pdf/bmp but its not the proper print.

Using rhino7
@dale @nathanletwory

Sorry If I am bothering you a bit these days but im approaching some deadlines, and thanks for the help as always to anyone who has any ideas

Carletto

sample.3dm (33.2 KB)
Sample 3dm file attached with the rectangles and some shapes inside that need printing

Can’t say I have printed from Rhino before, let alone from code. I’ll leave it to @dale to answer this.

Rhino 7 has far less printing abilities via RhinoCommon than Rhino 8 does however, you should be able to do what you’re describing.

You’ll need to configure a ViewCaptureSettings object with your scene settings

https://developer.rhino3d.com/api/rhinocommon/rhino.display.viewcapturesettings

To use the “Window” Selection you’ll want to have a look at SetWindowRect method on the ViewCaptureSettings class.

https://developer.rhino3d.com/api/rhinocommon/rhino.display.viewcapturesettings/setwindowrect

Once your ViewCaptureSettingsObject is configured with all of the property’s you want set you can then use the PDF writer by passing the ViewCaptureSettings object to it.

https://developer.rhino3d.com/api/rhinocommon/rhino.fileio.filepdf/addpage#(viewcapturesettings)

Below is a basic example that prompts the user to draw a rectangle and then prints the contents in raster to a pdf on an A4 size paper. You can skip the A4 paper size creation and just use your rectangle dimensions.

import Rhino
import Rhino.UI
import System.Drawing as sd
import Rhino.Geometry as rg

def capture_rectangle_to_a4_pdf():
    # Prompt the user to select a rectangle in world coordinates
    rc, corners = Rhino.Input.RhinoGet.GetRectangle()
    if rc != Rhino.Commands.Result.Success or corners is None:
        return

    # Find the active viewport that the rectangle was selected in
    view = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView
    if not view:
        return

    # Convert from world to client coordinates
    client_points = [view.ActiveViewport.WorldToClient(pt) for pt in corners]

    # Create a bounding box from the client points
    client_points_3d = [rg.Point3d(p.X, p.Y, 0) for p in client_points]
    bb = rg.BoundingBox(client_points_3d)

    # Create Point2d objects directly from the bounding box min and max
    top_left = rg.Point2d(bb.Min.X, bb.Min.Y)
    bottom_right = rg.Point2d(bb.Max.X, bb.Max.Y)

    # Determine the rectangle size in pixels
    rect_width = int(bb.Max.X - bb.Min.X)
    rect_height = int(bb.Max.Y - bb.Min.Y)

    # Create an A4 paper size in inches in landscape
    a4_width = 11.69
    a4_height = 8.27
    dpi = 300

    # Convert A4 inches to pixels
    width_pixels = int(a4_width * dpi)
    height_pixels = int(a4_height * dpi)

    # Set up ViewCaptureSettings for A4 page size
    vcs = Rhino.Display.ViewCaptureSettings(view, sd.Size(width_pixels, height_pixels), dpi)
    vcs.RasterMode = True
    vcs.ViewArea = Rhino.Display.ViewCaptureSettings.ViewAreaMapping.Window
    vcs.SetWindowRect(top_left, bottom_right)

    # Prompt for PDF save location
    dlg = Rhino.UI.SaveFileDialog()
    dlg.Filter = "PDF Files (*.pdf)|*.pdf"
    if not dlg.ShowDialog():
        return

    filename = dlg.FileName
    if not filename.endswith(".pdf"):
        filename += ".pdf"

    # Create and write the PDF
    pdf = Rhino.FileIO.FilePdf.Create()
    # Using A4 page size in inches
    if pdf.AddPage(vcs) >= 0:
        pdf.Write(filename)
        print("PDF saved to: " + filename)

capture_rectangle_to_a4_pdf()

2 Likes

A bit of a hack but works indeed.
Thanks @Trav :brain: and @nathanletwory :smiling_face_with_three_hearts:
Carletto

Hey @Trav,
could you please share an example on how to use the Window printing option via RhinoCommon or the c++ SDK?

This test snippet doesn’t seem to return what I’m looking for:

            var pdf = Rhino.FileIO.FilePdf.Create();
            var page = doc.Views.GetPageViews()[0]; // TODO get the proper page
            var origin = page.ClientToScreen(new System.Drawing.Point());
            ViewCaptureSettings settings = new ViewCaptureSettings(page, 96); 
            settings.SetWindowRect(new Rhino.Geometry.Point2d(origin.X, origin.Y), new Rhino.Geometry.Point2d(origin.X + 500, origin.Y + 500));

            pdf.AddPage(settings);
            pdf.Write("C:\\testPrint.pdf");

@mrhe see the sample above your post.

1 Like