////////////////////////////////////////////////////////////////////////////////
//
// 
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h" 
#include "eventsAuto.h"  

#ifndef _APPLICATIONEVENT_H
#include "ApplicationEvent.h"
#endif

#ifndef _DOCUMENTEVENT_H
#include "DocumentEvent.h"
#endif

#ifndef _OBJECT2DEVENT_H
#include "Object2DEvent.h"
#endif

#ifndef _OBJECT3DEVENT_H
#include "Object3DEvent.h"
#endif

#ifndef _SELECTMNGEVENT_H
#include "SelectMngEvent.h"
#endif

#ifndef _STAMPEVENT_H
#include "StampEvent.h"
#endif

#ifndef _SPECIFICATIONEVENT_H
#include "SpecificationEvent.h"
#endif

#ifndef _SPCOBJECTEVENT_H
#include "SpcObjectEvent.h"
#endif

#ifndef _SPCDOCUMENTEVENT_H
#include "SpcDocumentEvent.h"
#endif

#ifndef _DOCUMENT3DEVENT_H
#include "Document3DEvent.h"
#endif

#ifndef _DOCUMENT2DEVENT_H
#include "Document2DEvent.h"
#endif

#ifndef _DLGCONFIG_H
#include "DlgConfig.h"
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


////////////////////////////////////////////////////////////////////////////////
//
// 
//
////////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CeventsAutoApp, CWinApp)
	//{{AFX_MSG_MAP(CeventsAutoApp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//-------------------------------------------------------------------------------
// 
// ---
CeventsAutoApp::CeventsAutoApp() : 
	m_auto(false),
	m_mes_2DDOC(true),
  m_mes_3DDOC(true),
	m_mes_APP(true),
	m_mes_DOC(true),
	m_mes_OBJ_2DDOC(true),
	m_mes_OBJ_3DDOC(true),
	m_mes_OBJ_SPC(true),
	m_mes_SELECT(true),
	m_mes_SPC(true),
	m_mes_SPCDOC(true),
	m_mes_STAMP(true)
{         
}


//-------------------------------------------------------------------------------
// 
// ---
int CeventsAutoApp::ExitInstance() 
{
  m_iniFile.Reset();
	m_iniFile.SetValueI( "  EventAuto", "                              ", m_auto          );
	m_iniFile.SetValueI( "  EventAuto", "    2D           ", m_mes_2DDOC     );
	m_iniFile.SetValueI( "  EventAuto", "    3D           ", m_mes_3DDOC     );
	m_iniFile.SetValueI( "  EventAuto", "                   ", m_mes_APP       );
	m_iniFile.SetValueI( "  EventAuto", "                ", m_mes_DOC       );
	m_iniFile.SetValueI( "  EventAuto", "     2D   ", m_mes_OBJ_2DDOC );
	m_iniFile.SetValueI( "  EventAuto", "     3D   ", m_mes_OBJ_3DDOC );
	m_iniFile.SetValueI( "  EventAuto", "       ", m_mes_OBJ_SPC   );
	m_iniFile.SetValueI( "  EventAuto", "            ", m_mes_SELECT    );
	m_iniFile.SetValueI( "  EventAuto", "              ", m_mes_SPC       );
	m_iniFile.SetValueI( "  EventAuto", "     ", m_mes_SPCDOC    );
	m_iniFile.SetValueI( "  EventAuto", "                    ", m_mes_STAMP     );
  m_iniFile.WriteFile();

  BaseEvent::TerminateEvents();
	return CWinApp::ExitInstance();
}


//-------------------------------------------------------------------------------
// 
// ---
BOOL CeventsAutoApp::InitInstance() 
{
  char fileName[256];
  ::strcpy( fileName, "c:\\EventAuto.ini" );
  if( ::GetModuleFileName(theApp.m_hInstance, fileName, 256) ) 
  {
    char * buf = ::strrchr( fileName, '\\');
    if ( buf )
    {
      *(buf + 1) = '\0';
      ::strcat( fileName, "EventAuto.ini" );
    }
  }
  m_iniFile.SetPath( fileName ); 
  m_iniFile.ReadFile();
	m_auto          = m_iniFile.GetValueB( "  EventAuto", "                              ", false );
	m_mes_2DDOC     = m_iniFile.GetValueB( "  EventAuto", "    2D           ", true );
	m_mes_3DDOC     = m_iniFile.GetValueB( "  EventAuto", "    3D           ", true );
  m_mes_APP       = m_iniFile.GetValueB( "  EventAuto", "                   ", true );
	m_mes_DOC       = m_iniFile.GetValueB( "  EventAuto", "                ", true );
	m_mes_OBJ_2DDOC = m_iniFile.GetValueB( "  EventAuto", "     2D   ", true );
	m_mes_OBJ_3DDOC = m_iniFile.GetValueB( "  EventAuto", "     3D   ", true );
	m_mes_OBJ_SPC   = m_iniFile.GetValueB( "  EventAuto", "       ", true );
	m_mes_SELECT    = m_iniFile.GetValueB( "  EventAuto", "            ", true );
	m_mes_SPC       = m_iniFile.GetValueB( "  EventAuto", "              ", true );
	m_mes_SPCDOC    = m_iniFile.GetValueB( "  EventAuto", "     ", true );
	m_mes_STAMP     = m_iniFile.GetValueB( "  EventAuto", "                    ", true );
	
	return CWinApp::InitInstance();
}


