rubytextmodulestdoutsketchup

Create a plugin that puts text in a .txt file


I am working on creating a plugin in Ruby. On this moment I am unable to insert the coordinates, that are added to a Sketchup model, in a .txt file.

This is my code:

require 'sketchup.rb'
SKETCHUP_CONSOLE.show rescue Sketchup.send_action("showRubyPanel:")
$stdout = File.new('file.txt', 'w')

module HWmakemyownplug
  def self.fileplug
    model = Sketchup.active_model 
    #Make some coordinates.
    coordinates = [[2,0,39],[0,0,1],[1,1,0]]
    #Add the points in Sketchup. This works!
    coordinates.each { |point| model.active_entities.add_cpoint(point) }
    #Puts the coordinates to the textfile 'file.txt'. This doesn't work!
    $stdout.puts(coordinates)   

  end #def self.fileplug
end #module makemyownplug

if (!file_loaded?(__FILE__))

  #Add to the SketchUp tools menu
  extensions_menu = UI.menu("Plugins")
  extensions_menu.add_item("testtesttest") { HWmakemyownplug::fileplug }
  # Let Ruby know we have loaded this file
  file_loaded(__FILE__)

end

The coordinates have to be printed when I click on menu > plugins > testtesttest.


Solution

  • You forgot to close file after $stdout.puts(coordinates)

    $stdout.close