// step3d3.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "step3d3.h"
#include <ldefin2d.h>
#include <ldefin3d.h>

#ifdef __LIGHT_VERSION__
#include <klAPI5.h>
#else
#include <kAPI5.h>
#endif

#include <ksConstants.h>

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

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CStep3d3App

BEGIN_MESSAGE_MAP(CStep3d3App, CWinApp)
	//{{AFX_MSG_MAP(CStep3d3App)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStep3d3App construction

CStep3d3App::CStep3d3App()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CStep3d3App object

CStep3d3App theApp;

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

KompasObject kompas( NULL );

char buf[MAX_TEXT_LENGTH];


void ConstrAxisOperations( ksDocument3D& doc );
void ConstrAxis2Point( ksDocument3D& doc );
void ConstrAxisEdge( ksDocument3D& doc );
void ConstrPlane3Point( ksDocument3D& doc );
void CreateConstrElem( ksDocument3D& doc );
/*
void ConstrAxisConeface( ksDocument3D& doc );
void GetSetEntity( ksDocument3D& doc );
void CreateSkatch( ksDocument3D& doc );
void GetArraySketch( ksDocument3D& doc );
void GetSetUserParamComponent( ksDocument3D& doc );
void GetSetArrayMate( ksDocument3D& doc );
*/

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

      #ifdef __LIGHT_VERSION__
        libname.LoadString( IDS_STRING5 );  //klAPI5.dll
      #else
        libname.LoadString( IDS_STRING4 );  //kAPI5.dll
      #endif
      
			filename.Replace( filename.Right(filename.GetLength() - (filename.ReverseFind('\\') + 1)), 
												libname );

			HINSTANCE hAppAuto = LoadLibrary( filename ); //  kAPI5.dll
			if(  hAppAuto ) {
				typedef LPDISPATCH ( WINAPI *FCreateKompasObject )(); 
				FCreateKompasObject pCreateKompasObject = 
					(FCreateKompasObject)GetProcAddress( hAppAuto, "CreateKompasObject" );	
				if ( pCreateKompasObject ) 
					kompas = pCreateKompasObject();
				FreeLibrary( hAppAuto );
			}
		}
	}
}

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

	GetKompas();
	if ( kompas.m_lpDispatch ) {
		ksDocument3D doc( kompas.ActiveDocument3D() );
		if ( doc.m_lpDispatch  ) {
			switch ( comm ) {
        case 1  : ::ConstrAxisOperations( doc ); break; //   
				case 2  : ::ConstrAxis2Point( doc );  	 break; //     
				case 3  : ::ConstrAxisEdge( doc );	     break; //  ,   
//				case 4  : ::ConstrAxisConeface( doc );   break; //    
				case 5  : ::CreateConstrElem( doc );     break; //   ,          
				case 6  : ::ConstrPlane3Point( doc );    break; //    
/*				case 8  : ::GetArraySketch( doc );           break; //   ( )     ksEntityCollection ( IEntityCollection )
				case 9  : ::GetSetUserParamComponent( doc ); break; //       
				case 10 : ::GetSetArrayMate( doc );		       break; //    (ksEntityCollection)    .mates*/
			}
		}
	}
}


