#. $Id: importGeomHeal.eui 435634 2010-11-14 18:08:27Z jtm $
#.============================================================================
#. $Revision: 435634 $
#. $Date: 2010-11-14 13:08:27 -0500 (Sun, 14 Nov 2010) $
#.============================================================================
#.File Name:     importGeomHeal.eui
#.Description:   Provides all the mechanisms neccessary for importing geometry
#.               with healing of the geometry
#.History:
#.               $Log: importGeomHeal.eui,v $
#.               Revision 1.3  2009/11/13 16:19:21  jtm
#.               sysfix - blind copy of 130 into HEAD
#.
#.               Revision 1.1.6.1  2009/05/21 13:34:33  jtm
#.               sysfix - initial 13.0 version
#.
#.               Revision 1.1  2006/12/15 07:41:11  jtm
#.               Initial 12.0 Version
#.
#.               Revision 50.1  2005/06/03 15:20:35  upd111
#.               utility  split to next revn (100)
#.
#.               Revision 30.1  2003/04/24 17:43:19  upd111
#.               utility  split to next revn (80)
#.
#.               Revision 6.5  2002/04/29 18:34:59  ejp
#.               changed 61 to 70 in environment variables
#.
#.               Revision 6.4  2002/01/31 16:10:50  ejp
#.               added support for CADFIX61_VERSION environment variable
#.
#.               Revision 6.3  2002/01/18 15:26:53  ejp
#.               added check for HEAL_ANSYS_IMPORT
#.
#.               Revision 6.2  2002/01/18 13:27:06  ejp
#.               switched order for cadfix check
#.
#.               Revision 6.1  2002/01/14 18:06:53  ejp
#.               added changes for new environment variable
#.
#.               Revision 6.2  2002/01/03 14:29:52  ejp
#.               changed header
#.
#.               Revision 6.1  2002/01/03 14:02:43  ejp
#.               initial version
#.============================================================================

# NOTE 
#  ALL TEXT MUST USE msgcat::mc SO THAT LOCALIZATION CAN BE PERFORMED

namespace eval ::ITI {
   variable featureAvailable
   variable ansLicense
   variable itiLicense

   set ansLicense(chk) 0
   set itiLicense(chk) 0

   proc Healing {} {
      global env
      variable HealArr

      # Check the available import licenses from ITI
      array set itiImportLic [chkITILicenses]
      # Check the available import licenses from ANSYS
      array set ansImportLic [chkANSLicenses]

      # No licenses are available so return
      if {!$itiImportLic(found) && !$ansImportLic(found)} {
         ::euidl::messageBox \
               -message [msgcat::mc "No licenses are available for healing"]
         return
      }

      if {[winfo exists .dlgITIHealing]} {
         destroy .dlgITIHealing
      }
      set parent [::euidl::dialog .dlgITIHealing -class EUIDL]
      set HealArr(parent) $parent
      set wc [$parent component childsite]
      grid columnconfigure $wc 0 -weight 1

      wm title $parent [::msgcat::mc "ANSYS Healing Module"]

      # CAD Geometry File
      set filF [::euidl::labeledframe $wc.frmFile \
            -text [msgcat::mc "CAD file to use for the healing process"] ]
      set fcs [$filF component childsite]

      set rID 0
      grid $filF -row [incr rID] -column 0 -sticky we

      set types [getHealFileTypes]

      # Use the previous path if it was set.
      if { [catch {set HealArr(dir)} initDir] } {
         set initDir [pwd]
      }

      set HealArr(fileinline) [ \
         euidl::fileinline $fcs.filHeal \
            -variable [scope HealArr(geometryFile)] \
            -buttontext [::msgcat::mc "Browse ..."] \
            -relative 1 \
            -oper open \
            -filetypes $types]

      grid $HealArr(fileinline) -row 0 -column 0 -sticky ew

      grid columnconfigure $fcs 0 -weight 1

      # Validation
      set valF [::euidl::labeledframe $wc.frmValid \
            -text [msgcat::mc "ANSYS model validation check"] ]
      set vcs [$valF component childsite]

      grid $valF -row [incr rID] -column 0 -sticky we


      ::euidl::radiobutton $vcs.rbValBOOL \
            -variable [scope HealArr(validation)] \
            -value BOOL \
            -anchor w \
            -text [::msgcat::mc "That the model can support boolean operations."]
      ::euidl::radiobutton $vcs.rbValMESH \
            -variable [scope HealArr(validation)] \
            -value MESH \
            -anchor w \
            -text [::msgcat::mc "That the model can be meshed."]
      ::euidl::radiobutton $vcs.rbValNONE \
            -variable [scope HealArr(validation)] \
            -value NONE \
            -anchor w \
            -text [::msgcat::mc "No validation of the model."]

      set HealArr(validation) MESH

      grid $vcs.rbValMESH -row 0 -column 0 -sticky ew
      grid $vcs.rbValBOOL -row 1 -column 0 -sticky ew
      grid $vcs.rbValNONE -row 2 -column 0 -sticky ew
      grid columnconfigure $vcs 1 -weight 1

      update
      set w [expr [winfo reqwidth $parent] * 2]
      set h [winfo reqheight $parent]
      wm geom $parent ${w}x${h}

      $parent configure \
         -okcommand [namespace code Healing_OK] \
         -cancelcommand [list destroy $parent] \
         -helpcommand [list ans_loadhelp Hlp_HEAL]

      $parent activate
   }

