
  # funkce na vypis funkce pro akcelerogram (dynamic))
proc DejFunkciGround { x int question} {
#tk_messageBox -message "x = $x , int = $int" -icon warning -type ok
set soubor [GiD_AccessValue get intvdata $int $question] 
#tk_messageBox -message "$soubor" -icon warning -type ok

       set re {(?x)
      	\s+ ([[:graph:]]+)      # first number
      	\s+ ([[:graph:]]+)      # second number
       }
 
      set vypisX ""
      set vypisY ""        
       
    global GiDProjectDir
    #WarnWinText $GiDProjectDir
    append GiDProjectDir "\\" 
     cd $GiDProjectDir       
       
     #set soubor "c:/text.txt"  
     set fp [open $soubor r]
     set data [read $fp]
     close $fp
     
     #  Process data file
     set data [split $data "\n"]
     foreach line $data {
     
      set line " $line "
      set line [string map { \; " " } $line]
      set line [string map { \, \. } $line]
      set b [regexp $re $line match matchX matchY]

      if {$b==1} {
      set vypisX "$vypisX $matchX"
      set vypisY "$vypisY $matchY"
      #tk_messageBox -message "x=$matchX y=$matchY polozka b= $b" -icon warning -type ok
      }
          
     }
    #tk_messageBox -message "vypisX=$vypisX" -icon warning -type ok
    #tk_messageBox -message "vypisY=$vypisY" -icon warning -type ok 
    
if {$x==0} { return "$vypisX" }
if {$x==1} { return "$vypisY" }

return ""
}

proc TK_CleanMaterialNumberVSGroupID { } {
array unset ::SaveMaterialNumberVSGroupIDComb
array unset ::SaveMaterialNumberVSGroupIDText
}

proc TK_SaveMaterialNumberVSGroupID { matName groupID} {
 set comb "$matName-$groupID"
 
  if { [info exists ::SaveMaterialNumberVSGroupIDComb($comb)] } {
      #WarnWinText "$matName - $groupID - already exists"
  } else {
      #WarnWinText "$matName - $groupID - adding"  
     set ::SaveMaterialNumberVSGroupIDComb($comb) 1
     append ::SaveMaterialNumberVSGroupIDText($matName) " $groupID"  
     #WarnWinText $::SaveMaterialNumberVSGroupIDText($matName)
  } 
 return ""
}



proc TK_WriteMaterialNumberVSGroupID { matName } {
if { [info exists ::SaveMaterialNumberVSGroupIDText($matName)] } {
 return $::SaveMaterialNumberVSGroupIDText($matName)
} else {
  return "Warning: Material $matName is not used in the model... See Digicon Tracks"  
}
  return ""
       
}


proc TK_CleanMinMaxGroupIDVSMaterial { } {
array unset ::SaveMinGroupIDVSMaterialValue
array unset ::SaveMaxGroupIDVSMaterialValue
}

proc TK_SaveMinMaxGroupIDVSMaterial { matName groupID} {
 #set comb "$matName-$groupID"
 
  if { [info exists ::SaveMinGroupIDVSMaterialValue($matName)] } {
      if { $groupID < $::SaveMinGroupIDVSMaterialValue($matName) } {
          set ::SaveMinGroupIDVSMaterialValue($matName) $groupID
          #WarnWinText "lower then min for $matName - $groupID - already exists"
      }
      if { $groupID > $::SaveMinGroupIDVSMaterialValue($matName) } {
          set ::SaveMaxGroupIDVSMaterialValue($matName) $groupID
          #WarnWinText "upper then max for $matName - $groupID - already exists"
      }  
      #WarnWinText "$matName - $groupID - already exists"
  } else {
     #WarnWinText "$matName - $groupID - adding"  
     set ::SaveMinGroupIDVSMaterialValue($matName) $groupID
     set ::SaveMaxGroupIDVSMaterialValue($matName) $groupID
     #append ::SaveMaterialNumberVSGroupIDText($matName) " $groupID"  
     #WarnWinText $::SaveMaterialNumberVSGroupIDText($matName)
  } 
 return ""
}

proc TK_WriteMinGroupIDVSMaterial { matName minIDnumber } {
#WarnWinText "TK_WriteMinGroupIDVSMaterial $matName"
if { [info exists ::SaveMinGroupIDVSMaterialValue($matName)] } {
  #WarnWinText "TK_WriteMinGroupIDVSMaterial $matName 2"
  if { $::SaveMinGroupIDVSMaterialValue($matName) > $minIDnumber } {
     return $::SaveMinGroupIDVSMaterialValue($matName) 
  } else {
    return $minIDnumber
  }
 
} else {
  WarnWinText "Warning: Condition Monitor_for_Point_Nearest_Node_IP. Material $matName is not used in the model..."
  return $minIDnumber  
}
  return $minIDnumber
}

proc TK_WriteMaxGroupIDVSMaterial { matName maxIDnumber } {
if { [info exists ::SaveMaxGroupIDVSMaterialValue($matName)] } {
  if { $::SaveMaxGroupIDVSMaterialValue($matName) < $maxIDnumber } {
     return $::SaveMaxGroupIDVSMaterialValue($matName) 
  } else {
    return $maxIDnumber
  }
} else {
  WarnWinText "Warning: Condition Monitor_for_Point_Nearest_Node_IP. Material $matName is not used in the model..."
  return $maxIDnumber  
}
  return $maxIDnumber
}

 # funkce na vypis macroJointu pro digicon
