////////////////////////////////////////////////////////////////////////////////
//
// Step3_API7_3D.cpp -   Visual C++
//
//	1: Rough3DWork( doc );				-     3D
//	2: Base3DWork( doc );					-     3D
//	3: Leader3DWork( doc );				-	   - 3D
//	4: BrandLeader3DWork( doc );	-      3D
//	5: MarkLeader3DWork( doc );		-      3D
//	6: Tolerance3DWork( doc );		-      3D
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h>
#include "Step3_API7_3D.h"

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


//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE Step3_API7_3DDLL = { NULL, NULL };
HINSTANCE g_hInstance = NULL;
ksAPI7::IApplicationPtr newKompasAPI( NULL );               

void OnProcessDetach();                 //  

//  
int oType = o3d_edge;										//     


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

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

		AfxInitExtensionModule(Step3_API7_3DDLL, hInstance);

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


//-------------------------------------------------------------------------------
//     API
// ---
void GetNewKompasAPI() 
{
  if ( !( ksAPI7::IApplication * )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 /*AddRef*/); //   Application
        FreeLibrary( hAppAuto );  
      }
    }
  }
}


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


//-------------------------------------------------------------------------------
//   
// ---
ksAPI7::IKompasDocument3DPtr GetCurrentDocument()
{
	ksAPI7::IKompasDocument3DPtr doc = NULL;
	
	//      IApplication  
  if ( !(bool)newKompasAPI )
		//    
    GetNewKompasAPI();
	
	//     IApplication  
  if ( newKompasAPI ) 
		//   
    doc = newKompasAPI->GetActiveDocument();
	
	return doc;
}


//-------------------------------------------------------------------------------
//   
// ---
void Rough3DWork( ksAPI7::IKompasDocument3DPtr & doc );
void Base3DWork( ksAPI7::IKompasDocument3DPtr & doc );
void Leader3DWork( ksAPI7::IKompasDocument3DPtr & doc );
void BrandLeader3DWork( ksAPI7::IKompasDocument3DPtr & doc );
void MarkLeader3DWork( ksAPI7::IKompasDocument3DPtr & doc );
void Tolerance3DWork( ksAPI7::IKompasDocument3DPtr & doc );


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
	//   
	ksAPI7::IKompasDocument3DPtr doc( GetCurrentDocument() );
	
	if ( doc )
	{
		switch ( comm )
		{
			case 1: Rough3DWork( doc );					break;		//     3D
			case 2:	Base3DWork( doc );					break;		//     3D
			case 3: Leader3DWork( doc );				break;		//    - 3D
			case 4: BrandLeader3DWork( doc );		break;		//      3D
			case 5: MarkLeader3DWork( doc );		break;		//      3D
			case 6: Tolerance3DWork( doc );			break;		//      3D
		}
	}
	else
		MessageT( (LPTSTR)(LPCTSTR)LoadStr(IDS_NODOC) );
}

#pragma warning( disable : 4786 )
//-------------------------------------------------------------------------------
//    3D
// ---
ksAPI7::ISymbols3DContainerPtr GetSymbols3DContainer( ksAPI7::IKompasDocument3DPtr & doc )
{
  if ( doc )
  {
     //    3D
    return doc->GetTopPart(); 
  }
  return NULL;
}


//-----------------------------------------------------------------------------
//  
// ---
BOOL __stdcall  UserFilterProc( IEntity * e)
{
  if( e && (!oType || e->GetType() == oType) )
    return TRUE;
  else
    return FALSE;
}


//-------------------------------------------------------------------------------
//   
// ---
void SetRoughPars( ksAPI7::IRough3DPtr & rough )
{
	if ( rough )
	{
		//    
		ksAPI7::IRoughParamsPtr roughPars( rough );
		
		if ( roughPars )
		{
			//    
			roughPars->ProcessingByContour = TRUE;
			//   
			roughPars->LeaderLength = 20;
			//    
			roughPars->LeaderAngle = 45;
			
			//     
			ksAPI7::ITextPtr txt1( roughPars->RoughParamText );
			
			if ( txt1 )
				txt1->Str = _T("1");
			
			//      
			ksAPI7::ITextPtr txt2( roughPars->ProcessText );
			
			if ( txt2 )
				txt2->Str = _T("2");
			
			//     
			ksAPI7::ITextPtr txt3( roughPars->BaseLengthText );
			
			if ( txt3 )
				txt3->Str = _T("3");
		}
	}
}


