Ruby code to print multiple copies of the current view (in Landscape)?

Hello, I use a cabinet design software called “Mozaik” to design/draw cabinets. One of it’s features allows me to send the current room/cabinet to Sketchup (it generates all of the models for every cabinet part and groups them by cabinet, it’s actually kinda cool). I’m using Sketchup 8.0.1, which is installed by Mozaik. What I’m asking seems like it would be pretty simple. I want a piece of ruby code that I can run in the console, or stick in a plugin under a menu item that will simply print the current view X times in landscape orientation. I don’t even care if X is hard-coded for now. I want to skip all of the print dialogs and just print in landscape without any user interaction. I’ve scoured the web and I can’t seem to find anything.

I have experience in Python, C, JavaScript, and a few other languages. Ruby is not one of them. Even if I have to export the view as an Image, and then somehow print that image from ruby, that would be great. In the future, I would add headers for each page (like a different title for each copy). I know that’s possible, but it’s useless without the automated printing.

Anyone have any ideas?

I’ve included one of my project files, in case anyone was wondering. I just want to print whatever is on the screen (in Sketchup) when I run the code.

MozaikExampleDrawing.skp (3.3 MB)

Please verify, you are using SketchUp 8 M2 (ver 8.0.11752) or is it SketchUp 2018 (ver 18.0.16975) ?

As an aside, Mozaik should not be distributing copyrighted software without permission from the copyright holder.

The SketchUp API does not expose functions of the Print dialogs. However, on Windows you can use the WIN32OLE class to leverage the Windows Scripting Host’s SendKeys method. Then perhaps send keys to set the paper orientation and number of copies.

See:
Use the ruby code to simulate a mouse click on the sub-menu in menu

This is easily done, but it writes the images to files.

Examples …

[code] Example: Saving Iso views to PNG image files

In this example from an old topic, the call to open a savepanel could be removed and a fixed path string used instead …

It’s SketchUp 8.0.16846.

Thank you for the PNG function. I guess if I have an image saved to the hard-drive, I could write the “auto printer” in just about anything (and have ruby call it somehow), but I may do the SendKeys thing that you linked to. It seems like it might be less code to write. Thanks again for that.

Ruby has %x strings (choose whatever delimiter you like,) which are passed to the Kernel backquote method. (Beware the SketchUp releases often break the return value from commands run in subshell methods. So we usually have to pipe output into a text file and then read the textfile.)

There is also the Kernel.system() method.

Also again with the WIN32OLE class, you can leverage the Windows Scripting Host’s Shell object methods that can start external processes.

Ex:

# at top of module ...
require "win32ole"

   # Elsewhere ...
    def run_minimized(command)
      shell = WIN32OLE.new("WScript.Shell")
      shell.Run(command,7,false)
    end

Okay, then that version did not come with a Ruby Standard Library. (It was SU2014 that first shipped with Ruby’s Standard Library and Ruby 2.0.)

So for SketchUp 8 and 2013, you’ll need to go get the Ruby 1.8.6 Standard Library.

Never fear, I’ve already packaged it up as a SketchUp extension RBZ archive.

Download it, and use the “Install Extension…” button at the bottom of the Extensions panel, of the Preferences dialog in SU8. Navigate to where you downloaded the RBZ archive and select it, choose OK. (Then I always suggest restarting SketchUp after installing extensions.)


With the Ruby Standard Library, you’ll now have access to the WIN32OLE and the Win32API script that you might use to make Windows C SDK calls (if so inclined.)

Here is a quick example of running an external python script from a SketchUp Ruby plugin …

# "Welborn_AutoPrint_main.rb"

require "win32ole"

module Welborn
  module AutoPrint

    extend self
    
    TOPMENU ||= 'Plugins' # Change to 'File' if desired
    MENUITEM ||= 'AutoPrint...' # Change to suit taste
    
    # Assume "autoprint.py" is in the same folder as THIS FILE:
    PRINTUTILITY ||= File.join(File.dirname(__FILE__),'autoprint.py')

    def run_minimized(command)
      shell = WIN32OLE.new("WScript.Shell")
      shell.Run(command,7,false)
    end

    def save_view_to_png()
      #
      model = Sketchup.active_model
      dir = model.path.empty? ? ENV["USERPROFILE"] : File.dirname(model.path)
      filepath = UI.savepanel("Save PNG Image As",dir,"write_image.png")
      return false unless filepath
      view = model.active_view
      view.write_image(
        filename: filepath,
        width:    view.vpwidth,
        height:   view.vpheight,
        antialias:   true,
        transparent: true
      )
      #
      return filepath
    end

    def autoprint
      path = save_view_to_png()
      return false unless path
      run_minimized("\"#{PRINTUTILITY}\" \"#{path}\"")
    end

    if !@loaded
      UI.menu(TOPMENU).add_item(MENUITEM) { autoprint() }
      @loaded = true
    end

  end
end

You’re awesome. Thank you.

Thanks. Just thinking out loud … you may need quotes around the print utility path (within autoprint method command argument to run_minimized method call,) if there are any spaces in directory names (which there likely are.)

Yes, you’re right about the spaces in the directory names. Customer names and everything. Thanks again.

1 Like

okay, edited the above example and added the embedded escaped quotes.