proc DejDigiMacroJoint { x soubor id_mat type} {
#tk_messageBox -message "x = $x , int = $int" -icon warning -type ok
#set soubor [GiD_AccessValue get intvdata $int "Printing_trajectory_csv_file"] 
#tk_messageBox -message "$soubor" -icon warning -type ok

       set re {(?x)
      	\s+ ([[:graph:]]+)      # first number
      	\s+ ([[:graph:]]+)      # second number
        \s+ ([[:graph:]]+)      # third number
       }
 
      set vypisX ""
      set vypisY ""        
      set vypisZ ""
      set vypis ""
      set vypisSelection ""
      set vypisSelectionTrack ""
      set vypisSelectionMnode ""
      set jointId 80000
       
    global GiDProjectDir
    #WarnWinText $GiDProjectDir
    append GiDProjectDir "\\" 
     cd $GiDProjectDir       
       
     #set soubor "c:/text.txt"  
     set fp [open $soubor r]
     
     #pokus cist v unicode
     #fconfigure $fp -encoding cp1250
     #set data [encoding convertto utf-8 [read $fp]]
     #pokus cist v unicode
     
     set data [read $fp]
     close $fp
     
    
     
     #  Process data file
     set data [split $data "\n"]
     foreach line $data {
     
     # Match the first two words, the first one allows uppercase
     #set result [regexp {[a-z]+} $sample match]
     set match ""
     set result [regexp {[-+0-9A-Za-z\;.]+} $line match ]
     #WarnWinText "Result: $result match: $match"    
     
      set line $match
      set line " $line "
      set line [string map { \; " " } $line]
      set line [string map { \, \. } $line]
      set b [regexp $re $line match matchX matchY matchZ]

      if {$b==1 && $matchX!="x"} {
      #set vypisX "$vypisX $matchX"
      #set vypisY "$vypisY $matchY"
      #set vypisZ "$vypisZ $matchZ"
      set jointId [expr $jointId+1]
      set vypis "$vypis
\t$jointId \t$matchX \t$matchY \t$matchZ"

      
      set vypisSelection "$vypisSelection
SELECTION \"N$jointId\" GENERATE NODE NEAREST MACRO NODES $jointId EXECUTE"  

      set vypisSelectionTrack "$vypisSelectionTrack INSERT \"N$jointId\" " 

      set vypisSelectionMnode "$vypisSelectionMnode $jointId"

      }
      if {$b==1 && $matchX=="x"} {
          set vypisSelectionTrack "$vypisSelectionTrack INSERT \"N0\" "       
      }
          
     }      
      
if {$type=="Top_center_of_printing_path-first_layer_only" || $type=="Top_center_of_printing_path-all_layers"} {
    set vypisSelectionTrack "SELECTION \"TRACK_MNODES_IDS_NAME_$id_mat\" KEEP_DUPLICATES LIST $vypisSelectionMnode"
    set vypisSelection ""
} else {
      set vypisSelection "$vypisSelection
SELECTION \"N0\" LIST 0"
      set vypisSelectionTrack "SELECTION \"TRACK_NODES_IDS_NAME_$id_mat\" KEEP_DUPLICATES $vypisSelectionTrack"
}       
     
      return "
$vypis

$vypisSelection

$vypisSelectionTrack
"
      
if {$x==0} { return "$vypisX" }
if {$x==1} { return "$vypisY" }

return ""
}


proc DejObsahSouboru { soubor } {
#tk_messageBox -message "x = $x , int = $int" -icon warning -type ok
#tk_messageBox -message "$soubor" -icon warning -type ok

    global GiDProjectDir
    global problem_dir
    
     set template $problem_dir
     append template "\\..\\Include.src.bas\\Template_for_function.bas"    
     
     set inpfile $GiDProjectDir
     append inpfile "\\AtenaCalculation\\$soubor"        
 
 if { [file exists $inpfile] == 0} {  
     GiD_Process escape escape escape Files WriteForBAS "$template" "$inpfile"
     set fp [open $template r]
     set data [read $fp]
     close $fp
     return "$data"        
}

if { [file exists $inpfile] == 1} {
     #set soubor "c:/text.txt"  
     set fp [open $inpfile r]
     set data [read $fp]
     close $fp
     
     return "$data"     
} 

return "file not found"
}

proc DejObsahSouboruMaterial { soubor1 soubor2 templatefile } {
set soubor $soubor1
append soubor $soubor2
#tk_messageBox -message "x = $x , int = $int" -icon warning -type ok
#tk_messageBox -message "$soubor --- $soubor1 --- $soubor2 --- $templatefile" -icon warning -type ok

    global GiDProjectDir
    global problem_dir
    
     set template $problem_dir
     append template "\\..\\Include.src.bas\\$templatefile"
     # Template_for_material.bas    
     
     set inpfile $GiDProjectDir
     append inpfile "\\AtenaCalculation\\$soubor"        
 
 if { [file exists $inpfile] == 0} {  
     GiD_Process escape escape escape Files WriteForBAS "$template" "$inpfile"
     set fp [open $template r]
     set data [read $fp]
     close $fp
     return "$data"        
}

if { [file exists $inpfile] == 1} {
     #set soubor "c:/text.txt"  
     set fp [open $inpfile r]
     set data [read $fp]
     close $fp
     
     return "$data"     
} 

return "file not found"
}

proc JN_InitGIDPostProcess {} {
    global GiDProjectDir
    global problem_dir
     cd $GiDProjectDir
     set A3Post $problem_dir
     append A3Post "/GiD3DPost.bat"
#     WarnWinText $A3Post
     exec $A3Post >post.txt >& NUL:

     ReadResults

     set verze [GiD_Info GiDVersion]

     if {$verze  == "10.0.8" || $verze  =="10.0.9"} {
     ReadResults
     } 

     return ""
}