//-------------------------------------------------------------------------------
// 
// ---
void CeventsAutoApp::ConfigDlg()
{
  DlgConfig dlg;
  
  dlg.m_auto          = m_auto;   
  dlg.m_mes_2DDOC     = m_mes_2DDOC; 
  dlg.m_mes_3DDOC     = m_mes_3DDOC;    
  dlg.m_mes_APP       = m_mes_APP;      
  dlg.m_mes_DOC       = m_mes_DOC;      
  dlg.m_mes_OBJ_2DDOC = m_mes_OBJ_2DDOC;
  dlg.m_mes_OBJ_3DDOC = m_mes_OBJ_3DDOC;
  dlg.m_mes_OBJ_SPC   = m_mes_OBJ_SPC;  
  dlg.m_mes_SELECT    = m_mes_SELECT;   
  dlg.m_mes_SPC       = m_mes_SPC;      
  dlg.m_mes_SPCDOC    = m_mes_SPCDOC;   
  dlg.m_mes_STAMP     = m_mes_STAMP;    

  dlg.DoModal();

  m_auto          = !!dlg.m_auto;    
  m_mes_2DDOC     = !!dlg.m_mes_2DDOC;  
  m_mes_3DDOC     = !!dlg.m_mes_3DDOC;    
  m_mes_APP       = !!dlg.m_mes_APP;      
  m_mes_DOC       = !!dlg.m_mes_DOC;      
  m_mes_OBJ_2DDOC = !!dlg.m_mes_OBJ_2DDOC;
  m_mes_OBJ_3DDOC = !!dlg.m_mes_OBJ_3DDOC;
  m_mes_OBJ_SPC   = !!dlg.m_mes_OBJ_SPC;  
  m_mes_SELECT    = !!dlg.m_mes_SELECT;   
  m_mes_SPC       = !!dlg.m_mes_SPC;      
  m_mes_SPCDOC    = !!dlg.m_mes_SPCDOC;   
  m_mes_STAMP     = !!dlg.m_mes_STAMP;    
}


//-------------------------------------------------------------------------------
// 
// ---
CeventsAutoApp theApp; //     


//-------------------------------------------------------------------------------
//
// ---
unsigned int WINAPI LIBRARYID()
{
	return IDR_LIB;
}


//-------------------------------------------------------------------------------
// 
// ---
KompasObjectPtr kompas;


//-------------------------------------------------------------------------------
//
// ---
void GetKompas() 
{
  if ( !(bool)kompas ) 
  {
    CString filename;
    if ( ::GetModuleFileName(NULL, filename.GetBuffer(255), 255) ) 
    {
      filename.ReleaseBuffer( 255 );
      CString libname;

      libname.LoadString( IDS_DLL_NAME );  // kAPI5.dll / klAPI5.dll
      

      filename.Replace( filename.Right(filename.GetLength() - (filename.ReverseFind('\\') + 1)), 
        libname );
      
      //  appauto.dll
      HINSTANCE hAppAuto = ::LoadLibrary( filename ); 
      if ( hAppAuto ) 
      {
        typedef LPDISPATCH ( WINAPI *FCreateKompasObject )(); 
        FCreateKompasObject pCreateKompasObject = 
          (FCreateKompasObject)::GetProcAddress( hAppAuto, "CreateKompasObject" );	
        if ( pCreateKompasObject ) 
          kompas = IDispatchPtr( pCreateKompasObject(), false ); 
        ::FreeLibrary( hAppAuto );  
      }
    }
  }
}


//-------------------------------------------------------------------------------
//  2D   
// ---
bool Request2DObject( long& objType )
{
  objType = -1;
  ksDocument2DPtr doc2D( kompas->ActiveDocument2D() );
  ksRequestInfoPtr info( kompas->GetParamStruct(ko_RequestInfo) );
	if ( info != NULL && doc2D != NULL ) 
  {
  	info->Init();
	
    info->menuId = IDM_REQUEST_OBJECT_2D;
    info->title = " ";

		double x, y;
    long command = doc2D->ksCursor( info, &x, &y, NULL );
    if ( command ) 
    {
      if( command == -1 )
      {
        reference refObject = doc2D->ksFindObj( x, y, 1000 );
				if ( doc2D->ksExistObj(refObject) ) 
        {
          objType = refObject; // 
          return true;
        }
      }
      else
      {
        objType = --command; //  
        return true;
      }
		}
	}
  return false; // 
}


//-------------------------------------------------------------------------------
//    
// ---
bool Request3DObject( ksFeaturePtr& obj3D, 
                      long& objType )
{
  obj3D = NULL;
  objType = o3d_unknown;
  bool res = false;
  ksDocument3DPtr doc3D( kompas->ActiveDocument3D() );
  ksEntityPtr entity = doc3D ? doc3D->UserSelectEntity( NULL, "", " ", 0, NULL) : NULL;
  if( entity )
  {
    objType = entity->type;
		if ( objType == o3d_face || objType == o3d_edge || objType == o3d_vertex ) 
    {
			int resYesNo = kompas->ksYesNo( "   \n -   Part\n -   Feature\n -  ");
			switch ( resYesNo ) 
      {
				case 1 :    
          objType = o3d_part;  
					obj3D = entity->GetParent();
					res = true;
          break;
				case 0: 
          objType = o3d_feature;           
					obj3D = entity->GetFeature();
					res = true;
					break;
			}
		} 
	}
  return res;
}