//-------------------------------------------------------------------------------
//   
// ---
void ConstrAxisOperations( ksDocument3D& doc ) {
	ksPart part( doc.GetPart(pTop_Part) ); //  
	if ( part.m_lpDispatch ) {
    ksEntity entitySketch( part.NewEntity(o3d_sketch) );
		if ( entitySketch.m_lpDispatch ) {
			//   
			ksSketchDefinition sketchDef( entitySketch.GetDefinition() );
			if ( sketchDef.m_lpDispatch ) {
				//     XOY
				ksEntity basePlane( part.GetDefaultEntity(o3d_planeXOY) );
				sketchDef.SetPlane( basePlane ); //   XOY   
				entitySketch.Create();           //  

				//   
				ksDocument2D sketchEdit( sketchDef.BeginEdit() );
          sketchEdit.ksCircle( 20, 0, 10, 1 );
					sketchEdit.ksLineSeg( 0, 0, 0, 5, 3 );
				sketchDef.EndEdit();                   //   

				ksEntity entityRotate( part.NewEntity(o3d_baseRotated) );
        bool res = false;
				if ( entityRotate.m_lpDispatch ) {
					ksBaseRotatedDefinition rotateDef( entityRotate.GetDefinition() ); //    
					if ( rotateDef.m_lpDispatch ) {
     				ksRotatedParam rotproperty( rotateDef.RotatedParam() );
						if ( rotproperty.m_lpDispatch ) {
							rotproperty.SetDirection( dtBoth );
              rotproperty.SetToroidShape(false);
						}

//					rotateDef.SetTorShapeType(false);
//					rotateDef.SetDirectionType( dtNormal );       //  
						rotateDef.SetThinParam( true, dtBoth, 1, 1 ); //     
						rotateDef.SetSideParam( true, 180 );
            rotateDef.SetSideParam( false, 180 );
						rotateDef.SetSketch( entitySketch );          //   
						res = !!entityRotate.Create();                        //  
					}
        }
        if ( res ) {
          //     
          ksEntity entityAxisOperation( part.NewEntity(o3d_axisOperation) );
          if ( entityAxisOperation.m_lpDispatch ) {
            ksAxisOperationsDefinition axisOperation( entityAxisOperation.GetDefinition() );
            if ( axisOperation.m_lpDispatch ) {
              axisOperation.SetOperation(entityRotate);
              entityAxisOperation.Create();                        //  
            }
          }
          kompas.ksMessage( " " );
        }
        else
          kompas.ksMessage( "  " );
      }
		}
	}
}

//-------------------------------------------------------------------------------
//     
// ---
void ConstrAxis2Point( ksDocument3D& doc ) {
	ksPart part( doc.GetPart(pTop_Part) ); //  
	if ( part.m_lpDispatch ) {
		ksEntity entitySketch( part.NewEntity(o3d_sketch) );
		if ( entitySketch.m_lpDispatch ) {
			//   
			ksSketchDefinition sketchDef( entitySketch.GetDefinition() );
			if ( sketchDef.m_lpDispatch ) {
				//     XOY
				ksEntity basePlane( part.GetDefaultEntity(o3d_planeXOY) );
				sketchDef.SetPlane( basePlane ); //   XOY   
	//  				sketchDef.SetAngle( 45 );        //   
				entitySketch.Create();           //  

				//   
				ksDocument2D sketchEdit( sketchDef.BeginEdit() ); 
					//    - 
					sketchEdit.ksLineSeg(  50,  50, -50,  50, 1 );
					sketchEdit.ksLineSeg(  50, -50, -50, -50, 1 ); 
					sketchEdit.ksLineSeg(  50, -50,  50,  50, 1 );
					sketchEdit.ksLineSeg( -50, -50, -50,  50, 1 );
				sketchDef.EndEdit();                   //                   
				
				ksEntity entityExtr( part.NewEntity(o3d_baseExtrusion) );
				if ( entityExtr.m_lpDispatch ) {
					//     
					ksBaseExtrusionDefinition extrusionDef( entityExtr.GetDefinition() ); //    
					if ( extrusionDef.m_lpDispatch ) {
						extrusionDef.SetDirectionType( dtNormal );         //  
						extrusionDef.SetSideParam( true/* */, etBlind/*  */, 20, 0, false ); 
						extrusionDef.SetThinParam( true, dtBoth, 20, 20 ); //     
						extrusionDef.SetSketch( entitySketch ); //   
						entityExtr.Create();                    //  
					}
				}
			}
		}

		ksEntityCollection entityCollection( part.EntityCollection( o3d_vertex ) );

		if ( entityCollection.m_lpDispatch && entityCollection.GetCount() ) {
			//     
			ksEntity entityAxis2Point( part.NewEntity(o3d_axis2Points) ); 
			if ( entityAxis2Point.m_lpDispatch ) {
				ksAxis2PointsDefinition axis2Point( entityAxis2Point.GetDefinition() );
				if ( axis2Point.m_lpDispatch ) {
					axis2Point.SetPoint( 1, entityCollection.GetByIndex(0) );
					axis2Point.SetPoint( 2, entityCollection.GetByIndex(entityCollection.GetCount() - 1) );
					entityAxis2Point.Create();
				}
			}
			kompas.ksMessage( "   " );
		}
	}
}