proc AtenaCalculate {} {
    WarnWin [join [list "For calculation use Calculate Menu and item Calculate or ATENA and item Atena Analysis!"]]
#     exec "c:/Program Files/CervenkaConsulting/ATENA v2/AtenaWin.exe"
#     {-np- AtenaCalculate }
}

#proc userHelpWindow {} {
#    exec NOTEPAD.EXE
#}

proc EndMyBitmaps {} {
    global ProgramName ProblemTypePriv

    ReleaseToolbar "$ProgramName prebar"
#    ReleaseToolbar "$ProgramName postbar"
    rename AtenaPreBitmaps ""
#    rename AtenaPostBitmaps ""
    catch { destroy $ProblemTypePriv(pretoolbarwin) }
#    catch { destroy $ProblemTypePriv(posttoolbarwin) } 
}

proc EndGIDProject {} {
    EndMyBitmaps 
    global GidPriv
    set GidPriv(ProgName) GiD
    ChangeWindowTitle ""
    GiDMenu::Delete ATENA PRE
}


proc BeforeTransformProblemType { file oldproblemtype newproblemtype } {
#WarnWinText "old: $oldproblemtype"
#WarnWinText "new: $newproblemtype"
 set stornuj 0
 
 if { $oldproblemtype =="Atena/Static" && $newproblemtype=="Atena/Transport" || $oldproblemtype =="Atena/Transport" && $newproblemtype=="Atena/Static"} {
 set stornuj 1
 }
 if { $oldproblemtype =="Atena/Dynamic" && $newproblemtype=="Atena/Transport" || $oldproblemtype =="Atena/Transport" && $newproblemtype=="Atena/Dynamic"} {
 set stornuj 1
 }
 if { $oldproblemtype =="Atena/Creep" && $newproblemtype=="Atena/Transport" || $oldproblemtype =="Atena/Transport" && $newproblemtype=="Atena/Creep"} {
 set stornuj 1
 }
 
 if { $stornuj =="2"} {
 set warninfo "Transform from $oldproblemtype to $newproblemtype isn't allowed. Please use Update and define and assign the materials and conditions."
 WarnWinText "$warninfo"
  #tk_messageBox -title "Warning: Unsupported transformation" -message "$warninfo" -icon warning -type ok
  Atena_MessageBoxOk "Warning: Unsupported transformation" "$warninfo" warning   
   return "-cancel-"
 } 
 
  set CalculationAnalysis [GiD_AccessValue get gendata "Calculation_Analysis"]
  if { $CalculationAnalysis =="Temperature3D"} {
    GiD_AccessValue set gendata Calculation_Analysis "Transport"
  }    
  if { $CalculationAnalysis =="Temperature2D"} {
    GiD_AccessValue set gendata Calculation_Analysis "Transport"
  } 
  

  if { $CalculationAnalysis =="Static3D"} {
    GiD_AccessValue set gendata Calculation_Analysis "Static"
  } 
  if { $CalculationAnalysis =="Transport3D"} {
    GiD_AccessValue set gendata Calculation_Analysis "Transport"
  } 
  if { $CalculationAnalysis =="Dynamic3D"} {
    GiD_AccessValue set gendata Calculation_Analysis "Dynamic"
  } 
  if { $CalculationAnalysis =="Creep3D"} {
    GiD_AccessValue set gendata Calculation_Analysis "Creep"
  }         
  
  
  #set ForImportUse [GiD_AccessValue get gendata "For_Import_Use"]
  #if { $ForImportUse =="Atena5Console"} {
  #  GiD_AccessValue set gendata For_Import_Use "AtenaConsole"
  #}  
  #if { $ForImportUse =="Atena5Console64"} {
  #  GiD_AccessValue set gendata For_Import_Use "AtenaConsole64"
  #}     
  
  #GiD_Info unitssystems ?gid|prj|usersys|modunit|prbsys|udstate|magused?
  #WarnWinText "Before"
  #WarnWinText [GiD_Info unitssystems prbsys] 
  set ::UnitSystemBeforeMesh [GiD_Info unitssystems prbsys]
  #WarnWinText $::UnitSystemBeforeMesh
  return ""
 }
 
 proc dej_jmeno_projektu { ProjectName } {
  #WarnWinText "ProjectName = $ProjectName"
  regsub -all {\\} $ProjectName {/} ProjectName
  
  #WarnWinText "ProjectName2 = $ProjectName"
  if { [file extension $ProjectName] == ".gid" } {
     set ProjectName [file root $ProjectName]
   }
   #WarnWinText "ProjectName3 = $ProjectName" 
   #set ProjectName [file root $ProjectName]
   #WarnWinText "ProjectName4 = $ProjectName"
   set pozice [string last / $ProjectName]
   #WarnWinText "pozice = $pozice"

   set ProjectName [string range $ProjectName $pozice+1 $pozice+150]
   #WarnWinText "ProjectName5 = $ProjectName"
   return $ProjectName 
 }
 