//-------------------------------------------------------------------------------
//
// ---
void WINAPI LIBRARYENTRY( unsigned int comm ) 
{
	//      .exe ,     
	//   dll   define
  AFX_MANAGE_STATE(AfxGetStaticModuleState());

 	::GetKompas(); //    
  if ( kompas ) 
  {
    switch ( comm ) 
    {       

//-------------------------------------------------------------------------------

      //  
      case 1 : // 
      {
        if ( !BaseEvent::FindEvent( DIID_ksKompasObjectNotify ) ) 
        {
        	ApplicationEvent * aplEvent = new ApplicationEvent( kompas );  //    
          aplEvent->Advise();                                            //     
        }
				else 
          kompas->ksError( "     " );
				break;
      }
      case 2 : // 
      {
			  BaseEvent::TerminateEvents(DIID_ksKompasObjectNotify);
				break;
      }

//-------------------------------------------------------------------------------
      
      //  
      case 3 : // 
      { 
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
  			if ( doc ) 
        {
          if ( !BaseEvent::FindEvent( DIID_ksDocumentFileNotify, doc ) ) 
		        AdviseDoc( doc, docType, false, 
                                     false, 
                                     false, 
                                     false, 
                                     false, 
                                     false );
        }
			  else
				  kompas->ksError( "  " ); 
				break;
			} 
			case 4 : //     
      { 
				if ( kompas ) 
        {
          IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
  				if ( doc )
					  BaseEvent::TerminateEvents( GUID_NULL, doc );
          else
					  kompas->ksError( "  " );
        } 
				break;
			}

//-------------------------------------------------------------------------------

      //   2D 
      case 5 : // 
      { 
  			ksDocument2DPtr doc2D( kompas->ActiveDocument2D() );
        long docType = kompas->ksGetDocumentType( 0 );
		  	if ( doc2D ) 
        {
          long objType;
          if ( Request2DObject( objType ) )
            AdviseDoc( doc2D, docType, false, 
                                       true, 
                                       false, 
                                       false, 
                                       false, 
                                       false, objType );
        }
				else
  				kompas->ksError( "  2D " );
	  		break;
		 	}
      case 6  : // 
      {
  			ksDocument2DPtr doc2D( kompas->ActiveDocument2D() );
        long docType = kompas->ksGetDocumentType( 0 );
		  	if ( doc2D ) 
        {
          long objType;
          if ( Request2DObject( objType ) )
            BaseEvent::TerminateEvents( DIID_ksObject2DNotify, doc2D, objType ); 
        }
 				else
  				kompas->ksError( "  2D " );
	  		break;
      }
      case 7  : //      2D
      { 
				ksDocument2DPtr doc2D ( kompas->ActiveDocument2D() );
  			if ( doc2D ) 
	  			BaseEvent::TerminateEvents( DIID_ksObject2DNotify, doc2D ); 
        else
			  	kompas->ksError( "  2D " );
				break;
			}
      case 8  : //      
      {
        ksDocument2DPtr doc2D ( kompas->ActiveDocument2D() );
  			if ( doc2D ) 
        {
          long view = 0;
				  if ( kompas->ksReadInt( "  ", 0, 0, 254, &view ) ) 
          {
					  reference refView = doc2D->ksGetViewReference(view);
					  if ( refView )       
            {
              if ( !BaseEvent::FindEvent( DIID_ksObject2DNotify, doc2D, refView ) ) 
              {          
                IUnknownPtr objNotify( doc2D ? doc2D->GetObject2DNotify( refView ) : NULL );
				        if ( objNotify ) 
				        {  
                  Object2DEvent* objEvent = new Object2DEvent( objNotify, doc2D, refView, doc2D->GetObject2DNotifyResult() ); 
				          objEvent->Advise();
                }
              }
              else
                kompas->ksError( "   2D   " );
            }
            else
              kompas->ksError( "     " );
          }
        }
        else
			  	kompas->ksError( "  2D " );
				break;
      }
      case 9  : //       
      {
        ksDocument2DPtr doc2D ( kompas->ActiveDocument2D() );
  			if ( doc2D ) 
        {
          long view = 0;
				  if ( kompas->ksReadInt( "  ", 0, 0, 254, &view ) ) 
          {
					  reference refView = doc2D->ksGetViewReference(view);
					  if ( refView ) 
              BaseEvent::TerminateEvents( DIID_ksObject2DNotify, doc2D, refView );
            else
              kompas->ksError( "     " );
          }
        }
        else
			  	kompas->ksError( "  2D " );
				break;
      }
      case 10 : //      
      { 
        ksDocument2DPtr doc2D ( kompas->ActiveDocument2D() );
  			if ( doc2D ) 
        {
          long layer = 0;
				  if ( kompas->ksReadInt( "  ", 0, 0, 254, &layer ) ) 
          {
					  reference refLayer = doc2D->ksGetLayerReference(layer);
					  if ( refLayer ) 
              BaseEvent::TerminateEvents( DIID_ksObject2DNotify, doc2D, refLayer );
            else
              kompas->ksError( "     " );
          }
        }
        else
			  	kompas->ksError( "  2D " );
				break;
      }
      case 11 : //       
      { 
        ksDocument2DPtr doc2D ( kompas->ActiveDocument2D() );
  			if ( doc2D ) 
        {
          long layer = 0;
				  if ( kompas->ksReadInt( "  ", 0, 0, 254, &layer ) ) 
          {
					  reference refLayer = doc2D->ksGetLayerReference(layer);
					  if ( refLayer ) 
            {
              if ( !BaseEvent::FindEvent( DIID_ksObject2DNotify, doc2D, refLayer ) ) 
              {          
                IUnknownPtr objNotify( doc2D ? doc2D->GetObject2DNotify( refLayer ) : NULL );
				        if ( objNotify ) 
				        {  
                  Object2DEvent* objEvent = new Object2DEvent( objNotify, doc2D, refLayer, doc2D->GetObject2DNotifyResult() ); 
				          objEvent->Advise();
                }
              }
              else
                kompas->ksError( "   2D   " );
            }
            else
              kompas->ksError( "     " );
          }
        }
        else
			  	kompas->ksError( "  2D " );
				break;
      }   

//-------------------------------------------------------------------------------
 
      //    
      case 12 : //  
      { 
				IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL &&  
             ( docType == lt_DocPart3D || 
               docType == lt_DocAssemble3D ||
               docType == lt_DocSheetStandart ||
               docType == lt_DocSheetUser ||  
               docType == lt_DocFragment ) )
  		    AdviseDoc( doc, docType, true, 
                                   false, 
                                   false, 
                                   false, 
                                   false, 
                                   false );
				else
					kompas->ksError( "  2D  3D " );
				break;
			}
			case 13 : // 
      { 
				IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL &&  
             ( docType == lt_DocPart3D || 
               docType == lt_DocAssemble3D ||
               docType == lt_DocSheetStandart ||
               docType == lt_DocSheetUser ||  
               docType == lt_DocFragment ) )
          BaseEvent::TerminateEvents( DIID_ksSelectionMngNotify, doc ); 
				else
					kompas->ksError( "  2D  3D " );
				break;
			}

//-------------------------------------------------------------------------------
    
      //   
      case 14 : //  
      { 
				IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL && 
             docType != lt_DocPart3D && 
             docType != lt_DocAssemble3D ) 
          AdviseDoc( doc, docType, false, 
                                   false, 
                                   true, 
                                   false, 
                                   false, 
                                   false );
				else
					kompas->ksError( "  2D,     " );
			  break;
			} 
			case 15 : // 
      { 
				IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL && 
             docType != lt_DocPart3D && 
             docType != lt_DocAssemble3D ) 
				  BaseEvent::TerminateEvents( DIID_ksStampNotify, doc ); 
        else
					kompas->ksError( "  2D,     " );
				break;
			}   
			
