////////////////////////////////////////////////////////////////////////////////
//
//     
//
////////////////////////////////////////////////////////////////////////////////
unit Cube1;

interface
uses
  LDefin2D, ksAuto, Libtool, Windows, ks3DCOM_TLB, LDefin3D, LTool3D;

////////////////////////////////////////////////////////////////////////////////

  procedure  LIBRARYENTRY( command: WORD  ); Pascal;
  function   LIBRARYID   : Cardinal;         Pascal;
  procedure  MyDLLProc(Reason: Integer);

  function   LoadStr( ID : Integer ) : string;                     //    
  function   LibMessage( str : string; flags : integer ) : Integer;//   
  function   LibMessageByID( strId, flags : Integer ) : Integer;   //   
  procedure  GetNewKompasAPI;
  procedure  TestOpenGL;
  procedure  TestOpenGLForCurrentDoc( regrawType : Integer );

implementation

uses
  LtDefine,
  cBaseEvents,
  DocumentFrameEvents,
  ksConstTLB,
  forms,
{$IFDEF __LIGHT_VERSION__}
  klAPI7;
{$ELSE}
  ksApi7;
{$ENDIF}

var
  newKompasAPI : IApplication;                                //  Application 7


//------------------------------------------------------------------------------
// LibraryName
//---
procedure MyDLLProc(Reason: Integer);
begin
  if Reason = DLL_PROCESS_DETACH then begin
    cBaseEvent.TerminateEvents;
    newKompasAPI       := nil;
    Application.Handle := 0;
  end;
end;

//------------------------------------------------------------------------------
// LibraryId
//---
function LIBRARYID: UINT; pascal;
begin
  Result := 100;  // IDR_LIB
end;

//------------------------------------------------------------------------------
// LibraryEntry
//---
procedure LIBRARYENTRY( command: WORD ); pascal;
begin
  Application.Handle := GetHWindow;

  case command of
    1 : begin TestOpenGL(); end;                                 // 
    2, 3, 4, 5: begin TestOpenGLForCurrentDoc( command - 2 ); end;  //    
  end;
end;

//------------------------------------------------------------------------------
//     API
// ---
procedure GetNewKompasAPI;
  var
    disp : IDispatch;
begin
	if newKompasAPI = nil then
  begin
    disp := IDispatch( CreateKompasApplication );
    newKompasAPI := disp As IApplication;
  end
end;

//------------------------------------------------------------------------------
//    
//---
function LoadStr( ID : Integer ) : string;
var
  buf : array [0..255] of char;
begin
  ksConvertLangStrEx( hInstance, ID, buf, 255 );
  Result := buf;
end;

//------------------------------------------------------------------------------
//  
// ---
function LibMessage( str : string; flags : integer ) : Integer;
var
 enabse : Integer;
begin
  enabse := IsEnableTaskAccess();  //  
  if enabse <> 0 then              //     
    EnableTaskAccess(0);           //  

    //                                      
  Result := Application.MessageBox( PChar(str), PChar(LoadStr(LIBRARYID)), flags );

  if enabse <> 0 then                 //      
    EnableTaskAccess(1);              //    


end;

//------------------------------------------------------------------------------
//  
// ---
function LibMessageByID( strId, flags : Integer ) : Integer;
begin
  Result := LibMessage( LoadStr(strId), flags );
end;

//------------------------------------------------------------------------------
//  ,   
// ---
function CreateExtrusion( var part : IPart ) : boolean;
var
  iunkn              : IUnknown;
  entitySketch,
  basePlane,
  entityExtrusion    : IEntity;
  sketchDefinition   : ISketchDefinition;
  extrusionDefinition: IBaseExtrusionDefinition;

begin
  Result := FALSE;
  //   
  entitySketch := IEntity(part.NewEntity( o3d_sketch ));
  if entitySketch <> nil then
  begin
    entitySketch._Release;
    //        
    iunkn := IUnknown( entitySketch.GetDefinition() );