   proc Healing_OK {} {
      variable HealArr

      $HealArr(parent) deactivate

      if {![string match "" $HealArr(geometryFile)]} {
      set filename $HealArr(geometryFile)
      set fileDir [file dir $filename]
      set fileRoot [file root [file tail $filename]]
      set preFileExt [file ext $filename]
      set fileExt [list]
      regexp {\.(.*)} $preFileExt match fileExt
      
      set HealArr(ansGeomName) [euidl::convertFile $HealArr(geometryFile)]
      set HealArr(geomFileName) $HealArr(geometryFile)
      set HealArr(geomFileDir) $fileDir
      set HealArr(geomFileRoot) $fileRoot
      set HealArr(geomFileExt) $fileExt
      set fileDir [::ansys::getRelativePath $fileDir]
      set HealArr(dir) $fileDir
      
      # find out what type of file was selected
      set typeLabel ""

      # Check the available import licenses from ITI
      array set itiImportLic [chkITILicenses]
      # Check the available import licenses from ANSYS
      array set ansImportLic [chkANSLicenses]

      set types [getHealFileTypes]

      foreach type $types {
          set label [lindex $type 0]
          set filters [lindex $type 1]
          foreach filter $filters {
          if {[string match -nocase *$filter $preFileExt]} {
              set typeLabel $label
              break
          }
          }
          if {$typeLabel != ""} { break}
      }

      # define the labels
      if {$itiImportLic(proe) || $ansImportLic(proe)} {
         set proeLabel "Pro/E"
         set proeFilter "{.prt*}"
         set proeAssmLabel "Pro/E Assembly"
         set proeAssmFilter "{.asm*}"
      }
      if {$itiImportLic(sat) || $ansImportLic(sat)} {
         set satLabel "SAT"
         set satFilter "{.sat*}"
      }
      if {$itiImportLic(para) || $ansImportLic(para)} {
         set paraLabel "Parasolid"
         set paraFilter "{.x*_*t}"
      }
      if {$itiImportLic(ug) || $ansImportLic(ug)} {
         set ugLabel "UG"
         set ugFilter "{.prt}"
      }
      if {$itiImportLic(catia) || $ansImportLic(catia)} {
         set catiaLabel "CATIA"
         set catiaFilter ".model .exp"
      }
      if {$itiImportLic(ideas) || $ansImportLic(ideas)} {
         set ideasLabel "IDEAS"
         set ideasFilter "{.mf1}"
      }
      # additional imports from CADfix
      if {$itiImportLic(iges)} {
         set igesLabel "IGES"
         set igesFilter ".igs .ige .iges .IGS .IGE .IGES"
      }
      if {$itiImportLic(stl)} {
         set stlLabel "STL"
         set stlFilter ".stl .STL"
      }
      if {$itiImportLic(cadds)} {
         set caddsLabel "CADDS"
         set caddsFilter "_pd"
      }
      if {$itiImportLic(dxf)} {
          set dxfLabel "DXF/DWG"
          set dxfFilter ".dxf .DXF .dwg .DWG"
      }
      if {$itiImportLic(step)} {
          set stepLabel "STEP"
          set stepFilter ".step .STEP .stp .STP"
      }
      if {$itiImportLic(vdafs)} {
          set vdafsLabel "VDAFS"
          set vdafsFilter ".vda .VDA"
      }

      # For ACIS, Parasolid and CATIA there are two translators that could be used,
      # the ANSYS one or the CADfix one. For these filetypes we need to tell the
      # ~heal command when the CADfix one is available.  make sure environment
      # variable to override is not set.
      set HealArr(extraArgs) ""
      if { (($itiImportLic(sat) && $typeLabel == $satLabel) ||
                ($itiImportLic(para) && $typeLabel == $paraLabel) ||
                ($itiImportLic(catia) && $typeLabel == $catiaLabel)) &&
                ([info exists env(HEAL_ANSYS_IMPORT)] == "0") } {
              append HealArr(extraArgs) ",cadfix"
              append HealArr(extraArgs) ",$HealArr(validation)"
      } else {
              append HealArr(extraArgs) ",ansys"
              append HealArr(extraArgs) ",$HealArr(validation)"
      }

      HealModel
      }

      destroy $HealArr(parent)
   }