//-------------------------------------------------------------------------------
      
      //   3D 
      
      // 
      case 16: //     
      { 
				ksDocument3DPtr doc3D( kompas->ActiveDocument3D() );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc3D ) 
          AdviseDoc( doc3D, docType, false, 
                                     true, 
                                     false, 
                                     false, 
                                     false, 
                                     false );
				else
					kompas->ksError( "  3D " );
			  break;
			} 

      // 
      case 17: //     
      { 
				ksDocument3DPtr doc3D( kompas->ActiveDocument3D() );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc3D ) 
        {
          ksFeaturePtr obj3D;
          long objType;
          if ( Request3DObject( obj3D, objType ) )
            BaseEvent::TerminateEvents( DIID_ksObject3DNotify, doc3D, objType, obj3D ); 
        }
				else
					kompas->ksError( "  3D " );
			  break;
			} 

			case 18: //     3D 
      { 
				ksDocument3DPtr doc3D ( kompas->ActiveDocument3D() );
				if ( doc3D ) 
				  BaseEvent::TerminateEvents( DIID_ksObject3DNotify, doc3D ); 
        else
					kompas->ksError( "  3D " );
				break;
			}

//-------------------------------------------------------------------------------
      
      // C  
      case 19 : //  
      { 
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL && 
             docType != lt_DocTxtStandart && 
             docType != lt_DocTxtUser ) 
          AdviseDoc( doc, docType, false, 
                                   false, 
                                   false, 
                                   false, 
                                   true, 
                                   false );
				else
					kompas->ksError( "  2D, 3D    " );
			  break;
			} 
			case 20 : // 
      { 
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL && 
             docType != lt_DocTxtStandart && 
             docType != lt_DocTxtUser ) 
          BaseEvent::TerminateEvents( DIID_ksSpecificationNotify, doc ); 
				else
					kompas->ksError( "  2D, 3D    " );
			  break;
			}