//    iunkn.QueryInterface( IID_ISketchDefinition, sketchDefinition );
    sketchDefinition := iunkn As ISketchDefinition;
    if sketchDefinition <> nil then
    begin
      iunkn._Release;
      //     XOY
      basePlane := IEntity( part.GetDefaultEntity( o3d_planeXOY ) );
      if basePlane <> nil then
      begin
        basePlane._Release;
        //   
        sketchDefinition.SetPlane( basePlane ); //   XOY   
        sketchDefinition.SetAngle( 45 );        //   

        //  
        entitySketch.Create();

        //     
        if sketchDefinition.BeginEdit() <> 0 then
        begin
          //    - 
          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();
        end;
        //  
        entityExtrusion := IEntity( part.NewEntity( o3d_baseExtrusion ) );
        if entityExtrusion <> nil then
        begin
          entityExtrusion._Release;
          iunkn := IUnknown( entityExtrusion.GetDefinition() );
          //    
//          iunkn.QueryInterface( IID_IBaseExtrusionDefinition, extrusionDefinition );
          extrusionDefinition := iunkn As IBaseExtrusionDefinition;
          if extrusionDefinition <> nil then
          begin
            iunkn._Release;
            //    
            extrusionDefinition.SetDirectionType( dtNormal );     //   ( dtNormal	- 
                                                                   // ,    - ,
                                                                   // dtReverse	-  ,    - 
                                                                   // dtBoth -   , dtMiddlePlane    )
            //      
            extrusionDefinition.SetSideParam( 1,               //   ( TRUE -  ,
                                                                   // FALSE -   )
                                               etBlind,            //   ( etBlind -   ,
                                                                   // etThroughAll -   , etUpToVertexTo -    ,
                                                                   // etUpToVertexFrom -    , etUpToSurfaceTo - 
                                                                   //   , etUpToSurfaceFrom -    ,
                                                                   // etUpToNearSurface	-    )
                                               100,                //  
                                               0,                  //  
                                               1 );             //   ( TRUE -  , FALSE -   )
            //    
            extrusionDefinition.SetThinParam(  0,              //   
                                               dtBoth,             //    
                                               10,                 //     
                                               10 );               //     
            extrusionDefinition.SetSketch( entitySketch );        //   
            entityExtrusion.SetName( WideString(' - ') );

            //   
            Result := entityExtrusion.Create() <> 0;
          end
        end
      end
    end
  end
end;

//-----------------------------------------------------------------------------
//
// ---
procedure TestOpenGL();
var
  komDoc        : IKompasDocument;
  docFrames     : IDocumentFrames;
  documentFrame : IDocumentFrame;
  doc3D         : IDocument3D;
  part          : IPart;
begin
  doc3D := IDocument3D( ksGet3dDocument() );
  if doc3D <> nil then
  begin
    doc3D._Release;
    doc3D.Create( 0, 1 ); //   
    doc3D.SetDrawMode( vm_Shaded );
    doc3D.SetShadedWireframe( 1 );
    part := IPart( doc3D.GetPart( pTop_Part ) );
    if part <> nil then
    begin
      part._Release;
      if CreateExtrusion( part ) then
      begin
        part.SetName( WideString('') );
        part.Update();
        GetNewKompasAPI();
        if newKompasAPI <> nil then
        begin
          komDoc := newKompasAPI.ActiveDocument;
          if komDoc <> nil then
          begin
            docFrames := komDoc.DocumentFrames;
            if docFrames <> nil then
            begin
              documentFrame := docFrames.Item[0];
              if documentFrame <> nil then
              begin
	              NewDocumentFrameEvent( documentFrame, komDoc.Reference, 0 );
                documentFrame.SetGabaritModifying();
                documentFrame.RefreshWindow();
              end
            end
          end
        end
      end
    end
  end
end;

//-----------------------------------------------------------------------------
//
// ---
procedure TestOpenGLForCurrentDoc( regrawType : Integer );
var
  komDoc        : IKompasDocument;
  docFrames     : IDocumentFrames;
  documentFrame : IDocumentFrame;
begin
  GetNewKompasAPI();
  if newKompasAPI <> nil then
  begin
    komDoc := newKompasAPI.ActiveDocument;
    if komDoc <> nil then
    begin
      docFrames := komDoc.DocumentFrames;
      if docFrames <> nil then
      begin
        documentFrame := docFrames.Item[0];
        if documentFrame <> nil then
        begin
	        NewDocumentFrameEvent( documentFrame, komDoc.Reference, regrawType );
          documentFrame.SetGabaritModifying();
          documentFrame.RefreshWindow();
        end
      end
    end
  end
end;


end.


