//==============================================================================
//                      Conv_Export_PC_DMIS_202308
//
//    Inhalt        : Export-Konverter, der im Upload integriert eine Datei
//                    als Rueckmeldung bzw. Input fuer PC-DMIS.
//
//    Erstellt am 	: 02/2022
//
//    Beschreibung	:
//
//	  Konverter-Typ	: Export-Konverter
//	  				        Definition	:
//						 	      Group: 				    5
//							      Funktionskey: 		1350
//							      FunktionsSubkey: 	0
//
//	  Einzubinden	: automatisiert im Upload
//
//==============================================================================

unit Conv_Export_PC_DMIS_202308;

const
  CONVERT_AFTER_DYN_C = 3;
  FILENAME_C = 'MiniRoutines.XML';
  NameOfMiniRoutine_C = 'MiniRoutines.XML';

  KFELD1802_C             = 1802; // name of the measurement program
  KFELD_NameMiniRoutine_C = 1892; // name of the mini routine
  KFELD_Characteristic_C  = 2882; // reference to characteristic

function FillList( Key, SubKey : TStringList ): Integer;
begin
	Key.add( '15505');
	SubKey.add( '3');
	Result := Key.count;
end;

function ExistWordInString( const AText: String; const ASubText: string): Boolean;
begin
//  Result := AnsiContainsStr( AText, ASubText);
  Result := ( Pos( ASubText, AText) >= 1);
end;

function Main( QSystem    : Variant;
               Dummy1     : Variant;
               Dummy2     : Variant;
               ConvResult : TConvertResult): AnsiString;
var
  QDASConv       			: TQDASTextConversion;            // variable for text conversion interface
  QDASInterface  			: TQSSTATConverterInterface; // variable for qs-STAT interface
	I, J, K,
	PartNo        : Integer;
  Count  		    : Integer;
  Output       	: AnsiString;
  Key,
  SubKey: TStringList;
  lProgramName,
  lNameOfMiniRoutine,
  lFileName,
  lInListString,
  lKFieldString,
  lResult: string;
  lMiniRoutineFileExists,
  lMiniRoutineSectionFound: Boolean;
  lInList: TStringList;
  lInListCounter,
  lMiniRoutineLine,
  lNumberOfChar: Integer;
// lerrorList: TStringList;
begin
  Result := '';
	Output := '';
  lNameOfMiniRoutine := '';
  lMiniRoutineFileExists := False;
  lMiniRoutineSectionFound := False;
  lMiniRoutineLine := 0;

	Key	    := TStringList.Create;
	SubKey  := TStringList.Create;
	QDASConv := TQDASTextConversion.CreateExport( QSystem, true );
  QDASInterface := TQSSTATConverterInterface.CreateExport( QSystem, true );
  // lerrorList    := TStringList.Create;
  try
// lerrorList.add( 'HUHU!');
    if ( ConvResult <> nil) then
    begin
      ConvResult.ScriptVersion := '2023-08-10';
    end;

    Count := FillList( Key, SubKey );
		PartNo := QDASInterface.GetPartNo;
		for I := 1 to PartNo do
		begin
      QDASInterface.GetKField( I, 0, 0, KFELD1802_C, 0, lProgramName);
      QDASInterface.GetKField( I, 0, 0, KFELD_NameMiniRoutine_C, 0, lNameOfMiniRoutine);
// lerrorList.add( 'ProgramName: ' + lProgramName);
// lerrorList.add( 'NameOfMiniRoutine: ' + lNameOfMiniRoutine);
      if FileExists( lProgramName) and ( lNameOfMiniRoutine <> '') then
      begin
        lFileName := ChangeFileExt( lProgramName, '.' + NameOfMiniRoutine_C);
        if FileExists( lFileName) then
        begin
          lMiniRoutineFileExists := True;
        end;

// lerrorList.add( 'FileName: ' + lFileName);
        if lMiniRoutineFileExists then
        begin
// lerrorList.add( 'MiniRoutineFileExists');
          lInList := TStringList.Create;
          try
            lInList.LoadFromFile( lFileName);
            if ( lInList.Count > 0) then
            begin
// lerrorList.add( 'MiniRoutine number of lines: ' + IntTostr( lInList.Count));
              // search for the correct "MiniRoutine" entry
              for lInListCounter := 0 to lInList.Count - 1 do
              begin
                lInListString := lInList.Strings[lInListCounter];
