#
# Library functions for the ic_xml library
#
# File: ic_xml/lib/assembly_report.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2004 ICEM CFD Engineering
#
# Author: Jason Aftosmis (jason@icemcfd.com)
#
# TODO:
#   o fixme the addition of an existing mesh report : addMeshReport
#
#---------------------------------------------------------------------
#
#     <assembly_report>
#         <parts>
#             <part name=qwer>
#                <geometry_report>
#                    <cadfile></cadfile>
#                </geometry_report>
#                <mesh_report>
#                    ## MeshReport ##
#                </mesh_report>
#             </part>
#             ...
#         </parts>
#         <connections>
#             <welds>
#                ...
#             </welds>
#             <contacts>
#                ...
#             </contacts>
#         </connections>
#     </assembly_report>
#
#---------------------------------------------------------------------

namespace eval AssemblyReport {
    
    namespace import ::IcXml::*
    
    variable _root_element_name "assembly_report"
    variable _parts_element_name  "parts"
    variable _part_element_name  "part"
    
    variable _xsl_file [file join [ic_xml_lib_path] .. style assembly_report.xsl]
    variable _css_file [file join [ic_xml_lib_path] .. style generic_report.css]
    
    proc err {text}   { return [ic_xml_err   "AssemblyReport: $text"] }
    proc debug {text} { return [ic_xml_debug "AssemblyReport: $text"] }
}


#---------------------------------------------------------------------


## Test (some|all) of the AssemblyReport functions. Returns SUCCESS or FAILURE

proc AssemblyReport::test {} {
    
    IcXmlReport::checkEmpty
        
    set mesh_reports [list meshreport1a.xml meshreport1b.xml]
    set tests [list assy_test]
    
    foreach test $tests {
	
	set dom_key [AssemblyReport::createDocument "NAME $test TITLE Hello"]
	
	AssemblyReport::addHeader $dom_key ""
	
	foreach mr {meshreport1a.xml meshreport1b.xml} {
	    set mr [file join [ic_xml_lib_path] .. $mr]
	    AssemblyReport::addPart $dom_key "name $mr" "" $mr
	}
	
	AssemblyReport::closeReport $dom_key ${test}a.xml xml
    }
    
    foreach test $tests {
	set dom_key [AssemblyReport::openReport ${test}a.xml]
	AssemblyReport::closeReport $dom_key ${test}b.html html
	catch { file delete -force ${test}a.xml }
	catch { file delete -force ${test}b.html }
    }
    
    foreach mr {meshreport1a.xml meshreport1b.xml} {
	set mr [file join [ic_xml_lib_path] .. $mr]
	catch { file delete -force $mr }
    }
    
    return [IcXmlReport::checkEmpty]
}


## Create a document with a root element <assembly_report>. Returns the domKey

proc AssemblyReport::createDocument { attributes } {
    variable _root_element_name
    return [IcXmlReport::createDocument $_root_element_name $attributes]
}


## Open an XML file. Returns the domKey.

proc AssemblyReport::openReport { xml_file } {
    variable _root_element_name
    return [IcXmlReport::openReport $xml_file $_root_element_name]
}


## Depending on whether or not the given xml_file already exists, it will
## either open the existing, or create a new (in which case no filename association
## should be assumed). Returns the domKey.

proc AssemblyReport::appendReport { xml_file {attributes ""} } {
    
    if ![file exists $xml_file] {
	return [AssemblyReport::createDocument $attributes]
    } else {
	return [AssemblyReport::openReport $xml_file]
    }
}


## Add a <header> with children to the root node of the AssemblyReport.
## Not sure what it returns. FIXME

proc AssemblyReport::addHeader { domKey children } {
    return [IcXmlReport::addHeader $domKey $children]
}


## Write out the AssemblyReport associated with the given <em>domKey</em>
## to the path <em>file</em> in the format <em>format</em>.

proc AssemblyReport::writeReport { domKey file format } {
    variable _css_file
    variable _xsl_file
    
    set args ""
    
    if {("" != $_xsl_file) && [file exists $_xsl_file]} {
	append args "-xsl_file {$_xsl_file} "
    } elseif {"" != $_xsl_file}  {
	ic_xml_err "Warning: could not find XSL file $_xsl_file"
    }
    
    if {("" != $_css_file) && [file exists $_css_file]} {
	append args "-css_file {$_css_file} "
    } elseif {"" != $_css_file}  {
	ic_xml_err "Warning: could not find CSS file $_css_file"
    }
    
    return [IcXmlReport::writeReport $domKey "$file" $format $args]
}


## Close the AssemblyReport associated with the given <em>domKey</em>
## and, if specified, to write it to the path <em>file</em> in the
## format <em>format</em>.

proc AssemblyReport::closeReport { domKey {file ""} {format ""} } {
    
    if {"" != $file} {
	set stat [AssemblyReport::writeReport $domKey $file $format]
    }
    
    return [IcXmlReport::closeDocument $domKey]
}


#---------------------------------------------------------------------


##

proc AssemblyReport::addPart { domKey attributes {geomReportFile ""} {meshReportFile ""} } {
    variable _parts_element_name
    variable _part_element_name
    
    ##
    
    set partsObj [IcXmlReport::ensureReportElement $domKey $_parts_element_name]
    
    set newPartObj [IcXmlReport::addElement $domKey $partsObj $_part_element_name $attributes ""]
    
    if {"" != $geomReportFile} {
	addGeometryReport $domKey $newPartObj $geomReportFile
    }
    
    if {"" != $meshReportFile} {
	addMeshReport $domKey $newPartObj $meshReportFile
    }
    
    return $newPartObj

}


## Given an XML MeshReport file <em></em>, add its contents to
## the AssemblyReport associated with the given <em>domKey</em>.
## Unkown return FIXME.

proc AssemblyReport::addMeshReport { domKey parentPartObj meshReportFile } {

    # load up the mesh_report into a docObj
    
    set meshReportKey [MeshReport::openReport $meshReportFile]
    if {"" == $meshReportKey} {
	return -code error "Could not add mesh report \"$meshReportFile\" to assembly report"       
    }

    set meshReportObj [IcXmlReport::getReportObject $meshReportKey]
    
    # add the mrObj element to a <PART> this document
    
    IcXmlInter::elementCopy $parentPartObj $meshReportObj
    
    # close up the mesh_report
    
    MeshReport::closeReport $meshReportKey
    
    return FIXME
}


## Given an XML GeometryReport file <em></em>, add its contents to
## the AssemblyReport associated with the given <em>domKey</em>.
## Unkown return FIXME.

proc AssemblyReport::addGeometryReport { domKey parentPartObj geomReportFile } {

    # load up the mesh_report into a docObj
    
    set geomReportKey [GeometryReport::openReport $geomReportFile]
    set geomReportObj [IcXmlReport::getReportObject $geomReportKey]
    
    # add the mrObj element to a <PART> this document
    
    IcXmlInter::elementCopy $parentPartObj $geomReportObj
    
    # close up the mesh_report
    
    GeometryReport::closeReport $meshReportKey
    
    return FIXME
}