//-------------------------------------------------------------------------------
//  ,   
// ---
void ConstrAxisEdge( ksDocument3D& doc ) {
	ksPart part( doc.GetPart(pTop_Part) ); //  
	if ( part.m_lpDispatch ) {
		ksEntity entitySketch( part.NewEntity(o3d_sketch) );
		if ( entitySketch.m_lpDispatch ) {
			//   
			ksSketchDefinition sketchDef( entitySketch.GetDefinition() );
			if ( sketchDef.m_lpDispatch ) {
				//     XOY
				ksEntity basePlane( part.GetDefaultEntity(o3d_planeXOY) );
				sketchDef.SetPlane( basePlane ); //   XOY   
	//  				sketchDef.SetAngle( 45 );        //   
				entitySketch.Create();           //  

				//   
				ksDocument2D sketchEdit( sketchDef.BeginEdit() ); 
					//    - 
					sketchEdit.ksLineSeg(  50,  50, -50,  50, 1 );
					sketchEdit.ksLineSeg(  50, -50, -50, -50, 1 ); 

					sketchEdit.ksLineSeg(  50, -50,  50,  50, 1 );
					sketchEdit.ksLineSeg( -50, -50, -50,  50, 1 );
				sketchDef.EndEdit();                   //                   
				
				ksEntity entityExtr( part.NewEntity(o3d_baseExtrusion) );
				if ( entityExtr.m_lpDispatch ) {
					//     
					ksBaseExtrusionDefinition extrusionDef( entityExtr.GetDefinition() ); //    
					if ( extrusionDef.m_lpDispatch ) {
						extrusionDef.SetDirectionType( dtNormal );         //  
						extrusionDef.SetSideParam( true/* */, etBlind/*  */, 20, 0, false ); 
						extrusionDef.SetThinParam( true, dtBoth, 20, 20 ); //     
						extrusionDef.SetSketch( entitySketch ); //   
						entityExtr.Create();                    //  
					}
				}
			}
		}

		ksEntityCollection entityCollection( part.EntityCollection( o3d_edge ) );

		if ( entityCollection.m_lpDispatch && entityCollection.GetCount() > 1 ) {
			//    
			ksEntity entityAxisEdge( part.NewEntity(o3d_axisEdge) ); 
			if ( entityAxisEdge.m_lpDispatch ) {
				ksAxisEdgeDefinition axisEdge( entityAxisEdge.GetDefinition() );
				if ( axisEdge.m_lpDispatch ) {
					axisEdge.SetEdge( entityCollection.GetByIndex(0) );
					entityAxisEdge.Create();
				}
			}
			kompas.ksMessage( "  " );

			//     
			ksEntity entityAxisEdge2( part.NewEntity(o3d_axisEdge) ); 
			if ( entityAxisEdge2.m_lpDispatch ) {
				ksAxisEdgeDefinition axisEdge( entityAxisEdge2.GetDefinition() );
				if ( axisEdge.m_lpDispatch ) {
					axisEdge.SetEdge( entityCollection.GetByIndex(1) );
					entityAxisEdge2.Create();
				}
			}
			kompas.ksMessage( "   " );
		}
	}
}

