### Loads Tools and makes Menu & Toolbars etc...
###
require('deBabelizer.rb')
load '2Dtools/2Dtidyup.rb' ### obsolete
load '2Dtools/2DzplaneTool.rb'
load '2Dtools/2DlineTool.rb'
load '2Dtools/2DrectangleTool.rb'
load '2Dtools/2DfreehandTool.rb'
load '2Dtools/2DarcTool.rb'
load '2Dtools/2DcircleTool.rb'
load '2Dtools/2DpolygonTool.rb'
load '2Dtools/2DfacemakerTool.rb'
load '2Dtools/2DhatchingTool.rb'
load '2Dtools/2DfilletTool.rb'
load '2Dtools/2DadjustTool.rb'
load '2Dtools/2DlinestyleTool.rb'
load '2Dtools/2DtextTool.rb'
load '2Dtools/2DpolylineeditTool.rb'
###
module Tools2D
 class Setup2Dtools
  ###
  def db(string)
    dir=File.dirname(__FILE__) ### fix v4.1
    toolname="2Dtools"
    locale=Sketchup.get_locale.upcase
    path=File.join(dir, toolname+locale+".lingvo")
    unless File.exist?(path)
      return string
    else
      deBabelizer(string,path)
    end#if
  end#def db
  ###
  def initialize()
    ### get/set help file
    locale=Sketchup.get_locale.upcase
    help_locale=File.join(File.dirname(__FILE__), "2DtoolsHelp"+locale+".htm")
    if FileTest.exist?(help_locale)
      help=help_locale
    else
      help=File.join(File.dirname(__FILE__), "2DtoolsHelp.htm")
    end#if
    ###
    toolbar=UI::Toolbar.new(db("2D Tools"))
    ###
	cmd0=UI::Command.new(db("2D Set Z-plane")){Sketchup.active_model.select_tool Zplane2D.new}
	cmd0.small_icon="Icons/zplane2D_small.png"
	cmd0.large_icon="Icons/zplane2D.png"
	cmd0.tooltip=db("2D Set Z-plane")
	toolbar.add_item cmd0
    ###
	cmd1=UI::Command.new(db("2D Line")){Sketchup.active_model.select_tool LineTool2D.new}
	cmd1.small_icon="Icons/linetool2D_small.png"
	cmd1.large_icon="Icons/linetool2D.png"
	cmd1.tooltip=db("2D Line")
	toolbar.add_item cmd1
    ###
	cmd2=UI::Command.new(db("2D Rectangle")){Sketchup.active_model.select_tool RectangleTool2D.new}
	cmd2.small_icon="Icons/rectangletool2D_small.png"
	cmd2.large_icon="Icons/rectangletool2D.png"
	cmd2.tooltip=db("2D Rectangle")
	toolbar.add_item cmd2
    ###
	cmd3=UI::Command.new(db("2D Freehand")){Sketchup.active_model.select_tool FreehandTool2D.new}
	cmd3.small_icon="Icons/freehandtool2D_small.png"
	cmd3.large_icon="Icons/freehandtool2D.png"
	cmd3.tooltip=db("2D Freehand")
	toolbar.add_item cmd3
    ###
	cmd4=UI::Command.new(db("2D Arc")){Sketchup.active_model.select_tool ArcTool2D.new}
	cmd4.small_icon="Icons/arctool2D_small.png"
	cmd4.large_icon="Icons/arctool2D.png"
	cmd4.tooltip=db("2D Arc")
	toolbar.add_item cmd4
    ###
	cmd5=UI::Command.new(db("2D Circle")){Sketchup.active_model.select_tool CircleTool2D.new}
	cmd5.small_icon="Icons/circletool2D_small.png"
	cmd5.large_icon="Icons/circletool2D.png"
	cmd5.tooltip=db("2D Circle")
	toolbar.add_item cmd5
    ###
	cmd5a=UI::Command.new(db("2D Polygon")){Sketchup.active_model.select_tool PolygonTool2D.new}
	cmd5a.small_icon="Icons/polygontool2D_small.png"
	cmd5a.large_icon="Icons/polygontool2D.png"
	cmd5a.tooltip=db("2D Polygon")
	toolbar.add_item cmd5a
    ###
	cmd6=UI::Command.new(db("2D Face Maker")){Sketchup.active_model.select_tool FacemakerTool2D.new}
	cmd6.small_icon="Icons/facemakertool2D_small.png"
	cmd6.large_icon="Icons/facemakertool2D.png"
	cmd6.tooltip=db("2D Face Maker")
	toolbar.add_item cmd6
    ###
	cmd7=UI::Command.new(db("2D Hatching")){Sketchup.active_model.select_tool HatchingTool2D.new}
	cmd7.small_icon="Icons/hatchingtool2D_small.png"
	cmd7.large_icon="Icons/hatchingtool2D.png"
	cmd7.tooltip=db("2D Hatching")
	toolbar.add_item cmd7
    ###
	cmd8=UI::Command.new(db("2D Fillet")){Sketchup.active_model.select_tool FilletTool2D.new}
	cmd8.small_icon="Icons/fillettool2D_small.png"
	cmd8.large_icon="Icons/fillettool2D.png"
	cmd8.tooltip=db("2D Fillet")
	toolbar.add_item cmd8
    ###
	cmd8a=UI::Command.new(db("2D Adjust")){Sketchup.active_model.select_tool AdjustTool2D.new}
	cmd8a.small_icon="Icons/adjusttool2D_small.png"
	cmd8a.large_icon="Icons/adjusttool2D.png"
	cmd8a.tooltip=db("2D Adjust")
	toolbar.add_item cmd8a
    ###
	cmd9=UI::Command.new(db("2D Line Style")){Sketchup.active_model.select_tool LinestyleTool2D.new}
	cmd9.small_icon="Icons/linestyletool2D_small.png"
	cmd9.large_icon="Icons/linestyletool2D.png"
	cmd9.tooltip=db("2D Line Style")
	toolbar.add_item cmd9
    ###
	cmd10=UI::Command.new(db("2D Text")){Sketchup.active_model.select_tool TextTool2D.new}
	cmd10.small_icon="Icons/texttool2D_small.png"
	cmd10.large_icon="Icons/texttool2D.png"
	cmd10.tooltip=db("2D Text")
	toolbar.add_item cmd10
    ###
	cmd11=UI::Command.new(db("2D Polyline Edit")){Sketchup.active_model.select_tool PolylineeditTool2D.new}
	cmd11.small_icon="Icons/polylineedit2D_small.png"
	cmd11.large_icon="Icons/polylineedit2D.png"
	cmd11.tooltip=db("2D Polyline Edit")
	toolbar.add_item cmd11
    ###
	cmd99=UI::Command.new(db("2D Tools Help")){
      if RUBY_PLATFORM =~ /mswin|mingw/ ### it's a PC 
        UI.openURL("file:/"+help)
      else ### =a Mac
        UI.openURL("file:///"+help)
      end#if
    }
	cmd99.small_icon="Icons/help2D_small.png"
	cmd99.large_icon="Icons/help2D.png"
	cmd99.tooltip=db("2D Tools Help")
	toolbar.add_item cmd99
    ###
	toolbar.restore if toolbar.get_last_state.abs==1
    ### add sub-menu for 2D Tools...
    UI.menu("Draw").add_separator
    submenu2D=UI.menu("Draw").add_submenu(db("2D Tools..."))
    ###
    submenu2D.add_item(cmd0)
    submenu2D.add_item(cmd1)
    submenu2D.add_item(cmd2)
    submenu2D.add_item(cmd3)
    submenu2D.add_item(cmd4)
    submenu2D.add_item(cmd5)
    submenu2D.add_item(cmd5a)
    submenu2D.add_item(cmd6)
    submenu2D.add_item(cmd7)
    submenu2D.add_item(cmd8)
    submenu2D.add_item(cmd8a)
    submenu2D.add_item(cmd9)
    submenu2D.add_item(cmd10)
    submenu2D.add_item(cmd11)
    submenu2D.add_item(cmd99)
    ###
    ### Context Menus
    UI.add_context_menu_handler{|menu|
      ss=Sketchup.active_model.selection
      if TextTool2D.ok
        menu.add_separator
        menu.add_item(db("Edit 2D Text")){TextTool2D.edit()}
      end#if
      if LinestyleTool2D.ok
        menu.add_separator
        menu.add_item(db("Edit 2D Line Style")){LinestyleTool2D.edit()}
      end#if
			model=Sketchup.active_model
			unless model.entities == model.active_entities ### i.e. we are 'editing'
				menu.add_separator
				menu.add_item(db("2D Line Style: Settings...")){LinestyleTool2D.context_dialog()} if model.tools.active_tool_name=~/RubyTool/
				menu.add_item(db("2D Hatching: Choose Hatch PATT jpg...")){HatchingTool2D.context_dialog()} if model.tools.active_tool_name=~/RubyTool/
			end
    } #do menu
  end#def activate
   ###
end#class
###
### adds Toolbar for 2D Tools...
unless file_loaded?(__FILE__)
  Setup2Dtools.new()
end#if
file_loaded(__FILE__)
###
### tidier of obsolete files...
dir=File.dirname(File.dirname(__FILE__))
old=false
rb=File.join(dir, "2D#.rb")
if File.exist?(rb)
	begin
		File.delete(rb)
		old=true
	rescue
	end
end
rb=File.join(dir, "2D#.rb!")
if File.exist?(rb)
	begin
		File.delete(rb)
		old=true
	rescue
	end
end
if old
	UI.messagebox("Please restart SketchUp to complete the reinstallation of 2Dtools...")
end
###
end#module
