////////////////////////////////////////////////////////////////////////////////
//
// Step1_API7_2D.cpp.cpp -   
//
//   1: NewDocument();                -  
//   2: NewMark();                    -    -
//   3: NewUnitMarking();             -    
//   4: AddStrAxis();                 -  
//   5: AddCircleAxis();              -  
//   6: NewMarkOnLeader();            -    -
//   7: NewMarkOnLine();              - /.
//   8: NewBrace();                   -  
//   9: NewMultiTextLeader();         -     
//
////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <afxdllx.h>
#include "Step1_API7_2D.h"

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

class LpSelfText;

//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE DocCollectionDLL = { NULL, NULL };
HINSTANCE g_hInstance = NULL;

ksAPI7::IApplicationPtr newKompasAPI( NULL );               

void OnProcessDetach();                            //  

//-------------------------------------------------------------------------------
//   
//    DLL
// ---
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
  g_hInstance = hInstance;

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("DOCCOLLECTION.AWX Initializing!\n");

		AfxInitExtensionModule(DocCollectionDLL, hInstance);

		new CDynLinkLibrary(DocCollectionDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("DOCCOLLECTION.AWX Terminating!\n");
    OnProcessDetach();
		AfxTermExtensionModule(DocCollectionDLL);
	}
	return 1;   // ok
}

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

      libname = LoadStr( IDR_API7 );    // kAPI7.dll
			filename.Replace( filename.Right(filename.GetLength() - (filename.ReverseFind('\\') + 1)), 
												libname );

			HINSTANCE hAppAuto = LoadLibrary( filename ); //  kAPI7.dll
		
      if(  hAppAuto ) 
      {
				//   ,   KompasApplication  
        typedef LPDISPATCH ( WINAPI * FCreateKompasApplication )(); 
				 
        FCreateKompasApplication pCreateKompasApplication = 
					(FCreateKompasApplication)GetProcAddress( hAppAuto, "CreateKompasApplication" );	
				
        if ( pCreateKompasApplication )
					newKompasAPI = IDispatchPtr( pCreateKompasApplication(), false ); //   Application
				FreeLibrary( hAppAuto );  
			}
		}
	}
}

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


//-------------------------------------------------------------------------------
// _1_  
// ---
void NewDocument()
{
  if( !(bool)newKompasAPI )
    return;

  //     
  ksAPI7::IDocumentsPtr pDocuments( newKompasAPI->Documents );

  if( !(bool)pDocuments )
    return;

  //    IKompasDocument
  // Add  , (    )     
  ksAPI7::IKompasDocumentPtr pKompasDocument( pDocuments->Add(ksDocumentDrawing, true) );
}


//-------------------------------------------------------------------------------
//      (Auto)
// ---
ksAPI7::ILineSegmentsPtr GetILineSegments()
{
  ksAPI7::ILineSegmentsPtr pLineSegments;
  ksAPI7::IKompasDocument2DPtr pKompasDocument2D( newKompasAPI->GetActiveDocument() );
   
  if ( pKompasDocument2D )
  {
    //            
    ksAPI7::IViewsAndLayersManagerPtr pViewsAndLayersManager = pKompasDocument2D->GetViewsAndLayersManager();
    
    if( pViewsAndLayersManager )
    { 
      //     
      ksAPI7::IViewsPtr pViews = pViewsAndLayersManager->GetViews();
  
      if( pViews )
      {
        //    
        // View-    ,   (    )  
        ksAPI7::IViewPtr pView =  pViews->GetActiveView();  
        ksAPI7::IDrawingContainerPtr pDrawingContainer( pView );
  
        if( pDrawingContainer )
        {
          //    
          pLineSegments = pDrawingContainer->GetLineSegments();
  
        }
      }      
    } 
  }
  return pLineSegments;
}


//-------------------------------------------------------------------------------
//       (COM)
// ---
ksAPI7::IViewsPtr GetViewsPtr()
{
  ksAPI7::IViewsPtr res;
  
  if ( newKompasAPI )
  {
    //     
    ksAPI7::IKompasDocument2DPtr pKompasDocument2D;
    pKompasDocument2D = newKompasAPI->ActiveDocument;
      
    if ( (bool)pKompasDocument2D )
    {          
      //       
      ksAPI7::IViewsAndLayersManagerPtr pViewsAndLayersManager;
      pViewsAndLayersManager = pKompasDocument2D->GetViewsAndLayersManager();
      
      if ( pViewsAndLayersManager )
        res = pViewsAndLayersManager->GetViews();
    }  
  }
  return res;
}