//-------------------------------------------------------------------------------
//   3D
// ---
bool CreateRough3D( ksAPI7::IRough3DPtr & rough,
									  ksAPI7::IKompasDocument3DPtr & doc )
{
	bool res = false;

	if ( rough )
	{
		//    3D  API7  API5
		IDocument3DPtr doc3D( IUnknownPtr(ksTransferInterface( doc, ksAPI3DCom, 0/* */ ), false/*AddRef*/) );
		
		if ( doc3D )
		{
			//        
			IEntityPtr baseObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_OBJ)), false/*AddRef*/ );

			if ( baseObj )
			{
				double x, y, z;
				//   
				doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_POINT), &x, &y, &z );

				//     API5  API7
				ksAPI7::IModelObjectPtr mObj( IUnknownPtr(ksTransferInterface(baseObj, ksAPI7Dual, 0), false/*AddRef*/) );

				if ( mObj )
					//   
					rough->SetBasePosition( x, y, z, mObj );

				//   
				rough->BasePlane = o3d_planeXOZ;

				//   
				SetRoughPars( rough );

				//  
				rough->Update();
				res = true;
			}
		}
	}
	return res;
}


//-------------------------------------------------------------------------------
//       
// ---
void SetPosition( ksAPI7::IRough3DPtr & rough, 
								  ksAPI7::IModelObjectPtr & face )
{
	if ( (bool)rough && (bool)face )
	{
		//      API7  API5
		IEntityPtr faceEnt( IUnknownPtr(ksTransferInterface( face, ksAPI3DCom, o3d_entity ), false/*AddRef*/) );

		if ( faceEnt )
		{
			//    
			IFaceDefinitionPtr faceDef( IUnknownPtr(faceEnt->GetDefinition(), false/*AddRef*/) );

			if ( faceDef )
			{
				//   
				IEdgeCollectionPtr edgeCol( faceDef->EdgeCollection(), false/*AddRef*/ );

				if ( edgeCol )
				{
					//    
					IEdgeDefinitionPtr edgeDef( edgeCol->First(), false/*AddRef*/ );

					if ( edgeDef )
					{
						//    
						IVertexDefinitionPtr vertex( edgeDef->GetVertex(TRUE/* */), false/*AddRef*/ );

						if ( vertex )
						{
							//     API5  API7
							ksAPI7::IModelObjectPtr mObj( IUnknownPtr(ksTransferInterface(vertex, ksAPI7Dual, 0), false/*AddRef*/) );

							if ( mObj )
								//       
								rough->PositionObject = mObj;
						}
					}
				}
			}
		}
	}
}


//-------------------------------------------------------------------------------
//    
// ---
_bstr_t GetNewName( ksAPI7::IFeature7Ptr & featObj, int strID )
{
	_bstr_t res = _T("");

	if ( featObj )
	{
		//      
		CString newName =	LoadStr( strID );
		CString name = (LPCTSTR)featObj->Name;
		
		int pos = name.Find( _T(':') );
		
		if ( pos > 0 )
			newName += name.Mid( pos + 1 );
		
		res = (LPTSTR)(LPCTSTR)newName;
	}
	return res;
}


//-------------------------------------------------------------------------------
//   3D
// ---
void EditRough3D( ksAPI7::IRough3DPtr & rough,
								  ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( rough )
	{
		//    
		ksAPI7::IRoughParamsPtr roughPars( rough );

		if ( roughPars )
			//     
			roughPars->ProcessingByContour = FALSE;

		//    
		ksAPI7::IFeature7Ptr featObj( rough );

		if ( featObj )
		{	
			//     		
			rough->Name = GetNewName( featObj, IDS_ROUGH );
			//   
			ksAPI7::IModelObjectPtr baseObj( rough->BaseObject );
			//       
			SetPosition( rough, baseObj );
		}

		//     
		ksAPI7::IColorParam7Ptr colorPars( rough );

		//    
		if ( colorPars )
			colorPars->Color = RGB( 0, 0, 255 );

		//  
		rough->Update();
	}
}


