// $Source: /usr/local/SimSoft/Sfx3D/src/gts/gtsmdat.cpp,v $
// $Revision: 1.3 $
// $Author: baum $
// $Date: 2001/12/04 16:27:05 $
//
// Login name of the user who locked the revision
// $Locker:  $
// Tag name used to checkout the file
// $Name: Release-14 $
// State assigned to revision
// $State: Exp $
//
//
// $Log: gtsmdat.cpp,v $
// Revision 1.3  2001/12/04 16:27:05  baum
// -Verschiedene Bugs entfernt
//
// Revision 1.2  2001/11/29 15:18:10  baum
// -Realzeitsteuerung eingebaut
// -Handsteuerung eingebaut
//

#define _GTSMEN_CPP

/*** Windows headers ***/
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
//#include <winnt.h>
#endif /*WIN32*/

#ifdef __GNUC__
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "XGraph/Select.h"
#include "XGraph/Parent.h"
#include "XGraph/GeoObj.h"
#include "XGraph/Space.h"
#include "StrmSet.h"

#include <tcl.h>
#include <tk.h>

#include "IniFile.h"
//#include "StrmSet.h"
#include "FileSys.h"
#include "gtsglob.h"
#include "gtsgraf.h"
#include "gtsini.h"
#include "gtstgb.h"
#include "gtspal.h"
#include "gtsb00.h"
#include "gtsb01.h"
#include "gtsb02.h"
#include "gtsb03.h"
#include "gtsb04.h"
#include "gtsb05.h"
#include "gtsb06.h"
#include "gtsb07.h"
#include "gtsb08.h"
#include "gtsb09.h"
#include "gtsb10.h"
#include "gtsb12.h"
#include "gtsb16.h"
#include "gtsb17.h"
#include "gtsb18.h"
#include "gtsb19.h"
#include "gtsb23.h"
#include "gtsb24.h"
#include "gtsb25.h"
#include "gtsb26.h"
#include "gtsb28.h"
#include "gtsb29.h"
#include "gtsb30.h"
#include "gtsb31.h"
#include "gtsb32.h"
#include "gtsb33.h"
#include "gtsb34.h"
#include "gtsb35.h"
#include "gtsb36.h"
#include "gtsb37.h"
#include "gtsb38.h"
#include "gtsb39.h"
#include "gtstfk.h"
#include "gtstzt.h"
#include "gtstlm.h"
#include "gtstas.h"

//===========================================================================
//===========================================================================
void InitListenZaehler ()
{
   LASTBSTNR = 0;
   if (KLEINSTEBSTNR > 1) LASTBSTNR = KLEINSTEBSTNR;

   typBaustein *zhilfe = BausteinListe->Letztes();
   if (zhilfe != NULL)
   {
     LASTBSTNR = zhilfe->i_BstNr();
     if (LASTBSTNR < KLEINSTEBSTNR) LASTBSTNR = KLEINSTEBSTNR;
   }

   LASTFKNR = 0;
   typFahrkurs *zfkurs = FahrkursListe->Letztes();
   if (zfkurs != NULL) LASTFKNR = zfkurs->i_Nummer();

   LASTPROZESS = 0;
   Prozess *zprozess = ProzessListe->Letztes();
   if (zprozess != NULL) LASTPROZESS = zprozess->getNr();

   LASTAFNR = 0;
   Arbeitsplan *zarbeitsplan = ArbeitsplanListe->Letztes();
   if (zarbeitsplan != NULL) LASTAFNR = zarbeitsplan->getNr();
}  // ENDE InitListenZaehler

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TGSLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  TMPBenoetigteBausteine *TMP = new TMPBenoetigteBausteine (DatName);
  TMP->TGSLesen ();
  bool OK = TMP->BstAusTGBLesen();
  if (OK)
  {
    Meld.Ausgabe(1, DatName);
    OK = BausteinListe->TGSLesen(DatName);
  }
  delete TMP;
  return OK;
}  // ENDE TGSLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TGSSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  BausteinListe->TGSSchreiben(DatName);
}  // TGSSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TTPLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return BausteinListe->TTPLesen(DatName);
}  // ENDE TTPLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TTPSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  BausteinListe->TTPSchreiben(DatName);
}  // TTPSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TMWLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return BausteinListe->TMWLesen(DatName);
}  // ENDE TMWLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TMWSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  BausteinListe->TMWSchreiben(DatName);
}  // TMWSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TSTLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return BausteinListe->TSTLesen(DatName);
}  // ENDE TSTLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TSTSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  BausteinListe->TSTSchreiben(DatName);
}  // TSTSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TBVLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return BausteinListe->TBVLesen(DatName);
}  // ENDE TBVLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TBVSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  BausteinListe->TBVSchreiben(DatName);
}  // TBVSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TFKLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return FahrkursListe->TFKLesen(DatName);
}  // TFKLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TFKSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  FahrkursListe->TFKSchreiben(DatName);
}  // TFKSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TPVLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return BausteinListe->TPVLesen(DatName);
}  // ENDE TPVLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TPVSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  BausteinListe->TPVSchreiben (DatName);
}  // TPVSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TASLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return ArbeitsplanListe->read (DatName);
}  // ENDE TASLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TASSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  ArbeitsplanListe->write (DatName);
}  // TASSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TLMLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return ProzessListe->read (DatName);
}  // ENDE TLMLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TLMSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  ProzessListe->write (DatName);
}  // TLMSchreiben