//-------------------------------------------------------------------------------
// _2_    -
// ---
void NewMark()
{
  ksAPI7::IViewsPtr pViews = GetViewsPtr();
  if( !(bool)pViews )
    return;
  
  //    
  // ActiveView-     
  ksAPI7::IViewPtr pView( pViews->ActiveView );  
  
  
  //     
  ksAPI7::IBuildingContainerPtr pBuildingContainer( pView );
  
  if( !(bool)pBuildingContainer )
    return;
  
  //    
  ksAPI7::IMarksPtr pMarks( pBuildingContainer->Marks );
  
  if( !(bool)pMarks )
    return;
  //    
  ksAPI7::IMarkInsideFormPtr pMarkInF( pMarks->Add(ksDrMarkInsideForm) );      
  if ( !(bool)pMarkInF )
    return;  
  
  pMarkInF->X = 250;
  pMarkInF->Y = 250;
  pMarkInF->Form = ksMFormRectangle	;
  pMarkInF->FormGabarit = 45;
  pMarkInF->FormHeight = 50;
  pMarkInF->Angle = 45;
  pMarkInF->FormStyle = ksCSAxial;

              
  pMarkInF->Update(); 
 
  //   
  double x = pMarkInF->X;
  double y = pMarkInF->Y;
  TCHAR buf[255];
  _stprintf( buf, _T("  :/nX = %g, Y = %g"), x, y);
  MessageT( buf );
}


//-------------------------------------------------------------------------------
// _7_    -
// ---
void NewMarkOnLeader()
{
  ksAPI7::IViewsPtr ppViews = GetViewsPtr();
  if ( !(bool)ppViews )
    return;

  //    
  // ActiveView-     
  ksAPI7::IViewPtr ppView( ppViews->ActiveView );  
  
 
  //     
  ksAPI7::IBuildingContainerPtr ppBuildingContainer( ppView );
  
  if ( !(bool)ppBuildingContainer )
    return;
  
  //    
  ksAPI7::IMarksPtr ppMarks( ppBuildingContainer->Marks );
  
  if( !(bool)ppMarks )
    return;
  
  //    
  ksAPI7::IMarkOnLeaderPtr pMarkOnL( ppMarks->Add( ksDrMarkOnLeader	) );      
  if ( !(bool)pMarkOnL )
    return;  

  pMarkOnL->X = 150;
  pMarkOnL->Y = 150;
  pMarkOnL->ArrowType = ksLeaderArrow;
  pMarkOnL->ShelfDirection = ksLSLeft;
  pMarkOnL->AddBranchByPoint( 1, FALSE, 100, 100);
  pMarkOnL->Update();

  ksAPI7::ITextPtr pTextAfter( pMarkOnL->GetTextAfter() );   
  _bstr_t strAfter = _T("   ");
  pTextAfter->PutStr( strAfter );

  ksAPI7::ITextPtr pTextBefore( pMarkOnL->GetTextBefore() );
  _bstr_t strBefore = _T("   ");
  pTextBefore->PutStr( strBefore );    

   ksAPI7::ITextPtr pTextUnder( pMarkOnL->GetTextUnder() );
  _bstr_t strUnder = _T("    ");
  pTextUnder->PutStr( strUnder );    

  pMarkOnL->Update();
  MessageT( _T("  -") );
  
}

//-------------------------------------------------------------------------------
// _9_  / 
// ---
void NewMarkOnLine()
{
  ksAPI7::IViewsPtr pViews = GetViewsPtr();
  if ( !(bool)pViews )
    return;
  //    
  // ActiveView-     
  ksAPI7::IViewPtr pView( pViews->ActiveView );  
  
 
  //     
  ksAPI7::IBuildingContainerPtr ppBuildingContainer( pView );
  
  if ( !(bool)ppBuildingContainer )
    return;
  
  //    
  ksAPI7::IMarksPtr pMarks( ppBuildingContainer->Marks );
  
  if( !(bool)pMarks )
    return;
  
  //    
  ksAPI7::IMarkOnLinePtr pMarkOnLine( pMarks->Add( ksDrMarkOnLine	) );      
  if ( !(bool)pMarkOnLine )
    return;  
    //       
       
  ksAPI7::ITextPtr pTextAfter( pMarkOnLine->GetTextAfter() );   
  _bstr_t strAfter = _T("   ");
  pTextAfter->PutStr( strAfter );

      
  //  -    

  ksAPI7::ILineSegmentsPtr pLineSegments = GetILineSegments();
  ksAPI7::ILineSegmentPtr pLineSegment( pLineSegments->Add()); 
  
  if ( !(bool)pLineSegment )
    return;
  
  pLineSegment->PutX1   ( 100 );
  pLineSegment->PutY1   ( 100 );
  pLineSegment->PutX2   ( 150 );
  pLineSegment->PutY2   ( 110 );
  //   
  pLineSegment->PutStyle( 1 );
  pLineSegment->Update  ();  
          
  pMarkOnLine->Line = pLineSegment;
  pMarkOnLine->Position = ksMTextOnLine	;
     

  pMarkOnLine->Update();
  MessageT( _T("/ ") ); 
}


