
# Freecad mesh exporter
# Copyright (C) 2018 Adam Williams <broadcast at earthling dot net>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



import FreeCAD
import Mesh
import MeshPart


# export everything except what moves


doc = FreeCAD.ActiveDocument
guiDoc = Gui.activeDocument()





objects = [
    'landing leg ',
    'landing leg  (Mirror #3)',
    'landing leg  (Mirror #4)',
    'landing leg  (Mirror #5)',
    'wing',
    'Loft',
    'fuse revolve',
    'nose',
    'landing fairing revolve (Mirror #3)',
    'landing fairing revolve',
    'tank',
    'tank2',
    'fuel line',
    'fuel line002',
    'fuel line003',
    'fuel line004',
    'mount',
    'mount001',
    'mount002',
    'mount003',
    
]



doubleSided = [
    'wing',
    'Loft'
]

flipNormals = [
    'tank2',
]

def isPart(x):
    return str(type(x)) == '<type \'App.Part\'>'

# convert an App.Part to a mesh
def makePartPart(dsts, transform, part):
    transform = transform * part.Placement.toMatrix()
    for src in part.Group:
        if src.ViewObject.Visibility:
            if isPart(src):
                makePartPart(dsts, transform, src)
            else:
                makePolyPart(dsts, transform, src)
    #src.ViewObject.Visibility=False



# convert a polygon to a mesh
def makePolyPart(dsts, transform, src):
    #print "got it"
    mesh = doc.addObject("Mesh::Feature","Mesh")
    shape = src.Shape
    #mesh.Mesh = MeshPart.meshFromShape(Shape=shape,MaxLength=250.8)
    mesh.Mesh = MeshPart.meshFromShape(Shape=shape,LinearDeflection=10, AngularDeflection=0.523599, Relative=False)

    if src.Label in flipNormals:
        mesh.Mesh.flipNormals()
    if src.Label in doubleSided:
        mesh.ViewObject.Lighting = u"Two side"
        
    mesh.Label = "%s Mesh" % src.Label
    mesh.ViewObject.CreaseAngle=25.0
    mesh.Placement = App.Placement(transform)
    dsts.append(mesh)
    src.ViewObject.Visibility=False





def makePart(dstName):
    dsts = []
    for src in doc.Objects:
        if src.Label in objects:
            transform = App.Matrix()
            print 'label=%s name=%s' % (src.Label, src.Name)
            if isPart(src):
                makePartPart(dsts, transform, src)
            else:
                makePolyPart(dsts, transform, src)
                
                
        

    part = App.activeDocument().addObject('App::Part','Part')
    part.Label = dstName
    for i in dsts:
        part.addObject(i)
    return part



makePart("BFS")