# proc GiD_Event_AfterSaveAsGIDProject { old_modelname new_modelname } {
#    AfterSaveAs $old_modelname $new_modelname
#}
 
 proc AfterSaveAs { old_modelname new_modelname } {
  #WarnWinText "1GiD_Event_AfterSaveAsGIDProject"
  #WarnWinText "old_modelname = $old_modelname"
  #WarnWinText "new_modelname = $new_modelname"
  set file_name_old [dej_jmeno_projektu $old_modelname]
  set file_name_new [dej_jmeno_projektu $new_modelname]

  #WarnWinText "file_name_old = $file_name_old" 
  #WarnWinText "file_name_new = $file_name_new"  

  if { $old_modelname == $new_modelname} {
  return ""
  }  

  if { $old_modelname == "UNNAMED"} {
  return ""
  }    
  
  file mkdir $new_modelname.gid/AtenaCalculation
  
      #set answer [tk_messageBox -title "ATENA-GiD" -message "Do you also want to copy the ATENA project files? .inp, .out, .msg, .err. ccs and .layout ?" -type yesno -icon question]
      set answer [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Do you also want to copy the ATENA project files? .inp, .out, .msg, .err. ccs and .layout ?" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]      
      switch -- $answer {
                   yes { 
                  
                  set fexist [file exist $old_modelname.gid/AtenaCalculation/monitors.csv]
                  #WarnWinText "csv fexist = $fexist"
                    if { $fexist ==1} {
                       file copy -force $old_modelname.gid/AtenaCalculation/monitors.csv $new_modelname.gid/AtenaCalculation
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/monitors.csv has been copied"
                    } else {
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/monitors.csv does not exists"
                    }   
                  
                  set fexist [file exist $old_modelname.gid/AtenaCalculation/$file_name_old.inp]
                  #WarnWinText "inp fexist = $fexist"
                    if { $fexist ==1} {
                       file copy -force $old_modelname.gid/AtenaCalculation/$file_name_old.inp $new_modelname.gid/AtenaCalculation
                       file rename -force $new_modelname.gid/AtenaCalculation/$file_name_old.inp $new_modelname.gid/AtenaCalculation/$file_name_new.inp
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.inp has been copied and renamed"
                    } else {
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.inp does not exists"
                    }   

                  set fexist [file exist $old_modelname.gid/AtenaCalculation/$file_name_old.out]
                  #WarnWinText "out fexist = $fexist"
                    if { $fexist ==1} {
                       file copy -force $old_modelname.gid/AtenaCalculation/$file_name_old.out $new_modelname.gid/AtenaCalculation
                       file rename -force $new_modelname.gid/AtenaCalculation/$file_name_old.out $new_modelname.gid/AtenaCalculation/$file_name_new.out
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.out has been copied and renamed"
                    } else {
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.out does not exists"
                    }  
                    
                  set fexist [file exist $old_modelname.gid/AtenaCalculation/$file_name_old.msg]
                  #WarnWinText "msg fexist = $fexist"
                    if { $fexist ==1} {
                       file copy -force $old_modelname.gid/AtenaCalculation/$file_name_old.msg $new_modelname.gid/AtenaCalculation
                       file rename -force $new_modelname.gid/AtenaCalculation/$file_name_old.msg $new_modelname.gid/AtenaCalculation/$file_name_new.msg
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.msg has been copied and renamed"
                    } else {
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.msg does not exists"
                    }  
                    
                  set fexist [file exist $old_modelname.gid/AtenaCalculation/$file_name_old.err]
                  #WarnWinText "err fexist = $fexist"
                    if { $fexist ==1} {
                       file copy -force $old_modelname.gid/AtenaCalculation/$file_name_old.err $new_modelname.gid/AtenaCalculation
                       file rename -force $new_modelname.gid/AtenaCalculation/$file_name_old.err $new_modelname.gid/AtenaCalculation/$file_name_new.err
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.err has been copied and renamed"
                    } else {
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.err does not exists"
                    }     
                    
                  set fexist [file exist $old_modelname.gid/AtenaCalculation/$file_name_old.ccs]
                  #WarnWinText "ccs fexist = $fexist"
                    if { $fexist ==1} {
                       file copy -force $old_modelname.gid/AtenaCalculation/$file_name_old.ccs $new_modelname.gid/AtenaCalculation
                       
                       
                        set in  [open $new_modelname.gid/AtenaCalculation/$file_name_old.ccs r]
                        set out [open $new_modelname.gid/AtenaCalculation/$file_name_new.ccs w]
                        
                        while {[gets $in line] != -1} {
                            #transform $line somehow
                            #set line [string tolower $line]
                            set line [regsub $file_name_old $line $file_name_new]
                            puts $out $line
                        }
                        
                        close $in
                        close $out   
                        
                        file delete "$new_modelname.gid/AtenaCalculation/$file_name_old.ccs"                    
                       
                       #file rename -force $new_modelname.gid/AtenaCalculation/$file_name_old.ccs $new_modelname.gid/AtenaCalculation/$file_name_new.ccs
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.ccs has been copied and renamed"
                       
                       
                       
                    } else {
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.ccs does not exists"
                    }                                                           

                  set fexist [file exist $old_modelname.gid/AtenaCalculation/$file_name_old.layout]
                  #WarnWinText "layout fexist = $fexist"
                    if { $fexist ==1} {
                       file copy -force $old_modelname.gid/AtenaCalculation/$file_name_old.layout $new_modelname.gid/AtenaCalculation
                       file rename -force $new_modelname.gid/AtenaCalculation/$file_name_old.layout $new_modelname.gid/AtenaCalculation/$file_name_new.layout
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.layout has been copied and renamed"
                    } else {
                       WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.layout does not exists"
                    }    
               

                   }
                   no {
                   } 
      } 
      
    set tcl_files_csv [glob -nocomplain $old_modelname.gid/{*}.csv] 
    set numberall_csv [llength $tcl_files_csv]   
      
    #WarnWinText "numberall_csv $numberall_csv"
      
      if { $numberall_csv > 0} {
         #set answercsv [tk_messageBox -title "ATENA-GiD" -message "Do you also want to copy all *.csv files from GiD Project directory? $numberall_csv files" -type yesno -icon question]
         set answercsv [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Do you also want to copy all *.csv files from GiD Project directory? $numberall_csv files" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
      } else {
         set answercsv "no"
      }      
      switch -- $answercsv {
                   yes { 
                      foreach x $tcl_files_csv {
                         #WarnWinText "tcl_files ... = $x"
                         set fexist [file exist $x]
                          if { $fexist ==1} {
                          
                             file copy -force $x $new_modelname.gid
                             WarnWinText "File $x has been copied"
                          } 
                      } 
                   }
                   no {
                   } 
      } 

    set tcl_files_txt [glob -nocomplain $old_modelname.gid/{*}.txt] 
    set numberall_txt [llength $tcl_files_txt]     
   # WarnWinText "numberall_txt $numberall_txt"  
      if { $numberall_txt > 0} {
         #set answertxt [tk_messageBox -title "ATENA-GiD" -message "Do you also want to copy all *.txt files from GiD Project directory? $numberall_txt files" -type yesno -icon question]
         set answertxt [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Do you also want to copy all *.txt files from GiD Project directory? $numberall_txt files" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
      } else {
         set answertxt "no"
      }      
      switch -- $answertxt {
                   yes { 
                      foreach x $tcl_files_txt {
                         #WarnWinText "tcl_files ... = $x"
                         set fexist [file exist $x]
                          if { $fexist ==1} {
                          
                             file copy -force $x $new_modelname.gid
                             WarnWinText "File $x has been copied"
                          } 
                      } 
                   }
                   no {
                   } 
      } 
      
    set tcl_files_xml [glob -nocomplain $old_modelname.gid/AtenaCalculation/{*}.xml] 
    set numberall_xml [llength $tcl_files_xml]     
    #WarnWinText "numberall_xml $numberall_xml"   
      if { $numberall_xml > 0} {
         #set answerxml [tk_messageBox -title "ATENA-GiD" -message "Do you also want to copy all *.xml files from GiD Project directory? $numberall_xml files" -type yesno -icon question]
         set answerxml [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Do you also want to copy all *.xml files from GiD Project directory? $numberall_xml files" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]         
      } else {
         set answerxml "no"
      }      
      switch -- $answerxml {
                   yes { 
                      foreach x $tcl_files_xml {
                         #WarnWinText "tcl_files ... = $x"
                         set fexist [file exist $x]
                          if { $fexist ==1} {
                          
                             file copy -force $x $new_modelname.gid/AtenaCalculation
                             WarnWinText "File $x has been copied"
                          } 
                      } 
                   }
                   no {
                   } 
      }  
      
    set tcl_files_inp [glob -nocomplain $old_modelname.gid/AtenaCalculation/{*}.inp] 
    set numberall_inp [llength $tcl_files_inp] 
    #WarnWinText "numberall_inp $numberall_inp"
    set fexist [file exist $old_modelname.gid/AtenaCalculation/$file_name_old.inp]
    if { $fexist ==1} {
       set numberall_inp [expr $numberall_inp-1]            
    }        
    #WarnWinText "numberall_inp $numberall_inp"  
      if { $numberall_inp > 0} {
         #set answerinp [tk_messageBox -title "ATENA-GiD" -message "Do you also want to copy all *.inp files from GiD Project directory? $numberall_inp files" -type yesno -icon question]
         set answerinp [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Do you also want to copy all *.inp files from GiD Project directory? $numberall_inp files" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
      } else {
         set answerinp "no"
      }      
      switch -- $answerinp {
                   yes { 
                      foreach x $tcl_files_inp {
                         #WarnWinText "tcl_files ... = $x"
                         if {$x == "$file_name_old.inp"} {
                         } else {
                             set fexist [file exist $x]
                              if { $fexist ==1} {
                              
                                 file copy -force $x $new_modelname.gid/AtenaCalculation
                                 WarnWinText "File $x has been copied"
                              }
                         }  
                      } 
                      
                      set fexist [file exist $new_modelname.gid/AtenaCalculation/$file_name_old.inp]
                      if { $fexist ==1} {
                         file delete "$new_modelname.gid/AtenaCalculation/$file_name_old.inp"            
                      } 
                      
                   }
                   no {
                   } 
      }           
      
      
      
      
    set tcl_files [glob -nocomplain $old_modelname.gid/AtenaCalculation/$file_name_old.{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}]
    
    set sizesall 0
    set numberall [llength $tcl_files] 
    foreach x $tcl_files {
       #WarnWinText "tcl_files ... = $x"
       set file_one [file size $x]
       set sizesall [expr $sizesall+$file_one]
    } 
   
   set sizesall [expr $sizesall/1024]
   set sizesallText "$sizesall kB"
   if { $sizesall > 1024} {
   set sizesall [expr $sizesall/1024]
   set sizesallText "$sizesall MB"
   }
   if { $sizesall > 1024} {
   set sizesall [expr $sizesall/1024]
   set sizesallText "$sizesall GB"
   }
      
      if { $numberall > 0} {
         #set answer [tk_messageBox -title "ATENA-GiD" -message "Do you also want to copy all ATENA results files? $numberall files (size $sizesallText)" -type yesno -icon question]
         set answer [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Do you also want to copy all ATENA results files? $numberall files (size $sizesallText)" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
      } else {
         set answer "no"
      }
      switch -- $answer {
                   yes { 
                  
                  file mkdir $new_modelname.gid/AtenaCalculation
                  
                      foreach x $tcl_files {
                         #WarnWinText "tcl_files ... = $x"
                         set fexist [file exist $x]
                          if { $fexist ==1} {
                          
                             file copy -force $x $new_modelname.gid/AtenaCalculation
                             set pozice [string last . $x]
                             set koncovka [string range $x $pozice+1 $pozice+150]
                             #WarnWinText "pozice = $pozice, koncovka=$koncovka"
                             file rename -force $new_modelname.gid/AtenaCalculation/$file_name_old.$koncovka $new_modelname.gid/AtenaCalculation/$file_name_new.$koncovka
                             WarnWinText "File $old_modelname.gid/AtenaCalculation/$file_name_old.$koncovka has been copied and renamed"
                          } 
                      } 
                   }
                   no {
                   } 
      }       
  return ""
}

 
 proc AfterTransformProblemType { file oldproblemtype newproblemtype } {
# WarnWinText $oldproblemtype
# WarnWinText $newproblemtype
 JN_AfterTransformProblemType $file $oldproblemtype $newproblemtype
 
global VersionNumberOnly VersionNumber
 #source "Problemtypes\\Atena\\include.src.bas\\Ver_number.tcl"
  GiD_Process Mescape Data ProblemData "-SingleField-" Script_version $VersionNumberOnly
  #GiD_Process Mescape Files Save
  #tk_messageBox -message "Project transformed to ATENA-GiD scripts version $VersionNumberOnly" -type ok
  WarnWinText "\n\n Project transformed to ATENA-GiD scripts version $VersionNumberOnly \n\n"
   }

proc loadGlobals { filename } {
  
  #WarnWinText "filename = $filename"
  set ProjectName 0
  set aa [GiD_Info Project]
  set ProblemType [lindex $aa 0]
  #WarnWinText "ProblemType = $ProblemType"
  set ProjectName [lindex $aa 1]
  
  if { $ProjectName == "UNNAMED" } {
    set ProjectName $filename
  }
  #WarnWinText "ProjectName = $ProjectName"

  regsub -all {\\} $ProjectName {/} ProjectName
  
  #WarnWinText "ProjectName2 = $ProjectName"
  if { [file extension $ProjectName] == ".gid" } {
     set ProjectName [file root $ProjectName]
   }
   #WarnWinText "ProjectName3 = $ProjectName" 
   #set ProjectName [file root $ProjectName]
   #WarnWinText "ProjectName4 = $ProjectName"
   set pozice [string last / $ProjectName]
   #WarnWinText "pozice = $pozice"
   global GiDProjectDir
   global GiDProjectName
   
   set comp -1
   catch {
       set comp [GiDVersionCmp "9.0.0"]
   }
   if {$comp < 0 } {
       set aa [.central.s info Project]
       #set GiDProjectName [file root $ProjectName]
       #set GiDProjectName [lindex $aa 1]
       set GiDProjectDir $ProjectName.gid
       set GiDProjectName [file tail $ProjectName]
   } else {
      set GiDProjectName [string range $ProjectName $pozice+1 $pozice+150]
      set GiDProjectDir [string range $ProjectName 0 $pozice-1]   
      append GiDProjectDir "/$GiDProjectName.gid"
   }   
   #WarnWinText "GiDProjectDir = $GiDProjectDir"
   #WarnWinText "GiDProjectDir after append = $GiDProjectDir"
   #WarnWinText "GiDProjectDir = $GiDProjectDir"
   #WarnWinText "GiDProjectName = $GiDProjectName"
   
	#	  set sc [DWLocalGetValue $GDN $STRUCT "TaskName"]
   GiD_AccessValue set gendata TaskName $GiDProjectName
   GiD_AccessValue set gendata RestartTaskName $GiDProjectName
	 #set on [DWLocalSetValue $GDN $STRUCT "TaskName" $GiDProjectName]
   #set on [DWLocalSetValue $GDN $STRUCT "RestartTaskName" $GiDProjectName]
   global taskName
   set taskName $GiDProjectName
   #WarnWinText "taskName = $taskName"
}



#title: text window caption
#question: text, the message to be asked to the user
#options: is the list of keywords, one of them is the return value
#labels: are the list of strings associated to options (could be translated)
#image: error, info, question or warning
#option_do_it_for_all_text, "" to not show checkbow, otherwise show this text and return a list of two values {result do_it_for_all}
proc Atena_MessageBoxOptionsButtons { title question options labels image option_do_it_for_all_text } {
   set res 0
   if { [GidUtils::VersionCmp "15.0"] >=0 } {
      set res [MessageBoxOptionsButtons $title $question $labels $image $option_do_it_for_all_text $options]
              #MessageBoxOptionsButtons $title $question [list ok] [list [_ "Ok"]] $image ""
#set answer [Atena_MessageBoxOptionsButtons "ATENA-GiD" "You?"   "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
#                  MessageBoxOptionsButtons $title        $question [list ok] [list [_ "Ok"]] $image ""
                                
   } else {
      set message_small ""
      set res [lindex $options [tk_dialogRAM2 .gid.tempwin $title $question $message_small $image 0 {*}$labels]]
   }
   return $res
}

proc Atena_MessageBoxOk { title question image } {
   set res 0
   if { [GidUtils::VersionCmp "15.0"] >=0 } {
      set res [MessageBoxOk $title $question $image]
   } else {
      set message_small ""
      set res [tk_dialogRAM2 .gid.tempwin $title $question $message_small $image 0 [list [_ "Ok"]]]
   }
   return $res
}

proc AfterOpenFile { filename format error } {
global VersionNumberOnly VersionNumber
#source "Problemtypes\\Atena\\include.src.bas\\Ver_number.tcl"
global GiDProjectDir
global GiDProjectName

#WarnWinText "GiDProjectName = $GiDProjectName"
#global taskName 
#WarnWinText "taskName = $taskName"

#  set ProjectName 0
#  set aa [GiD_Info Project]
#  set ProblemType [lindex $aa 0]
#  #WarnWinText "ProblemType = $ProblemType"
#  set ProjectName [lindex $aa 1]
#  set ProjectName $filename
#  
#  if { [file extension $ProjectName] == ".gid" } {
#     set ProjectName [file root $ProjectName]
#   }
#   set ProjectName [file root $ProjectName]
#   set pozice [string last / $ProjectName]
#   set GiDProjectName [string range $ProjectName $pozice+1 $pozice+150]
#   set GiDProjectDir [string range $ProjectName 0 $pozice-1]
#   append GiDProjectDir "/$GiDProjectName.gid"
    
 loadGlobals $filename
 
 
set answer [GiD_Info gendata]
# WarnWinText "$answer"
if [ string match -nocase "*Script_Version*" $answer ] {
# WarnWinText "ano retezec obsahuje Script_Version"
 set Exampleversion [GiD_AccessValue get gendata "Script_Version"]
# tk_messageBox -message "$Exampleversion  < $VersionNumberOnly" -type ok
# WarnWinText "$Exampleversion"
} else {
# WarnWinText "ne retezec neobsahuje Script_Version"
  set Exampleversion 0
}


 # if the example is too old
    if { $Exampleversion < $VersionNumberOnly} {
        if { [info exists ::Atena_AskToTransform] && $::Atena_AskToTransform == 0 } {
           set answer yes
        } else {
           #set answer [tk_messageBox -title "ATENA-GiD" -message "Your project was created in ATENA-GiD scripts ($Exampleversion), but you are now using scripts ($VersionNumberOnly). \n\nDo you want to transform the project to the installed scripts?" -type yesno -icon question]
           #Atena_MessageBoxOptionsButtons { title question options labels image option_do_it_for_all_text }
           set answer [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Your project was created in ATENA-GiD scripts ($Exampleversion), but you are now using scripts ($VersionNumberOnly). \n\nDo you want to transform the project to the installed scripts?" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
           #set answer [MessageBoxOptionsButtons "ATENA-GiD" "Your project was created in ATENA-GiD scripts ($Exampleversion), but you are now using scripts ($VersionNumberOnly). \n\nDo you want to transform the project to the installed scripts?" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
        }
      switch -- $answer {
                   yes {
                     DoTransform 
                     # WarnWinText "transformuji"
                   }
                   no {
                     #tk_messageBox -title "ATENA-GiD Warning" -message "Working on project which isn't transformed to the installed ATENA-GiD version is on your own risk!" -type ok -icon warning
                     #GidUtils::SetWarnLine "Working on project which isn't transformed to the installed ATENA-GiD version is on your own risk!"
                     #MessageBoxOk "ATENA-GiD Warning" "Working on project which isn't transformed to the installed ATENA-GiD version is on your own risk!" question
                     Atena_MessageBoxOk "ATENA-GiD Warning" "Working on project which isn't transformed to the installed ATENA-GiD version is on your own risk!" question
                   # WarnWinText "NE transformuji"
                   }
                   
                   }
    }
 # if the example is too new
     if { $Exampleversion > $VersionNumberOnly} {
     if { [info exists ::Atena_AskToDownload] && $::Atena_AskToDownload == 0 } {
        set answer no
      } else {
      #set answer [tk_messageBox -title "ATENA-GiD" -message "Your ATENA-GiD scripts ($VersionNumberOnly) are older then scripts used to create this project ($Exampleversion). \nUsing newer project in older ATENA-GiD is on your own risk!\nPlease check our website for \"ATENA-GiD Interface\" updates. \n\nOpen http://www.cervenka.cz/downloads ?" -type yesno -icon question]
      set answer [Atena_MessageBoxOptionsButtons "ATENA-GiD" "Your ATENA-GiD scripts ($VersionNumberOnly) are older then scripts used to create this project ($Exampleversion). \nUsing newer project in older ATENA-GiD is on your own risk!\nPlease check our website for \"ATENA-GiD Interface\" updates. \n\nOpen http://www.cervenka.cz/downloads ?" "" [list yes no] [list [_ "Yes"] [_ "No"]] question]
      }
      switch -- $answer {
                   yes {VisitWeb http://www.cervenka.cz/downloads
                   # WarnWinText "ano vase skripty jsou starsi nez prikladove"
                   }
                   no {
                   # WarnWinText "NE vase skripty jsou starsi nez prikladove"
                   }
                   }
    }
  #zmena temperature na transport
    
  ZapisAktualniScriptPriSpusteni
 
  #prejmenuj AtenaCreepCalculation,AtenaDynamicCalculation,AtenaTransportCalculation na AtenaCalculation
  #set task5dir $filename
  #append task5dir ".gid"  

  if { [file exists "$GiDProjectDir\\AtenaCreepCalculation"] == 1} {
     file rename -force "$GiDProjectDir\\AtenaCreepCalculation" "$GiDProjectDir\\AtenaCalculation"   
     WarnWinText "Directory AtenaCreepCalculation was renamed to AtenaCalculation"
  }
  if { [file exists "$GiDProjectDir\\AtenaDynamicCalculation"] == 1} {
     file rename -force "$GiDProjectDir\\AtenaDynamicCalculation" "$GiDProjectDir\\AtenaCalculation"   
     WarnWinText "Directory AtenaDynamicCalculation was renamed to AtenaCalculation"
  }
  if { [file exists "$GiDProjectDir\\AtenaTransportCalculation"] == 1} {
     file rename -force "$GiDProjectDir\\AtenaTransportCalculation" "$GiDProjectDir\\AtenaCalculation"   
     WarnWinText "Directory AtenaTransportCalculation was renamed to AtenaCalculation"
  }    
} 

proc Kontrolanovinek {oldproblemtype newproblemtype} {
#tk_messageBox -title "ATENA-GiD news" -message "$oldproblemtype $newproblemtype" -type ok -icon info

if { $oldproblemtype == 0 } { set oldproblemtype 10000 }

# 2187
    set version 2187
    if { $oldproblemtype < $version && $newproblemtype > $version } {
    #set answer [tk_messageBox -title "ATENA-GiD news" -message "This is information about news in this version of ATENA-GiD\n\nThe SHELL and BEAM material definitions have changed in ATENA-GiD version 2187. We are sorry they can not be updated automatically, please re-define and re-assign them." -type ok -icon info]
    Atena_MessageBoxOk "ATENA-GiD news" "This is information about news in this version of ATENA-GiD\n\nThe SHELL and BEAM material definitions have changed in ATENA-GiD version 2187. We are sorry they can not be updated automatically, please re-define and re-assign them." info
    }
# 2187
    set version 2198
    if { $oldproblemtype < $version && $newproblemtype > $version } {
    #set answer [tk_messageBox -title "ATENA-GiD news" -message "This is information about news in this version of ATENA-GiD\n\nIn the previous versions, the temperature values in the temp.dep. functions had to be given relative to the reference temperature, which means the user had to re-define all the functions to adjust to another reference/initial temperature. Since version 2198, the functions are defined in absolute temperatures and the adjustment due to the initial temperature is done automatically when writing the input file. You can set the init. temp. in this dialog to 0 to have the same behavior as in the previous version." -type ok -icon info]
    Atena_MessageBoxOk "ATENA-GiD news" "This is information about news in this version of ATENA-GiD\n\nIn the previous versions, the temperature values in the temp.dep. functions had to be given relative to the reference temperature, which means the user had to re-define all the functions to adjust to another reference/initial temperature. Since version 2198, the functions are defined in absolute temperatures and the adjustment due to the initial temperature is done automatically when writing the input file. You can set the init. temp. in this dialog to 0 to have the same behavior as in the previous version." info
    }
# 2860
    set version 2843
    if { $oldproblemtype < $version && $newproblemtype > $version } {
    #set answer [tk_messageBox -title "ATENA-GiD news" -message "This is information about news in this version of ATENA-GiD\n\nNew material parameters FC_REDUCTION and AGG_SIZE have been added in version 2843. To achieve the same results as in the previous versions, you have to turn off Active_Aggregate_Interlock and set FC_REDUCTION = 1. Not for transport problem." -type ok -icon info]
    Atena_MessageBoxOk "ATENA-GiD news" "This is information about news in this version of ATENA-GiD\n\nNew material parameters FC_REDUCTION and AGG_SIZE have been added in version 2843. To achieve the same results as in the previous versions, you have to turn off Active_Aggregate_Interlock and set FC_REDUCTION = 1. Not for transport problem." info
    }
# 6160
    set version 6159
    if { $oldproblemtype < $version && $newproblemtype > $version } {
    #set answer [tk_messageBox -title "ATENA-GiD news" -message "In models with construction process is necessary to replace conditions for NEW MATERIAL." -type ok -icon info]
    #set answer [tk_messageBox -title "ATENA-GiD news" -message "Condition \"Spring for Line\" is not supported any more. Please assign Spring Material to the line instead." -type ok -icon info]
    Atena_MessageBoxOk "ATENA-GiD news" "In models with construction process is necessary to replace conditions for NEW MATERIAL." info
    Atena_MessageBoxOk "ATENA-GiD news" "Condition \"Spring for Line\" is not supported any more. Please assign Spring Material to the line instead." info    
    }
# 7839
    set version 7839
    if { $oldproblemtype < $version && $newproblemtype > $version } {
    #set answer [tk_messageBox -title "ATENA-GiD news" -message "The implementation of reinforcement bond functions has been reworked. If you are using bond, please redefine the functions for all reinforcement bond materials." -type ok -icon info]
    Atena_MessageBoxOk "ATENA-GiD news" "The implementation of reinforcement bond functions has been reworked. If you are using bond, please redefine the functions for all reinforcement bond materials." info
    }    
# 19975
    set version 19974
    if { $oldproblemtype < $version && $newproblemtype > $version } {
   set comp -1
   catch {
       set comp [GiDVersionCmp "15.0.2"]
   }
   if {$comp < 0 } {
        #set answer [tk_messageBox -title "ATENA-GiD news" -message "Minor changes in point monitors for the nearest NODE/IP were implemented. In GiD versions older 15.0.2, please check the settings of your monitors." -type ok -icon info]
        Atena_MessageBoxOk "ATENA-GiD news" "Minor changes in point monitors for the nearest NODE/IP were implemented. In GiD versions older 15.0.2, please check the settings of your monitors." info
        #WarnWinText "comp = $comp mensi "
   } else {
        #WarnWinText "comp = $comp vetsi  "
   }

    }  
    
         
    
    
return ""
}
