=begin
#-------------------------------------------------------------------------------------------------------------------------------------------------
#*************************************************************************************************
# Designed July 2010 by Fredo6

# Permission to use this software for any purpose and without fee is hereby granted
# Distribution of this software for commercial purpose is subject to:
#  - the expressed, written consent of the author
#  - the inclusion of the present copyright notice 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			:   bootstrap_TopoShaper.rb
# Original Date	:   11 July 2010 - version 1.0
# Description	:   Loader for the TopoShaper scripts
# Usage			:   See Tutorial and Quick Ref Card in PDF format
#-------------------------------------------------------------------------------------------------------------------------------------------------
#*************************************************************************************************
=end

module F6_TopoShaper

#---------------------------------------------------------------------------------------------------------------------------
# Overall Configuration for the Tools	
#---------------------------------------------------------------------------------------------------------------------------

MENU_PERSO = [:T_MenuTools_Fredo6Collection, "Tools", :T_MenuPlugins_Fredo6Collection, "Plugins"]

#---------------------------------------------------------------------------------------------------------------------------
#Constants for TOS  Modules (do not translate here, use Translation Dialog Box instead)	
#---------------------------------------------------------------------------------------------------------------------------

#Menu and Toolbar icons
T6[:TPS_QUADTERRAIN_Menu] = "Generate QuadMesh Terrain (Isocontours)"
T6[:TPS_QUADTERRAIN_Tooltip] = "Generate terrain from iso-contours with Quad mesh"
TPS_QUADTERRAIN_Icon = "QuadTerrain"
T6[:TPS_EDITTERRAIN_Menu] = "Edit Terrain"
T6[:TPS_EDITTERRAIN_Tooltip] = "Edit a previously generated terrain"

T6[:TPS_CLOUD_Menu] = "Generate Trimesh Terrain (Cloud)"
T6[:TPS_CLOUD_Tooltip] = "Generate terrain from Cloud of Points with Triangular mesh"
TPS_CLOUD_Icon = "Cloud"

#--------------------------------------------------------
# Default Parameters
#--------------------------------------------------------

T6[:TPS_DEFAULT_SECTION_General] = "Common Parameters"
T6[:TPS_UseColorTerrain] = "Use Color for the generated Terrain"
T6[:TPS_ColorTerrain] = "Color for the generated Terrain"
T6[:TPS_UseColorIso] = "Use Color for the generated Iso-Contours"
T6[:TPS_ColorIso] = "Color for the generated Iso-Contours"
T6[:TPS_UseColorSkirt] = "Use Color for the generated Skirt"
T6[:TPS_ColorSkirt] = "Color for the generated Skirt"

T6[:TPS_DEFAULT_SECTION_Contour] = "Parameters for TopoShaper::IsoContour"
T6[:TPS_OPTION_Debug] = "Debug option to show algorithm details"
T6[:TPS_OPTION_ZoomInSitu] = "Automatic Zooming on generated terrain"

T6[:TPS_DEFAULT_SECTION_Cloud] = "Parameters for TopoShaper::PointsCloud"
T6[:TPS_ColorSharp] = "Color for Sharp edges"
T6[:TPS_ColorVerySharp] = "Color for Very Sharp edges"
T6[:TPS_ColorTinyVerySharp] = "Color for Very Sharp but Tiny edges"
T6[:TPS_OPTION_SkirtEnabled] = "Include Skirt in Generated terrain"
T6[:TPS_OPTION_IsoEnabled] = "Include Iso-Contours in Generated terrain"
T6[:TPS_OPTION_PointsEnabled] = "Include Cloud Points in Generated terrain"
T6[:TPS_OPTION_PointsLine] = "Lead Guide lines for Cloud Points"
T6[:TPS_OPTION_Roundness] = "Roundness factor"
T6[:TPS_OPTION_DeviationMax] = "Smoothing factor in degree"
T6[:TPS_OPTION_TrianglesMax] = "Maximum number of triangles"

#---------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------
# EXTERNAL API to launch RoundCorner menus
# Action code must be passed as a symbol
#---------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------
	
def F6_TopoShaper.launch_action(action_code)
	MYPLUGIN.launch_action action_code
end

#--------------------------------------------------------------
# Condition for contextual menu selection
#--------------------------------------------------------------

def F6_TopoShaper.selection_contextual?(mode)
	selection = Sketchup.active_model.selection
	return nil if selection.empty?
	
	#Selection of an existing terrain group
	lst_groups = selection.grep(Sketchup::Group)
	lst_groups.each do |group|
		return :edit if mode ==:iso && group.attribute_dictionary("TOPOSHAPER", false)
	end
	
	lsent = lst_groups.collect { |g| g.entities }
	lsent += selection.grep(Sketchup::ComponentInstance).collect { |c| c.definition.entities }
	
	#Selection if a group or a component instance with edges
	if mode == :iso
		ent_proc = proc { |e| e.instance_of?(Sketchup::Edge) && e.faces.length == 0 }
		vmin = 10
	elsif mode == :cloud	
		ent_proc = proc { |e| e.instance_of?(Sketchup::ConstructionPoint) }
		vmin = 3
	end	
	lsent.each do |entities|
		lent = entities.find_all { |e| ent_proc.call(e) }
		return :ok if lent.length >= vmin
	end
	nil