//-------------------------------------------------------------------------------
// _10_      
// ---
void NewMultiTextLeader()
{
  ksAPI7::IViewsPtr ppViews = GetViewsPtr();
  if ( !(bool)ppViews )
    return;
  
  //    
  // ActiveView-     
  ksAPI7::IViewPtr ppView( ppViews->ActiveView );  
  
 
  //     
  ksAPI7::IBuildingContainerPtr ppBuildingContainer( ppView );
  
  if( !(bool)ppBuildingContainer )
    return;
  //         ()
  ksAPI7::IMultiTextLeadersPtr pMTLs( ppBuildingContainer->MultiTextLeaders );
  
  if ( !(bool)pMTLs )
    return;
  
  //    
  ksAPI7::IMultiTextLeaderPtr pMTL( pMTLs->Add() );      
  if ( !(bool)pMTL )
    return;  

  pMTL->Form = TRUE;
  pMTL->ArrowType = ksLeaderArrow;
  pMTL->ShelfX = 150;
  pMTL->ShelfY = 150;
  pMTL->TextDirection = TRUE;
  pMTL->ShelfDirection = ksLSRight;
  pMTL->AddBranchByPoint(  1, 100, 100);
  pMTL->AddBranchByPoint( -1, 120, 100);
  pMTL->AddBranchByPoint( -1, 130, 100);
  pMTL->Update();

  ksAPI7::ITextPtr pText1( pMTL->GetText() );   
  _bstr_t strTxt1 = _T("  1");
  pText1->PutStr( strTxt1 );

  pMTL->Update();
  MessageT(_T("    ") );
}


//-------------------------------------------------------------------------------
// _11_   
// ---
void NewBrace()
{
  ksAPI7::IViewsPtr ppViews = GetViewsPtr();
  if ( !(bool)ppViews )
    return;
  
  //    
  // ActiveView-     
  ksAPI7::IViewPtr ppView( ppViews->ActiveView );  
  
 
  //     
  ksAPI7::IBuildingContainerPtr ppBuildingContainer( ppView );
  
  if ( !(bool)ppBuildingContainer )
    return;
  
  //     
  ksAPI7::IBracesPtr pBraces( ppBuildingContainer->Braces );
  
  if ( !(bool)pBraces )
    return;
  
  //     
  ksAPI7::IBracePtr pBrace( pBraces->Add() );      
  if ( !(bool)pBrace )
    return;  
         
  pBrace->X1 = 50;
  pBrace->Y1 = 200;   
  pBrace->Alignment = ksATArbitrary;
  pBrace->Angle = 200;
  pBrace->ShelfDirection = ksLSRight;
  pBrace->Direction = TRUE;
  pBrace->Length = 100;
  pBrace->Radius = 25;
  pBrace->ShelfDirection = ksLSRight;
  pBrace->Style = 7;
  pBrace->Update();

  ksAPI7::ITextPtr pText1( pBrace->GetText() );   
  _bstr_t strTxt1 = _T("  ");
  pText1->PutStr( strTxt1 );

  pBrace->Update();
  MessageT(_T(" "));
}


