unit Unit1;
{$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, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, CADExport,
  {$IFDEF CADExportVCL}
  DXFExport, CADtoDXF, CADtoSVG, CADtoCGM, CADtoHPGL, CADtoPDF, CADtoSWF, CADtoDWG,
  {$ENDIF}
  {$IFDEF DEMO}
  fInfo,
  {$ENDIF}
  CADImage, DXFConv, sgConsts;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    sgPaintBox: TImage;
    Panel2: TPanel;
    btnCreateDXFDirectly: TButton;
    SaveDialog: TSaveDialog;
    btPicture1: TButton;
    btPicture2: TButton;
    btPicture3: TButton;
    btPicture4: TButton;
    btSave: TButton;
    procedure btnCreateDXFDirectlyClick(Sender: TObject);
    procedure btPicture1Click(Sender: TObject);
    procedure btPicture2Click(Sender: TObject);
    procedure btPicture3Click(Sender: TObject);
    procedure btPicture4Click(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FCADFile: TsgCADImage;
    procedure AddEntToSection(Section: TsgDXFGroup; Entity: TsgDXFEntity);
  protected
    procedure Add2DVertex(ALWPoly: TsgDXFLWPolyline; const X,Y, ABulge: Double);
    procedure Add3DVertex(APoly: TsgDXFPolyline; const X,Y,Z: Double);
    procedure AddFaceByVertices(APoly: TsgDXFPolyline; const N1,N2,N3,N4: Integer);
    function AddEntityToLayout(AEntity: TsgDXFEntity; const ALayoutName: string = ''): Boolean;
    function AddArc: TsgDXFArc;
    function AddCircle: TsgDXFCircle;
    function AddDimension: TsgDXFDimension;
    function AddDimensionStyle: TsgDXFDimensionStyle;
    function AddDisplayViewPort: TsgDXFViewPort;
    function AddImageEnt: TsgDXFImageEnt;
    function AddInsert: TsgDXFInsert;
    function AddHatch: TsgCADHatch;
    function AddLayer: TsgDXFLayer;
    function AddLineType: TsgDXFLineType;
    function AddLine: TsgDXFLine;
    function AddLWPolyline: TsgDXFLWPolyline;
    function AddMText: TsgDXFMText;
    function AddModelViewPort: TsgDXFViewPort;
    function AddLayout: TsgDXFLayout;
    function AddPolyline1: TsgDXFPolyline;
    function AddPolyline2: TsgDXFPolyline;
    function AddSolid: TsgDXFSolid;
    function AddSpline: TsgDXFSpline;
    function AddStyle: TsgDXFStyle;
    function AddText1: TsgDXFText;
    function AddText2: TsgDXFText;
    function AddXref: Boolean;
    function CreateCADFile: Boolean;
    procedure SetGraphic;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  cnstLayerName: string = 'new_Layer';
  cnstSpline: string = 'spline_Layer';
  cnstLTypeName: string = 'new_LType';
  cnstStyleChinese: string = 'SimSun_font_style';
  cnstLayoutTest: string = 'Layout Test';
  cnstKnots: array [1..12] of Single = (0,0,0,0, 9.36936792, 17.27242735, 26.43203326, 34.54490857, 43.56494422, 43.56494422, 43.56494422, 43.56494422);
  cnstControls: array [1..8] of TFPoint = (
    (X:193.7569940313442; Y:215.218107688496; Z:0.0),
    (X:190.008602213157; Y:216.4199761372127; Z:0.0),
    (X:183.0984428627155; Y:218.6356203125639; Z:0.0),
    (X:182.2793581329382; Y:203.2744864580255; Z:0.0),
    (X:195.7311751699836; Y:211.4104840390326; Z:0.0),
    (X:194.9308559557102; Y:195.9080522313814; Z:0.0),
    (X:188.1155501874722; Y:198.1118624085731; Z:0.0),
    (X:184.5274675101238; Y:199.2721116167301; Z:0.0));

procedure TForm1.Add2DVertex(ALWPoly: TsgDXFLWPolyline; const X,Y, ABulge: Double);
var
  vVertex: TsgDXFVertex;
begin
  vVertex := TsgDXFVertex.Create;
  vVertex.Point := MakeFPoint(X, Y, 0);
  vVertex.Bulge := ABulge;
  ALWPoly.AddEntity(vVertex);
end;

procedure TForm1.Add3DVertex(APoly: TsgDXFPolyline; const X,Y,Z: Double);
var
  vVertex: TsgDXFVertex;
begin
  vVertex := TsgDXFVertex.Create;
  vVertex.Point := MakeFPoint(X, Y, Z);
  APoly.AddEntity(vVertex);
end;

function TForm1.AddArc: TsgDXFArc;
begin
  Result := TsgDXFArc.Create;
  Result.StartAngle := 60;
  Result.EndAngle := 210;
  Result.Radius := 50;
  Result.Point := MakeFPoint(110, 160, 0);
  Result.Color := clGreen;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  Result.SetLType(FCADFile.Converter.LTypeByName(cnstLTypeName));
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddCircle: TsgDXFCircle;
begin
  Result := TsgDXFCircle.Create;
  Result.LineTypeScale := 0.1;
  Result.Radius := 30;
  Result.Point := MakeFPoint(110, 160, 0);
  Result.Color := clGreen;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  Result.SetLType(FCADFile.Converter.LTypeByName(cnstLTypeName));
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddDimensionStyle: TsgDXFDimensionStyle;
begin
  Result := TsgDXFDimensionStyle.Create;
  Result.ArrowSize := 10;
  Result.ArrowType := 3;
  Result.TextHeight := 12;
  Result.ExtLineExt := 5;
  Result.ExtLineOffset := 20;
  Result.DIMDEC := 2;
  Result.TextPosVert := pvAbove;
  Result.Name := 'TEST';
  Result.Scale := 1;
  AddEntToSection(FCADFile.Converter.Sections[csDimStyles], Result);
end;

function TForm1.AddDimension: TsgDXFDimension;
begin
  Result := TsgDXFDimension.Create;
  Result.DefPoint := MakeFPoint(200,150,0);
  Result.LinDefPoint1 := MakeFPoint(20,88,0);
  Result.LinDefPoint2 := MakeFPoint(200,38,0);
  Result.TextOffset := 5;
  Result.Style := FCADFile.Converter.DimensionStyleByName('TEST');
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddDisplayViewPort: TsgDXFViewPort;
begin
  Result := TsgDXFViewPort.Create;
  Result.PSpaceCenter := cnstFPointZero;
  Result.PSpaceWidth := 297;
  Result.PSpaceHeight := 210;
  Result.MSpaceCenter := Result.PSpaceCenter;
  Result.MSpaceHeight := Result.PSpaceHeight;
  Result.Flags := 32800;
  if not AddEntityToLayout(Result, cnstLayoutTest) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddEntityToLayout(AEntity: TsgDXFEntity; const ALayoutName: string = ''): Boolean;
var
  vLayout: TsgDXFLayout;
begin
  Result := False;
  if ALayoutName = '' then
    vLayout := FCADFile.Converter.Layouts[0]
  else
    vLayout := FCADFile.Converter.LayoutByName(ALayoutName);
  if vLayout <> nil then
  begin
    if Assigned(FCADFile.Converter.OnCreate) then
      FCADFile.Converter.OnCreate(AEntity);
    FCADFile.Converter.Loads(AEntity);
    vLayout.AddEntity(AEntity);
    Result := True;
  end;
end;

procedure TForm1.AddEntToSection(Section: TsgDXFGroup; Entity: TsgDXFEntity);
begin
  FCADFile.Converter.Loads(Entity);
  Section.AddEntity(Entity);
end;

procedure TForm1.AddFaceByVertices(APoly: TsgDXFPolyline; const N1,N2,N3,N4: Integer);
var
  vVertex: TsgDXFVertex;
begin
  vVertex := TsgDXFVertex.Create;
  vVertex.PolyFaceVertexIndex1  := N1;
  vVertex.PolyFaceVertexIndex2  := N2;
  vVertex.PolyFaceVertexIndex3  := N3;
  vVertex.PolyFaceVertexIndex4  := N4;
  vVertex.Flags := 128;
  APoly.AddEntity(vVertex);
end;

function TForm1.AddHatch: TsgCADHatch;
var
  vPoint2D: TF2DPoint;
  v2DPolyline: Tsg2DPolyline;
  v2DBList: Tsg2DBoundaryList;
  vHPData: PsgHatchPatternData;
begin
  Result := TsgCADHatch.Create;
  Result.HatchName := 'ANSI31';
  v2DBList := Result.AddBoundaryList(7);
  v2DPolyline := Tsg2DPolyline.Create;
  v2DBList.Add(v2DPolyline);
  vPoint2D := MakeF2DPoint(200.0, 40.0);
  v2DPolyline.AddVertex(vPoint2D);
  vPoint2D := MakeF2DPoint(93.0,29.0);
  v2DPolyline.AddVertex(vPoint2D);
  vPoint2D := MakeF2DPoint(150.0, 120.0);
  v2DPolyline.AddVertex(vPoint2D);
  vPoint2D := MakeF2DPoint(200.0,40.0);
  v2DPolyline.AddVertex(vPoint2D);
  Result.Color := clBlue;
  New(vHPData);
  vHPData.BaseP := MakeFPoint(0, 0, 0);
  vHPData.Offset := MakeFPoint(-0.1, 0.1, 0);
  vHPData.LineAngle := 45.0;
  vHPData.IsDash := False;
  vHPData.Lines := nil;
  vHPData.DashNum := 0;
  Result.HatchPatternData.Add(vHPData);
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddImageEnt: TsgDXFImageEnt;
var
  vImageDef: TsgDXFImageDef;
begin
  Result := nil;
  vImageDef := TsgDXFImageDef.Create;
  vImageDef.FileName := 'Images\cadsofttools.bmp';
  FCADFile.Converter.Loads(vImageDef);
  if vImageDef.Picture.Graphic <> nil then
  begin
    FCADFile.Converter.Sections[csImageDefs].AddEntity(vImageDef);
    Result := TsgDXFImageEnt.Create;
    Result.ImageDef := vImageDef;
    Result.Point := MakeFPoint(180, 100, 0);
    Result.UVector := cnstXOrtAxis;
    Result.VVector := cnstYOrtAxis;
    Result.Size := Result.ImageDef.Size;
    Result.Width := 59;
    if not AddEntityToLayout(Result) then
    begin
      FCADFile.Converter.Sections[csImageDefs].RemoveEntity(vImageDef);
      vImageDef.Free;
      Result.Free;
      Result := nil;
    end;
  end
  else
    vImageDef.Free;
end;

function TForm1.AddInsert: TsgDXFInsert;
var
  vBlock: TsgDXFBlock;
  vAttdef: TsgDXFAttdef;
  vAttrib: TsgDXFAttrib;
begin
  Result := TsgDXFInsert.Create;
  vBlock := TsgDXFBlock.Create;
  vBlock.Name := 'AttribsTest';
  vBlock.Flags := 2;
  vAttdef := TsgDXFAttdef.Create;
  vAttdef.Tag := 'ATTDEF#1';
  vAttdef.Text := 'This is ATTDEF text';
  vAttdef.Height := 1;
  if Assigned(FCADFile.Converter.OnCreate) then
    FCADFile.Converter.OnCreate(vAttdef);
  FCADFile.Converter.Loads(vAttdef);
  vBlock.AddEntity(vAttdef);
  if Assigned(FCADFile.Converter.OnCreate) then
    FCADFile.Converter.OnCreate(vBlock);
  FCADFile.Converter.Loads(vBlock);
  FCADFile.Converter.Sections[csBlocks].AddEntity(vBlock);
  Result.Block := vBlock;
  Result.Point := MakeFPoint(90, 90, 0);
  vAttrib := TsgDXFAttrib.Create;
  vAttrib.Point := MakeFPoint(10, 10, 0);
  vAttrib.Tag := 'ATTDEF#1';
  vAttrib.Value := 'ATTRIB text!';
  vAttrib.Height := 20;
  if Assigned(FCADFile.Converter.OnCreate) then
    FCADFile.Converter.OnCreate(vAttrib);
  FCADFile.Converter.Loads(vAttrib);
  Result.Attribs.Add(vAttrib);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddLayer: TsgDXFLayer;
begin
  Result := TsgDXFLayer.Create;
  Result.Name := cnstSpline;
  Result.Color := $7F9FFF;
  AddEntToSection(FCADFile.Converter.Sections[csLayers], Result);
end;

function TForm1.AddLayout: TsgDXFLayout;
begin
  Result := TsgDXFLayout.Create;
  Result.Name := cnstLayoutTest;
  FCADFile.Converter.Loads(Result);
  FCADFile.Converter.AddLayout(Result)
end;

function TForm1.AddLine: TsgDXFLine;
begin
  Result := TsgDXFLine.Create;
  Result.Point := MakeFPoint(80, 100, 0);
  Result.Point1 := MakeFPoint(150, 150, 0);
  Result.Color := clBlue;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  Result.SetLType(FCADFile.Converter.LTypeByName(cnstLTypeName));
  Result.LineWeight := 0.7;
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddLineType: TsgDXFLineType;
begin
  Result := TsgDXFLineType.Create;
  Result.Name := cnstLTypeName;
  Result.Lines.Initialize([25, -12.5]);
  AddEntToSection(FCADFile.Converter.Sections[csLTypes], Result);
end;

function TForm1.AddLWPolyline: TsgDXFLWPolyline;
begin
  Result := TsgDXFLWPolyline.Create;
  Result.Closed := True;
  Result.GlobalWidth := 4;
  Add2DVertex(Result, -50, -50, 0.2);
  Add2DVertex(Result, -50, 350, 0.2);
  Add2DVertex(Result, 300, 350, 0.2);
  Add2DVertex(Result, 300, -50, 0.2);
  Result.Color := clNavy;
  Result.SetLType(FCADFile.Converter.LTypeByName(cnstLTypeName));
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddModelViewPort: TsgDXFViewPort;
begin
  Result := FCADFile.Converter.GenerateModelViewPort(cnstFPointZero,
    297, 210, False);
  Result.MSpaceHeight := 170;
  if not AddEntityToLayout(Result, cnstLayoutTest) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddMText: TsgDXFMText;
begin
  Result := TsgDXFMText.Create;
  Result.Text  := 'MText (center)';
  Result.Point := MakeFPoint(125, 250, 0);
  Result.RectWidth := 250;
  Result.Align := 5;
  Result.Height := 15;
  Result.Color := clTeal;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddPolyline1: TsgDXFPolyline;
begin
  Result := TsgDXFPolyline.Create;
  Result.Closed := True;
  Result.LineWeight := 0.01*sgDXFLineWeights[High(sgDXFLineWeights)];
  Add3DVertex(Result, 0, 0, 0);
  Add3DVertex(Result, 0, 300, 0);
  Add3DVertex(Result, 250, 300, 0);
  Add3DVertex(Result, 250, 0, 0);
  Result.Color := clMaroon;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddPolyline2: TsgDXFPolyline;
begin
  Result := TsgDXFPolyline.Create;
  Result.Flags := 64;
  Result.MeshM := 10;
  Result.MeshN := 3;

  Add3DVertex(Result, 157, 177, 0);
  Add3DVertex(Result, 190, 196, 0);
  Add3DVertex(Result, 223, 176, 0);
  Add3DVertex(Result, 209, 203, 0);
  Add3DVertex(Result, 234, 218, 0);
  Add3DVertex(Result, 201, 218, 0);
  Add3DVertex(Result, 190, 244, 0);
  Add3DVertex(Result, 179, 219, 0);
  Add3DVertex(Result, 148, 219, 0);
  Add3DVertex(Result, 172, 204, 0);

  AddFaceByVertices(Result, 1, 5, 8, 0);
  AddFaceByVertices(Result, 3, 7, 10, 0);
  AddFaceByVertices(Result, 9, 8, 10, 0);
  Result.Color := clRed;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddSolid: TsgDXFSolid;
begin
  Result := TsgDXFSolid.Create;
  Result.Point := MakeFPoint(20, 50, 0);
  Result.Point1 := MakeFPoint(80, 50, 0);
  Result.Point2 := MakeFPoint(80, 90, 0);
  Result.Point3 := MakeFPoint(20, 90, 0);
  Result.Color := clNavy;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddSpline: TsgDXFSpline;
var
  I: Integer;
  vPPoint3D: TFPoint;
begin
  Result := TsgDXFSpline.Create;
  for I := Low(cnstControls) to High(cnstControls) do
  begin
    vPPoint3D := cnstControls[I];
    Result.Controls.Add(vPPoint3D);
  end;
  for I := Low(cnstKnots) to High(cnstKnots) do
    Result.Knots.Add(cnstKnots[I]);
  Result.Layer := FCADFile.Converter.LayerByName(cnstSpline);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddStyle: TsgDXFStyle;
begin
  Result := TsgDXFStyle.Create;
  Result.Name := cnstStyleChinese;
  Result.PrimaryFont := 'SimSun';
  AddEntToSection(FCADFile.Converter.Sections[csStyles], Result);
end;

function TForm1.AddText1: TsgDXFText;
begin
  Result := TsgDXFText.Create;
  Result.Text  := 'Text';
  Result.Point := MakeFPoint(10, 280, 0);
  Result.Height := 10;
  Result.Color := clTeal;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddText2: TsgDXFText;
begin
  Result := TsgDXFText.Create;
  Result.Text  := 'Unicode+Chinese: \U+00B11\U+00B0\U+7777';
  Result.Style := FCADFile.Converter.StyleByName(cnstStyleChinese);
  Result.Point := MakeFPoint(10, 265, 0);
  Result.Height := 10;
  Result.Color := clTeal;
  Result.Layer := FCADFile.Converter.LayerByName(cnstLayerName);
  if not AddEntityToLayout(Result) then
  begin
    Result.Free;
    Result := nil;
  end;
end;

function TForm1.AddXref: Boolean;
const
  // Please use only DWG files for correct export to DXF
  cnstXrefFileName: string = 'XREF.dwg';
var
  vCADforXRef: TsgCADImage;
begin
  try
    vCADforXRef := GetCADFile(cnstXrefFileName);
    Result := vCADforXRef <> nil;
    if Result then
      FCADFile.Converter.AddXRefEx(
        vCADforXRef,                   // TsgCADImage object for inserting
        ChangeFileExt(ExtractFileName(cnstXrefFileName), ''), // Name
        MakeFPoint(20.0, 50.0, 0.0),   // Position in ACAD coords
        MakeFPoint(3, 2, 1),           // Scale factor
        45.0,                          // Rotation angle (degrees)
        FCADFile.Converter.Layouts[0]);// Current layout
  except
    on E: Exception do raise Exception.Create('Xref cannot be added with message:'#13#10 +
      '"'+ E.Message + '"'#13#10 +
      'The possible reasons:'#13#10 +
      '1) unit is not declared in section uses (i.e. DWG).'#13#10 +
      '2) CAD file is not found.'#13#10 +
      'In other cases please contact developers at support@cadsofttools.com.');
  end;
end;

procedure TForm1.btnCreateDXFDirectlyClick(Sender: TObject);
begin
  if not CreateCADFile then
    Exit;
  AddLayer;
  AddLineType;
  AddStyle;
  AddText1;
  AddText2;
  AddMText;
  AddLine;
  AddPolyline1;
  AddPolyline2;
  AddLWPolyline;
  AddSpline;
  AddArc;
  AddCircle;
  AddSolid;
  AddDimension;
  AddHatch;
  AddImageEnt;
  AddInsert;
  FCADFile.GetExtents;
  AddLayout;
  AddDisplayViewPort;
  AddModelViewPort;
  SetGraphic;
end;

procedure TForm1.btPicture1Click(Sender: TObject);
begin
  if not CreateCADFile then
    Exit;
  AddLineType;
  AddPolyline1;
  AddLWPolyline;
  AddInsert;
  FCADFile.GetExtents;
  SetGraphic;
end;

procedure TForm1.btPicture2Click(Sender: TObject);
begin
  if not CreateCADFile then
    Exit;
  AddPolyline1;
  AddStyle;
  AddText1;
  AddText2;
  AddMText;
  FCADFile.GetExtents;
  SetGraphic;
end;

procedure TForm1.btPicture3Click(Sender: TObject);
begin
  if not CreateCADFile then
    Exit;
  AddLineType;
  AddLine;
  AddLWPolyline;
  AddArc;
  AddCircle;
  FCADFile.GetExtents;
  SetGraphic;
end;

procedure TForm1.btPicture4Click(Sender: TObject);
begin
  if not CreateCADFile then
    Exit;
  AddLayer;
  AddPolyline1;
  AddPolyline2;
  AddSpline;
  AddSolid;
  AddDimensionStyle;
  AddDimension;
  AddHatch;
  FCADFile.GetExtents;
  SetGraphic;
end;

procedure TForm1.btSaveClick(Sender: TObject);
  {$IFDEF CADExportVCL}
const
  ExportClass: array[0..6] of TsgCADExportClass =
    (TsgCADtoDXF, TsgCADtoDWG, TsgHPGLExport, TsgPDFExport, TsgCADtoCGM, TsgCADtoSVG, TsgCADtoSWF);
var
  vExp: TsgCADExport;
begin
    if FCADFile = nil then
  begin
    MessageBox(Application.Handle,PChar('Image don''t exists, press another button.'), PChar('Warning'), MB_OK or MB_ICONWARNING);
    Exit;
  end;
  SaveDialog.FilterIndex := 1;
  if not SaveDialog.Execute or (SaveDialog.FileName = '') then
    Exit;
  vExp := ExportClass[SaveDialog.FilterIndex - 1].Create(FCADFile);
  vExp.SaveToFile(SaveDialog.FileName);
  vExp.Free;
  {$ELSE}
  begin
    ShowMessage('Save option requires CAD Export VCL software');
  {$ENDIF}
end;

function TForm1.CreateCADFile: Boolean;
var
  vDimStyle: TsgDXFDimensionStyle;
begin
  Result := True;
  if FCADFile <> nil then
    FCADFile.Free;
  FCADFile := TsgCADImage.Create;
  try
    FCADFile.Converter.InitializeSectionsBegin;
    FCADFile.Converter.InitializeSectionsEnd;
    vDimStyle := TsgDXFDimensionStyle(FCADFile.Converter.DimensionStyleByName(FCADFile.Converter.HeadVarStruct.DimStyle));
    vDimStyle.DIMBLK1T := datOpen;
    FCADFile.Converter.Loads(vDimStyle);
  except
    FCADFile := nil;
    Result := False;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FCADFile := nil;
  SaveDialog.Filter := 'AutoCADŽ DXF (*.dxf)|*.dxf|AutoCADŽ DWG (*.dwg)|*.dwg|HPGL/2 (*.plt)|*.plt|AdobeŽ Portable Document Format (*.pdf)|*.pdf|Computer Graphics Metafile (*.cgm)|*.cgm|Scalable Vector Graphics (*.svg)|*.svg|SWF Flash (*.swf)|*.swf';
  {$IFDEF DEMO}
  fInfo.CreateInfoFrame(Handle, Point(1, 1));
  {$ENDIF}
end;

procedure TForm1.SetGraphic;
begin
  sgPaintBox.Picture.Graphic := FCADFile;
  sgPaintBox.Width := Round(FCADFile.AbsWidth);
  sgPaintBox.Height := Round(FCADFile.AbsHeight);
  sgPaintBox.Invalidate;
end;

end.
