How can I close an opened Layout file in ruby script?

How can I close an opened Layout file in ruby script like #quit in Sketchup?
And how can I check a layout file is opened. Something like File.is_opened? filePath

Sketchup.quit does not close a model file, but the application. (Notice that it’s a singleton method . not an instance method # of a Sketchup::Model instance).

As far as I’m aware, the Ruby API does not control the LayOut GUI application, but writing/modifying LayOut documents from within SketchUp. It seems opened document references are automatically closed when they get out of scope, without explicitely needing to close the document reference. But you should explicitely save changes to the LayOut document.

2 Likes

I’ll second what Aerilius has said …

… as being currently true.

Also, the LayOut application has an MDI interface, which can have multiple documents open.
We can use a “hack” on Windows to close the currently open LayOut application, but not a specific document that it may have open.

LayOut_Control_from_SketchUp.rb (1.8 KB)

LayOut will prompt the user to save any modified .layout documents that might be open.

Caveat: The close would fail if the user has redefined ALT+F4 in LayOut or systemwide.
To workaround this, Windows system calls can be made via the Fiddle library. (not shown here)

(... If you'd rather not download the file, expand to see code ...)
# encoding: UTF-8
#
# File: LayOut_Control_from_SketchUp.rb

module SomeAuthor # <<----<<<< Replace with your unique toplevel namespace
  module LO

    require 'win32ole' unless defined?(WIN32OLE)

    extend self

    CAPTION ||= 'LayOut'

    # NOTE: Some Windows Scripting Host methods need quoted string arguments.

    PATH ||= "\"#{File.join(
      File.dirname(Sketchup.find_support_file('Tools')),
      'LayOut/LayOut.exe')}\""

    @debug = false

    # Set debug flag when testing.
    def debug( arg = true )
      @debug = arg ? true : false
    end

    # Activate the LayOut application window, if any.
    # Returns boolean whether the window was found and activated.
    def activate
      #
      WIN32OLE.new('WScript.Shell').AppActivate( CAPTION )
      #
    end ###

    # Close the currently running LayOut instance.
    def close
      #
      activated = activate()
      puts "Activate result: #{activated.inspect}" if @debug
      #
      if activated
        UI.start_timer(1.0, false) {
          result = WIN32OLE.new('WScript.Shell').SendKeys( '"%{F4}"' ) # ALT+F4
          puts "SendKeys result: #{result.inspect}" if @debug
          nil
        }
        return true
      else
        return false
      end
      #
    end ###

    # Run an instance of the LayOut application.
    # If LO is running, this is a no-op.
    def run
      # returns 0 when the WaitOnReturn arg is defaulting to false:
      WIN32OLE.new('WScript.Shell').Run(PATH)
      nil # suppress the meaningless 0 return value
      #
    end ###

    # Send the current model file to LayOut. The file must have been saved.
    # If not, false is returned and this is a no-op.
    def send
      Sketchup::send_to_layout(Sketchup.active_model.path)
    end

  end # module LO
end

(…scroll to see all code…)


REFERENCES:

1 Like