require 'sketchup.rb'
#-----------------------------------------------------------------------------
#
#  Copyright (C) 2012 Uli Tessel (utessel@gmx.de)
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------

module UTessel_GCodeExport

def self.gcodeSetAttribute( dict, typ, value )
    model=Sketchup.active_model
    what=model.selection
    
    what.each{ |entity| entity.set_attribute dict, typ, value }
end
#--------------------------------------------------------------------------

def self.gcodeDeleteAttribute( dict, typ )
    model=Sketchup.active_model
    what=model.selection
    
    what.each{ |entity| entity.delete_attribute dict, typ }
end
#--------------------------------------------------------------------------

def self.gcodeAddAttribMenu( it, menu, name, dict, key, values )

    current = it.get_attribute( dict, key, "default" )
    sub = menu.add_submenu( name + " (" + current + ")")
    
    sub.add_item("default") { self.gcodeDeleteAttribute(dict, key) }
    
    values.each{ |value| sub.add_item( value) { self.gcodeSetAttribute(dict, key, value) } }
end
#--------------------------------------------------------------------------

def self.gcodeMenu( menu )
    ss = Sketchup.active_model.selection
    #return nil if ss.length!=1
        
    it = ss.first
    return if not ((it.is_a? Sketchup::Group) or (it.is_a? Sketchup::Edge))

    menu.add_separator
    self.gcodeAddAttribMenu( it, menu, "G-Code: Speed", "gcode", "speed", ["F100", "F400", "F800", "F1600" ])
    self.gcodeAddAttribMenu( it, menu, "G-Code: Entry", "gcodehelp", "entry", ["A", "B", "C", "D" ]) if it.is_a? Sketchup::Edge
    self.gcodeAddAttribMenu( it, menu, "G-Code: Exit", "gcodehelp", "exit", ["A", "B", "C", "D" ]) if it.is_a? Sketchup::Edge
    
    return if not (it.is_a? Sketchup::Group)
    self.gcodeAddAttribMenu( it, menu, "G-Code: Tool", "tool", ["T1 M6", "T2 M6"])
end

#--------------------------------------------------------------------------
# Register within Sketchup

unless file_loaded?( __FILE__ )

   UI.add_context_menu_handler do |menu|
       self.gcodeMenu( menu ) 
   end

end

#--------------------------------------------------------------------------
file_loaded( __FILE__ )

end