//-------------------------------------------------------------------------------

      //   
    
      // 
      case 21 : //  
      {
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
        //      ( 2D, 3D     )
				if ( doc != NULL && 
             docType != lt_DocTxtStandart && 
             docType != lt_DocTxtUser ) 
          AdviseDoc( doc, docType, false, 
                                   false, 
                                   false, 
                                   false, 
                                   false, 
                                   true );
				else
					kompas->ksError( "  2D, 3D    " );
			  break;
      }
      case 22 : //  
      case SPC_BASE_OBJECT + 350 : //  
      case SPC_COMMENT     + 350 : // 
      {     
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
        //      ( 2D, 3D     )
				if ( doc != NULL && 
             docType != lt_DocTxtStandart && 
             docType != lt_DocTxtUser ) 
        {
          long objType = comm == 22 ? 0 : comm - 350; 
          AdviseDoc( doc, docType, false, 
                                   false, 
                                   false, 
                                   false, 
                                   false, 
                                   true, objType);
        }
				else
					kompas->ksError( "  2D, 3D    " );
			  break;
      }
      //     
      case 23 : //  
      {
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
        //      ( 2D, 3D     )
				if ( doc != NULL && 
             docType != lt_DocTxtStandart && 
             docType != lt_DocTxtUser ) 
        {
          ksSpecificationPtr specification;
          switch( docType ) 
          {
            case lt_DocSheetStandart : //  
            case lt_DocSheetUser :     //  
            case lt_DocFragment :      // 
            {
              ksDocument2DPtr doc2D( doc ); //  
              specification = doc2D->GetSpecification();
          	  break;
            }
            case lt_DocPart3D :     // 3d- 
            case lt_DocAssemble3D : // 3d- 
            {
              ksDocument3DPtr doc3D( doc ); //  
              specification = doc3D->GetSpecification(); 
          	  break;
            }
            case lt_DocSpc :     // 
            case lt_DocSpcUser : //   
            {
              ksSpcDocumentPtr spcDoc( doc ); //  
              specification = spcDoc->GetSpecification();
          	  break;
            }
          }
          if( specification )
          {
            reference refSpcObj = specification->ksGetCurrentSpcObject();
            if( !refSpcObj )
              kompas->ksError( "   " );
            else
              BaseEvent::TerminateEvents( DIID_ksSpcObjectNotify, doc, refSpcObj );        
          }
        }
        else
					kompas->ksError( "  2D, 3D    " );
			  break;
      }
      case 24 : //  
      case SPC_BASE_OBJECT + 400 : //  
      case SPC_COMMENT     + 400 : // 
      {     
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
 				if ( doc != NULL && 
             docType != lt_DocTxtStandart && 
             docType != lt_DocTxtUser ) 
        {
          long objType = comm == 22 ? 0 : comm - 350; 
          BaseEvent::TerminateEvents( DIID_ksSpcObjectNotify, doc, objType ); 
        }
				else
					kompas->ksError( "  2D, 3D    " );
			  break;
      }      
      case 25 : //     
      {
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL && 
             docType != lt_DocTxtStandart && 
             docType != lt_DocTxtUser ) 
          BaseEvent::TerminateEvents( DIID_ksSpcObjectNotify, doc ); 
				else
					kompas->ksError( "  2D, 3D    " );
			  break;
      }

//-------------------------------------------------------------------------------

      //  3D 
      case 26 : // 
      {
        ksDocument3DPtr doc3D ( kompas->ActiveDocument3D() );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc3D ) 
          AdviseDoc( doc3D, docType, false, 
                                     false, 
                                     false, 
                                     true, 
                                     false, 
                                     false );
				else
					kompas->ksError( "  3D " );
        break;
      }
      case 27 : // 
      {
        ksDocument3DPtr doc3D ( kompas->ActiveDocument3D() );
 				if ( doc3D ) 
          BaseEvent::TerminateEvents( DIID_ksDocument3DNotify, doc3D ); 
				else
					kompas->ksError( "  3D " );
        break;
      }

//-------------------------------------------------------------------------------
     
      //   
      case 28 : // 
      {
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc != NULL && 
             ( docType == lt_DocSpcUser ||
               docType == lt_DocSpc ) ) 
          AdviseDoc( doc, docType, false, 
                                   false, 
                                   false, 
                                   true, 
                                   false, 
                                   false );
				else
					kompas->ksError( "   " );
        break;
      }
      case 29 : // 
      {
        IDispatchPtr doc( kompas->ksGetDocumentByReference(0) );
        long docType = kompas->ksGetDocumentType( 0 );
 				if ( doc != NULL && 
             ( docType == lt_DocSpcUser ||
               docType == lt_DocSpc ) ) 
          BaseEvent::TerminateEvents( DIID_ksSpcDocumentNotify, doc ); 
				else
					kompas->ksError( "   " );
        break;
      }

//-------------------------------------------------------------------------------

      //  2D 
      case 30 : // 
      {
        ksDocument2DPtr doc2D ( kompas->ActiveDocument2D() );
        long docType = kompas->ksGetDocumentType( 0 );
				if ( doc2D ) 
          AdviseDoc( doc2D, docType, false, 
                                     false, 
                                     false, 
                                     true, 
                                     false, 
                                     false );
				else
					kompas->ksError( "  2D " );
        break;
      }
      case 31 : // 
      {
        ksDocument2DPtr doc2D ( kompas->ActiveDocument2D() );
				if ( doc2D ) 
          BaseEvent::TerminateEvents( DIID_ksDocument2DNotify, doc2D ); 
				else
					kompas->ksError( "  2D " );
        break;
      }

//-------------------------------------------------------------------------------

			case 32 : //    
      {
				BaseEvent::TerminateEvents();
				break;
			}

