unit events;

interface
uses
  Windows,
  ksAuto,
{$IFDEF __LIGHT_VERSION__}
  klTLB;
{$ELSE}
  ksTLB;
{$ENDIF}

var
  iKompasObject: KompasObject;
/////////////////////////////////////////////////////////////////////////////
// ApplicationEvent command target

  procedure  LIBRARYENTRY( command: WORD  );         Pascal;
  function   LIBRARYNAME                 : PChar;    Pascal;
  function   LIBRARYID                   : Cardinal; Pascal;
  function   LibInterfaceNotifyEntry (app : PIDispatch) : boolean;  Pascal;
  procedure  MyDLLProc(Reason: Integer);
procedure AdviseDocuments;
//procedure DocumentEvents();
procedure AdviseDocumentEvents();
procedure UnAdviseDocumentEvents();
procedure AdviseDoc( pDoc : LongInt );

implementation

uses
  LDefin2D,
  ActiveX,
  forms,
  BaseEvents;

var
  aplEvent  : ApplicationEvent;

procedure MyDLLProc(Reason: Integer);

begin
  case Reason of
    DLL_PROCESS_DETACH: begin
      BaseEvent.TerminateEvents;
//      if aplEvent <> nil then begin
//        aplEvent.Unadvise;
      aplEvent := nil;
//      end;
//      doc       := nil;
      Application.Handle := 0;
      iKompasObject := nil;
    end;
    DLL_PROCESS_ATTACH: begin
      if  iKompasObject = nil  then
        iKompasObject := KompasObject(CreateKompasObject);
      Application.Handle := iKompasObject.ksGetHWindow;
    end;
  end;
end;


//------------------------------------------------------------------------------
// LibraryName
//---
function LIBRARYNAME: PChar; pascal;
begin
  Result := '    (Delphi AUTO)';
end;

//------------------------------------------------------------------------------
// LibraryId
//---
function LIBRARYID: UINT; pascal;
begin
  Result := 100;
end;

//------------------------------------------------------------------------------
// LibraryEntry
//---
procedure LIBRARYENTRY(command: WORD); pascal;
begin
  if  iKompasObject = nil  then
    iKompasObject := KompasObject(CreateKompasObject);
  if iKompasObject <> nil then
  begin
    Application.Handle := iKompasObject.ksGetHWindow;
    case command of
      10 : begin  //    
            if not BaseEvent.FindEvent( DIID_ksKompasObjectNotify, 0, -1 ) then
            begin
              aplEvent := ApplicationEvent.Create( Addr( iKompasObject ) );  //    
              aplEvent.Advise;                         //     
            end
            else
              LibMessage( ' ', MB_OK );
          end;

      11 : begin
          if not BaseEvent.FindEvent( DIID_ksKompasObjectNotify, 0, -1 ) then
          begin
            if aplEvent <> nil then  begin
              aplEvent.Unadvise;
              aplEvent := nil;                            //    
            end
          end
        end;
			20: AdviseDocumentEvents(); //    
      21: UnAdviseDocumentEvents();
    end;
  end;
end;

procedure AdviseDocumentEvents();
var
  doc : Reference;
begin
  doc := GetActiveDocumentReference( 0 );
  if doc <> 0 then begin
    if not BaseEvent.FindEvent( DIID_ksDocumentFileNotify, doc, -1 ) then
      if NewDocumentEvent( doc ) <> nil then
        LibMessage( '', MB_OK )
      else
        LibMessage( '  ', MB_OK )
		else
  		LibMessage( ' ', MB_OK );
  end
	else
		LibMessage( '  ', MB_OK );
end;

procedure UnAdviseDocumentEvents();
var
  doc : Reference;
begin
	if iKompasObject <> nil then
  begin
    doc := GetActiveDocumentReference( 0 );
		if doc <> 0 then
			BaseEvent.TerminateEventsFromIID( GUID_NULL, doc, -1 );
  end;
end;

procedure AdviseObj2DEvents();
var
  doc : Reference;
begin
  doc := GetActiveDocumentReference( 1 );
  if doc <> 0 then begin
//		if  not BaseEvent.FindEvent( DIID_ksObject2DNotify, doc, -1 ) then
//  	  AdviseDoc( doc, false, true, false )
//		else
// 			LibMessage( ' ', MB_OK )
  end
  else
		LibMessage( '  ', MB_OK );
end;

