#author : wikii
#email  : Wikii2008@126.com
#data   :2015-10-21 14:30:35 +0800

require 'sketchup.rb'
require 'ams_Lib.rb'
require 'whdialog' if Sketchup.find_support_file "whdialog.rb"
module Wikii_Tools
   class Sharp_edit_comp
      @@props=[]
      case Sketchup.os_language
      when "zh-CN"
         @@props=%W{组件编辑重载 单独编辑 重新加载 全部重载 请先选择需要更新的组件。 缺少WHDialog组件，无法以对话框模式运行。}
      else
         @@props=["Component Sharp Editor","Edit Component","Reload Component" ,"Reload All Changed Component","First,please select component to be reload ." ,"Component Sharp Editor\nCan't run in dialog mode with out WHDialog."]
      end
      def Sharp_edit_comp.ver
         1.0
      end
      def Sharp_edit_comp.date
         "2015-10-21 14:30:35 +0800"
      end
      def Sharp_edit_comp.props
         @@props
      end
      # 捉虫模式开关
      @@debug=false
      def Sharp_edit_comp.debug
         @@debug=!@@debug
      end
      # 捉虫模式下打印
      def dp *arg
         puts *arg  if @@debug
      end
      def initialize
         @mm=Sketchup.active_model
         @ss=@mm.selection
         @ee=@mm.active_entities
         if Sketchup.find_support_file "whdialog.rb" #and false
            @dlg=WHDialog.new(Sharp_edit_comp.props[0],false,"Sharp_edit_comp",332,459,600,600)
            htm=@dlg.html
            htm.add_button(Sharp_edit_comp.props[3]){|x|
               Sharp_edit_comp.reload_all
            }
            htm.add_button(Sharp_edit_comp.props[2]){|x|
               Sharp_edit_comp.reload
            }
            htm.add_button(Sharp_edit_comp.props[1]){|x|
               Sharp_edit_comp.edit
            }
            @dlg.show
            @dlg.fit_size
         else
            UI.messagebox Sharp_edit_comp.props[-1]
         end
      end
      def Sharp_edit_comp.find_window caption
         caption=caption+"\s+.*SketchUp"
         reg= Regexp.compile(caption)
         p reg
         hds=AMS::Window.get_windows
         hds=hds.map{|x |
            c=AMS::Window.get_caption(x)
            if c=~reg
               x
            end
         }
         hds.compact!
         p hds
         hds[0]
      end
      # 另存 单独开窗口编辑组件
      def Sharp_edit_comp.edit
         ss=Sketchup.active_model.selection
         if ss[0] and ss[0].typename=="ComponentInstance"
            defn=ss[0].definition
            if defn.path!="" and FileTest.exist?(defn.path)
               path=defn.path

               # 组件是否已经打开？通过查找标题寻找窗口句柄
               caption=File.basename(path)
               hnd=Sharp_edit_comp.find_window(caption)
               if hnd
                  AMS::Window.bring_to_top(hnd)
               else
                  UI.openURL(path)
               end
            else
               name= defn.name
               path=File.expand_path(Sketchup.active_model.path )
               path=  FileTest.directory?(path) ? path : File.dirname(path)
               fname = File.join(path, name + ".skp")
               defn.save_as(fname)
               UI.openURL(fname)
            end
         else
            UI.messagebox Sharp_edit_comp.props[-2]
         end
      end
      def Sharp_edit_comp.reload
         ss=Sketchup.active_model.selection
         Sketchup.active_model.start_operation "Sharp_edit_comp"
         if ss[0] and ss[0].typename=="ComponentInstance"
            defn=ss[0].definition
            defn.entities.clear!
            new_dfn = Sketchup.active_model.definitions.load(defn.path)
            defn.instances.each { |inst| inst.definition = new_dfn }
         else
            UI.messagebox Sharp_edit_comp.props[-2]
         end
         Sketchup.active_model.commit_operation
      end
      def Sharp_edit_comp.reload_all
         Sketchup.active_model.start_operation "Sharp_edit_comp"
         defs=Sketchup.active_model.definitions.to_a.delete_if{|x|x.path=="" or x.count_instances==0 or !FileTest.exist?(x.path)}
         defs.each{|defn|
            defn.entities.clear!
            new_dfn = Sketchup.active_model.definitions.load(defn.path)
            defn.instances.each { |inst| inst.definition = new_dfn }
         }
         Sketchup.active_model.commit_operation
      end
   end # end of class ToolTests
end
#~ # Add a menu choice for tooltests

eval "def wikii_tools_Sharp_edit_comp
Wikii_Tools::Sharp_edit_comp.new
end"

if !file_loaded?("Sharp_edit_comp.rb") or true
UI.add_context_menu_handler do |menu|
   if Sketchup.active_model.selection[0].class==Sketchup::ComponentInstance
      menu.add_separator
      menu.add_item(Wikii_Tools::Sharp_edit_comp.props[1]) { Wikii_Tools::Sharp_edit_comp.edit }
      menu.add_item(Wikii_Tools::Sharp_edit_comp.props[2]) { Wikii_Tools::Sharp_edit_comp.reload }
      menu.add_item(Wikii_Tools::Sharp_edit_comp.props[3]) { Wikii_Tools::Sharp_edit_comp.reload_all }
   end
end
if   defined?(Sutool) and false
   Sutool.add_cmd(Wikii_Tools::Sharp_edit_comp.props[0]) { Wikii_Tools::Sharp_edit_comp.new }
   Sutool.add_cmd(Wikii_Tools::Sharp_edit_comp.props[1]) { Wikii_Tools::Sharp_edit_comp.edit }
   Sutool.add_cmd(Wikii_Tools::Sharp_edit_comp.props[2]) { Wikii_Tools::Sharp_edit_comp.reload }
   Sutool.add_cmd(Wikii_Tools::Sharp_edit_comp.props[3]) { Wikii_Tools::Sharp_edit_comp.reload_all }
else
   plugins_menu = UI.menu("Plugins")
   submenu = plugins_menu.add_submenu(Wikii_Tools::Sharp_edit_comp.props[0])

   submenu.add_item(Wikii_Tools::Sharp_edit_comp.props[1]) { Wikii_Tools::Sharp_edit_comp.edit }
   submenu.add_item(Wikii_Tools::Sharp_edit_comp.props[2]) { Wikii_Tools::Sharp_edit_comp.reload }
   submenu.add_item(Wikii_Tools::Sharp_edit_comp.props[3]) { Wikii_Tools::Sharp_edit_comp.reload_all }
end
end
file_loaded("Sharp_edit_comp.rb")
__END__
clear
wikii_tools_Sharp_edit_comp