// lerrorList.add( 'InListString: ' + lInListString);
                if ExistWordInString( lInListString, lNameOfMiniRoutine) then
                begin
                  // next mini routine section found
                  lMiniRoutineSectionFound := True;
                  lMiniRoutineLine := lInListCounter + 1;
// lerrorList.add( 'mini routine starts at MiniRoutineLine: ' + IntToStr( lMiniRoutineLine));
                  Break;
                end;
              end;

              if lMiniRoutineSectionFound then
              begin
// lerrorList.add( 'lMiniRoutineSectionFound');
                // now delete all lines between <MiniRoutine ...> and </MiniRoutine>
                for lInListCounter := lMiniRoutineLine to lInList.Count - 1 do
                begin
                  lInListString := lInList.Strings[lInListCounter];
                  if ( Pos( '</MiniRoutine>', lInListString) > 0) then
                  begin
// lerrorList.add( 'mini routine ends at MiniRoutineLine: ' + IntToStr( lInListCounter));
                    Break;
                  end;
                end;

                // now delete all lines between <MiniRoutine> and </MiniRoutine>
                for lInListCounter := lInListCounter - 1 downto lMiniRoutineLine do
                begin
// lerrorList.add( 'mini routine delete line : ' + IntToStr( lInListCounter));
                  lInListString := lInList.Strings[lInListCounter];
//                  if ( lInListString = '') then
//                  begin
                    lInList.Delete( lInListCounter);
//                  end;
                end;

                try
                  lNumberOfChar := QDASInterface.GetCharNo( I);
// lerrorList.add( 'NumberOfChar: ' + IntToStr( lNumberOfChar));
                  for J := 1 to lNumberOfChar do
                  begin
                    for K := 0 to Count - 1 do
                    begin
                      QDASInterface.GetRFieldExt( I, J, 0, StrToInt( Key.Strings[K] ), StrToInt( SubKey.Strings[K] ), lKFieldString, 0);
                      Output := lKFieldString;
                      if ( Output = '1') then
                      begin
                        QDASInterface.GetKField( I, J, 0, 8504, '', lKFieldString);
                        if ( lKFieldString <> '-1') then
                        begin
                          QDASInterface.GetKField( I, J, 0, KFELD_Characteristic_C, '', lKFieldString);
// lerrorList.add( 'KFieldString: ' + lKFieldString);
// lerrorList.add( 'insert KFieldString on position: ' + IntToStr( lMiniRoutineLine));
                          lInList.Insert( lMiniRoutineLine, '    ' + lKFieldString);
                          lMiniRoutineLine := lMiniRoutineLine + 1;
                        end;
                      end;
                    end;
                  end;
                finally
                end;
                lInList.SaveToFile( lFileName);
              end else
              begin
                Result := 'mini routine section missing!'
              end;
            end;
          finally
            lInList.Free;
          end;
        end else
        begin
          Result := 'file with mini routines missing!'
        end;
      end else
      begin
        Result := 'program file or name of mini routine missing!'
      end;
    end;
	finally
    // lerrorList.SaveToFile( 'c:\Temp\Conv_Export_PC_DMIS_2024_01.txt');
    // free resources
    // lerrorList.Free;
		Key.Clear;
		Key.Free;
		SubKey.Clear;
		SubKey.Free;
    QDASInterface.Free;
    QDASInterface := NIL;
		QDASConv.Free;
		QDASConv := NIL;
  end;
end;

function CheckIsFile( QSystem        : Variant;
                      SourceFileName : Variant) : Integer;
begin
  Result := 1;
  if ( Pos( '.DFD', AnsiUpperCase( ExtractFileName( SourceFileName))) > 0) OR
     ( Pos( '.DFQ', AnsiUpperCase( ExtractFileName( SourceFileName))) > 0) then
  begin
    Result := 0;
  end;
end;

function CheckPositionInUpload( QSystem : Variant) : Integer;
begin
  Result := CONVERT_AFTER_DYN_C;
end;

end.
//check
//Gq?q8@/,PU13Qd)Y!Nprl,2889t3"I=b
//check
//Gq?q8@/,PU13Qd)Y!Nprl,2889t3"I=b
//check
//Gq?q8@/,PU13Qd)Y!Nprl,2889t3"I=b