bool TZTLesen (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  Meld.Ausgabe(1, DatName);
  return EreignisListe->read (DatName);
}  // ENDE TZTLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TZTSchreiben (Ustring typ)
{
  Ustring DatName = ExpName + typ;
  EreignisListe->write (DatName);
}  // TZTSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ModellLesen ()
{
  sprintf (DlgBuffer, "%s configure -cursor watch", GTSView->WidgetName ());
  Tcl_GlobalEval (GTSInterpreter, DlgBuffer);
  Meld.Ausgabe ("Modell wird eingelesen:\n");

  if (ModellRelease > 0) {
#ifdef WIN32
     char mybuf [MAX_PATH];
#else
     char mybuf [255];
#endif
     sprintf (mybuf, "Release = %5.2f\n", ModellRelease);
     Meld.Ausgabe (mybuf);
  }
  InitListenZaehler ();
  bool ok = TGSLesen (".2gs");
  if (ok) ok = TBVLesen (".tbv");
  if (ok) ok = TTPLesen (".ttp");
  if (ok) ok = TFKLesen (".tfk");
  if (ok) ok = TSTLesen (".tst");
  if (ok) ok = TPVLesen (".tpv");
  if (ok) ok = TASLesen (".tas");
  if (ok) ok = TMWLesen (".tmw");
  if (ok) ok = TLMLesen (".tlm");
  if (ok) ok = TZTLesen (".tzt");
  if (!ok)
    Meld.Ausgabe(3);
  else
    GTSSpace->flushAllViews ();
  sprintf (DlgBuffer, "%s configure -cursor arrow", GTSView->WidgetName ());
  Tcl_GlobalEval (GTSInterpreter, DlgBuffer);
}  // ModellLesen

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ModellSchreiben ()
{
  sprintf (DlgBuffer, "%s configure -cursor watch", GTSView->WidgetName ());
  Tcl_GlobalEval (GTSInterpreter, DlgBuffer);
  SchreibeModellIniDatei();
  TGSSchreiben (".2gs");
  TBVSchreiben (".tbv");
  TTPSchreiben (".ttp");
  TMWSchreiben (".tmw");
  TFKSchreiben (".tfk");
  TSTSchreiben (".tst");
  TASSchreiben (".tas");
  TPVSchreiben (".tpv");
  TLMSchreiben (".tlm");
  TZTSchreiben (".tzt");
  Meld.Ausgabe (7);
  sprintf (DlgBuffer, "%s configure -cursor arrow", GTSView->WidgetName ());
  Tcl_GlobalEval (GTSInterpreter, DlgBuffer);
}  // ModellSchreiben

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void AboutWindow ()
{
    char msg[255];
    char rev[] = "Release: 14 ";
    strtok (rev, ":");
    char *tok = strtok (NULL, "$");
    strcpy (msg, tok);
    float frev;
    sscanf (msg, " %f", &frev);
    sprintf (msg, "SIMFLEX/3D - GTS\n\n(c) 2010 rei/FPS/ipl/Uni Kassel\n\nRevision %1.2f (rei/har 04)\n\nwww.fps.maschinenbau.uni-kassel.de", frev);
    sprintf(DlgBuffer, "tk_messageBox -parent . -title About -icon info -type ok \
    -message \"%s\"", msg);
    Tcl_GlobalEval (GTSInterpreter, DlgBuffer);
}  // AboutWindow

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int DlgDatei (ClientData /*clientData*/,
                     Tcl_Interp *interp, int argc, const char *argv[] )
{
  GTSInterpreter = interp;
  if (testprog) {
    Write("DlgDatei: Anfang >%s<", argv[1]);
    for (int i = 2; i <= argc; i++) Write(" >%s< ", argv[i]);
  }
  sprintf(DlgBuffer, "%s", "");

  if ( !strcmp("ModellLesen", argv[1]) )
  {
    if (!BausteinListe->Leer())
       Meld.Ausgabe (13);
    else
    if (PruefeModellIniDatei(argv[2]))
    {
      ExpName = argv[2];
      ModellLesen ();
    }
    if (BausteinListe->Leer())
      sprintf (DlgBuffer, "%d", 0);
    else
      sprintf (DlgBuffer, "%d", 1);
  }
  else if ( !strcmp("ModellSpeichern", argv[1]) )
  {
    if (BausteinListe->Leer())
       Msg.Warning ("Modell speichern", "Es sind keine Bausteine vorhanden");
    else
    {
      ExpName = argv[2];
      ExpExtension = argv[3];
      ModellSchreiben ();
    }
  }
  else if ( !strcmp("GTSInit", argv[1]) )
  {
   GTSInit();
  }
  else if ( !strcmp("GTSEnde", argv[1]) )
  {
    GTSExit();
    return TCL_OK;
  }
  else if ( !strcmp("ABOUTWINDOW", argv[1]) )
  {
    AboutWindow();
  }
  else
  {
    sprintf (DlgBuffer, "DlgDatei: Unbekannte Methode \"%.32s\"\n " , argv[1]);
    Tcl_SetResult( interp, DlgBuffer, TCL_STATIC );
    return TCL_ERROR;
  }
  if (testprog) Write("  -> Ende: DlgBuffer >%s<\n", DlgBuffer);
  Tcl_SetResult(interp, DlgBuffer, TCL_STATIC);
  return TCL_OK;
} //  ENDE DlgDatei