//-------------------------------------------------------------------------------
// _3_      (Auto)
// ---
void UnitMarkingksParam()
{
  ksAPI7::IKompasDocument2DPtr pKompasDocument2D = newKompasAPI->GetActiveDocument();
   
  if ( pKompasDocument2D )
  {
    //            
    ksAPI7::IViewsAndLayersManagerPtr pViewsAndLayersManager = pKompasDocument2D->GetViewsAndLayersManager();
    
    if( pViewsAndLayersManager )
    { 
      //     
      ksAPI7::IViewsPtr pViews = pViewsAndLayersManager->GetViews();
  
      if( pViews )
      {
        //    
        // View-    ,   (    )  
        ksAPI7::IViewPtr pView =  pViews->GetActiveView();  
  
          //     
        ksAPI7::IBuildingContainerPtr pBuildingContainer( pView );
        if( pBuildingContainer )
        {
            //      
          ksAPI7::IUnitMarkingsPtr pUnitMarkings( pBuildingContainer->GetUnitMarkings() );
          
          if( pUnitMarkings )
          {
            //     
            _variant_t v = (long)0;
            ksAPI7::IUnitMarkingPtr pUnitMarking( pUnitMarkings->GetUnitMarking(v) );
             
            if( pUnitMarkings )
            {
            
              double Radius = pUnitMarking->GetRadius();
              double Height = pUnitMarking->GetHeight();
              double Width  = pUnitMarking->GetWidth();
            }
          } 
        }
      }
    }  
  }
}


    

//-------------------------------------------------------------------------------
// _19_    ..
//      
// --- 
void AddStrAxis()
{
  if ( newKompasAPI )
  {  
    //  2D 
    ksAPI7::IKompasDocument2DPtr doc2D = newKompasAPI->GetActiveDocument();

    if ( doc2D )
    {
      //     
      ksAPI7::IViewsAndLayersManagerPtr view_men = doc2D->GetViewsAndLayersManager();
      
      if ( view_men )
      {       
        //   
        ksAPI7::IViewsPtr views = view_men->GetViews();
        
        if ( views )
        {
          //   QueryInterface     
          // (   )
          ksAPI7::IBuildingContainerPtr pBuildContainer( views->GetActiveView() );
          
          if ( pBuildContainer )
          {
            //    
            ksAPI7::IBuildingAxesPtr axes;
            pBuildContainer->get_BuildingAxes(&axes);
          
            if ( axes )
            {
              //   
              ksAPI7::IStraightAxisPtr ax1( axes->Add(ksDrStraightAxis) );
              //ksAPI7::IBuildingAxisPtr ax1 = axes->Add(ksDrStraightAxis);
              if (ax1)
              {
                ax1->Angle = 45;
                ax1->Length = 100;
              
                ax1->Update(); 
              }

            }
          }
        }
      }
    }
  }
}


//-------------------------------------------------------------------------------
// _20_    ..
// 
// --- 
void AddCircleAxis()
{
  if ( newKompasAPI )
  {  
    //  2D 
    ksAPI7::IKompasDocument2DPtr doc2D = newKompasAPI->GetActiveDocument();

    if ( doc2D )
    {
      //     
      ksAPI7::IViewsAndLayersManagerPtr view_men = doc2D->GetViewsAndLayersManager();
      
      if ( view_men )
      {       
        //   
        ksAPI7::IViewsPtr views = view_men->GetViews();
        
        if ( views )
        {
          //   QueryInterface     
          // (   )
          ksAPI7::IBuildingContainerPtr pBuildContainer( views->GetActiveView() );
          
          if ( pBuildContainer )
          {
            //    
            ksAPI7::IBuildingAxesPtr axes;
            pBuildContainer->get_BuildingAxes(&axes);
          
            if ( axes )
            {
              //   
              ksAPI7::ICircleAxisPtr axC( axes->Add(ksDrCircleAxis) );
              //ksAPI7::IBuildingAxisPtr ax1 = axes->Add(ksDrStraightAxis);
              if (axC)
              {
                axC->Xc = 100;
                axC->Yc = 100;
                axC->Radius = 100;
                axC->MarkOn = TRUE;
                axC->MarkAngle	= 45;
                 
                axC->Update(); 
              }
            }
          }
        }
      }
    }
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{  
  GetNewKompasAPI(); 

  switch ( comm )
  {  
    case  1: NewDocument();           break; //  
    case  2: NewMark();               break; //    -
    case  3: AddStrAxis();            break; //  
    case  4: AddCircleAxis();         break; //  
    case  5: NewMarkOnLeader();       break; //    -
    case  6: NewMarkOnLine();         break; // /.
    //     
	// case  7: NewBrace();              break; //  
    //case  8: NewMultiTextLeader();    break; //       
	// 
	case  7: NewMultiTextLeader();    break; //      
    case  8: NewBrace();              break; //  		
  }
}


//-------------------------------------------------------------------------------
//  
// ---
void OnProcessDetach()
{
  newKompasAPI = NULL;
}


//------------------------------------------------------------------------------
//    
// ---
CString LoadStr( int strID ) 
{
	TCHAR temp[512];
	LoadString( DocCollectionDLL.hModule, strID, temp, _MAX_PATH ); 
	return temp;
}


