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

**URL:** <https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712>\
**Category:** Ruby API\
**Tags:** ruby, printing, rubyapi\
**Created:** [March 12, 2020, 4:57pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712 "2020-03-12T16:57:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjwelborn](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/cjwelborn/32/85792_2.png) [@cjwelborn](https://forums.sketchup.com/u/cjwelborn)\
**Post date:** [March 12, 2020, 4:57pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/1 "2020-03-12T16:57:21Z")

</div>

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](https://forums.sketchup.com/uploads/short-url/z0oDwHG1TSwWVBivcfPD29rFYaW.skp) (3.3 MB)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [March 13, 2020, 12:28pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/2 "2020-03-13T12:28:00Z")

</div>

> [@cjwelborn](#):
>
> 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.

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](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348)

> [@cjwelborn](#):
>
> Even if I have to export the view as an Image, …

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

- [`Sketchup::View.write_image()`](http://ruby.sketchup.com/Sketchup/View.html#write_image-instance_method)

Examples …

[[Code] Example: Saving Iso views to PNG image files](https://forums.sketchup.com/t/code-example-saving-iso-views-to-png-image-files/56762)

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

> [@Write Image as Standard camera views in Ruby](https://forums.sketchup.com/t/write-image-as-standard-camera-views-in-ruby/74700/11):
>
> ```ruby
> def save_view_to_png()
> #
> path = UI.savepanel("Save PNG Image As","write_image.png")
> return unless path
> view = Sketchup.active_model.active_view
> view.write_image(
> filename: path,
> width: view.vpwidth,
> height: view.vpheight,
> antialias: true,
> transparent: true
> )
> #
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![cjwelborn](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/cjwelborn/32/85792_2.png) [@cjwelborn](https://forums.sketchup.com/u/cjwelborn)\
**Post date:** [March 13, 2020, 6:39pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/3 "2020-03-13T18:39:43Z")

</div>

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](https://forums.sketchup.com/t/use-the-ruby-code-to-simulate-a-mouse-click-on-the-sub-menu-in-menu/71348) that you linked to. It seems like it might be less code to write. Thanks again for that.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [March 13, 2020, 7:12pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/4 "2020-03-13T19:12:49Z")

</div>

Ruby has `%x` strings (choose whatever delimiter you like,) which are passed to the [`Kernel` backquote](https://ruby-doc.org/core-1.8.6/Kernel.html#method-i-60) 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()`](https://ruby-doc.org/core-1.8.6/Kernel.html#method-i-system) method.

Also again with the `WIN32OLE` class, you can leverage the Windows Scripting Host’s [Shell object](https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/aew9yb99%28v%3dvs.84%29) methods that can start external processes.

Ex:

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

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

```

> [@cjwelborn](#):
>
> It’s **SketchUp 8.0.16846**.

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.

> **[Release SketchUp Extension RBZ archive of Ruby 1.8.6 p287 Standard Library ·...](https://github.com/DanRathbun/sketchup-ruby186-stdlib-extension/releases/tag/2)**
>
> Platform : MS Windows ONLY
> SketchUp versions : 8 .. 13
> (... and 7.x, if you manually upgrade the "msvcrt-ruby18.dll" to one from Ruby 1.8.6-1.8.7)
> Installs the Ruby Standard Library into a "Plugins...

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`](https://ruby-doc.org/stdlib-1.8.6/libdoc/win32ole/rdoc/WIN32OLE.html) and the `Win32API` script that you might use to make Windows C SDK calls (if so inclined.)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [March 13, 2020, 7:48pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/5 "2020-03-13T19:48:43Z")

</div>

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

```ruby
# "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

```

---

<div class="post-metadata">

**Author:** ![cjwelborn](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/cjwelborn/32/85792_2.png) [@cjwelborn](https://forums.sketchup.com/u/cjwelborn)\
**Post date:** [March 13, 2020, 8:23pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/6 "2020-03-13T20:23:32Z")

</div>

You’re awesome. Thank you.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [March 13, 2020, 8:50pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/7 "2020-03-13T20:50:06Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![cjwelborn](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/cjwelborn/32/85792_2.png) [@cjwelborn](https://forums.sketchup.com/u/cjwelborn)\
**Post date:** [March 18, 2020, 8:38pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/8 "2020-03-18T20:38:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [March 18, 2020, 9:23pm UTC](https://forums.sketchup.com/t/ruby-code-to-print-multiple-copies-of-the-current-view-in-landscape/118712/9 "2020-03-18T21:23:59Z")

</div>

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