////////////////////////////////////////////////////////////////////////////////
//
// step3d1.cpp - 
//
// 1.    - CreateExtrusion
// 2.               - OperationRotated
// 3.             - OperationLoft
// 4.       - CreateFilletAndChamfer
// 5.  : , ,  ,  "
//                                  - CreateNextOper
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h>
#include "resource.h"

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


//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE StepDLL = { NULL, NULL };


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

  if ( dwReason == DLL_PROCESS_ATTACH )
  {
    TRACE0( "DLL Initializing!" );
 
    if ( !AfxInitExtensionModule( StepDLL, hInstance ) )
      return 0;

    new CDynLinkLibrary( StepDLL );
  }
  else if ( dwReason == DLL_PROCESS_DETACH )
  {
    TRACE0( "DLL Terminating!" );
    AfxTermExtensionModule( StepDLL );
  }
  return 1;
}

 
//-------------------------------------------------------------------------------
//   
//  (  ,  ,  )   <res\step3d1.rc2> ,   <step3d1.rh>
// ---
unsigned int WINAPI LIBRARYID()
{
  return IDR_LIBID;
}


//-------------------------------------------------------------------------------
//   
// ---
void CreateExtrusion( IPartPtr& part );
void OperationRotated( IPartPtr& part );
void OperationLoft( IPartPtr& part );
void CreateFilletAndChamfer( IPartPtr& part );
void CreateNextOper( IPartPtr& part );


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( UINT comm )
{
  IDocument3DPtr pDocument3d( ksGetActive3dDocument(), false ); //       
  if ( (bool)pDocument3d && pDocument3d->IsDetail() )
  {
    //           ( pNew_Part -  ,
    // pEdit_Part -  , pTop_Part -   )    
    IPartPtr part( pDocument3d->GetPart( pTop_Part ), false );
    if ( part )
    {
      switch ( comm )
      {
        case 1 : CreateExtrusion       ( part ); break; //   
        case 2 : OperationRotated      ( part ); break; //  
        case 3 : OperationLoft         ( part ); break; //   
        case 4 : CreateFilletAndChamfer( part ); break; //    
        case 5 : CreateNextOper        ( part ); break; //  : , ,  ,  
      }
    }
  }
  else
  {
    CString strError;
    strError.LoadString( IDS_3DDOCERROR ); // "      3D-"
    ErrorT( ( LPTSTR )( LPCTSTR )strError ); //    
  }
  if ( ReturnResult() == etError10 )       // "!  "
    ResultNULL();                          //  
}


//-------------------------------------------------------------------------------
//      
// ---
void ClearCurrentSketch()
{
  //         
  reference rIterator = CreateIterator( ALL_OBJ, 0 );
  if ( rIterator )
  {
    reference rObject = MoveIterator( rIterator, 'F' ); //       
    //              
    while ( rObject )
    {
      //     
      if ( ExistObj( rObject ) )
        DeleteObj( rObject );
      //    
      rObject = MoveIterator( rIterator, 'N' );
    }
    DeleteIterator( rIterator ); //  
  }
}