/*
//-------------------------------------------------------------------------------
//    
// ---
void ConstrAxisConeface( ksDocument3D& doc ) {
	ksPart part( doc.GetPart(pNew_Part) ); //  
	if ( part.m_lpDispatch ) {
		ksEntity entitySketch( part.NewEntity(o3d_sketch) );
		if ( entitySketch.m_lpDispatch ) {
			//   
			ksSketchDefinition sketchDef( entitySketch.GetDefinition() );
			if ( sketchDef.m_lpDispatch ) {
				//     XOY
				ksEntity basePlane( part.GetDefaultEntity(o3d_planeXOY) );
				sketchDef.SetPlane( basePlane ); //   XOY   
				entitySketch.Create();           //  

				//   
				ksDocument2D sketchEdit( sketchDef.BeginEdit() ); 
				  sketchEdit.ksCircle( 0, 0, 50, 1 );
				sketchDef.EndEdit();                   //                   
				
				ksEntity entityExtr( part.NewEntity(o3d_baseExtrusion) );
				if ( entityExtr.m_lpDispatch ) {
					//     
					ksBaseExtrusionDefinition extrusionDef( entityExtr.GetDefinition() ); //    
					if ( extrusionDef.m_lpDispatch ) {
						extrusionDef.SetDirectionType( dtNormal );         //  
						extrusionDef.SetSideParam( true, etBlind, 20, 30, false, false ); 
						extrusionDef.SetThinParam( true, dtBoth, 10, 10 ); //     
						extrusionDef.SetSketch( entitySketch ); //   
						entityExtr.Create();                    //  
					}
				}
			}
		}

		ksEntityCollection entityCollection( part.EntityCollection( o3d_face ) );
		ksEntity entityConFace;

		for ( int i = 0, count = entityCollection.GetCount(); i < count; i++ ) {
			entityConFace.AttachDispatch( entityCollection.GetByIndex(i) );
			ksFaceDefinition face( entityConFace.GetDefinition() );
			if ( face.IsCone() ) //    ,    
//  			entityConFace.DetachDispatch();
//			else
				break;
    }


		//     
		ksEntity entityAxisConFace( part.NewEntity(o3d_axisConeFace) ); 
		if ( entityAxisConFace.m_lpDispatch ) {
			ksConstrAxisConefaceDefinition axisConFace( entityAxisConFace.GetDefinition() );
			if ( axisConFace.m_lpDispatch ) {
				axisConFace.SetFace( entityConFace );
				entityAxisConFace.Create();
			}
		}
		entityConFace.DetachDispatch();
		kompas.ksMessage( "   " );
	}
}
*/
//-------------------------------------------------------------------------------
//    
// ---
void ConstrPlane3Point( ksDocument3D& doc ) {
	ksPart part( doc.GetPart(pTop_Part) ); //  
	if ( part.m_lpDispatch ) {
		ksEntity entitySketch( part.NewEntity(o3d_sketch) );
		if ( entitySketch.m_lpDispatch ) {
			//   
			ksSketchDefinition sketchDef( entitySketch.GetDefinition() );
			if ( sketchDef.m_lpDispatch ) {
				//     XOY
				ksEntity basePlane( part.GetDefaultEntity(o3d_planeXOY) );
				sketchDef.SetPlane( basePlane ); //   XOY   
				entitySketch.Create();           //  

				//   
				ksDocument2D sketchEdit( sketchDef.BeginEdit() ); 
					//    - 
					sketchEdit.ksLineSeg(  50,  50, -50,  50, 1 );
					sketchEdit.ksLineSeg(  50, -50, -50, -50, 1 ); 

					sketchEdit.ksLineSeg(  50, -50,  50,  50, 1 );
					sketchEdit.ksLineSeg( -50, -50, -50,  50, 1 );
				sketchDef.EndEdit();                   //                   
				
				ksEntity entityExtr( part.NewEntity(o3d_baseExtrusion) );
				if ( entityExtr.m_lpDispatch ) {
					//     
					ksBaseExtrusionDefinition extrusionDef( entityExtr.GetDefinition() ); //    
					if ( extrusionDef.m_lpDispatch ) {
						extrusionDef.SetDirectionType( dtNormal );         //  
						extrusionDef.SetSideParam( true/* */, etBlind/*  */, 20, 30, false ); 
						extrusionDef.SetThinParam( true, dtBoth, 10, 10 ); //     
						extrusionDef.SetSketch( entitySketch ); //   
						entityExtr.Create();                    //  
					}
				}
			}
		}

		ksEntityCollection entityCollection( part.EntityCollection( o3d_vertex ) );
    if ( entityCollection.GetCount() > 2 ) {
		  //    
		  ksEntity entityConstrPlane3Point( part.NewEntity(o3d_plane3Points) ); 
		  if ( entityConstrPlane3Point.m_lpDispatch ) {
			  ksPlane3PointsDefinition constrPlane3Point( entityConstrPlane3Point.GetDefinition() );
			  if ( constrPlane3Point.m_lpDispatch ) {
				  constrPlane3Point.SetPoint( 1, entityCollection.GetByIndex(0) );
				  constrPlane3Point.SetPoint( 2, entityCollection.GetByIndex(1) );
				  constrPlane3Point.SetPoint( 3, entityCollection.GetByIndex(2) );
				  entityConstrPlane3Point.Create();
			  }
		  }
  		kompas.ksMessage( "   " );
    }
	}
}