   proc getHealFileTypes { } {
      variable env
      variable HealArr

      # Check the available import licenses from ITI
      array set itiImportLic [chkITILicenses]
      # Check the available import licenses from ANSYS
      array set ansImportLic [chkANSLicenses]

      # define the labels
      if {$itiImportLic(proe) || $ansImportLic(proe)} {
         set proeLabel "Pro/E"
         set proeFilter "{.prt*}"
         set proeAssmLabel "Pro/E Assembly"
         set proeAssmFilter "{.asm*}"
      }
      if {$itiImportLic(sat) || $ansImportLic(sat)} {
         set satLabel "SAT"
         set satFilter "{.sat*}"
      }
      if {$itiImportLic(para) || $ansImportLic(para)} {
         set paraLabel "Parasolid"
         set paraFilter "{.x*_*t}"
      }
      if {$itiImportLic(ug) || $ansImportLic(ug)} {
         set ugLabel "UG"
         set ugFilter "{.prt}"
      }
      if {$itiImportLic(catia) || $ansImportLic(catia)} {
         set catiaLabel "CATIA"
         set catiaFilter ".model .exp"
      }
      if {$itiImportLic(ideas) || $ansImportLic(ideas)} {
         set ideasLabel "IDEAS"
         set ideasFilter "{.mf1}"
      }
      # additional imports from CADfix
      if {$itiImportLic(iges)} {
         set igesLabel "IGES"
         set igesFilter ".igs .ige .iges .IGS .IGE .IGES"
      }
      if {$itiImportLic(stl)} {
         set stlLabel "STL"
         set stlFilter ".stl .STL"
      }
      if {$itiImportLic(cadds)} {
         set caddsLabel "CADDS"
         set caddsFilter "_pd"
      }
      if {$itiImportLic(dxf)} {
          set dxfLabel "DXF/DWG"
          set dxfFilter ".dxf .DXF .dwg .DWG"
      }
      if {$itiImportLic(step)} {
          set stepLabel "STEP"
          set stepFilter ".step .STEP .stp .STP"
      }
      if {$itiImportLic(vdafs)} {
          set vdafsLabel "VDAFS"
          set vdafsFilter ".vda .VDA"
      }

      set types [list]
      if {[info exists paraLabel]} {
         lappend types [list $paraLabel $paraFilter]
      }

      if {[info exists satLabel]} {
         lappend types [list $satLabel $satFilter]
      }
      if {[info exists proeLabel]} {
         lappend types [list $proeLabel $proeFilter]
         lappend types [list $proeAssmLabel $proeAssmFilter]
      }
      if {[info exists ugLabel]} {
         lappend types [list $ugLabel $ugFilter]
      }
      if {[info exists catiaLabel]} {
         lappend types [list $catiaLabel $catiaFilter]
      }
      if {[info exists ideasLabel]} {
         lappend types [list $ideasLabel $ideasFilter]
      }
      if {[info exists igesLabel]} {
         lappend types [list $igesLabel $igesFilter]
      }
      # additional imports from CADfix
      if {[info exists stlLabel]} {
         lappend types [list $stlLabel $stlFilter]
      }
      if {[info exists caddsLabel]} {
         lappend types [list $caddsLabel $caddsFilter]
      }
      if {[info exists dxfLabel]} {
         lappend types [list $dxfLabel $dxfFilter]
      }
      if {[info exists stepLabel]} {
         lappend types [list $stepLabel $stepFilter]
      }
      if {[info exists vdafsLabel]} {
         lappend types [list $vdafsLabel $vdafsFilter]
      }

      # Open a dialog to query for additional arguments and to have
      # access to help

      # Use the previous path if it was set.
#      if { [catch {set HealArr(dir)} initDir] } {
#         set initDir [pwd]
#      }
#      set geometryFile [euidl::getOpenFile -filetypes $types \
#                  -title [msgcat::mc "Select Geometry File"] \
#                  -initialdir $initDir ]

      return $types
   }

   proc HealModel {} {
      variable HealArr

      if {![info exists HealArr(geomFileName)]} {
         return
      }
      catch {ans_sendcommand ~heal,$HealArr(ansGeomName)$HealArr(extraArgs)} err

   }
   
