# Copyright 2013, Trimble Navigation Limited

# This software is provided as an example of using the Ruby interface
# to SketchUp.

# Permission to use, copy, modify, and distribute this software for 
# any purpose and without fee is hereby granted, provided that the above
# copyright notice appear in all copies.

# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#-----------------------------------------------------------------------------
# Name        :   Onion Dome Tool 1.0
# Description :   A script to create onion domes.
# Menu Item   :   Draw->Onion Dome
# Date        :   4/2/2013
# Type        :   Dialog Box
#-----------------------------------------------------------------------------

require 'su_onion_dome/mesh_additions.rb'
require 'su_onion_dome/parametric.rb'
require 'su_onion_dome/bezier.rb'

module Sketchup::Samples::OnionDome

#=============================================================================

class OnionDome < Sketchup::Samples::Parametric

def create_entities(data, container)

    r = data["r"]
    h = data["h"]
    r1 = data["r1"]
    h1 = data["h1"]
    r2 = data["r2"]
    h2 = data["h2"]
    n1 = 24
    n2 = 16

    # Create the Bezier curve to sweep
    pts = []
    pts[0] = [r, 0, 0]
    pts[1] = [r1, 0, h1]
    pts[2] = [r2, 0, h2]
    pts[3] = [0, 0, h]
    curvepts = Sketchup::Samples::Bezier.points(pts, n2)
    
    # create the mesh and revolve the points
    axis = [ORIGIN, Z_AXIS]
    numpts = n1 * n2
    numpoly = n1 * (n2-1)
    mesh = Geom::PolygonMesh.new numpts, numpoly
    mesh.add_revolved_points curvepts, axis, n1
    
    # Create the faces from the mesh
    container.add_faces_from_mesh mesh, 12
    
end

def default_parameters
    defaults = {"r" => 2.feet, "h" => 4.feet, "r1" => 3.feet, "h1" => 2.feet,
                "r2" => 6.inch, "h2" => 3.feet}
    defaults
end

def translate_key(key)
    prompt = key
    case( key )
        when "r"
            prompt = "Radius"
        when "h"
            prompt = "Height"
    end
    prompt
end

end # class OnionDome

#=============================================================================
# Add a menu to create shapes
if (not $oniondome_loaded)
    UI.menu("Draw").add_item("Onion Dome") { OnionDome.new }
    $oniondome_loaded = true
end

end # module Sketchup::Samples::OnionDome
