=begin
###
Copyright 2011-2015 TIG (c)
Permission to use, copy, modify, and distribute this software for any 
purpose, and currently without fee, is hereby granted, provided that  
this text and the above copyright (c) notice appear 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.
###
Parts are based on ThomThom's 'solid_inspector' tool which can also be 
used prior to this tool, so that you get a better understanding of what 
is wrong with your Non-Solid Group...
###
Name: TIG-solidsolver.rb
###
Menu: 'Tools' > 'SolidSolver'
Context-menu: > 'SolidSolver'
###
Usage: [Note this works on >= v8 only]
Select a Non-Solid Group/Component-Instance and run the tool from the menu
or context-menu.
If it contains nested groups or instances you are warned, and given the 
option to explode these - it could never be a 'solid' otherwise.
It analyzes the object's form for holes [which it heals], small faces 
[also healed] and removes partitions/flaps that are making the form 
'non-manifold', and it merges any 'split' colinear edges.
If there are any coplanar edges it asks if you want to remove them.
It then reports on the results 'solidity' & what was done to achieve it.
It is one step undoable.
If the object remains 'non-solid' it's likely be due to some edges still 
having more than two faces - e.g. two otherwise solid cubes touching at 
an edge will therefore use that edge for four faces, which is therefore 
regarded as making it a non-solid.  An additional dialog explains, and it 
then offers the chance to try to rectify this.  A 'fix' might produce odd 
results - for example the common edge to two cubes might be removed so 
there are no longer any edges with more than two faces, but the form 
might then be folded back on itself to achieve interpenetrating faces - 
which, although then regarded as forming a 'solid', is an 'impossible 
object' in normal three dimensions... You are given a separate undo for 
this step if you do try it.
Tip: IF you have this type of form temporarily group one of the 'cubes' 
and move if over say 0.5mm, so that there are then two distinct edges 
and it will become a proper solid of two separated volumes, without the 
involuted faces issue.
###
Version:
1.0 20111013 First issue.
1.1 20111014 Nested groups now considered.
1.2 20111016 Unsolvable groups etc now spotted.
1.3 20111019 Split colinear edges now merged.
1.4 20111027 Typo in small-hole-heal code fixed.
1.5 20121012 Works on groups/component=instances, makes the result unique.
1.6 20121024 Improved internal partition removal even when they are not full 
width. Context-menu now available if appropriate selection.
Progress ... is now indicated in the status-bar.
1.7 20121025 Further improvements with speed and internal partition removal.
Removal of Coplanar-Edges now final option.
1.8 20121025 Internal partition removal further improved.
1.9 20121025 Internal partition removal improved again, multiple parallel face  
to face partitions now removed [up to 10,000!].
2.0 20121025 More hole healing added to suit varied forms.
2.1 20121026 Healing certain kinds of triangular holes now more robust.
2.2 20121213 Coincident faces also resolved better.
3.0 20150106 Catch up for intersecting forms etc.
3.1 20150111 Code glitches fixed.
3.2 20150119 Robuster coding, Errors in intersecting trapped, Faster etc.
3.3 20150121 Glitch with some intersections fixed.
3.4 20150203 Unusual face glitches trapped.
3.5 20150217 Tiny folded faces fixed [needs second try].
4.0 20150403 Nested contents now stay exploded. Speed greatly improved.
4.1 20150409 Rare fail with tiny hole healing resolved.
5.0 20151109 Made as an Extension. EWH certified.
###
Donations: PayPal.com to info @ revitrev.org
###
=end
###
require('sketchup.rb')
require('extensions.rb')
###
module TIG
  module SolidSolver
		if defined?(Encoding)
			PLUGINS = File.dirname(__FILE__).force_encoding("UTF-8") ### >=v2014 lashup
		else
			PLUGINS = File.dirname(__FILE__)
		end
		FNAME = File.basename(__FILE__, ".*")
		FOLDER = File.join(PLUGINS, FNAME)
		DATA = File.join(FOLDER, "Data")
		### set Constants
		eval(IO.read(File.join(DATA, "Data.txt")))
		###
		EXT=SketchupExtension.new(NAME, File.join(FOLDER, "#{FNAME}_code.rb"))
		EXT.name = NAME
		EXT.description = DESC
		EXT.version = VERSION
		EXT.creator = CREATOR
		EXT.copyright = COPYRIGHT
		ext = Sketchup.register_extension(EXT, true) # show on 1st install
		###
  end
end
###