//-------------------------------------------------------------------------------

      case 33 : // 
      { 
        theApp.ConfigDlg();
        break;
			} 

      case 34 : //  
      {
        BaseEvent::ListEvents();
        break; 
      }
      

//-------------------------------------------------------------------------------

      default :
      {
        //   3D 
        // 
        if( comm >= o3d_unknown + 50 && comm <= o3d_feature + 50 )
        {
          ksDocument3DPtr doc3D ( kompas->ActiveDocument3D() );
          long docType = kompas->ksGetDocumentType( 0 );
				  if ( doc3D ) 
          {
            long objType = comm - 50;
            AdviseDoc( doc3D, docType, false, 
                                       true,
                                       false, 
                                       false, 
                                       false, 
                                       false, objType );
          }
        }
				else
					kompas->ksError( "  3D " );
        break;
      }
      
      // 
      if( comm >= o3d_unknown + 200 && comm <= o3d_feature + 200 )
      {
        if( comm >= o3d_unknown + 50 && comm <= o3d_feature + 50 )
        {
          ksDocument3DPtr doc3D ( kompas->ActiveDocument3D() );
          long docType = kompas->ksGetDocumentType( 0 );
				  if ( doc3D ) 
          {
            long objType = comm - 50;
            BaseEvent::TerminateEvents( DIID_ksObject3DNotify, doc3D, objType ); 
          }
        }
				else
					kompas->ksError( "  3D " );
        break;
      }
    }
  }
}

     
//-------------------------------------------------------------------------------
// 
// ---
void AdviseDocuments() 
{
  if ( kompas ) 
  {
    //  
		ksIteratorPtr iter = kompas->GetIterator();       

    //      
		if ( iter != NULL && iter->ksCreateIterator(ALL_DOCUMENTS, 0) ) 
    {
			reference refDoc = iter->ksMoveIterator( "F" ); //  
      while ( refDoc )
      {
        //    
        AdviseDoc( kompas->ksGetDocumentByReference(refDoc), 
                   kompas->ksGetDocumentType( refDoc ) );

        // C 
				refDoc = iter->ksMoveIterator( "N" );           
			}
  		
      //  
      iter->ksDeleteIterator();                       
		}
	}
}


//-----------------------------------------------------------------------------
//      
// ---
bool WINAPI LibInterfaceNotifyEntry( IDispatch *application ) 
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());

  if( theApp.m_auto )
  {
    //    
    if ( kompas == NULL && application )                
    {
      kompas = application;
      kompas.AddRef();
    }

    if ( kompas ) 
    {
      //    
      ApplicationEvent * aplEvent = new ApplicationEvent( kompas );        
      //     
      aplEvent->Advise(); 
    
      AdviseDocuments();
	  }
  }

  return false;
}


