//==============================================================================
//                      Conv_Export_PC_DMIS_202401
//
//    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
//
//==============================================================================

// CMM Support: PC-DMIS 2024.1

unit Conv_Export_PC_DMIS_202401;

const
  CONVERT_AFTER_DYN_C   = 3;
  FilenameMiniRoutine_C = 'DynamicInspection.XML';

  KFELD1802_C             = 1802; // name of the measurement program
  NameMiniRoutine_C       = 'DynamicInspection'; // name of the mini routine
  KFELD_Characteristic_C  = 2882; // reference to characteristic

const // for test and debugging
  FilenameDebug_C = 'c:\Temp\Conv_Export_PC_DMIS_2024_01.txt';
  DebugOutput_C = True;

const // for writing XML if it doesn't exists
  XML_DEKLARATION_C       = '<?xml version="1.0" encoding="utf-8"?>';
  PCDMIS_COMMENT_C        = '<!--PC-DMIS XML Output File-->';
  TAG_FBM_START_C         = '<FeatureBasedMeasurement>';
  TAG_FBM_END_C           = '</FeatureBasedMeasurement>';
  TAG_ENTIRE_ROUTINE_C    = '  <EntireRoutine Time="00:00:00" TimeUpdatable="True" />';
  TAG_MINIROUTINE_START_C = '  <MiniRoutine ID="DynamicInspection" UID="" Time="00:00:00" TimeUpdatable="True">';
  TAG_MINIROUTINE_END_C   = '  </MiniRoutine>';

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 DuplicatesCheck( AList: TStringList; const AKFieldString: string): Boolean;
var
  I: Integer;
  lEntry: string;
begin
  Result := True;
  for I := 0 to AList.Count - 1 do
  begin
    lEntry := Trim( AList.Strings[i]);
    if AnsiSameStr( lEntry, Trim( AKFieldString)) then
    begin
      Result := False;
      Break;
    end;
  end;
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,
  lKFieldString: string;
  lInList: TStringList;
  lMiniRoutineLine,
  lNumberOfChar: Integer;
  lerrorList: TStringList;
begin
  Result := '';
	Output := '';
  lNameOfMiniRoutine := '';
  lMiniRoutineLine := 0;

	Key	    := TStringList.Create;
	SubKey  := TStringList.Create;
	QDASConv := TQDASTextConversion.CreateExport( QSystem, true );
  QDASInterface := TQSSTATConverterInterface.CreateExport( QSystem, true );
  lerrorList    := TStringList.Create;
  try
    if DebugOutput_C then
    begin
      lerrorList.add( 'Start of debug output!');
    end;
    if ( ConvResult <> nil) then
    begin
      ConvResult.ScriptVersion := '2023-08-09';
    end;

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

        if DebugOutput_C then
        begin
          lerrorList.add( 'FileName: ' + lFileName);
        end;

        lInList := TStringList.Create;
        try
          lInList.Duplicates := dupIgnore;
//          lInList.Sorted := True;

          if DebugOutput_C then
          begin
            lerrorList.add( 'MiniRoutineFileExists does not exists');
            lerrorList.add( 'creating MiniRoutineFile by my own ...');
          end;
          lInList.Add( XML_DEKLARATION_C);
          lInList.Add( PCDMIS_COMMENT_C);
          lInList.Add( TAG_FBM_START_C);
          lInList.Add( TAG_ENTIRE_ROUTINE_C );
          lInList.Add( TAG_MINIROUTINE_START_C);
          lMiniRoutineLine := lInList.Count + 1;
          // writing all characteristics
          try
            lNumberOfChar := QDASInterface.GetCharNo( I);
            if DebugOutput_C then
            begin
              lerrorList.add( 'NumberOfChar: ' + IntToStr( lNumberOfChar));
            end;
            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);
                    if DebugOutput_C then
                    begin
                      lerrorList.add( 'KFieldString: ' + lKFieldString);
                      lerrorList.add( 'insert KFieldString on position: ' + IntToStr( lMiniRoutineLine));
                    end;
                    if DuplicatesCheck( lInList, lKFieldString) then
                    begin
                      lInList.Add( '    ' + lKFieldString);
                      lMiniRoutineLine := lMiniRoutineLine + 1;
                    end;
                    lMiniRoutineLine := lMiniRoutineLine + 1;
                  end;
                end;
              end;
            end;
          finally
          end;
          lInList.Add( TAG_MINIROUTINE_END_C);
          lInList.Add( TAG_FBM_END_C);
          if DebugOutput_C then
          begin
            lerrorList.add( 'done MiniRoutineFile by my own');
          end;
        finally
          lInList.SaveToFile( lFileName);
          lInList.Free;
        end;
      end else
      begin
        Result := 'program file or name of mini routine missing!';
        if DebugOutput_C then
        begin
          lerrorList.add( Result);
        end;
      end;
    end;
	finally
    if DebugOutput_C then
    begin
      lerrorList.SaveToFile( FilenameDebug_C);
    end;
    // 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