procedure UnAdviseObj2DEvents();
var
  doc : Reference;
begin
  doc := GetActiveDocumentReference( 1 );
  if doc <> 0 then begin
 		BaseEvent.TerminateEventsFromIID( DIID_ksObject2DNotify, doc, -1 );
    LibMessage( '', MB_OK );
  end
  else
		LibMessage( '  ', MB_OK );
end;

procedure AdviseSelectionEvents();
var
  doc : Reference;
begin
  doc := GetActiveDocumentReference( 0 );
  if doc <> 0 then begin
//    if not BaseEvent.FindEvent( DIID_ksSelectionMngNotify, doc, -1 ) then
//      AdviseDoc( doc, true, false, false )
//    else
// 		  LibMessage( ' ', MB_OK );
  end
	else
		LibMessage( '  ', MB_OK );
end;

procedure UnAdviseSelectionEvents();
var
  doc : Reference;
begin
  doc := GetActiveDocumentReference( 0 );
  if doc <> 0 then begin
		BaseEvent.TerminateEventsFromIID( DIID_ksSelectionMngNotify, doc, -1 );
    LibMessage( '', MB_OK );
  end
	else
		LibMessage( '  ', MB_OK );
end;

procedure AdviseStampEvents();
var
  doc : Reference;
begin
  doc := GetActiveDocumentReference( 0 );
  if doc <> 0 then begin
//		if not BaseEvent.FindEvent( DIID_ksStampNotify, doc, -1 ) then
//      AdviseDoc( 0, false, false, true )
//		else
//			LibMessage( ' ', MB_OK );
  end
  else
    LibMessage( '  ', MB_OK + MB_ICONERROR );
end;

procedure UnAdviseStampEvents();
var
  doc : Reference;
begin
  doc := GetActiveDocumentReference( 0 );
	if doc <> 0 then
		BaseEvent.TerminateEventsFromIID( DIID_ksStampNotify, doc, -1 );
end;


procedure AdviseDocuments();
var
  pDoc : LongInt;
  iter : ksIterator;
begin
	if iKompasObject <> nil then
  begin
		iter := ksIterator( iKompasObject.GetIterator );       //  
    //      
		if ( iter <> nil ) and iter.ksCreateIterator( ALL_DOCUMENTS, 0 ) then
    begin
  		pDoc := iter.ksMoveIterator( 'F' );   //  
      while pDoc <> 0 do
      begin
        AdviseDoc( pDoc );
        pDoc := iter.ksMoveIterator( 'N' );           //  
			end;
      iter.ksDeleteIterator;                       //  
		end;
  end;
end;


//-----------------------------------------------------------------------------
//      
// ---
function LibInterfaceNotifyEntry( app : PIDispatch ) : boolean;
begin
  if ( iKompasObject = nil ) and  ( app <> nil ) then               //    
    iKompasObject := KompasObject( app );


  if iKompasObject <> nil then
  begin
    Application.Handle := iKompasObject.ksGetHWindow;
    aplEvent := ApplicationEvent.Create( Addr(iKompasObject) );        //    
    aplEvent.Advise;                               //     
    AdviseDocuments;
	end;

  Result := false;
end;

//-----------------------------------------------------------------------------
//    
// ---
procedure AdviseDoc( pDoc : Reference );
var
  docType   : Integer;
begin
  if pDoc = 0 then
    pDoc := GetActiveDocumentReference( 0 )
  else
    ActivateDoc( pDoc );
  if  pDoc <> 0 then begin
		docType := iKompasObject.ksGetDocumentType( pDoc );
    if ( NewDocumentEvent( pDoc ) <> nil ) then begin
      case docType of

        lt_DocSheetStandart ,                        // 1-  
        lt_DocSheetUser     :                        // 2-  
        begin
          NewStampEvents(pDoc);
          NewSelectMngEvent( pDoc );
          NewObj2DEvent(pDoc, ALL_OBJ );
        end;
        lt_DocFragment      :                        // 3- 
        begin
          NewSelectMngEvent( pDoc );
          NewObj2DEvent(pDoc, ALL_OBJ );
        end;
        lt_DocSpc           ,                       // 
        lt_DocSpcUser       :                       //   
          NewStampEvents(pDoc);
        lt_DocPart3D        ,                       // 3d- 
        lt_DocAssemble3D    :                       // 3d- 
          NewSelectMngEvent( pDoc );
      end
    end
  end;
end;

end.









