unit dlContr;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComObj, ActiveX, Db, IniFiles, LDefin2D, ksConstTLB,
{$IFDEF __LIGHT_VERSION__}
  klTLB;
{$ELSE}
  ksTLB;
{$ENDIF}

type
  TForm1 = class(TForm)
    btLoadGraphic: TButton;
    btQuitUnload: TButton;
    btQuitWoUnload: TButton;
    btUnloadGraphic: TButton;
    OpenDialog1: TOpenDialog;
    btLoadFile: TButton;
    btNewFile: TButton;
    btLoadLibrary: TButton;
    btRunLibCommand: TButton;
    btUnloadLibrary: TButton;
    btSaveFile: TButton;
    btCloseFile: TButton;
    ExecuteCommand: TButton;
    btActive: TButton;
    btVisible: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btLoadGraphicClick(Sender: TObject);
    procedure btQuitUnloadClick(Sender: TObject);
    procedure btQuitWoUnloadClick(Sender: TObject);
    procedure btUnloadGraphicClick(Sender: TObject);
    procedure btLoadFileClick(Sender: TObject);
    procedure btNewFileClick(Sender: TObject);
    procedure btLoadLibraryClick(Sender: TObject);
    procedure btRunLibCommandClick(Sender: TObject);
    procedure btUnloadLibraryClick(Sender: TObject);
    procedure btSaveFileClick(Sender: TObject);
    procedure btCloseFileClick(Sender: TObject);
    procedure ExecuteCommandClick(Sender: TObject);
    procedure btActiveClick(Sender: TObject);
    procedure btVisibleClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;
  libraryId  : integer; // HANDLE  
  Kompas :KompasObject;
implementation

{$R *.DFM}

{------------------------------------------------------------------------------}
{                                                                              }
{------------------------------------------------------------------------------}
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Kompas <> nil then
  begin
    //  
    Kompas.Quit;
    Kompas := nil;
  end;
end;

{------------------------------------------------------------------------------}
{                                                                                }
{------------------------------------------------------------------------------}
procedure TForm1.btLoadGraphicClick(Sender: TObject);
begin
  //    KOMPAS_Graphic
  if Kompas = nil then
  begin
{$IFDEF __LIGHT_VERSION__}
    Kompas:= KompasObject( CreateOleObject('KompasLT.Application.5') );
{$ELSE}
    Kompas:= KompasObject( CreateOleObject('Kompas.Application.5') );
{$ENDIF}
    if Kompas <> nil then
      Kompas.Visible := true;
  end;
end;

{------------------------------------------------------------------------------}
{                                                                                 }
{------------------------------------------------------------------------------}
procedure TForm1.btQuitUnloadClick(Sender: TObject);
begin
  btUnloadGraphicClick(Sender);
  close; // 
end;

{------------------------------------------------------------------------------}
{   -                                                                                }
{------------------------------------------------------------------------------}
procedure TForm1.btQuitWoUnloadClick(Sender: TObject);
begin
  if Kompas <> nil then
  begin
    //  
    Kompas := nil;
  end;
  close; // 
end;

{------------------------------------------------------------------------------}
{                                                                               }
{------------------------------------------------------------------------------}
procedure TForm1.btUnloadGraphicClick(Sender: TObject);
begin
  if Kompas <> nil then
  begin
    //   
    Kompas.Quit;
    Kompas := nil;
  end;

end;

{------------------------------------------------------------------------------}
{                                                                               }
{------------------------------------------------------------------------------}
procedure TForm1.btLoadFileClick(Sender: TObject);
var
 doc : ksDocument2D;
begin
  if Kompas <> nil then
  begin
    OpenDialog1.Filter := '(*.cdw)|*.cdw|(*.frw)|*.frw';
    OpenDialog1.FileName := '';
    if OpenDialog1.Execute then
    begin
//       
//      -   
//           " . ?"   
//      -   IDispatch,         
      doc := ksDocument2D( Kompas.Document2D );
      if doc <> nil then
        doc.ksOpenDocument( OpenDialog1.FileName, false );
    end;
  end;
end;

{------------------------------------------------------------------------------}
{                                                                               }
{------------------------------------------------------------------------------}
procedure TForm1.btNewFileClick(Sender: TObject);
var
 doc    : ksDocument2D;
 docPar : ksDocumentParam;

begin
  if Kompas <> nil then
  begin

