# copyright= 'Huynh Duong Phuong Vi @ 2014-2015'
# suforyou.vn@gmail.com
module S4U::S4u_Explode
	def self.explode_all(ent)
		Sketchup::status_text=Strings.GetString("Explode ")+(@count+=1).to_s
		(ent.explode).each{ |e| explode_all(e) if((e.is_a?Sketchup::Group)||(e.is_a?Sketchup::ComponentInstance)) }
	end
	def self.explode_in(g)
		ents=[];ed=[];entities=[];
		if g.is_a?Sketchup::Group
			g.make_unique
			entities=g.entities 
			@count_group+=1
		elsif g.is_a?Sketchup::ComponentInstance
			#g.make_unique
			entities=g.definition.entities
			@count_component+=1
		end
		ents =entities.select{|e| (e.is_a?Sketchup::Group)||(e.is_a?Sketchup::ComponentInstance) }
		@count=0
		ents.each{ |e| explode_all (e)}    
		entities=[]
		if g.is_a?Sketchup::Group   
			entities=g.entities      
		elsif g.is_a?Sketchup::ComponentInstance
			entities=g.definition.entities
		end
		entities.each{|e| ed<< e.curve if (e.is_a?Sketchup::Edge) && e.curve}
		ed.uniq!
		if !ed.empty?             
			(@explode_curve=UI.messagebox Strings.GetString("Explode Curve?"),MB_YESNO) if !@explode_curve               
			ed.each{ |e| e.first_edge.explode_curve } if @explode_curve==6         
		end
	end
	def self.main_explode
		model =Sketchup.active_model
		sel=Sketchup.active_model.selection
		@explode_curve=nil
		if sel.empty?
			UI.messagebox Strings.GetString("Select Groups or Components")
			return
		else
			model.start_operation Strings.GetString("Explode in Group,Component"),true,false,false
			@count_group=0;@count_component=0
			sel.each{|s| explode_in(s) }
			model.commit_operation			
			str=Strings.GetString("Explode ") + (@count_group+@count_component).to_s + Strings.GetString(" objects: ")             
			(str=str+ @count_group.to_s + Strings.GetString(" groups")) if @count_group>0
			(str=str+"-" + @count_component.to_s + Strings.GetString(" components")) if @count_component>0
			Sketchup::status_text= str
		end	   
	end
end