//-------------------------------------------------------------------------------
//     3D
// ---
void Rough3DWork( ksAPI7::IKompasDocument3DPtr & doc )
{
	//    3D
	ksAPI7::ISymbols3DContainerPtr symbCont( GetSymbols3DContainer(doc) );
	
	if ( symbCont )
	{
		//    3D
		ksAPI7::IRoughs3DPtr roughsCol( symbCont->GetRoughs3D() );
		
		if ( roughsCol )
		{
			//     3D
			ksAPI7::IRough3DPtr newRough( roughsCol->Add() );
			
			if ( newRough )
			{
				//    3D
				if ( CreateRough3D(newRough, doc) )
				{	
					_bstr_t name = _T("");
					//     
					ksAPI7::IFeature7Ptr feature( newRough );
					
					//   
					if ( feature )
						name = feature->Name;
					
					if ( YesNoT((LPTSTR)(LPCTSTR)LoadStr(IDS_EDIT)) == 1 )
					{
						//      
						ksAPI7::IRough3DPtr rough( roughsCol->GetRough3D(name) );
						//  
						EditRough3D( rough, doc );
					}
				}
				else
					//   "  "
					MessageT( (LPTSTR)(LPCTSTR)LoadStr(IDS_NOCREATE) );
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
bool CreateBase3D( ksAPI7::IBase3DPtr & base, 
									 ksAPI7::IKompasDocument3DPtr & doc )
{
	bool res = false;

	if ( base )
	{
		//    3D  API7  API5
		IDocument3DPtr doc3D( IUnknownPtr(ksTransferInterface( doc, ksAPI3DCom, 0/* */ ), false/*AddRef*/) );
		
		if ( doc3D )
		{
			//        
			IEntityPtr baseObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_OBJ)), false/*AddRef*/ );
			
			if ( baseObj )
			{
				double x, y, z;
				//   
				doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_POINT), &x, &y, &z );
				
				//     API5  API7
				ksAPI7::IModelObjectPtr mObj( IUnknownPtr(ksTransferInterface(baseObj, ksAPI7Dual, 0), false/*AddRef*/) );
				
				if ( mObj )
					//   
					base->SetBranchBeginPoint( x, y, z, mObj );
				
				//   
				base->BasePlane = o3d_planeXOZ;
				
				//  
				base->Update();
				res = true;
			}
		}
	}
	return res;
}