//        
//        -   
//        0 -  
//        1 - 
//        2 -  
//        3 - 
//        4 - 3D-
//             " . ?"   
//        -   IDispatch,         
//      -  HANDLE  
    doc := ksDocument2D( Kompas.Document2D );
    if doc <> nil then
    begin
      docPar := ksDocumentParam( kompas.GetParamStruct(ko_DocumentParam) );
      if ( docPar <> nil ) then
      begin
        docPar.Init();
        docPar.type_ := lt_DocSheetStandart;
        doc.ksCreateDocument( docPar );
      end;
    end
  end;
end;

{------------------------------------------------------------------------------}
{  
{------------------------------------------------------------------------------}
procedure TForm1.btLoadLibraryClick(Sender: TObject);
begin
  if Kompas <> nil then
  begin
    OpenDialog1.Filter := '(*.rtw)|*.rtw';
    OpenDialog1.FileName := '';
    if OpenDialog1.Execute then
    begin
//     
//    -  HANDLE  
      libraryId := Kompas.ksAttachKompasLibrary(OpenDialog1.FileName)
    end;
  end;
end;

{------------------------------------------------------------------------------}
{   
{------------------------------------------------------------------------------}
procedure TForm1.btRunLibCommandClick(Sender: TObject);
begin
  if  ( Kompas <> nil ) and ( libraryId <> 0 ) then
  begin
//          
//        - HANDLE 
//        -   
    Kompas.ksExecuteKompasLibraryCommand( libraryId, 1 );
  end;
end;

{------------------------------------------------------------------------------}
{  
{------------------------------------------------------------------------------}
procedure TForm1.btUnloadLibraryClick(Sender: TObject);
begin
  if ( Kompas <> nil ) and ( libraryId <> 0 ) then
  begin
    Kompas.ksDetachKompasLibrary (libraryId);
    libraryId := 0;
  end;
end;

{------------------------------------------------------------------------------}
{  ,   
{------------------------------------------------------------------------------}
procedure TForm1.btSaveFileClick(Sender: TObject);
var
 doc : ksDocument2D;
begin
  if Kompas <> nil then
  begin
    doc := ksDocument2D( kompas.ActiveDocument2D() );
    if  doc <> nil then
    begin
      doc.ksSaveDocument(WideString(''));
      doc := nil;
    end;
  end;
end;

{------------------------------------------------------------------------------}
{  ,   
{------------------------------------------------------------------------------}
procedure TForm1.btCloseFileClick(Sender: TObject);
var
 doc : ksDocument2D;
begin
  if Kompas <> nil then
  begin
    doc := ksDocument2D( kompas.ActiveDocument2D() );
    if  doc <> nil then
    begin
      doc.ksCloseDocument();
      doc := nil;
    end
  end;
end;

{------------------------------------------------------------------------------}
{   
{------------------------------------------------------------------------------}
procedure TForm1.ExecuteCommandClick(Sender: TObject);
var
  doc : ksDocument2D;
  ellPar : ksEllipseParam;
begin
  if Kompas <> nil then
  begin
    doc := ksDocument2D(kompas.ActiveDocument2D);
    if  doc <> nil then
    begin
      doc.ksCircle( 50, 50, 20, 1 );
      doc.ksCircle( 50, 50, 50, 2 );
      ellPar := ksEllipseParam(kompas.GetParamStruct(ko_EllipseParam));
      if ellPar <> nil then begin
        ellPar.Init();
        ellPar.xc := 10;
        ellPar.yc := 20;
        ellPar.A  := 35;
        ellPar.B  := 45;
        ellPar.style := 1;
        doc.ksEllipse( ellPar );
      end;
      doc := nil;
    end;
  end;
end;

procedure TForm1.btActiveClick(Sender: TObject);
begin
  if Kompas = nil then
  begin
    //    KOMPAS_Graphic
{$IFDEF __LIGHT_VERSION__}
   Kompas:= KompasObject( GetActiveOleObject('KompasLT.Application.5') );
{$ELSE}
   Kompas:= KompasObject( GetActiveOleObject('Kompas.Application.5') );
{$ENDIF}
    if Kompas <> nil then
      Kompas.Visible := true;
  end;
end;

procedure TForm1.btVisibleClick(Sender: TObject);
begin
  if Kompas <> nil then
    Kompas.Visible := not Kompas.Visible;
end;

end.