   proc ansinfo_featureavailable {featId} {
      set iProd1 -1
      set iProd2 -1
      if {[info exists featureAvailable($featId)]} {
         return $featureAvailable($featId)
      }
      set featureAvailable($featId) 0
      switch -- $featId {
         proe {
            set iProd1 82
            set iProd2 211
         }
         sat {
            set iProd1 91
            set iProd2 214
         }
         para {
            set iProd1 94
            set iProd2 215
         }
         ug {
            set iProd1 86
            set iProd2 213
         }
         catiav4 {
            set iProd1 97
            set iProd2 219
         }
         ideas {
            set iProd1 96
            set iProd2 221
         }
      }
      if {[ansinfo_productavailable $iProd1]} {
         set featureAvailable($featId) 1
      } elseif {$iProd2 >= 0} {
         if {[ansinfo_productavailable $iProd2]} {
            set featureAvailable($featId) 1
         }
      }
      return $featureAvailable($featId)
   }

   proc chkANSLicenses {} {
      variable ansLicense

      if {$ansLicense(chk)} {
         return [array get ansLicense]
      }
      set ansLicense(found) 0
      # determine if connection products are licensed
      if {![catch {package require ansinfo} err]} {
         ansinfo_init
         if {![ansinfo_featureavailable "proe"]} {
            set ansLicense(proe) 0
         } else {
            set ansLicense(proe) 1
            set ansLicense(found) 1
         }
         if {![ansinfo_featureavailable "sat"]} {
            set ansLicense(sat) 0
         } else {
            set ansLicense(sat) 1
            set ansLicense(found) 1
         }
         if {![ansinfo_featureavailable "para"]} {
            set ansLicense(para) 0
         } else {
            set ansLicense(para) 1
            set ansLicense(found) 1
         }
         if {![ansinfo_featureavailable "ug"]} {
            set ansLicense(ug) 0
         } else {
            set ansLicense(ug) 1
            set ansLicense(found) 1
         }
         if {![ansinfo_featureavailable "catiav4"]} {
            set ansLicense(catia) 0
         } else {
            set ansLicense(catia) 1
            set ansLicense(found) 1
         }
         if {![ansinfo_featureavailable "ideas"]} {
            set ansLicense(ideas) 0
         } else {
            set ansLicense(ideas) 1
            set ansLicense(found) 1
         }
      }
      set ansLicense(chk) 1
      return [array get ansLicense]
  }

   proc chkITILicenses {} {
       variable itiLicense
       global env
       global tcl_platform
       global ansysRevision

       if {$itiLicense(chk)} {
           return [array get itiLicense]
       }

       set itiLicense(found) 0
       # determine if import products are licensed, default is unavailable.
       # Note: some of these are NOT currently available through CADfix and
       # so will always be 0, e.g. proe, ug and ideas
       set itiLicense(proe) 0
       set itiLicense(sat) 0
       set itiLicense(para) 0
       set itiLicense(ug) 0
       set itiLicense(catia) 0
       set itiLicense(ideas) 0
       # the following are new options available via CADfix
       set itiLicense(iges) 0
       set itiLicense(stl) 0
       set itiLicense(cadds) 0
       set itiLicense(dxf) 0
       set itiLicense(step) 0
       set itiLicense(vdafs) 0

       if {![catch {package require ansinfo} err]} {
            ansinfo_init
            set SATLicense [ansinfo_productavailable 291]
            if {$SATLicense != 0} {
               set itiLicense(sat) 1 
            }
            set CADDSLicense [ansinfo_productavailable 293]
            if {$CADDSLicense != 0} {
               set itiLicense(cadds) 1 
            }
            set CATIALicense [ansinfo_productavailable 294]
            if {$CATIALicense != 0} {
               set itiLicense(catia) 1 
            }
            set IGESLicense [ansinfo_productavailable 295]
            if {$IGESLicense != 0} {
               set itiLicense(iges) 1 
            }
            set PARALicense [ansinfo_productavailable 296]
            if {$PARALicense != 0} {
               set itiLicense(para) 1 
            }
            set STEPLicense [ansinfo_productavailable 297]
            if {$STEPLicense != 0} {
               set itiLicense(step) 1 
            }
            set STLLicense [ansinfo_productavailable 298]
            if {$STLLicense != 0} {
               set itiLicense(stl) 1 
            }
            set VDAFSLicense [ansinfo_productavailable 299]
            if {$VDAFSLicense != 0} {
               set itiLicense(vdafs) 1 
            }
       }

       if {$itiLicense(iges) || $itiLicense(stl) || $itiLicense(cadds) || \
            $itiLicense(dxf) || $itiLicense(step) || $itiLicense(vdafs)} {
         set itiLicense(found) 1
       }

       set itiLicense(chk) 1
       return [array get itiLicense]
   }
   
}