//-------------------------------------------------------------------------------
//   
// ---
void AddTextItem( ksAPI7::ITextLinePtr & line, _bstr_t str, ksTextItemEnum type )
{
	if ( line )
	{
		//    
		ksAPI7::ITextItemPtr item( line->Add() );
		
		if ( item )
		{
			//   
			item->Str = str;
			//  
			item->ItemType = type;
			//  
			item->Update();
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
void EditBase3D( ksAPI7::IBase3DPtr & base,
								 ksAPI7::IKompasDocument3DPtr & doc)
{
	if ( base )
	{
		//  
		base->AutoSorted = FALSE;

		//     
		ksAPI7::ITextPtr txt( base->Text );
		
		if ( txt )
		{
			txt->Str = _T("");
			
			ksAPI7::ITextLinePtr line( txt->Add() );
			
			if ( line )
			{
				//  
				AddTextItem( line, _T("A"), ksTItString );
				//     (   
				//    
				AddTextItem( line, _T(""), ksTItSBase );
				//   
				AddTextItem( line, _T("1"), ksTItSLowerIndex );
				//    
				AddTextItem( line, _T(""), ksTItSEnd );
			}
		}

		//  -  
		base->DrawType = FALSE;
		double x, y, z;
		//     
		base->GetBranchEndPoint( &x, &y, &z );
		base->SetBranchEndPoint( x + 10, y + 10, z );

		//    
		ksAPI7::IFeature7Ptr featObj( base );

		if ( featObj )
			//     
			base->Name = GetNewName( featObj, IDS_BASE );

		//     
		ksAPI7::IColorParam7Ptr colorPars( base );
		
		//    
		if ( colorPars )
			colorPars->Color = RGB( 0, 0, 0 );


		base->Update();
	}
}


//-------------------------------------------------------------------------------
//     
// ---
void Base3DWork( ksAPI7::IKompasDocument3DPtr & doc )
{
	//    3D
	ksAPI7::ISymbols3DContainerPtr symbCont( GetSymbols3DContainer(doc) );
	
	if ( symbCont )
	{
		//     3D
		ksAPI7::IBases3DPtr basesCol( symbCont->GetBases3D() );
		
		if ( basesCol )
		{
			//     3D
			ksAPI7::IBase3DPtr newBase( basesCol->Add() );
			
			if ( newBase )
			{
				//    3D
				if ( CreateBase3D(newBase, doc) )
				{	
					_bstr_t name = _T("");
					//     
					ksAPI7::IFeature7Ptr feature( newBase );
					
					//   
					if ( feature )
						name = feature->Name;
					
					if ( YesNoT((LPTSTR)(LPCTSTR)LoadStr(IDS_EDIT)) == 1 )
					{
						//      
						ksAPI7::IBase3DPtr base( basesCol->GetBase3D(name) );
						//   
						EditBase3D( base, doc );
					}
				}
				else
					//   "  "
					MessageT( (LPTSTR)(LPCTSTR)LoadStr(IDS_NOCREATE) );
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   -
// ---
void SetLeader3DTexts( ksAPI7::IBaseLeader3DPtr & baseLeader )
{
	ksAPI7::ILeaderPtr leader( baseLeader );

	if ( leader )
	{
		//     
		ksAPI7::ITextPtr txtOnSh( leader->TextOnShelf );
		
		if ( txtOnSh )
			//  
			txtOnSh->Str = _T("1");
		
		//     
		ksAPI7::ITextPtr txtUnderSh( leader->TextUnderShelf );
		
		if ( txtUnderSh )
			//  
			txtUnderSh->Str = _T("2");
		
		//     
		ksAPI7::ITextPtr txtOnBr( leader->TextOnBranch );
		
		if ( txtOnBr )
			//  
			txtOnBr->Str = _T("3");
		
		//     
		ksAPI7::ITextPtr txtUnderBr( leader->TextUnderBranch );
		
		if ( txtUnderBr )
			//  
			txtUnderBr->Str = _T("4");
		
		//     
		ksAPI7::ITextPtr txtAfterSh( leader->TextAfterShelf );
		
		if ( txtAfterSh )
			//  
			txtAfterSh->Str = _T("5");
	}
}


//-------------------------------------------------------------------------------
//   -
// ---
void CreateBranchsLeader( ksAPI7::IBaseLeader3DPtr & leader, 
													ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( (bool)leader && (bool)doc )
	{
		//    3D  API7  API5
		IDocument3DPtr doc3D( IUnknownPtr(ksTransferInterface( doc, ksAPI3DCom, 0/* */ ), false/*AddRef*/) );
		
		if ( doc3D )
		{
			//     
			IEntityPtr baseObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_OBJ)), false/*AddRef*/ );
			
			double x, y, z, x1, y1, z1;
			//    .
			//      
			doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_BEGINBRANCH), &x, &y, &z );
			
			//  ,   
			IEntityPtr posObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_POSOBJ)), false/*AddRef*/ );
			
			if ( (bool)baseObj && (bool)posObj )
			{
				//  ,   
				//      
				doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_BEGINSHELF), &x1, &y1, &z1 );
				//     API5  API7
				ksAPI7::IModelObjectPtr mObj1( IUnknownPtr(ksTransferInterface(baseObj, ksAPI7Dual, 0), false/*AddRef*/) );
				ksAPI7::IModelObjectPtr mObj2( IUnknownPtr(ksTransferInterface(posObj, ksAPI7Dual, 0), false/*AddRef*/) );
				
				if ( (bool)mObj1 && (bool)mObj2 )
				{	
					//   
					ksAPI7::IBranchs3DPtr branchs3D( leader );
					
					//  
					if ( branchs3D )
						branchs3D->AddBranchByPoint( x, y, z, mObj1 );
					
					//  ,   
					leader->PositionObject = mObj2;
					leader->SetPosition( x1, y1, z1 );
				}
			}
		}
	}
}


//-------------------------------------------------------------------------------
//  -
// ---
bool CreateLeader3D( ksAPI7::IBaseLeader3DPtr & leader, 
										 ksAPI7::IKompasDocument3DPtr & doc )
{
	bool res = false;

	if ( leader )
	{
		//   -
		CreateBranchsLeader( leader, doc );
		//   -
		SetLeader3DTexts( leader );
		//  
		leader->Update();
		res = true;
	}
	return res;
}