//-----------------------------------------------------------------------------
//    
// ---
void AdviseDoc( LPDISPATCH doc, long docType, 
                bool fSelectMng /*true*/, 
                bool fObject /*true*/, 
                bool fStamp /*true*/,
                bool fDocument/*true*/,
                bool fSpecification/*true*/,
                bool fSpcObject/*true*/,
                long objType/*-1*/) 
{ 
  if ( !doc )
    return;
  //  ,    
  if ( !BaseEvent::FindEvent( DIID_ksDocumentFileNotify, doc ) ) 
  {
    bool fFileDoc = !fSelectMng && !fObject && !fStamp && !fDocument && !fSpecification && !fSpcObject;

    //    
		DocumentEvent * docEvent = new DocumentEvent( doc, fFileDoc ); 
    //    
		int advise = docEvent->Advise(); 
    
    //     
    if ( !advise )
      return;
	}
	else
  	kompas->ksError( "    " );

	switch ( docType ) 
	{
		case lt_DocSheetStandart : // 1 -  
		case lt_DocSheetUser     : // 2 -  
		case lt_DocFragment      : // 3 - 
		{
			ksDocument2DPtr doc2D( doc ); //  

      //  2D
      if( fDocument ) 
      {
        if( !BaseEvent::FindEvent( DIID_ksDocument2DNotify, doc2D ) )
        { 
          IUnknownPtr doc2DNotify = doc2D->GetDocument2DNotify();
				  if ( doc2DNotify ) 
				  {
					  Document2DEvent * document2DEvent = new Document2DEvent( doc2DNotify, doc2D ); 
					  document2DEvent->Advise();                                               
				  }
        }
        else
          kompas->ksError( "  3D   " );
      }

      //   2D 
      if( fSpecification ) 
      {
        if( !BaseEvent::FindEvent( DIID_ksSpecificationNotify, doc2D ) )
        { 
          ksSpecificationPtr specification = doc2D->GetSpecification();
				  if ( specification ) 
				  {
					  SpecificationEvent * specificationEvent = new SpecificationEvent( specification, doc2D ); 
					  specificationEvent->Advise();                                               
				  }
        }
        else
          kompas->ksError( "    " );
      }

      //  
      if ( fSpcObject ) 
      { 
        ksSpecificationPtr specification = doc2D ? doc2D->GetSpecification() : NULL;
			  if ( specification ) 
			  {
          reference refSpcObj = specification->ksGetCurrentSpcObject();
          if( !refSpcObj )
            kompas->ksError( "   " );
          else
          {
            if ( !BaseEvent::FindEvent( DIID_ksSpcObjectNotify, doc2D, refSpcObj ) ) 
            {       
              IUnknownPtr objNotify( specification ? specification->GetSpcObjectNotify( refSpcObj ) : NULL); 
		          if ( objNotify ) 
			        {  
                SpcObjectEvent* objEvent = new SpcObjectEvent( objNotify, doc2D, refSpcObj ); 
			          objEvent->Advise();
              }
            }
            else
              kompas->ksError( "    (2D)  " );
          }
        }
      }   

      // 
      if ( fSelectMng ) 
      { 
        if( !BaseEvent::FindEvent( DIID_ksSelectionMngNotify, doc2D ) )
        { 
				  IUnknownPtr selMsg( doc2D ? doc2D->GetSelectionMngNotify() : NULL );
				  if ( selMsg ) 
				  {
					  SelectMngEvent * selEvent = new SelectMngEvent( selMsg, doc2D ); 
					  selEvent->Advise();                                               
				  }
        }
        else
          kompas->ksError( "    " );
			}

      // 
      if ( fStamp && docType != lt_DocFragment )
      {
        if ( !BaseEvent::FindEvent( DIID_ksStampNotify, doc2D ) ) 
        {
          ksStampPtr stamp( doc2D->GetStamp() ); 
				  if ( stamp ) 
          {
					  StampEvent* stampEvent = new StampEvent( stamp, doc2D );
            stampEvent->Advise();
				  }
        }
        else
          kompas->ksError( "     " );
			}

      //  2D 
      if ( fObject && objType >= 0 ) //   
      { 
        if ( !BaseEvent::FindEvent( DIID_ksObject2DNotify, doc2D, objType ) ) 
        {          
          IUnknownPtr objNotify( doc2D ? doc2D->GetObject2DNotify( objType ) : NULL );
  		    if ( objNotify ) 
			    {  
            Object2DEvent* objEvent = new Object2DEvent( objNotify, doc2D, objType, doc2D->GetObject2DNotifyResult() ); 
	  	      objEvent->Advise();
          }
        }
        else
          kompas->ksError( "   2D   " );
      }  
			break;      
		}
		case lt_DocPart3D        : // 5 - 3d- 
		case lt_DocAssemble3D    : // 6 - 3d- 
		{
			ksDocument3DPtr doc3D( doc ); //  

      //  3D
      if( fDocument ) 
      {
        if( !BaseEvent::FindEvent( DIID_ksDocument3DNotify, doc3D ) )
        { 
          IUnknownPtr doc3DNotify = doc3D->GetDocument3DNotify();
				  if ( doc3DNotify ) 
				  {
					  Document3DEvent * document3DEvent = new Document3DEvent( doc3DNotify, doc3D ); 
					  document3DEvent->Advise();                                               
				  }
        }
        else
          kompas->ksError( "  3D   " );
      }

      //   3D 
      if( fSpecification ) 
      {
        if( !BaseEvent::FindEvent( DIID_ksSpecificationNotify, doc3D ) )
        { 
          ksSpecificationPtr specification = doc3D->GetSpecification();
				  if ( specification ) 
				  {
					  SpecificationEvent * specificationEvent = new SpecificationEvent( specification, doc3D ); 
					  specificationEvent->Advise();                                               
				  }
        }
        else
          kompas->ksError( "    " );
      }

      //  
      if ( fSpcObject ) 
      { 
        ksSpecificationPtr specification = doc3D ? doc3D->GetSpecification() : NULL;
        if ( specification ) 
				{
          reference refSpcObj = specification->ksGetCurrentSpcObject();
          if( !refSpcObj )
            kompas->ksError( "   " );
          else
          {
            if ( !BaseEvent::FindEvent( DIID_ksSpcObjectNotify, doc3D, refSpcObj ) ) 
            {       
              IUnknownPtr objNotify( specification ? specification->GetSpcObjectNotify( refSpcObj ) : NULL); 
		          if ( objNotify ) 
			        {  
                SpcObjectEvent* objEvent = new SpcObjectEvent( objNotify, doc3D, refSpcObj ); 
			          objEvent->Advise();
              }
          }
          else
            kompas->ksError( "    (3D)  " );
          }
        }
      }  

      // 
      if ( fSelectMng )
      {
        if( !BaseEvent::FindEvent( DIID_ksSelectionMngNotify, doc3D ) ) 
        {
          ksSelectionMngPtr selMng( doc3D ? doc3D->GetSelectionMng() : NULL );
				  if ( selMng ) 
				  {
  					SelectMngEvent * selEvent = new SelectMngEvent( selMng, doc3D ); 
	  				selEvent->Advise();                                              
		  		}
        }
        else
          kompas->ksError( "    " );
      }
      
      //  3D 
      if ( fObject ) 
      { 
        if( objType >= 0 )
        {
          if ( !BaseEvent::FindEvent( DIID_ksObject3DNotify, doc3D, objType ) ) 
          {  
            ksPartPtr containerPart = doc3D->GetPart( pTop_Part );
            IUnknownPtr objNotify( containerPart ? containerPart->GetObject3DNotify( objType, NULL ) : NULL );
			      if ( objNotify ) 
            { 
              Object3DEvent* objEvent = new Object3DEvent( objNotify, doc3D, objType, NULL, containerPart->GetObject3DNotifyResult() ); 
				      objEvent->Advise();
            }
          }
          else
            kompas->ksError( "   3D   " );
        }
        else
        {
          ksFeaturePtr obj3D;
          if ( Request3DObject( obj3D, objType ) )
          {  
            if ( !BaseEvent::FindEvent( DIID_ksObject3DNotify, doc3D, objType, obj3D ) ) 
            {  
              ksPartPtr containerPart;
	            switch ( objType ) 
              {          
                case o3d_part: 
                {
                  ksPartPtr objPart( obj3D );
		              if ( objPart )
                    containerPart = objPart->GetPart( pTop_Part );
		              break;
                }
                
                case o3d_feature: 
                {
                  ksFeaturePtr objFeature( obj3D );
				          if ( objFeature ) 
                  { 
                    IDispatchPtr obj = objFeature->GetObject();
					          if ( obj ) 
                    {
                      ksEntityPtr objEntity( obj );
                      if ( objEntity )
					              containerPart = objEntity->GetParent();
						          else 
                      {
                        ksPartPtr objPart( obj ); 
		                    if ( objPart )
                          containerPart = objPart->GetPart( pTop_Part );
						          }
					          }
				          }
		              break;
                }

                default: 
                {
                  ksEntityPtr objEntity( obj3D );
                  if ( objEntity )
                    containerPart = objEntity->GetParent();
		              break;
                }
              }       
              if ( containerPart == NULL ) 
                containerPart = doc3D->GetPart( pTop_Part );
            
              IUnknownPtr objNotify( containerPart ? containerPart->GetObject3DNotify( objType, obj3D ) : NULL );
			        if ( objNotify ) 
              { 
                Object3DEvent* objEvent = new Object3DEvent( objNotify, doc3D, objType, obj3D, containerPart->GetObject3DNotifyResult() ); 
				        objEvent->Advise();
              }
            }
            else
              kompas->ksError( "   3D   " );
          }
        }
      }
			break; 
		}
		case lt_DocTxtStandart   : // 7 -   
		case lt_DocTxtUser       : // 8 -   
		{
		  ksDocumentTxtPtr docTxt( doc ); //  

      // 
      if ( fStamp )
      {
        if ( !BaseEvent::FindEvent( DIID_ksStampNotify, docTxt ) ) 
        {
          ksStampPtr stamp( docTxt->GetStamp() ); 
				  if ( stamp ) 
          {
					  StampEvent* stampEvent = new StampEvent( stamp, docTxt );
            stampEvent->Advise();
				  }
        }
        else
          kompas->ksError( "     " );
			}

			break;
		}
		case lt_DocSpcUser       : // 9 -   
		case lt_DocSpc           : // 4 - 
		{
		  ksSpcDocumentPtr spcDoc( doc ); //  

      //  
      if( fDocument ) 
      {
        if( !BaseEvent::FindEvent( DIID_ksSpcDocumentNotify, spcDoc ) )
        { 
          IUnknownPtr spcDocNotify = spcDoc->GetSpcDocumentNotify();
				  if ( spcDocNotify ) 
				  {
					  SpcDocumentEvent * spcDocumentEvent = new SpcDocumentEvent( spcDocNotify, spcDoc ); 
					  spcDocumentEvent->Advise();                                               
				  }
        }
        else
          kompas->ksError( "     " );
      }

      //    
      if( fSpecification ) 
      {
        if( !BaseEvent::FindEvent( DIID_ksSpecificationNotify, spcDoc ) )
        { 
          ksSpecificationPtr specification = spcDoc->GetSpecification();
				  if ( specification ) 
				  {
					  SpecificationEvent * specificationEvent = new SpecificationEvent( specification, spcDoc ); 
					  specificationEvent->Advise();                                               
				  }
        }
        else
          kompas->ksError( "    " );
      }

      //   
      if ( fSpcObject ) 
      { 
        ksSpecificationPtr specification = spcDoc ? spcDoc->GetSpecification() : NULL;
        if ( specification ) 
				{
          reference refSpcObj = specification->ksGetCurrentSpcObject();
          if( !refSpcObj )
            kompas->ksError( "   " );
          else
          {
            if ( !BaseEvent::FindEvent( DIID_ksSpcObjectNotify, spcDoc, refSpcObj ) ) 
            {       
              IUnknownPtr objNotify( specification ? specification->GetSpcObjectNotify( refSpcObj ) : NULL); 
      		    if ( objNotify ) 
      		    {  
                SpcObjectEvent* objEvent = new SpcObjectEvent( objNotify, spcDoc, refSpcObj ); 
      			    objEvent->Advise();
              }
            }
            else
              kompas->ksError( "    (Spc)  " );
          }
        }
      }  

      // 
      if ( fStamp )
      {
        if ( !BaseEvent::FindEvent( DIID_ksStampNotify, spcDoc ) ) 
        {
          ksStampPtr stamp( spcDoc->GetStamp() ); 
				  if ( stamp ) 
          {
					  StampEvent* stampEvent = new StampEvent( stamp, spcDoc );
            stampEvent->Advise();
				  }
        }
        else
          kompas->ksError( "     " );
			}

			break;
		}
  }
}


