unit Unit1;
{$DEFINE USE_ADO}
{$INCLUDE CADSoftTools.INC}
// If you see error "File not found: 'CADSoftTools.INC'",
// then please contact info@cadsofttools.com to get a compilable version.
// If you have a compilable version, you need to copy the contents of
// Lib7/LibXE5win32/LibXE5win64... to Lib directory according to your
// Delphi version and target CPU platform.

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs,  ExtCtrls, StdCtrls, Mask, DBCtrls, ExtDlgs,
  DXFConv, DXF, CADImage, DWG, HPGL2, SVG, CGM, DWF, sgConsts, DB
{$IFDEF USE_ADO}
    , ADODB
{$ELSE}
    , DBTables
{$ENDIF}
    ;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    DBNavigator1: TDBNavigator;
    DBEdit1: TDBEdit;
    Label1: TLabel;
    Button1: TButton;
    OpenPictureDialog1: TOpenPictureDialog;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Table1AfterScroll(DataSet: TDataSet);
    procedure Button1Click(Sender: TObject);
  private
{$IFDEF USE_ADO}
    FTable: TADOTable;
{$ELSE}
    FTable: TTable;
{$ENDIF}
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  vFieldDef: TFieldDef;
begin
  sSHXSearchPaths := ExpandFileName('../../SHX');
  OpenPictureDialog1.Filter := GraphicFilter(TGraphic);
{$IFDEF USE_ADO}
  FTable := TADOTable.Create(Self);
  FTable.ConnectionString :=
    'Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=demo.mdb;';
  FTable.TableName := 'demo';
{$ELSE}
  FTable := TTable.Create(Self);
  FTable.Active := False;
  FTable.TableType := ttParadox;
  FTable.TableName := 'demo';
  if not FTable.Exists then
  begin
    FTable.FieldDefs.Clear;
    vFieldDef := FTable.FieldDefs.AddFieldDef;
    vFieldDef.Name := 'ID';
    vFieldDef.DataType := ftAutoInc;
    vFieldDef.Required := True;
    vFieldDef := FTable.FieldDefs.AddFieldDef;
    vFieldDef.Name := 'FILE_NAME';
    vFieldDef.DataType := ftString;
    vFieldDef := FTable.FieldDefs.AddFieldDef;
    vFieldDef.Name := 'CAD';
    vFieldDef.DataType := ftBlob;
    try
      FTable.CreateTable;
    except
      MessageBox(Application.Handle,
        PChar('Database is not found. Run this demo on PC with Delphi installed.'),
        'WARNING', MB_ICONWARNING);
    end;
  end;
{$ENDIF}
  FTable.AfterScroll := Table1AfterScroll;
  DataSource1.DataSet := FTable;
  FTable.Open;
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
const
  cnstExts: array [0 .. 18] of string = ('.dxf', '.dwg', '.plt', '.hgl', '.rtl',
    '.hg', '.plo', '.hp', '.hp1', '.hp2', '.hpg', '.hpgl', '.hpgl2', '.gl2',
    '.prn', '.spl', '.svg', '.svgz', '.cgm');
var
  vStream: TStream;
  vFileExt: string;
  vImg: TsgCADImage;
begin
  vStream := FTable.CreateBlobStream(FTable.FieldByName('CAD'), bmRead);
  try
    vFileExt := ExtractFileExt(FTable.FieldByName('FILE_NAME').AsString);
    case StrIndex(vFileExt, cnstExts) of
      0:
        vImg := TsgCADdxfImage.Create;
//{$IFNDEF sgDXFONLY}
//      1:
//        vImg := TsgDWGImage.Create;
//      2 .. 15:
//        vImg := TsgHPGLImage.Create;
//      16, 17:
//        vImg := TsgSVGImage.Create;
//      18:
//        vImg := TsgCGMImage.Create;
//{$ENDIF}
    else
      vImg := nil;
    end;
    try
      if vImg <> nil then
        vImg.LoadFromStream(vStream);
      Image1.Picture.Assign(vImg);
    finally
      vImg.Free;
    end;
  finally
    vStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    if FTable.IsEmpty then
      FTable.Insert;
    if (FTable.State <> dsEdit) or (FTable.State <> dsInsert) then
      FTable.Edit;
    FTable.FieldByName('FILE_NAME').AsString :=
      ExtractFileName(OpenPictureDialog1.FileName);
    (FTable.FieldByName('CAD') as TBlobField)
      .LoadFromFile(OpenPictureDialog1.FileName);
    FTable.Post;
    Table1AfterScroll(FTable);
  end;
end;

end.