//-------------------------------------------------------------------------------
//   ,          
// ---
void CreateConstrElem( ksDocument3D& doc ) {
	ksPart part( doc.GetPart(pTop_Part) ); //  
	if ( part.m_lpDispatch ) {
		ksEntity entity( part.NewEntity(o3d_planeOffset) );
		if ( entity.m_lpDispatch ) {
			//    
			ksPlaneOffsetDefinition offsetDef( entity.GetDefinition() );
			if ( offsetDef.m_lpDispatch ) {
				offsetDef.SetOffset( 150 ); //    
				ksEntity basePlane( part.GetDefaultEntity(o3d_planeXOY) );
				basePlane.SetName( "XOY" ); //   
				basePlane.Update();         //  
      
				offsetDef.SetPlane( basePlane ); //  
				offsetDef.SetDirection( false );       //     
				entity.SetName( " " ); //    
				entity.Create();                 //    
				
				kompas.ksMessage( "   " );
     
				offsetDef.SetOffset( 50 );       //     
				basePlane.DetachDispatch();      
				//    
				basePlane.AttachDispatch( part.GetDefaultEntity(o3d_planeYOZ) );
				basePlane.SetName( "YOZ" );
		  	basePlane.Update();              //  

				offsetDef.SetDirection( true );        //      
				offsetDef.SetPlane( basePlane ); 
				entity.Update();                 //  

				//    
				basePlane.AttachDispatch( part.GetDefaultEntity(o3d_planeXOY) );
				basePlane.SetName( "XOY" );
				
				kompas.ksMessage( "    " );

				//     
				ksEntity entityAxis( part.NewEntity(o3d_axis2Planes) );
				if ( entityAxis.m_lpDispatch ) {
					ksAxis2PlanesDefinition axis2PlanesDef( entityAxis.GetDefinition() );
					if ( axis2PlanesDef.m_lpDispatch ) {
						axis2PlanesDef.SetPlane( 1, entity );    //   1
						axis2PlanesDef.SetPlane( 2, basePlane ); //   2
						entityAxis.SetName( "   " ); //   
						entityAxis.Create();                   //    

						kompas.ksMessage( "       " );
						
						//    
						basePlane.AttachDispatch( part.GetDefaultEntity(o3d_planeXOZ) );
						basePlane.SetName( "XOZ" );
						
						axis2PlanesDef.SetPlane( 2, basePlane ); //   2
						entityAxis.Update();

						kompas.ksMessage( "      \n     45" );

						ksEntity entityAnglePlane( part.NewEntity(o3d_planeAngle) ); 
						if ( entityAnglePlane.m_lpDispatch )  {
							//        
							ksPlaneAngleDefinition planeAngleDef( entityAnglePlane.GetDefinition() );
							if ( planeAngleDef.m_lpDispatch ) {
								planeAngleDef.SetAngle( 45 );        //     
								planeAngleDef.SetPlane( entity );    //  
								planeAngleDef.SetAxis( entityAxis ); //  
								entityAnglePlane.SetName( "     " );
								entityAnglePlane.Create();           //     

								kompas.ksMessage( "    " );

								planeAngleDef.SetPlane( basePlane ); //  
								entityAnglePlane.Update();           //   
							}
						}
					}
				}
			}     
		}
	}
}
