Skip to main content

Write your first app

In the previous guide you've install CAD VCL. Now let's create the first app to view CAD files.

Throughout this tutorial, we'll walk you through the creation of a basic CAD viewer application.

It consists of:

Note

In this tutorial we will use RAD Studio 10.4 IDE.

Part 1. Windows VCL Application

Select File → New → Windows VCL Application.

win_vcl.gif

Add components to the form. We will display the image in the TImage component by click on TButton. You can arrange the components differently. Also add TOpenDialog to make it possible to select the file.

TForm

When the form is ready, you can start writing code. Let's start by preparing all the dependencies. Write {$INCLUDE CADSoftTools.INC} or {$I CADSoftTools.INC}.

unit My_first_app;
{$INCLUDE CADSoftTools.INC}
Note

If you have problems with the installation, you may see a File not found error about CADSoftTools.INC. See this topic.

Part 2. Dependencies

Import the modules from the library. The ones you need to write your application. This is done by adding them to the Uses section. A list of modules available for import can be found in Files reference.

In our app we will use CADImage, DXF, DXFConv, DWG, HPGL2, SVG, CGM modules.

ClassFile formatModule
TsgHPGLImagePLT, HGL, HG, HPG, PLO, HP, HP1, HP2, HP3, HPGL, HPGL2, HPP, GL, GL2, PRN, SPL, RTL, PCLHPGL2
TsgSVGImageSVG, SVGZSVG
TsgDXFImageDXFDXFExport
TsgCADDXFImageDXFDXF
TsgDWFImageDWFDWF
TsgDWGImageDWGDWG
TsgCGMImageCGMCGM

So, add it.

unit My_first_app;
{$INCLUDE CADSoftTools.INC}

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
CADImage, DXF, DXFConv, DWG, HPGL2, SVG, CGM;

Part 3. Procedure

Now declare the global variable Img: TsgCADImage;. This variable will store the drawing.

var
Form1: TForm1;
Img: TsgCADImage;

By double-clicking on TButton on the form create the procedure to view CAD files.

procedure TForm1.FButtonClick(Sender: TObject);
begin

end;

To be able to handle multiple file extensions, we create an array of strings that list all the extensions available to us.

procedure TForm1.FButtonClick(Sender: TObject);
const
cnstExts: array[0..17] of string = ( '.dxf', '.dwg', '.plt', '.hgl', '.rtl',
'.hg', '.plo', '.hp', '.hp1', '.hp2', '.hpg', '.hpgl', '.hpgl2', '.gl2',
'.prn', '.spl', '.svg', '.cgm');

In the first part of this guide, we added the TOpenDialog component to the form. We will use it to select a file. Declare the vFileExt variable to store the file extension. We will assign a value to it using the ExtractFileExt function. The following code snippet shows a simple algorithm for creating an object of a certain class based on a given file extension.

procedure TForm1.FButtonClick(Sender: TObject);
const
cnstExts: array[0..17] of string = ( '.dxf', '.dwg', '.plt', '.hgl', '.rtl',
'.hg', '.plo', '.hp', '.hp1', '.hp2', '.hpg', '.hpgl', '.hpgl2', '.gl2',
'.prn', '.spl', '.svg', '.cgm');
var
vFileExt: string;
begin
OpenDialog1.FileName := '';
if OpenDialog1.Execute then
begin
vFileExt := ExtractFileExt(LowerCase(OpenDialog1.FileName));
Img.Free;
Img := nil;
case StrIndex(vFileExt, cnstExts) of
0: Img := TsgCADdxfImage.Create;
{$IFNDEF sgDFONLY}
1: Img := TsgDWGImage.Create;
2..15: Img := TsgHPGLImage.Create;
16: Img := TsgSVGImage.Create;
17: Img := TsgCGMImage.Create;
{$ENDIF}
end;

Now that we have created an object of the class we want, we can use the LoadFromFile method to load the drawing and then display it in the TImage component.

      if Img <> nil then
begin
Img.LoadFromFile(OpenDialog1.FileName);
FImage.Picture.Assign(Img);
end;

The full code listing of this procedure.

procedure TForm1.FButtonClick(Sender: TObject);
const
cnstExts: array[0..17] of string = ( '.dxf', '.dwg', '.plt', '.hgl', '.rtl',
'.hg', '.plo', '.hp', '.hp1', '.hp2', '.hpg', '.hpgl', '.hpgl2', '.gl2',
'.prn', '.spl', '.svg', '.cgm');
var
vFileExt: string;
begin
OpenDialog1.FileName := '';
if OpenDialog1.Execute then
begin
vFileExt := ExtractFileExt(LowerCase(OpenDialog1.FileName));
Img.Free;
Img := nil;
case StrIndex(vFileExt, cnstExts) of
0: Img := TsgCADdxfImage.Create;
{$IFNDEF sgDFONLY}
1: Img := TsgDWGImage.Create;
2..15: Img := TsgHPGLImage.Create;
16: Img := TsgSVGImage.Create;
17: Img := TsgCGMImage.Create;
{$ENDIF}
end;
if Img <> nil then
begin
Img.LoadFromFile(OpenDialog1.FileName);
FImage.Picture.Assign(Img);
end;
end;
end;

Part 4. Execute

Press Shift + Ctrl + F9 to execute your first app with CAD VCL. Try downloading the drawing Gasket.dwg (It is in the cadvcl_demo\Lib\Drawings library folder)

result

Part 5. Code listing

The full code listing of the unit.

unit My_first_app;
{$INCLUDE CADSoftTools.INC}

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
CADImage, DXF, DXFConv, DWG, HPGL2, SVG, CGM;

type
TForm1 = class(TForm)
FImage: TImage;
FButton: TButton;
OpenDialog1: TOpenDialog;
procedure FButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Img: TsgCADImage;

implementation

{$R *.dfm}

procedure TForm1.FButtonClick(Sender: TObject);
const
cnstExts: array [0 .. 17] of string = ('.dxf', '.dwg', '.plt', '.hgl', '.rtl',
'.hg', '.plo', '.hp', '.hp1', '.hp2', '.hpg', '.hpgl', '.hpgl2', '.gl2',
'.prn', '.spl', '.svg', '.cgm');
var
vFileExt: string;
begin
OpenDialog1.FileName := '';
if OpenDialog1.Execute then
begin
vFileExt := ExtractFileExt(LowerCase(OpenDialog1.FileName));
Img.Free;
Img := nil;
case StrIndex(vFileExt, cnstExts) of
0:
Img := TsgCADdxfImage.Create;
{$IFNDEF sgDFONLY}
1:
Img := TsgDWGImage.Create;
2 .. 15:
Img := TsgHPGLImage.Create;
16:
Img := TsgSVGImage.Create;
17:
Img := TsgCGMImage.Create;
{$ENDIF}
end;
if Img <> nil then
begin
Img.LoadFromFile(OpenDialog1.FileName);
FImage.Picture.Assign(Img);
end;
end;
end;

end.