//-------------------------------------------------------------------------------
//   -
// ---
void EditLeaderPars( ksAPI7::IBaseLeader3DPtr & baseLeader )
{
	if ( baseLeader )
	{
		ksAPI7::ILeaderPtr leader( baseLeader );

		if ( leader )
		{
			//   -  
			leader->SignType = ksLGlueSign;
			//     
			leader->Arround = TRUE;
			
			//    -   
			leader->BranchBegin[2] = FALSE;
			
			//    
			ksAPI7::ITextPtr txt( leader->TextOnShelf );
			
			if ( txt )
			{
				txt->Str = _T("");
				
				ksAPI7::ITextLinePtr line( txt->Add() );
				
				if ( line )
				{
					//  
					AddTextItem( line, _T("1"), ksTItString );
					//   
					AddTextItem( line, _T("1"), ksTItNumerator );
					//   
					AddTextItem( line, _T("1"), ksTItDenominator );
					//  
					AddTextItem( line, _T("1"), ksTItFractionEnd );
				}
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
void LeaderAddBranch( ksAPI7::IBaseLeader3DPtr & leader, 
											ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( (bool)leader && (bool)doc )
	{
		//    3D  API7  API5
		IDocument3DPtr doc3D( IUnknownPtr(ksTransferInterface( doc, ksAPI3DCom, 0/* */ ), false/*AddRef*/) );
		
		if ( doc3D )
		{
			//     
			IEntityPtr baseObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_OBJ)), false/*AddRef*/ );
			
			double x, y, z;
			//    
			doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_BEGINBRANCH), &x, &y, &z );
			
			if ( baseObj )
			{
				//     API5  API7
				ksAPI7::IModelObjectPtr mObj( IUnknownPtr(ksTransferInterface(baseObj, ksAPI7Dual, 0), false/*AddRef*/) );
				
				if ( mObj )
				{	
					//   
					ksAPI7::IBranchs3DPtr branchs3D( leader );
					
					//  
					if ( branchs3D )
						branchs3D->AddBranchByPoint( x, y, z, mObj );
				}
			}
		}
	}
}


//-------------------------------------------------------------------------------
//  -
// ---
void EditLeader3D( ksAPI7::IBaseLeader3DPtr & leader, 
									 ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( leader )
	{
		//   
		LeaderAddBranch( leader, doc );
		//   -
		EditLeaderPars( leader );

		//    
		ksAPI7::IFeature7Ptr featObj( leader );
		
		if ( featObj )
			//     
			leader->Name = GetNewName( featObj, IDS_LEADER );
		
		//     
		ksAPI7::IColorParam7Ptr colorPars( leader );
		
		//    
		if ( colorPars )
			colorPars->Color = RGB( 123, 40, 0 );

		//  
		leader->Update();
	}
}


//-------------------------------------------------------------------------------
//    - 3D
// ---
void Leader3DWork( ksAPI7::IKompasDocument3DPtr & doc )
{
	//    3D
	ksAPI7::ISymbols3DContainerPtr symbCont( GetSymbols3DContainer(doc) );
	
	if ( symbCont )
	{
		//   - 3D
		ksAPI7::ILeaders3DPtr leadsCol( symbCont->GetLeaders3D() );
		
		if ( leadsCol )
		{
			//   - 3D
			ksAPI7::IBaseLeader3DPtr newLeader( leadsCol->Add(o3d_leader3D) );
			
			if ( newLeader )
			{
				//  - 3D
				if ( CreateLeader3D(newLeader, doc) )
				{	
					_bstr_t name = _T("");
					//     
					ksAPI7::IFeature7Ptr feature( newLeader );
					
					//   
					if ( feature )
						name = feature->Name;
					
					if ( YesNoT((LPTSTR)(LPCTSTR)LoadStr(IDS_EDIT)) == 1 )
					{
						//      
						ksAPI7::IBaseLeader3DPtr leader( leadsCol->GetLeader3D(name) );
						//  -
						EditLeader3D( leader, doc );
					}
				}
				else
					//   "  "
					MessageT( (LPTSTR)(LPCTSTR)LoadStr(IDS_NOCREATE) );
			}
		}
	}
}


//-------------------------------------------------------------------------------
//    3D
// ---
bool CreateBrandLeader3D( ksAPI7::IBaseLeader3DPtr & baseLeader,
													ksAPI7::IKompasDocument3DPtr & doc )
{
	bool res = false;
	
	if ( (bool)baseLeader && (bool)doc )
	{
		//  
		CreateBranchsLeader( baseLeader, doc );

		//    
		ksAPI7::IBrandLeaderPtr brandLeader( baseLeader );

		if ( brandLeader )
			// 
			brandLeader->Direction = FALSE;
			
		//  
		baseLeader->Update();
		res = true;
	}
	return res;
}
 