end

#--------------------------------------------------------
# Startup routine (called from LibFredo6)
#--------------------------------------------------------
	
#Create all commands with menus and toolbar icons
def F6_TopoShaper.startup	

	#Conditions for contextual menus
	tc_edit_iso = Traductor::TestCondition.new() { F6_TopoShaper.selection_contextual?(:iso) == :edit }
	tc_iso = Traductor::TestCondition.new() { F6_TopoShaper.selection_contextual?(:iso) == :ok }
	tc_cloud = Traductor::TestCondition.new() { F6_TopoShaper.selection_contextual?(:cloud) == :ok }
	submenu = "TopoShaper"
	
	#Top menu
	MYPLUGIN.declare_topmenu nil, MENU_PERSO

	#Declaring command icons
	MYPLUGIN.declare_separator
	MYPLUGIN.declare_command(:TPS_QUADTERRAIN_) { F6_TopoShaper.launch_action :quad_terrain }
	MYPLUGIN.declare_context_handler(:TPS_EDITTERRAIN_, tc_edit_iso, nil, submenu) { F6_TopoShaper.launch_action :quad_terrain }
	MYPLUGIN.declare_context_handler(:TPS_QUADTERRAIN_, tc_iso, nil, submenu) { F6_TopoShaper.launch_action :quad_terrain }

	MYPLUGIN.declare_command(:TPS_CLOUD_) { F6_TopoShaper.launch_action :cloud }
	MYPLUGIN.declare_context_handler(:TPS_CLOUD_, tc_cloud, nil, submenu) { F6_TopoShaper.launch_action :cloud }
	
	#Declaring the default parameter
	F6_TopoShaper.default_param
	
	#Startup of the Plugin
	MYPLUGIN.go	
end
	
#--------------------------------------------------------
# Service API
#--------------------------------------------------------
	
def F6_TopoShaper.api_isocontour_calculation(lst_contours, hsh_options=nil)
	MYPLUGIN.load_full_rubies
	F6_TopoShaper.api_isocontour_calculation_protected(lst_contours, hsh_options)
end
	
#--------------------------------------------------------
# Default Parameters
#--------------------------------------------------------
	
#Declaring the Default Parameters	
def F6_TopoShaper.bounds_param ; @hsh_bounds ; end
def F6_TopoShaper.default_param
	dp = MYDEFPARAM
	dp.separator :TPS_DEFAULT_SECTION_General
	dp.declare :TPS_UseColorTerrain, true, "B"
	dp.declare :TPS_ColorTerrain, "lightgreen", "K"
	dp.declare :TPS_UseColorIso, true, "B"
	dp.declare :TPS_ColorIso, "green", "K"
	dp.declare :TPS_UseColorSkirt, true, "B"
	dp.declare :TPS_ColorSkirt, "sandybrown", "K"
	
	dp.separator :TPS_DEFAULT_SECTION_Contour
	dp.declare :TPS_OPTION_Debug, true, "B"	
	dp.declare :TPS_OPTION_ZoomInSitu, true, "B"
	
	@hsh_bounds = { :roundness_min => 1, :roundness_max => 4,
	                :deviation_max_min => 5, :deviation_max_max => 45,
				    :triangles_max_min => 100, :triangles_max_max => 100000 }
				   
	dp.separator :TPS_DEFAULT_SECTION_Cloud
	dp.declare :TPS_OPTION_Roundness, 3, "F:>=#{@hsh_bounds[:roundness_min]}<=#{@hsh_bounds[:roundness_max]}"
	dp.declare :TPS_OPTION_DeviationMax, 20, "I:>=#{@hsh_bounds[:deviation_max_min]}<=#{@hsh_bounds[:deviation_max_max]}"
	dp.declare :TPS_OPTION_TrianglesMax, 10000, "I:>=#{@hsh_bounds[:triangles_max_min]}<=#{@hsh_bounds[:triangles_max_max]}"
	
	dp.declare :TPS_OPTION_IsoEnabled, false, "B"
	dp.declare :TPS_OPTION_SkirtEnabled, true, "B"
	dp.declare :TPS_OPTION_PointsEnabled, false, "B"
	dp.declare :TPS_OPTION_PointsLine, true, "B"
	dp.declare :TPS_ColorSharp, "darkorange", "K"
	dp.declare :TPS_ColorVerySharp, "red", "K"
	dp.declare :TPS_ColorTinyVerySharp, "lightpink", "K"
end
		
end	#End Module F6_TopoShaper
