module CADFATHER
  module Material_Isolator
  @dir = File.dirname(__FILE__)

class Mat_Isolator
    @@stext = 'LEFT-CLICK: Sample Mat.  |  RIGHT-CLICK  or  CTRL/ALT: Isolate Mat.  '
    @@dir = File.dirname(__FILE__)

def activate
	   cu = File.join(@@dir + '/cu_sel.png')   ;   @cu = UI.create_cursor(cu, 2, 2)		if cu
	   cu2 = File.join(@@dir + '/cu2_sel.png')   ;   @cu2 = UI.create_cursor(cu2, 2, 2)		if cu2
    Sketchup::set_status_text(@@stext)
    @@v.refresh
end#

def initialize()
    @@m = Sketchup.active_model   ;   @@s = @@m.selection   ;   @@e = @@m.active_entities   ;   @@v = @@m.active_view   ;   @@eye = @@v.camera.eye    ;   @@mt = @@m.materials
    @@hide=[]  ;  @@pot=[]  ;  @@faces=[]  ;  @@others=[]  ;  @@mat=[]  ;  @@cgs=[]  ;  @@processed=0  ;  @@picked=0  ;  @@ctrl=0   ;   @@vis = 0
end#


def onMouseMove(flags, x, y, view)
    Sketchup::set_status_text(@@stext)
end#


def reset
    @@vis = 0
    @@hide = []
    @@pot = []
    @@faces = []
    @@others = []
    @@mat = []
    @@cgs = []
    @@processed = 0
    @@rclick = 0
    @@picked = 0
    @@ctrl = 0
end#

def onSetCursor
            case @@vis
                  when 0
                      UI.set_cursor(@cu)
                  when 1
                      UI.set_cursor(@cu2)
                  end
end#


def unhide
    @@m.rendering_options['EdgeDisplayMode'] = 1
    Sketchup.send_action('editUndo:')
end#


def onLButtonDown(flags, x, y, view)
    unhide unless @@processed == 0   #=>
        reset   #=>
        pick_material(flags, x, y, view)   #=>
end#

def onLButtonUp(flags, x, y, view)
    view.refresh
end#

def onRButtonDown(flags, x, y, view)
    @@faces = []
end#

def onRButtonUp(flags, x, y, view)
    unless @@vis == 1
        start_process unless @@processed == 1   #=>
    end
end#

def start_process
    if @@picked == 0
            @@mat = @@mt.current
            Sketchup::set_status_text('Active Material (in Browser) = ', SB_VCB_LABEL)
            Sketchup::set_status_text(@@mat.name.to_s, SB_VCB_VALUE)
            question = UI.messagebox('Nothing Picked.  Do you want to search for the Active Material?', MB_YESNO)
                    if question == 6
                          @@picked = 1
                          @@m.rendering_options['DrawSilhouettes'] = false
                          hide_the_stuff   #=>
                     else
                          return nil
                     end
    elsif  @@picked == 1
            @@m.rendering_options['DrawSilhouettes'] = false
            hide_the_stuff   #=>
    end
end#


def pick_material(flags, x, y, view)
        ph = view.pick_helper(x, y)
        ph.do_pick(x, y)
        range = 0...ph.count
range.each{ |z|
        @@element = ph.leaf_at(z)
                if @@element.is_a?(Sketchup::Face)
                      @n = @@element.normal
                      @vec = @@element.bounds.center.vector_to(@@eye)
                      @ang = @n.angle_between(@vec)
                            if @ang < 90.degrees
                                @@mat = @@element.material
                            elsif @ang > 90.degrees
                                @@mat = @@element.back_material
                            end
                     @@selected_face = @@element
                end
        if @@mat == nil
                      dig = ph.path_at(z)
                      dig.each{ |x|
                          if x.material != nil
                              @@mat = x.material  if x.is_a?( Sketchup::Group ) ||  x.is_a?( Sketchup::ComponentInstance )
                          end  }
                      begin
                          @@vis = 1
                          @@mt.current = @@mt["#{@@mat.name}"]
                          Sketchup::set_status_text('Sampled Texture (CG) = ', SB_VCB_LABEL)
                          Sketchup::set_status_text(@@mat.name.to_s, SB_VCB_VALUE)
                      rescue
                      end
        else
                      @@vis = 0
                      @@mt.current = @@mt["#{@@mat.name}"]
                      Sketchup::set_status_text('Sampled Texture (Face) = ', SB_VCB_LABEL)
                      Sketchup::set_status_text(@@mat.name.to_s, SB_VCB_VALUE)
         end
                      @@s.add @@selected_face
                      @@picked = 1 unless @@selected_face == nil
}
end#


def onKeyUp(key, repeat, flags, view)
	case key
		when COPY_MODIFIER_KEY then
          start_process unless @@processed == 1   #=>
		when CONSTRAIN_MODIFIER_KEY
          @@s.clear
end
end#



def hide_the_stuff
    @@m.start_operation('JHS_Standard Hide the Shit', true)
    digger(@@e)   #=>
    @@m.rendering_options['EdgeDisplayMode'] = 0
    @@m.rendering_options['DisplayInstanceAxes'] = false
    @@m.rendering_options['HideConstructionGeometry'] = true
    @@hide.each{ |z| z.hidden = true }
    @@others.each{ |z|  z.hidden = true  }
    @@processed = 1
    @@m.commit_operation
end#

def digger(ss)
      ss.each { |obj|
      if obj.is_a?(Sketchup::Group)
          @@cgs << obj  ; digger(obj.entities)
      elsif obj.is_a?(Sketchup::ComponentInstance)
          @@cgs << obj    ;   digger(obj.definition.entities)
      elsif obj.is_a?(Sketchup::Face)
          @@faces << obj
      else
          @@others << obj unless obj.is_a?(Sketchup::Edge) || obj.is_a?(Sketchup::Group) ||  obj.is_a?(Sketchup::ComponentInstance)
      end  }
      @@faces.each{ |fcs| @@pot << fcs if fcs.material == @@mat || fcs.back_material ==@@mat  }
      @@hide = @@faces.to_a - @@pot.to_a
end#

def draw(view)
    unless  @@picked == 0
        case @@vis
            when 0 
                view.drawing_color = 'Green'
                view.draw_text [5,5], 'Face Material: ' + "#{@@mat.name.to_s}"
            when 1
                view.drawing_color = 'Red'
                view.draw_text [5,5], 'CG Material: ' + "#{@@mat.name.to_s}"
        end
    end
end#

def deactivate(view)
    view.refresh
    unhide unless @@processed == 0   #=>
    reset unless @@processed == 0   #=>
end#

end # CLASS





unless file_loaded?(__FILE__)
UI.menu('Tools').add_item('Material Isolator')   { Sketchup.active_model.select_tool Mat_Isolator.new }

tb = UI::Toolbar.new('Cadfather')
cmd = UI::Command.new('Material Isolator')   { Sketchup.active_model.select_tool Mat_Isolator.new }
cmd.small_icon = @dir + '/matiso24.png'  ; cmd.large_icon = @dir + '/matiso32.png'
cmd.tooltip = 'Material Isolator' ; cmd.status_bar_text = 'Click on a face.  Faces with same material will remain visible.'
tb.add_item(cmd)
end    

end#mod
end#mod