//-------------------------------------------------------------------------------
//   
// ---
void EditBrandLeader3D( ksAPI7::IBaseLeader3DPtr & leader, 
												ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( (bool)leader && (bool)doc )
	{
		//   
		LeaderAddBranch( leader, doc );

		//    
		ksAPI7::IBrandLeaderPtr brandLeader( leader );
		
		if ( brandLeader )
		{
			//    
			ksAPI7::ITextPtr des( brandLeader->Designation );
			
			if ( des )
				//  
				des->Str = (LPTSTR)(LPCTSTR)LoadStr( IDS_MARK2 );
		}

		//    
		ksAPI7::IFeature7Ptr featObj( leader );
		
		if ( featObj )
			//     
			leader->Name = GetNewName( featObj, IDS_BRAND );
		
		//     
		ksAPI7::IColorParam7Ptr colorPars( leader );
		
		//    
		if ( colorPars )
			colorPars->Color = RGB( 128, 0, 128 );

		//  
		leader->Update();
	}
}


//-------------------------------------------------------------------------------
//     
// ---
void BrandLeader3DWork( ksAPI7::IKompasDocument3DPtr & doc )
{
	//    3D
	ksAPI7::ISymbols3DContainerPtr symbCont( GetSymbols3DContainer(doc) );
	
	if ( symbCont )
	{
		//   - 3D
		ksAPI7::ILeaders3DPtr leadsCol( symbCont->GetLeaders3D() );
		
		if ( leadsCol )
		{
			//     3D
			ksAPI7::IBaseLeader3DPtr newBrand( leadsCol->Add(o3d_brandLeader3D) );
			
			if ( newBrand )
			{
				//    3D
				if ( CreateBrandLeader3D(newBrand, doc) )
				{	
					_bstr_t name = _T("");
					//     
					ksAPI7::IFeature7Ptr feature( newBrand );
					
					//   
					if ( feature )
						name = feature->Name;
					
					if ( YesNoT((LPTSTR)(LPCTSTR)LoadStr(IDS_EDIT)) == 1 )
					{
						//      
						ksAPI7::IBaseLeader3DPtr brandLeader( leadsCol->GetLeader3D(name) );
						//   
						EditBrandLeader3D( brandLeader, doc );
					}
				}
				else
					//   "  "
					MessageT( (LPTSTR)(LPCTSTR)LoadStr(IDS_NOCREATE) );
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
bool CreateMarkLeader3D( ksAPI7::IBaseLeader3DPtr & baseLeader,
												 ksAPI7::IKompasDocument3DPtr & doc )
{
	bool res = false;

	if ( (bool)baseLeader && (bool)doc )
	{
		//  
		CreateBranchsLeader( baseLeader, doc );

		//    
		ksAPI7::IMarkLeaderPtr markLeader( baseLeader );

		if ( markLeader )
		{	
			//     
			ksAPI7::ITextPtr textOnBranch( markLeader->TextOnBranch );
			
			if ( textOnBranch )
				//  
				textOnBranch->Str = _T("2");
			
			//     
			ksAPI7::ITextPtr textUnderBranch( markLeader->TextUnderBranch );
			
			if ( textUnderBranch )
				//  
				textUnderBranch->Str = _T("3");
		}
		//  
		baseLeader->Update();
		res = true;
	}
	return res;
}


//-------------------------------------------------------------------------------
//   
// ---
void EditMarkLeader3D( ksAPI7::IBaseLeader3DPtr & baseLeader,
											 ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( (bool)baseLeader && (bool)doc )
	{
		//   
		LeaderAddBranch( baseLeader, doc );

		//    
		ksAPI7::IMarkLeaderPtr markLeader( baseLeader );
		
		if ( markLeader )
		{
			//    
			ksAPI7::ITextPtr des( markLeader->Designation );
			
			if ( des )
				//  
				des->Str = _T("10");
		}

		//    
		ksAPI7::IFeature7Ptr featObj( baseLeader );
		
		if ( featObj )
			//     
			baseLeader->Name = GetNewName( featObj, IDS_MARK );
		
		//     
		ksAPI7::IColorParam7Ptr colorPars( baseLeader );
		
		//    
		if ( colorPars )
			colorPars->Color = RGB( 204, 153, 255 );
		
		//  
		baseLeader->Update();
	}
}


//-------------------------------------------------------------------------------
//      3D
// ---
void MarkLeader3DWork( ksAPI7::IKompasDocument3DPtr & doc )
{
	//    3D
	ksAPI7::ISymbols3DContainerPtr symbCont( GetSymbols3DContainer(doc) );
	
	if ( symbCont )
	{
		//   - 3D
		ksAPI7::ILeaders3DPtr leadsCol( symbCont->GetLeaders3D() );
		
		if ( leadsCol )
		{
			//     3D
			ksAPI7::IBaseLeader3DPtr newMark( leadsCol->Add(o3d_markLeader3D) );
			
			if ( newMark )
			{
				//    3D
				if ( CreateMarkLeader3D(newMark, doc) )
				{	
					_bstr_t name = _T("");
					//     
					ksAPI7::IFeature7Ptr feature( newMark );
					
					//   
					if ( feature )
						name = feature->Name;
					
					if ( YesNoT((LPTSTR)(LPCTSTR)LoadStr(IDS_EDIT)) == 1 )
					{
						//      
						ksAPI7::IBaseLeader3DPtr markLeader( leadsCol->GetLeader3D(name) );
						//   
						EditMarkLeader3D( markLeader, doc );
					}
				}
				else
					//   "  "
					MessageT( (LPTSTR)(LPCTSTR)LoadStr(IDS_NOCREATE) );
			}
		}
	}
}


//-------------------------------------------------------------------------------
//     
// ---
void CreateToleranceBranch( ksAPI7::ITolerance3DPtr & tolerance, 
														ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( (bool)tolerance && (bool)doc )
	{
		//    3D  API7  API5
		IDocument3DPtr doc3D( IUnknownPtr(ksTransferInterface( doc, ksAPI3DCom, 0/* */ ), false/*AddRef*/) );
		
		if ( doc3D )
		{
			//    ,     
			IEntityPtr baseObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_OBJ)), false/*AddRef*/ );
			
			double x, y, z, x1, y1, z1;
			//    
			//      
			doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_ENDPOINT), &x, &y, &z );
			
			//  ,   
			IEntityPtr posObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_POSOBJ)), false/*AddRef*/ );
			
			if ( (bool)baseObj && (bool)posObj )
			{
				//    
				doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_TABLE), &x1, &y1, &z1 );
				//     API5  API7
				ksAPI7::IModelObjectPtr mObj1( IUnknownPtr(ksTransferInterface(baseObj, ksAPI7Dual, 0), false/*AddRef*/) );
				ksAPI7::IModelObjectPtr mObj2( IUnknownPtr(ksTransferInterface(posObj, ksAPI7Dual, 0), false/*AddRef*/) );
				
				if ( (bool)mObj1 && (bool)mObj2 )
				{	
					//   
					ksAPI7::IBranchs3DPtr branchs3D( tolerance );
					
					//  
					if ( branchs3D )
						branchs3D->AddBranchByPoint( x, y, z, mObj1 );
					
					//  ,   
					tolerance->PositionObject = mObj2;
					//  ,   
					tolerance->SetPosition( x1, y1, z1 );
				}
			}
		}
	}
}


