# copyright= 'Huynh Duong Phuong Vi @ 2013-2015'
# suforyou.vn@gmail.com
module S4U::S4u_Delete_Overlap
		def self.del_ins(entities)
			list_ins=[]
			list_group=[]
			entities.each{|e|
				if e.is_a?Sketchup::ComponentInstance
					list_ins<< e 
				elsif e.is_a?Sketchup::Group
					list_group<< e 
				end
			}   
			if list_group.length>1
				to_delete=[] 
				list_group.each do |g|
					 i=list_group.length
					 (0..i-2).each { |i2|
						keeping_instance=list_group[i2]
						next if to_delete.include?(keeping_instance)
						(i2+1..i-1).each { |i3|
							instance=list_group[i3]
							next if to_delete.include?(instance)
							(to_delete << instance) if ((keeping_instance.parent==instance.parent)&&compare_transformation(instance,keeping_instance))
						}
					}
				end 
				@dels=@dels+to_delete
			end
			if list_ins.length>1
				to_delete=[] 
				list_ins.each do |ins|
					i=list_ins.length
					(0..i-2).each { |i2|
						keeping_instance=list_ins[i2]
						next if to_delete.include?(keeping_instance)
						(i2+1..i-1).each { |i3|
							instance=list_ins[i3]
							next if to_delete.include?(instance)
							(to_delete << instance) if (keeping_instance.parent==instance.parent)&&(keeping_instance.definition==instance.definition)&&compare_transformation(instance,keeping_instance)                                                   
						}
					}
				end 
				@dels=@dels+to_delete
			end
			list_ins.each{|ins|
				list=[]
				next if @dels.include?(ins)
				list=ins.definition.entities.select{|ent| (ent.is_a?Sketchup::ComponentInstance)||(ent.is_a?Sketchup::Group) }
				self.del_ins(ins.definition.entities) if list.length>1
			}
			list_group.each{|grp|
				list=[]
				next if @dels.include?(grp)
				list=grp.entities.select{|g| (g.is_a?Sketchup::Group)||(g.is_a?Sketchup::ComponentInstance) }
				self.del_ins(grp.entities) if list.length>1
			}
		end
		def self.compare_transformation(element1,element2) 
			s=10000000000000.0
			return true if(element1.transformation.to_a.collect!{|i| (i*s).round/ s})==(element2.transformation.to_a.collect!{|i| (i*s).round/ s})
			return false
		end
		def self.main
			model=Sketchup.active_model
			sel=model.selection
			defs=[];@list_ins=[];@list_group=[];@defs_group=[];@dels=[] ;count=0 
			if sel.empty?     
				status=UI.messagebox Strings.GetString("Do you want to Delete Instances Overlap in Model?"),MB_YESNO
				return if status!=6
				defs=model.definitions           
				defs.each{ |compo_def|       
						to_delete=[]
						list_instances_def=compo_def.instances       
						list_instances=list_instances_def
						i=list_instances.length 
						if i>1
							(0..i-2).each { |i2|
								keeping_instance=list_instances[i2]
								next if to_delete.include?(keeping_instance)
								(i2+1..i-1).each { |i3|
										instance=list_instances[i3] 
										next if to_delete.include?(instance)
										(to_delete << instance) if ((keeping_instance.parent==instance.parent) && compare_transformation(instance,keeping_instance))
								}
							}
							@dels=@dels+to_delete
						end 
				}
			else
				@defs_group=model.definitions.select{|d| d.group? }
				self.del_ins(sel)       
			end
			@dels.uniq!
			if @dels.empty?
				UI.messagebox Strings.GetString("There's no component instances(or new_group) overlap positions.")
			else
				model.start_operation(Strings.GetString("Delete Overlap"),true,false,false)
				model.active_entities.erase_entities @dels
				UI.messagebox Strings.GetString("Deleted ") + @dels.length.to_s + Strings.GetString(" instances(or new_group) overlap positions.")
				model.commit_operation
			end
		end 
end
