How to add, change, and remove an entity?
CAD VCL provides a wide range of methods to work with drawing entities.
For example, let's load the Entities.dxf
file and create a procedure to add, change, and remove entities. You can find this file in the CAD VCL package.
As you can see, the file extension is DXF
.
- Add
DXF
,DXFConv
, andsgConsts
to theuses
section. Create a new procedure to add, change, and remove entities.
uses DXF, DXFConv, sgConsts, sgFunction;
...
type
TForm1 = class(TForm)
FImage: TImage;
...
implementation
procedure TForm1.AddChangeDeleteClick(ASender: TObject);
- Declare the local variable
vDrawing
and specifyTsgDXFImage
as its type.
More information about formats and their corresponding classes
Class | File format |
---|---|
TsgHPGLImage | PLT, HGL, HG, HPG, PLO, HP, HP1, HP2, HP3, HPGL, HPGL2, HPP, GL, GL2, PRN, SPL, RTL, PCL |
TsgSVGImage | SVG, SVGZ |
TsgDXFImage | DXF |
TsgCADDXFImage | DXF |
TsgDWFImage | DWF |
TsgDWGImage | DWG |
TsgCGMImage | CGM |
Declare the entity you want to add. For example, let's add an ellipse. Declare vEllipse
and specify TsgDXFEllipse
as its type. Do the same with the vEntity
variable of the TsgDXFEntity
type.
var
vDrawing: TsgDXFImage;
vEllipse: TsgDXFEllipse;
vEntity: TsgDXFEntity;
- Create an instance of the
TsgDXFImage
object. Remember to use thetry...finally
construct to avoid memory leaks. Then call theLoadFromFile
method of this class. The parameter of this method is a DXF file.
begin
vDrawing := TsgDXFImage.Create;
try
vDrawing.LoadFromFile('Entities.dxf');
- The
Entities[const AIndex: Integer]: TsgDXFEntity
property lists all the entities of the CAD file. TheIndex
parameter indicates the index of the object and it counts from 0.
Use theColorCAD
property to change color and theLineWeight
property to change lineweight (values are measured in millimeters) of the second entity.
vDrawing.CurrentLayout.Entities[1].ColorCAD:= MakeColorCAD(acIndexColor, 5);
vDrawing.CurrentLayout.Entities[1].LineWeight := 2;
- Assign the
vDrawing.CurrentLayout.Entities[2]
value to thevEntity
variable. Remove the third entity using the following conditional statement.
vEntity := vDrawing.CurrentLayout.Entities[2];
if vDrawing.CurrentLayout.RemoveEntity(vEntity) then FreeAndNil(vEntity);
- Create the ellipse entity. Add the entity to the current layout. Set two points. Points have three floating values:
x, y, z
. Finally, set its radius and ratio. Use theMakeColorCAD
method to change theColorCAD
property.
vEllipse:= TsgDXFEllipse.Create;
vDrawing.CurrentLayout.AddEntity(vEllipse);
vEllipse.Point := MakeFPoint(10, 10, 0);
vEllipse.EndPoint := MakeFPoint(15, 15, 0);
vEllipse.Radius := 3;
vEllipse.Ratio := 1.5;
vEllipse.ColorCAD:= MakeColorCAD(acIndexColor, 5);
- The
Loads
method fills the internal data of the entity to prepare it for drawing.
vDrawing.Converter.Loads(vEllipse);
- Finally, write the code to recalculate drawing extents. Draw the entity on the canvas with
StretchDraw
, and then destroy thevDrawing
object. Dimensions of the drawing should not be equal to zero.
vDrawing.GetExtents;
FImage.Canvas.StretchDraw(Rect(0, 0,
Round(vDrawing.Width * FImage.Height / vDrawing.Height), FImage.Height), vDrawing);
finally
vDrawing.Free();
end;
You have created the procedure to add, change, and remove entities.
The following picture illustrates the result of your procedure.
The full code listing.
uses DXF, DXFConv, sgConsts, sgFunction;
...
type
TForm1 = class(TForm)
FImage: TImage;
...
implementation
procedure TForm1.AddChangeRemoveClick(ASender: TObject);
var
vDrawing: TsgDXFImage;
vEllipse: TsgDXFEllipse;
vEntity: TsgDXFEntity;
begin
vDrawing := TsgDXFImage.Create();
try
vDrawing.LoadFromFile('Entities.dxf');
vDrawing.CurrentLayout.Entities[1].Color := clBlue; // changing properties
vDrawing.CurrentLayout.Entities[1].LineWeight := 5;
vEntity := vDrawing.CurrentLayout.Entities[2];
if vDrawing.CurrentLayout.RemoveEntity(vEntity) then FreeAndNil(vEntity); // deleting the third entity
vEllipse:= TsgDXFEllipse.Create; // creating a new ellipse
vDrawing.CurrentLayout.AddEntity(vEllipse);
vEllipse.Point := MakeFPoint(10, 10, 0);
vEllipse.EndPoint := MakeFPoint(15, 15, 0);
vEllipse.Radius := 3;
vEllipse.Ratio := 1.5;
vEllipse.ColorCAD:= MakeColorCAD(acIndexColor, 5);
vDrawing.Converter.Loads(vEllipse);
vDrawing.GetExtents;
FImage.Canvas.StretchDraw(Rect(0, 0,
Round(vDrawing.Width * FImage.Height / vDrawing.Height), FImage.Height), vDrawing);
finally
vDrawing.Free();
end;
end;