//-------------------------------------------------------------------------------
//      
// ---
void SetToleranceText( ksAPI7::IToleranceParamPtr & tolPar )
{
	if ( tolPar )
	{
		//       
		ksAPI7::ITablePtr tolTable( tolPar->Table );
		
		if ( tolTable )
		{
			//  3  (1  )
			tolTable->AddColumn( -1, TRUE/**/ );
			tolTable->AddColumn( -1, TRUE/**/ );
			tolTable->AddColumn( -1, TRUE/**/ );
			
			//    1- 
			ksAPI7::ITableCellPtr cell( tolTable->GetCell(0, 0) );
			ksAPI7::ITextLinePtr txt = NULL;
			
			if ( cell )
			{
				txt = cell->Text;
				
				if ( txt )
					txt->Str = _T("@22~");
			}
			
			//    2- 
			cell = tolTable->GetCell( 0, 1 );
			
			if ( cell )
			{
				txt = cell->Text;
				
				if ( txt )
					txt->Str = "@2~";
			}
			
			//    3- 
			cell = tolTable->GetCell( 0, 2 );
			
			if ( cell )
			{
				txt = cell->Text;
				
				if ( txt )
					txt->Str = "B";
			}
			
			//    4- 
			cell = tolTable->GetCell( 0, 3 );
			
			if ( cell )
			{
				txt = cell->Text;
				
				if ( txt )
					txt->Str = "@30~";
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
bool CreateTolerance3D( ksAPI7::ITolerance3DPtr & tolerance, 
											  ksAPI7::IKompasDocument3DPtr & doc )
{
	bool res = false;

	if ( (bool)tolerance && (bool)doc )
	{
		//  
		CreateToleranceBranch( tolerance, doc );
		//     
		ksAPI7::IToleranceParamPtr tolPar( tolerance );
		
		if ( tolPar )
		{
			//    
			SetToleranceText( tolPar );
			//      -  
			tolPar->BasePointPos = ksTPBottomCenter;
		}
		//   1-  - 
		tolerance->PutArrowType( 0, FALSE );
		//  
		tolerance->Update();
		res = true;
	}
	return res;
}


//-------------------------------------------------------------------------------
//     
// ---
void ToleranceAddBranch( ksAPI7::ITolerance3DPtr & tolerance, 
												 ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( (bool)tolerance && (bool)doc )
	{
		//    3D  API7  API5
		IDocument3DPtr doc3D( IUnknownPtr(ksTransferInterface( doc, ksAPI3DCom, 0/* */ ), false/*AddRef*/) );
		
		if ( doc3D )
		{
			//    ,      -
			IEntityPtr baseObj( doc3D->UserSelectEntity(NULL, NULL, (LPTSTR)(LPCTSTR)LoadStr(IDS_OBJ)), false/*AddRef*/ );
			
			double x, y, z;
			//    
      //      
			doc3D->UserGetCursor( (LPTSTR)(LPCTSTR)LoadStr(IDS_ENDPOINT), &x, &y, &z );
			
			if ( baseObj )
			{
				//     API5  API7
				ksAPI7::IModelObjectPtr mObj( IUnknownPtr(ksTransferInterface(baseObj, ksAPI7Dual, 0), false/*AddRef*/) );
				
				if ( mObj )
				{	
					//   
					ksAPI7::IBranchs3DPtr branchs3D( tolerance );
					
					//  
					if ( branchs3D )
						branchs3D->AddBranchByPoint( x, y, z, mObj );

					//   
					tolerance->PutArrowType( 1, TRUE );
				}
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
void EditTolerance3D( ksAPI7::ITolerance3DPtr & tolerance, 
											ksAPI7::IKompasDocument3DPtr & doc )
{
	if ( (bool)tolerance && (bool)doc )
	{
		ToleranceAddBranch( tolerance, doc );

		//    
		ksAPI7::IToleranceParamPtr tolPar( tolerance );

		if ( tolPar )
		{
			//   
			tolPar->Vertical = TRUE;
			//      -  
			tolPar->BasePointPos = ksTPLeftBottom;
		}

		//    
		ksAPI7::IFeature7Ptr featObj( tolerance );
		
		if ( featObj )
			//     
			tolerance->Name = GetNewName( featObj, IDS_TOLERANCE );
		
		//     
		ksAPI7::IColorParam7Ptr colorPars( tolerance );
		
		//    -
		if ( colorPars )
			colorPars->Color = RGB( 186, 12, 34 );

		//  
		tolerance->Update();
	}
}


//-------------------------------------------------------------------------------
//      3D
// ---
void Tolerance3DWork( ksAPI7::IKompasDocument3DPtr & doc )
{
	//    3D
	ksAPI7::ISymbols3DContainerPtr symbCont( GetSymbols3DContainer(doc) );
	
	if ( symbCont )
	{
		//     3D
		ksAPI7::ITolerances3DPtr tolerCol( symbCont->GetTolerances3D() );
		
		if ( tolerCol )
		{
			//     3D
			ksAPI7::ITolerance3DPtr newTolerance( tolerCol->Add() );
			
			if ( newTolerance )
			{
				//    3D
				if ( CreateTolerance3D(newTolerance, doc) )
				{	
					_bstr_t name = _T("");
					//     
					ksAPI7::IFeature7Ptr feature( newTolerance );
					
					//   
					if ( feature )
						name = feature->Name;
					
					if ( YesNoT((LPTSTR)(LPCTSTR)LoadStr(IDS_EDIT)) == 1 )
					{
						//      
						ksAPI7::ITolerance3DPtr tolerance( tolerCol->GetTolerance3D(name) );
						//   
						EditTolerance3D( tolerance, doc );
					}
				}
				else
					//   "  "
					MessageT( (LPTSTR)(LPCTSTR)LoadStr(IDS_NOCREATE) );
			}
		}
	}
}

#pragma warning( once : 4786 ) 
//-------------------------------------------------------------------------------
//  
// ---
void OnProcessDetach()
{
  newKompasAPI = NULL;
}


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