//-------------------------------------------------------------------------------
//  ,   
// ---
void CreateExtrusion( IPartPtr& part )
{
  //   
  IEntityPtr entitySketch( part->NewEntity(o3d_sketch), false /*AddRef*/ );
  if ( entitySketch )
  {
    //        
    ISketchDefinitionPtr sketchDefinition( IUnknownPtr( entitySketch->GetDefinition(), false /*AddRef*/ ) );
    if ( sketchDefinition )
    {
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity(o3d_planeXOY), false /*AddRef*/ );
 
      //   
      sketchDefinition->SetPlane( basePlane ); //   XOY   
      sketchDefinition->SetAngle( 45 );        //   
 
      //  
      entitySketch->Create();

      //     
      if ( sketchDefinition->BeginEdit() )
      {
        //    - 
        LineSeg(  50,  50, -50,  50, 1 );
        LineSeg(  50, -50, -50, -50, 1 );
        LineSeg(  50, -50,  50,  50, 1 );
        LineSeg( -50, -50, -50,  50, 1 );
        //     
        sketchDefinition->EndEdit();
      }

      //  
      IEntityPtr entityExtrusion( part->NewEntity( o3d_baseExtrusion ), false /*AddRef*/ );
      if ( entityExtrusion )
      {
        //    
        IBaseExtrusionDefinitionPtr extrusionDefinition( IUnknownPtr( entityExtrusion->GetDefinition(), false/*AddRef*/ ) );
        if ( extrusionDefinition )
        {
          //    
          extrusionDefinition->SetDirectionType( dtNormal );     //   ( dtNormal	- 
                                                                 // ,    - ,
                                                                 // dtReverse	-  ,    - 
                                                                 // dtBoth -   , dtMiddlePlane    )
          //      
          extrusionDefinition->SetSideParam( true,               //   ( TRUE -  ,
                                                                 // FALSE -   )
                                             etBlind,            //   ( etBlind -   ,
                                                                 // etThroughAll -   , etUpToVertexTo -    ,
                                                                 // etUpToVertexFrom -    , etUpToSurfaceTo - 
                                                                 //   , etUpToSurfaceFrom -    ,
                                                                 // etUpToNearSurface	-    )
                                             200,                //  
                                             0,                  //  
                                             false );            //   ( TRUE -  , FALSE -   )
          //    
          extrusionDefinition->SetThinParam( true,               //   
                                             dtBoth,             //    
                                             10,                 //     
                                             10 );               //     
          extrusionDefinition->SetSketch( entitySketch );        //   
 
          //   
          entityExtrusion->Create();  
          MessageT( _T(" ") );

          //                  
          extrusionDefinition->SetDirectionType( dtBoth );       //   ( dtNormal	- 
                                                                 // ,    - ,
                                                                 // dtReverse	-  ,    - 
                                                                 // dtBoth -   , dtMiddlePlane    )
          //      
          extrusionDefinition->SetSideParam( false,              //   ( TRUE -  ,
                                                                 // FALSE -   )
                                             etBlind,            //   ( etBlind -   ,
                                                                 // etThroughAll -   , etUpToVertexTo -    ,
                                                                 // etUpToVertexFrom -    , etUpToSurfaceTo - 
                                                                 //   , etUpToSurfaceFrom -    ,
                                                                 // etUpToNearSurface	-    )
                                             150,                //  
                                             0,                  //  
                                             false );            //   ( TRUE -  , FALSE -   )
              
          //  
          entityExtrusion->Update(); 
          MessageT( _T("   ") );

          //     
          if ( sketchDefinition->BeginEdit())
          {
            //      
            ClearCurrentSketch();                
            //    
            Circle( 0, 0, 100, 1 );
            //     
            sketchDefinition->EndEdit(); 
          }
              
          //   
          entitySketch->Update();               
          //    
          entityExtrusion->Update();   
          MessageT( _T(" ") );

          //   
          IEntityPtr entitySketch2( part->NewEntity( o3d_sketch ), false /*AddRef*/ );
          if ( entitySketch2 ) 
          {
            //        
            ISketchDefinitionPtr sketchDefinition2( IUnknownPtr( entitySketch2->GetDefinition(), false /*AddRef*/ ) );
            if ( sketchDefinition2 ) 
            {
              //   
              sketchDefinition2->SetPlane( basePlane ); //   XOY   
              sketchDefinition2->SetAngle( 45 );        //    45 .
                
              //  
              entitySketch2->Create(); 

              //     
              if ( sketchDefinition2->BeginEdit() )
              {
                //    - 
                Circle( 0, 0, 150, 1 );
                //     
                sketchDefinition2->EndEdit();
              }

              //  
              IEntityPtr entityBossExtrusion( part->NewEntity( o3d_bossExtrusion ), false /*AddRef*/ );
              if ( entityBossExtrusion ) 
              {
                
                //        
                //     
                IBossExtrusionDefinitionPtr bossExtrusionDefinition( IUnknownPtr( entityBossExtrusion->GetDefinition(), false /*AddRef*/ ) );
                if ( bossExtrusionDefinition ) 
                {
                  IExtrusionParamPtr extrusionParam( bossExtrusionDefinition->ExtrusionParam(), false /*AddRef*/ ); //    
                  IThinParamPtr      thinParam( bossExtrusionDefinition->ThinParam(), false /*AddRef*/ );           //     
                  if ( (bool)extrusionParam && (bool)thinParam ) 
                  {
                    //    
                    bossExtrusionDefinition->SetSketch( entitySketch2 ); //   

                    extrusionParam->SetDirection( dtNormal ); //   ( dtNormal	- 
                                                              // ,    - ,
                                                              // dtReverse	-  ,    - 
                                                              // dtBoth -   , dtMiddlePlane    )
                    extrusionParam->SetTypeNormal( etBlind ); //   ( etBlind -   ,
                                                              // etThroughAll -   , etUpToVertexTo -    ,
                                                              // etUpToVertexFrom -    , etUpToSurfaceTo -  
                                                              //  , etUpToSurfaceFrom -    ,
                                                              // etUpToNearSurface -    )
                    extrusionParam->SetDepthNormal( 100 );    //  
                    thinParam->SetThin( false );              //      ( TRUE	-  , 
                                                              // , FALSE -     )
                    entityBossExtrusion->Create(); //  
                    MessageT( _T(" ") );            
                  }
                }

                //   
                IEntityPtr entitySketch3( part->NewEntity( o3d_sketch ), false /*AddRef*/ );
                if ( entitySketch3 )
                {
                  
                  //        
                  //   
                  ISketchDefinitionPtr sketchDefinition3( IUnknownPtr(entitySketch3->GetDefinition(), false /*AddRef*/ ) );
                  if ( sketchDefinition3 )
                  {
                    //   
                    sketchDefinition3->SetPlane( basePlane ); //   XOY   
                    sketchDefinition3->SetAngle( 45 );        //    45 .
                      
                    //                
                    entitySketch3->Create(); 

                    //     
                    if ( sketchDefinition3->BeginEdit() )
                    {
                      //    - 
                      LineSeg(  50,  50, -50,  50, 1 );
                      LineSeg(  50, -50, -50, -50, 1 );
                      LineSeg(  50, -50,  50,  50, 1 );
                      LineSeg( -50, -50, -50,  50, 1 );
                      //     
                      sketchDefinition3->EndEdit();
                    }
                      
                    //  
                    IEntityPtr entityCutExtrusion( part->NewEntity( o3d_cutExtrusion ), false /*AddRef*/ ); 
                    if ( entityCutExtrusion ) 
                    {
                      
                      //        
                      //     
                      ICutExtrusionDefinitionPtr cutExtrusionDefinition( IUnknownPtr( entityCutExtrusion->GetDefinition(), false /*AddRef*/ ) );
                      if ( cutExtrusionDefinition ) 
                      {
                        //    
                        cutExtrusionDefinition->SetDirectionType( dtReverse );   //   ( dtNormal	- 
                                                                                // ,    - ,
                                                                                // dtReverse	-  ,    - 
                                                                                // dtBoth -   , dtMiddlePlane    )
                        //      
                        cutExtrusionDefinition->SetSideParam( false,            //   ( TRUE -  ,
                                                                                // FALSE -   )
                                                              etBlind,          //   ( etBlind -   ,
                                                                                // etThroughAll -   , etUpToVertexTo -    ,                  
                                                                                // etUpToVertexFrom -    , etUpToSurfaceTo - 
                                                                                //   , etUpToSurfaceFrom -    ,
                                                                                // etUpToNearSurface	-    )
                                                              50,               //  
                                                              0,                //  
                                                              false );          //   ( TRUE -  , FALSE -   )
                        //    
                        cutExtrusionDefinition->SetThinParam( false,                 //   
                                                              0,                     //    
                                                              0,                     //     
                                                              0 );                   //     
                        cutExtrusionDefinition->SetSketch( entitySketch3 );          //   

                        //  
                        entityCutExtrusion->Create();
                        MessageT( _T(" ") );
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}


//-------------------------------------------------------------------------------
// C   
// ---
void CreateFilletAndChamfer( IPartPtr& part ) 
{
  //   
  IEntityPtr entitySketch( part->NewEntity(o3d_sketch), false /*AddRef*/ );
  if ( entitySketch )
  {
    //        
    //   
     
    ISketchDefinitionPtr sketchDefinition( IUnknownPtr( entitySketch->GetDefinition(), false /*AddRef*/ ) );
    if ( sketchDefinition )
    {
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );
 
      //   
      sketchDefinition->SetPlane( basePlane ); //   XOY   
      sketchDefinition->SetAngle( 45 );        //   
 
      //  
      entitySketch->Create();

      //     
      if ( sketchDefinition->BeginEdit() )
      {
        //    - 
        LineSeg(  50,  50, -50,  50, 1 );
        LineSeg(  50, -50, -50, -50, 1 );
        LineSeg(  50, -50,  50,  50, 1 );
        LineSeg( -50, -50, -50,  50, 1 );
        //     
        sketchDefinition->EndEdit();
      }
        
      //  
      IEntityPtr entityExtrusion( part->NewEntity( o3d_baseExtrusion ), false /*AddRef*/ );
      if ( entityExtrusion )
      {
        
        //    
        IBaseExtrusionDefinitionPtr extrusionDefinition( IUnknownPtr( entityExtrusion->GetDefinition(), false /*AddRef*/ ) );
        if ( extrusionDefinition )
        {
          //    
          extrusionDefinition->SetDirectionType( dtNormal );     //   ( dtNormal	- 
                                                                 // ,    - ,
                                                                 // dtReverse	-  ,    - 
                                                                 // dtBoth -   , dtMiddlePlane    )
          //      
          extrusionDefinition->SetSideParam( true,               //   ( TRUE -  ,
                                                                 // FALSE -   )
                                             etBlind,            //   ( etBlind -   ,
                                                                 // etThroughAll -   , etUpToVertexTo -    ,
                                                                 // etUpToVertexFrom -    , etUpToSurfaceTo - 
                                                                 //   , etUpToSurfaceFrom -    ,
                                                                 // etUpToNearSurface	-    )
                                             100,                //  
                                             0,                  //  
                                             false );            //   ( TRUE -  , FALSE -   )
          //    
          extrusionDefinition->SetThinParam( false,              //   
                                             0,                  //    
                                             0,                  //     
                                             0 );                //     
          extrusionDefinition->SetSketch( entitySketch );        //   
 
          //   
          entityExtrusion->Create();  
              
          //          -          
          IEntityCollectionPtr entityCollection( part->EntityCollection( o3d_face ), false /*AddRef*/ );
          if ( (bool)entityCollection && entityCollection->SelectByPoint( 0, 0, 0 ) && //   
               entityCollection->GetCount() )                //     
          {
            //  
            IEntityPtr entityFillet( part->NewEntity( o3d_fillet ), false /*AddRef*/ );
            if ( entityFillet ) 
            {
              
              //        
              IFilletDefinitionPtr filletDefinition( IUnknownPtr( entityFillet->GetDefinition(), false /*AddRef*/ ) );
              if ( filletDefinition ) 
              {
                //   
                filletDefinition->SetRadius( 10 );      //  
                filletDefinition->SetTangent( false );  //   
                IEntityCollectionPtr arrayFilletDefinition( filletDefinition->Array(), false /*AddRef*/ ); //     
                if ( arrayFilletDefinition ) 
                {
                  //     
                  for ( int i = 0, count = entityCollection->GetCount(); i < count; i++ ) {
                    IEntityPtr ent( entityCollection->GetByIndex(i), false/*AddRef*/ ); 
                    arrayFilletDefinition->Add( ent );
                  }
                  //   
                  entityFillet->Create();
                  MessageT( _T("") );
                }
              }
            }

            //          -           
            IEntityCollectionPtr entityCollection2( part->EntityCollection( o3d_face ), false /*AddRef*/ );
            if ( (bool)entityCollection2 && 
                       entityCollection2->SelectByPoint( 0, 0, 100 ) && //   
                       entityCollection2->GetCount() )                  //     
            {
              //  
              IEntityPtr entityChamfer( part->NewEntity( o3d_chamfer ), false /*AddRef*/ );
              if ( entityChamfer ) 
              {
                 
                //        
                IChamferDefinitionPtr chamferDefinition( IUnknownPtr( entityChamfer->GetDefinition(), false /*AddRef*/ ) );
                if ( chamferDefinition ) 
                {
                   //   
                  chamferDefinition->SetTangent( false );   //   
                  chamferDefinition->SetChamferParam( true, //   
                                                      10,   //     
                                                      10 ); //    
                  IEntityCollectionPtr arrayChamferDefinition( chamferDefinition->Array(), false /*AddRef*/ ); //     
                  if ( arrayChamferDefinition ) 
                  {
                    //     
                    for ( int i = 0, count = entityCollection2->GetCount(); i < count; i++ ) {
                      IEntityPtr ePtr ( entityCollection2->GetByIndex( i ), false/*AddRef*/ );
                      arrayChamferDefinition->Add( ePtr );
                    }
                        //  
                    entityChamfer->Create();
                    MessageT( _T("") );
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

//-------------------------------------------------------------------------------
//  
// ---
void OperationRotated( IPartPtr & part ) 
{
  //   
  IEntityPtr entitySketch( part->NewEntity(o3d_sketch), false/*AddRef*/ );
  if ( entitySketch )
  {
    //        
    //   
    ISketchDefinitionPtr sketchDefinition( IUnknownPtr( entitySketch->GetDefinition(), false /*AddRef*/ ) );
    if ( sketchDefinition )
    {
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );
 
      //   
      sketchDefinition->SetPlane( basePlane ); //   XOY   
 
      //  
      entitySketch->Create();

      //     
      if ( sketchDefinition->BeginEdit() )
      {
        //   
        ArcByAngle( 0, 0, 20, -90, 90, 1, 1 );
        LineSeg( 0, -20, 0, 20, 3 );
        //     
        sketchDefinition->EndEdit();
      }
      //  
      IEntityPtr entityRotate( part->NewEntity( o3d_baseRotated ), false/*AddRef*/ );
      if ( entityRotate ) 
      {
        //        
        //    
        IBaseRotatedDefinitionPtr baseRotatedDefinition( IUnknownPtr( entityRotate->GetDefinition(), false/*AddRef*/ ) ); 
        if ( baseRotatedDefinition ) 
        {
          baseRotatedDefinition->SetToroidShapeType( false );      //   ( TRUE - , FALSE -  )
          baseRotatedDefinition->SetDirectionType( dtBoth );       //   ( dtNormal -  ,    - 
                                                                   // dtReverse -  ,    - , dtBoth -   ,
                                                                   // dtMiddlePlane -    )
          //    
          baseRotatedDefinition->SetThinParam( true,               //   
                                               dtBoth,             //    
                                               1,                  //     
                                               1 );                //     
          //      
          baseRotatedDefinition->SetSideParam( true,               //   ( TRUE - , FALSE -  )
                                               180 );              //  
          //      
          baseRotatedDefinition->SetSideParam( false,              //   ( TRUE - , FALSE -  )
                                               180 );              //  
          baseRotatedDefinition->SetSketch( entitySketch );        //                                                                    
          //                
          entityRotate->Create();    
          MessageT( _T("  ") );                    
        }
      }
    }
  }  

  //   
  IEntityPtr entitySketch2( part->NewEntity( o3d_sketch ), false /*AddRef*/ );
  if ( entitySketch2 )
  {
    //        
    //   
    ISketchDefinitionPtr sketchDefinition2( IUnknownPtr( entitySketch2->GetDefinition(), false /*AddRef*/ ) );
    if ( sketchDefinition2 )
    {
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );
 
      //   
      sketchDefinition2->SetPlane( basePlane );    //   XOY   
 
      //  
      entitySketch2->Create();

      //     
      if ( sketchDefinition2->BeginEdit() )
      {
        //   
        ArcByAngle( 15, 0, 10, -90, 90, 1, 1 );
        LineSeg( 15, -10, 15, 10, 3 );
        //     
        sketchDefinition2->EndEdit();
      }
        
      //   
      IEntityPtr entityBossRotate( part->NewEntity( o3d_bossRotated ), false /*AddRef*/ );
      if ( entityBossRotate ) 
      {
        //        
        //    
        IBossRotatedDefinitionPtr bossRotatedDefinition( IUnknownPtr( entityBossRotate->GetDefinition(), false/*AddRef*/ ) ); 
        if ( bossRotatedDefinition ) 
        {
          bossRotatedDefinition->SetToroidShapeType( false );       //   ( TRUE - , FALSE -  )
          bossRotatedDefinition->SetDirectionType( dtNormal );      //   ( dtNormal -  ,    -  
                                                                    // dtReverse -  ,    - , dtBoth -   ,
                                                                    // dtMiddlePlane -    )
          //      
          bossRotatedDefinition->SetSideParam( true,                //   ( TRUE - , FALSE -  )
                                               360 );               //  
          //    
          bossRotatedDefinition->SetThinParam( true,                //   
                                               dtBoth,              //    
                                               1,                   //     
                                               1 );                 //     
          bossRotatedDefinition->SetSketch( entitySketch2 );    //                                                                    
          //               
          entityBossRotate->Create();  
          MessageT( _T("  ") );                      
        }
      }
    }
  }

  //   
  IEntityPtr entitySketch3( part->NewEntity( o3d_sketch ), false /*AddRef*/ );
  if ( entitySketch2 )
  {
    //        
    //   
    ISketchDefinitionPtr sketchDefinition3( IUnknownPtr( entitySketch3->GetDefinition(), false /*AddRef*/ ) );
    if ( sketchDefinition3 )
    {
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );
 
      //   
      sketchDefinition3->SetPlane( basePlane ); //   XOY   
 
      //  
      entitySketch3->Create();

      //     
      if ( sketchDefinition3->BeginEdit() )
      {
        //   
        ArcByAngle( 20, 0, 20, 90, 270, 1, 1 );
        LineSeg( 20, -20, 20, 20, 3 );
        //     
        sketchDefinition3->EndEdit();
      }
        
      //   
      IEntityPtr entityCutRotate( part->NewEntity( o3d_cutRotated ), false /*AddRef*/ );
      if ( entityCutRotate ) 
      {
        //        
        //    
        ICutRotatedDefinitionPtr cutRotatedDefinition( IUnknownPtr( entityCutRotate->GetDefinition(), false /*AddRef*/ ) ); 
        if ( cutRotatedDefinition ) 
        {
          cutRotatedDefinition->SetToroidShapeType( false );           //   ( TRUE - , FALSE -  )
          cutRotatedDefinition->SetDirectionType( dtNormal );          //   ( dtNormal -  ,    - 
                                                                       // dtReverse -  ,    - , dtBoth -   ,
                                                                       // dtMiddlePlane -    )
          //      
          cutRotatedDefinition->SetSideParam( true,                    //   ( TRUE - , FALSE -  )
                                              90 );                    //  
          //    
          cutRotatedDefinition->SetThinParam( true,                    //   
                                              dtBoth,                  //    
                                              5,                       //     
                                              7 );                     //     
          cutRotatedDefinition->SetSketch( entitySketch3 );            //                                                                    
          //             
          entityCutRotate->Create();  
          MessageT( _T("  ") );                      
        }
      }
    }
  }
}


//-------------------------------------------------------------------------------
//   
// ---
void OperationLoft( IPartPtr & part ) 
{
  //   
  IEntityPtr entitySketch( part->NewEntity( o3d_sketch ), false /*AddRef*/ );  
  if (  entitySketch )
  {
    //        
    //   
    ISketchDefinitionPtr sketchDefinition( IUnknownPtr( entitySketch->GetDefinition(), false /*AddRef*/ ) );
    if ( sketchDefinition )
    {
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );
          
      //   
      sketchDefinition->SetPlane( basePlane ); //   XOY   

      //  
      entitySketch->Create();
      entitySketch->SetHidden(false);

      //     
      if ( sketchDefinition->BeginEdit() )
      {
        //    - 
         Circle( 0, 0, 4.5, 1 );
        //     
        sketchDefinition->EndEdit();
      }
    }
  }

  //    (      )
  IEntityPtr entityPlaneOffset2( part->NewEntity( o3d_planeOffset ), false /*AddRef*/ );
  //   
  IEntityPtr entitySketch2( part->NewEntity( o3d_sketch ), false /*AddRef*/ );  
  if ( (bool)entityPlaneOffset2 && (bool)entitySketch2 )
  {
    //        
    //    
    IPlaneOffsetDefinitionPtr planeOffsetDefinition2( IUnknownPtr( entityPlaneOffset2->GetDefinition(), false /*AddRef*/ ) );
    if ( planeOffsetDefinition2 ) 
    {
      //    
      planeOffsetDefinition2->SetOffset( 30 ); //    
        
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );

      planeOffsetDefinition2->SetPlane( basePlane );                     //   XOY   
        
      entityPlaneOffset2->SetName( _bstr_t( _T("  2") ) ); //    
        
      entityPlaneOffset2->SetHidden( true );                //   
        
      //   
      entityPlaneOffset2->Create();             
        
      //        
      //   
      ISketchDefinitionPtr sketchDefinition2( IUnknownPtr( entitySketch2->GetDefinition(), false/*AddRef*/ ) );
      if ( sketchDefinition2 )
      {
        //   
        sketchDefinition2->SetPlane( entityPlaneOffset2 ); //      
 
        //  
        entitySketch2->Create();

        //     
        if ( sketchDefinition2->BeginEdit() )
        {
          //    - 
          Circle( 0, 0, 8, 1 );
          //     
          sketchDefinition2->EndEdit();
        }
      }
    }
  }
  
  //    (      )
  IEntityPtr entityPlaneOffset3( part->NewEntity( o3d_planeOffset ), false /*AddRef*/ );
  //   
  IEntityPtr entitySketch3( part->NewEntity( o3d_sketch ), false /*AddRef*/ );  
  if ( (bool)entityPlaneOffset3 && (bool)entitySketch3 )
  {
    //        
    //    
    IPlaneOffsetDefinitionPtr planeOffsetDefinition3( IUnknownPtr( entityPlaneOffset3->GetDefinition(), false /*AddRef*/ ) );
    if ( planeOffsetDefinition3 ) 
    {
      //    
      planeOffsetDefinition3->SetOffset( 60 ); //    
        
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );

      planeOffsetDefinition3->SetPlane( basePlane );         //   XOY   
        
      //  
      entityPlaneOffset3->SetName( _bstr_t(_T("  3")) );                 //    
      //  
        
      entityPlaneOffset3->SetHidden( true );               //   
        
      //   
      entityPlaneOffset3->Create();             
        
      //        
      //   
      ISketchDefinitionPtr sketchDefinition3( IUnknownPtr( entitySketch3->GetDefinition(), false/*AddRef*/ ) );
      if ( sketchDefinition3 )
      {
        //   
        sketchDefinition3->SetPlane( entityPlaneOffset3 ); //      

        //  
        entitySketch3->Create();

        //     
        if ( sketchDefinition3->BeginEdit() )
        {
          //    - 
          Circle( 0, 0, 1.5, 1 );
          //     
          sketchDefinition3->EndEdit();
        }
      }
    }
  }
 
  //     
  IEntityPtr entityBaseLoft( part->NewEntity( o3d_baseLoft ), false /*AddRef*/ );
  if ( entityBaseLoft ) 
  {
    IBaseLoftDefinitionPtr baseLoftDefinition( IUnknownPtr( entityBaseLoft->GetDefinition(), false /*AddRef*/ ) );
    if ( baseLoftDefinition ) 
    {
      IEntityCollectionPtr entityCollection( baseLoftDefinition->Sketchs(), false /*AddRef*/ );
      if ( entityCollection ) 
      {
        entityCollection->Add( entitySketch );
        entityCollection->Add( entitySketch2 );
        entityCollection->Add( entitySketch3 );
      }
        
      entityBaseLoft->SetName( _bstr_t(_T("")) ); //     
      //       
      entityBaseLoft->SetAdvancedColor( 12345678,  // 
                                        0.8,       //  
                                        0.8,       // 
                                        0.8,       // 
                                        0.8,       // 
                                        1,         // 
                                        0.8 );     // 

      // C   
      entityBaseLoft->Create(); 
      MessageT( _T("   ") );                       
    }
  }
  
  //       
  IEntityPtr entitySketch4( part->NewEntity( o3d_sketch ), false /*AddRef*/ );  
  if (  entitySketch4 )
  {
    //        
    //   
    ISketchDefinitionPtr sketchDefinition4( IUnknownPtr( entitySketch4->GetDefinition(), false /*AddRef*/ ) );
    if ( sketchDefinition4 )
    {
      //   
      sketchDefinition4->SetPlane( entityPlaneOffset3 ); //      
        
      //  
      entitySketch4->Create();

      //     
      if ( sketchDefinition4->BeginEdit() )
      {
        //    - 
        Circle( 0, 0, 1.5, 1 );
        //     
        sketchDefinition4->EndEdit();
      }
    }
  }
    
  //    (      )
  IEntityPtr entityPlaneOffset5( part->NewEntity( o3d_planeOffset ), false /*AddRef*/ );
  //   
  IEntityPtr entitySketch5( part->NewEntity( o3d_sketch ), false /*AddRef*/ );  
  if ( (bool)entityPlaneOffset5 && (bool)entitySketch5 )
  {
    //        
    //    
    IPlaneOffsetDefinitionPtr planeOffsetDefinition5( IUnknownPtr( entityPlaneOffset5->GetDefinition(), false /*AddRef*/ ) );
    if ( planeOffsetDefinition5 ) 
    {
      //    
      planeOffsetDefinition5->SetOffset( 120 ); //    
        
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );

      planeOffsetDefinition5->SetPlane( basePlane ); //   XOY   
      
      entityPlaneOffset5->SetName( _bstr_t(_T("  5")) );                 //    
      
      entityPlaneOffset5->SetHidden( true );               //   
      
      //   
      entityPlaneOffset5->Create();             
        
      //        
      //   
      ISketchDefinitionPtr sketchDefinition5( IUnknownPtr( entitySketch5->GetDefinition(), false /*AddRef*/ ) );
      if ( sketchDefinition5 )
      {
        //   
        sketchDefinition5->SetPlane( entityPlaneOffset5 ); //      
 
        //  
        entitySketch5->Create();

        //     
        if ( sketchDefinition5->BeginEdit() )
        {
          //    - 
          Circle( 0, 0, 1.8, 1 );
          //     
          sketchDefinition5->EndEdit();
        }
      }
    }
  }  

  //     
  IEntityPtr entityBossLoft( part->NewEntity( o3d_bossLoft ), false /*AddRef*/ );
  if ( entityBossLoft ) 
  {
    IBossLoftDefinitionPtr bossLoftDefinition( IUnknownPtr( entityBossLoft->GetDefinition(), false /*AddRef*/ ) );
    if ( bossLoftDefinition ) 
    {
      IEntityCollectionPtr entityCollection( bossLoftDefinition->Sketchs(), false /*AddRef*/ );
      if ( entityCollection ) 
      {
        entityCollection->Add( entitySketch4 );
        entityCollection->Add( entitySketch5 );
      }
        
      entityBossLoft->SetName( _bstr_t(_T("")) );  //     
      //       
      entityBossLoft->SetAdvancedColor( 1234567890, // 
                                        0.8,        //  
                                        0.8,        // 
                                        0.8,        // 
                                        0.8,        // 
                                        1,          // 
                                        0.8 );      // 

      // C    
      entityBossLoft->Create(); 
      MessageT( _T("   ") );                       
    }
  }

  //       
  IEntityPtr entitySketch6( part->NewEntity(o3d_sketch), false/*AddRef*/ );  
  if (  entitySketch6 )
  {
    //        
    //   
    ISketchDefinitionPtr sketchDefinition6( IUnknownPtr( entitySketch6->GetDefinition(), false/*AddRef*/ ) );
    if ( sketchDefinition6 )
    {
      //   
      sketchDefinition6->SetPlane( entityPlaneOffset5 );       //      
        
      //  
      entitySketch6->Create();

      //     
      if ( sketchDefinition6->BeginEdit() )
      {
        //    - 
        RectangleParam parRectangle; //   
        memset( &parRectangle, 0, sizeof( parRectangle ) );          
        parRectangle.x       = -1.8;                         //     -    
        parRectangle.y       = -.4;
        parRectangle.height  = .8;                           //  
        parRectangle.width   = 3.6;                          //  
        parRectangle.style   = 1;                            //  
        parRectangle.pCorner = CreateArray( CORNER_ARR, 0 ); //      ( ) 
        ksRectangle( &parRectangle, 0 ); //  
        //     
        sketchDefinition6->EndEdit();
      }
    }
  }

  //    (      )
  IEntityPtr entityPlaneOffset7( part->NewEntity( o3d_planeOffset ), false /*AddRef*/ );
  //   
  IEntityPtr entitySketch7( part->NewEntity( o3d_sketch ), false /*AddRef*/ );  
  if ( (bool)entityPlaneOffset7 && (bool)entitySketch7 )
  {
    //        
    //    
    IPlaneOffsetDefinitionPtr planeOffsetDefinition7( IUnknownPtr(entityPlaneOffset7->GetDefinition(), false /*AddRef*/ ) );
    
    if ( planeOffsetDefinition7 ) 
    {
      //    
      planeOffsetDefinition7->SetOffset( 110 ); //    
        
      //     XOY
      IEntityPtr basePlane( part->GetDefaultEntity( o3d_planeXOY ), false /*AddRef*/ );

      planeOffsetDefinition7->SetPlane( basePlane );                   //   XOY   
        
      entityPlaneOffset7->SetName( _bstr_t(_T("  7")) ); //    
      
      entityPlaneOffset7->SetHidden( true );                           //   
        
      //   
      entityPlaneOffset7->Create();             
        
      //        
      //   
      ISketchDefinitionPtr sketchDefinition7( IUnknownPtr(entitySketch7->GetDefinition(), false/*AddRef*/ ) );
      if ( sketchDefinition7 )
      {
        //   
        sketchDefinition7->SetPlane( entityPlaneOffset7 );     //      
 
        //  
        entitySketch7->Create();

        //     
        if ( sketchDefinition7->BeginEdit() )
        {
          //    - 
          RectangleParam parRectangle; //   
          memset( &parRectangle, 0, sizeof( parRectangle ) );          
          parRectangle.x       = -1.8;                         //     -    
          parRectangle.y       = -1.8;
          parRectangle.height  = 3.6;                          //  
          parRectangle.width   = 3.6;                          //  
          parRectangle.style   = 1;                            //  
          parRectangle.pCorner = CreateArray( CORNER_ARR, 0 ); //      ( ) 
          ksRectangle( &parRectangle, 0 ); //  
          //     
          sketchDefinition7->EndEdit();
        }
      }
    }
  } 

  //     
  IEntityPtr entityCutLoft( part->NewEntity( o3d_cutLoft ), false /*AddRef*/ );
  if ( entityCutLoft ) 
  {
    ICutLoftDefinitionPtr cutLoftDefinition( IUnknownPtr( entityCutLoft->GetDefinition(), false /*AddRef*/ ) );
    if ( cutLoftDefinition ) 
    {
      IEntityCollectionPtr entityCollection( cutLoftDefinition->Sketchs(), false /*AddRef*/ );
      if ( entityCollection ) 
      {
        entityCollection->Add( entitySketch6 );
        entityCollection->Add( entitySketch7 );
      }
        
      //    
      cutLoftDefinition->SetThinParam( true,     //   
                                       dtNormal, //    
                                       3,        //     
                                       0 );      //     
      entityCutLoft->SetName( _bstr_t(_T(" ")) ); //     

      //       
      entityCutLoft->SetAdvancedColor( 1234,  // 
                                       0.8,   //  
                                       0.8,   // 
                                       0.8,   // 
                                       0.8,   // 
                                       1,     // 
                                       0.8 ); // 

      // C    
      entityCutLoft->Create(); 
      MessageT( _T("   ") );                       
    }
  }  
}

//-----------------------------------------------------------------------------------
//  : , ,  ,  
//---
void CreateNextOper( IPartPtr& part )
{
  //     
  IEntityPtr entitySketch( part->NewEntity(o3d_sketch), false /*AddRef*/ );
	if ( entitySketch ) {
		//    
		ISketchDefinitionPtr sketchDef( IUnknownPtr( entitySketch->GetDefinition(), false /*AddRef*/ ) );
		if ( sketchDef ) {
			//     XOY
			IEntityPtr basePlane( part->GetDefaultEntity(o3d_planeXOY), false /*AddRef*/ );
			sketchDef->SetPlane( basePlane );    //   XOY   
  		sketchDef->SetAngle( 0 );            //   
			entitySketch->Create();              //  

			//   
			sketchDef->BeginEdit(); 
				//    - 
        LineSeg(  50,  50, -50,  50, 1 );
		  	LineSeg(  50, -50, -50, -50, 1 ); 
				LineSeg(  50, -50,  50,  50, 1 );
				LineSeg( -50, -50, -50,  50, 1 );
			sketchDef->EndEdit();                   //                   

			MessageT( _T("  ") );
			
      //   
			IEntityPtr entityExtr( part->NewEntity(o3d_baseExtrusion), false/*AddRef*/ );
			if ( entityExtr ) {
				//     
        IBaseExtrusionDefinitionPtr extrusionDef( IUnknownPtr( entityExtr->GetDefinition(), false/*AddRef*/ ) ); 
				if ( extrusionDef ) {
					extrusionDef->SetDirectionType( dtNormal );         //  
					extrusionDef->SetSideParam( true/* */, etBlind/*  */, 200, 0, false ); 
					extrusionDef->SetThinParam( true, dtBoth, 10, 10 ); //     
					extrusionDef->SetSketch( entitySketch );            //   
					entityExtr->Create();                               //  
				}

        bool update = false;                                 //  update = true,     
        if ( YesNoT( _T("  ?") ) == 1 ) {
          //   
					IEntityPtr entShell( part->NewEntity(o3d_shellOperation), false/*AddRef*/ );
					if ( entShell ) {
						//     
            IShellDefinitionPtr incDef( IUnknownPtr( entShell->GetDefinition(), false/*AddRef*/ ) );
						if ( incDef ) {
              IEntityCollectionPtr entCol( incDef->FaceArray(), false /*AddRef*/ );               //      
              IEntityCollectionPtr collect( part->EntityCollection(o3d_face), false /*AddRef*/ ); //   
              if ( (bool)entCol && (bool)collect ) {
                incDef->SetThickness( 8 );                      //  
                incDef->SetThinType( true );                    //   
                collect->SelectByPoint( 50, 0, 0 );             //         ,    
                IEntityPtr ePtr( collect->GetByIndex(0), false /*AddRef*/ );
                entCol->Add( ePtr );           //          = 0 
                collect->Refresh();                             //  
                entShell->Create();                    				 //  

                if ( YesNoT( _T("   ?") ) == 1 ) {
                  incDef->SetThickness( 25 );                   //  
                  incDef->SetThinType( false );                 //   
                  collect->SelectByPoint( 60, 0, 10 );          //         ,    
                  IEntityPtr ePtr1( collect->GetByIndex(0), false /*AddRef*/ );
                  entCol->Add( ePtr1 );        //          = 0 
                  collect->Refresh();                           //  
                  entShell->Update();                           //  
                  update = true;
                }
              }
            }
          }

          if ( YesNoT( _T("  ?") ) == 1 ) {
            //   
            IEntityPtr entInc( part->NewEntity(o3d_incline), false /*AddRef*/ );
      			if ( entInc ) {
							//     
							IInclineDefinitionPtr incDef( IUnknownPtr( entInc->GetDefinition(), false /*AddRef*/ ) );
              IEntityCollectionPtr  collect( part->EntityCollection(o3d_face), false /*AddRef*/ ); //   
							if ( (bool)incDef && (bool)collect ) {
							  incDef->SetDirection( true );                          //   -   
                incDef->SetAngle( 3 );                                 //    
								incDef->SetPlane( basePlane );                         //    
                IEntityCollectionPtr entColInc( incDef->FaceArray(), false /*AddRef*/ ); //      
                if ( entColInc ) {
                  collect->SelectByPoint( 0, update ? 85 : 60, 10 );  //         ,    
                  IEntityPtr ePtr( collect->GetByIndex(0), false/*AddRef*/ );
                  entColInc->Add( ePtr );                             //          = 0 
                  collect->Refresh();                                 //  
                  entInc->Create();                    				        //  

                  if ( YesNoT( _T("   ?") ) == 1 ) {
    								incDef->SetDirection( false );                        //   - 
		    						incDef->SetAngle( 25 );                               //    
                    collect->SelectByPoint( 0, update ? -85 : -60, 10 );  //         ,    
                    IEntityPtr ePtr1( collect->GetByIndex(0), false/*AddRef*/ );
                    entColInc->Add( ePtr1 );                              //          = 0 
                    collect->Refresh();                                   //  
                    entInc->Update();
                  }
                }
              }
            }
          }

          if ( YesNoT( _T("   ?") ) == 1 ) {
            //    
            IEntityPtr entityOpr( part->NewEntity(o3d_cutByPlane), false /*AddRef*/ );
            if ( entityOpr ) {
              //     
              ICutByPlaneDefinitionPtr incOpr( IUnknownPtr( entityOpr->GetDefinition(), false/*AddRef*/ ) ); //  
              if ( incOpr ) {
       		      //     XOZ,     
  	    			  IEntityPtr basePlaneXOZ( part->GetDefaultEntity(o3d_planeXOZ), false/*AddRef*/ );
                incOpr->SetPlane( basePlaneXOZ );               //   
                incOpr->SetDirection( false );                  //   - 
                entityOpr->Create();                    				//  

                if ( YesNoT( _T("    ?") ) == 1 ) {
                  incOpr->SetDirection( true );                 //   - 
                  entityOpr->Update();                  				//  
                }
              }
            }
          }

          if ( YesNoT( _T("   ?") ) == 1 ) {
            //      
            IEntityPtr entitySketch2( part->NewEntity(o3d_sketch), false /*AddRef*/ );  
            if ( entitySketch2 ) {
              //   
              ISketchDefinitionPtr sketchDef2( IUnknownPtr( entitySketch2->GetDefinition(), false /*AddRef*/ ) );
              if ( sketchDef2 ) {
       		      //     YOZ
  	    			  IEntityPtr basePlaneYOZ( part->GetDefaultEntity(o3d_planeYOZ), false /*AddRef*/ );
                sketchDef2->SetPlane( basePlaneYOZ ); //   yoz   
                sketchDef2->SetAngle( 45 );     			//   
                entitySketch2->Create();    	  			//  

                //   
      		      sketchDef2->BeginEdit(); 
                  //   
                  ArcBy3Points( -200, 45, -150, 10, -50, 10, 1 );
                  LineSeg     ( -200, 45, -300, 20, 1 );
                  LineSeg     (  -50, 10,   60, 10, 1 );
                sketchDef2->EndEdit();                   //   
              
                //    
                IEntityPtr entityOpr( part->NewEntity(o3d_cutBySketch), false /*AddRef*/ );
                if ( entityOpr ) {
                  //    
                  ICutBySketchDefinitionPtr incOpr( IUnknownPtr( entityOpr->GetDefinition(), false /*AddRef*/ ) ); 
                  if ( incOpr ) {
                    incOpr->SetSketch( entitySketch2 );             //  
                    incOpr->SetDirection( true );                   //   
                    entityOpr->Create();                    				//  
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}