Skip to main content

How to create a hatch on the basis of an existing entity?

For example, you have created an ellipse and want to hatch it.

To create a hatch on the basis of an existing TsgDXFEllipse entity, at first you need to create boundaries of the hatch that will repeat the ellipse outline with the Tsg2DEllipse class.

  1. Add DXF, DXFConv, and sgConsts to the uses section. Create a new procedure to add and hatch the ellipse.
uses DXF, DXFConv, sgConsts, sgFunction;
...

type

TForm1 = class(TForm)
FImage: TImage;

...

implementation

procedure TForm1.FAddAndHatch(ASender: TObject);
  1. Declare the local variables:
    • Declare vDrawing and specify TsgCADDXFImage as its type.
    • Declare vEllipse and specify TsgDXFEllipse as its type.
    • For hatching, you need to declare vHatch and vBoundaryList. Specify TsgCADCurvePolygon and Tsg2DBoundaryList as their types respectively.
var
vDrawing: TsgCADImage;
vEllipse: TsgDXFEllipse;
v2dEllipse: Tsg2DEllipse;
vHatch: TsgCADCurvePolygon;
vBoundaryList: Tsg2DBoundaryList;
  1. Create an instance of the TsgDXFImage object. Call InitializeSection to create the CAD object manually. Remember to use the try...finally construct to avoid memory leaks. Then define the current layout as the first one.
begin
vDrawing := TsgCADImage.Create();
try
vDrawing.Converter.InitializeSections();
vDrawing.CurrentLayout := vDrawing.Layouts[0];
  1. Create an ellipse. Add the ellipse to the current layout. Set its points, radius, color, and ratio. Add this entity to the current layout. The Loads method fills the internal data of the entity to prepare it for drawing. Use the MakeColorCAD method to change the ColorCAD property.
vEllipse:= TsgDXFEllipse.Create;
vDrawing.CurrentLayout.AddEntity(vEllipse);
vEllipse.Point := MakeFPoint(10, 10, 0);
vEllipse.EndPoint := MakeFPoint(25, 25, 0);
vEllipse.Radius:= 10;
vEllipse.Ratio := 1.5;
vEllipse.ColorCAD:= MakeColorCAD(acIndexColor, 5);
vDrawing.Converter.Loads(vEllipse);

The following picture illustrates how this ellipse looks.

Ellipse

Ellipse without a hatch

  1. Create vHatch, an instance of the TsgCADCurvePolygon object. Set its color. Using the AddBoundaryList method, create a new boundary path and add it to the boundary data. For more information about its parameters, please see the CAD VCL help system.
vHatch := TsgCADCurvePolygon.Create;
vDrawing.CurrentLayout.AddEntity(vHatch);
vHatch.ColorCAD:= MakeColorCAD(acIndexColor, 1);
vBoundaryList := vHatch.AddBoundaryList();
  1. Create v2dEllipse that will repeat the vEllipse outline. Then add v2dEllipse to the vBoundaryList list.
v2dEllipse:= Tsg2DEllipse.Create;
vBoundaryList.Add(v2dEllipse);
v2dEllipse.CenterPoint := MakeF2DPointFrom3D(vEllipse.Point);
v2dEllipse.MajorPoint := MakeF2DPointFrom3D (vEllipse.RadPt);
v2dEllipse.Radius := vEllipse.Ratio;
  1. Add vHatch to the current layout.
  vDrawing.CurrentLayout.AddEntity(vHatch);

  1. Finally, render the result.
    vDrawing.Converter.Loads(vHatch);
vDrawing.GetExtents;
FImage.Canvas.StretchDraw(Rect(0, 0,
Round(vDrawing.Width * FImage.Height / vDrawing.Height), FImage.Height), vDrawing);
finally
vDrawing.Free();
end;
end;

You have created the procedure to create a hatch on the basis of an existing entity.

The following picture illustrates the result of your procedure.

Ellipse

Ellipse with the hatch

The full code listing.

uses DXF, DXFConv, sgConsts, sgFunction;
...
type

TForm1 = class(TForm)
FImage: TImage;
...
implementation

procedure TForm1.AddAndHatch(ASender: TObject);

var
vDrawing: TsgCADImage;
vHatch: TsgCADCurvePolygon;
vBoundaryList: Tsg2DBoundaryList;
vEllipse: TsgDXFEllipse;
v2dEllipse: Tsg2DEllipse;

begin
vDrawing := TsgCADImage.Create();
try
vDrawing.Converter.InitializeSections();
vDrawing.CurrentLayout := vDrawing.Layouts[0];
vEllipse:= TsgDXFEllipse.Create; // creating a new ellipse
vDrawing.CurrentLayout.AddEntity(vEllipse);
vEllipse.Point := MakeFPoint(10, 10, 0);
vEllipse.EndPoint := MakeFPoint(25, 25, 0);
vEllipse.Radius:= 10;
vEllipse.Ratio := 1.5;
vEllipse.ColorCAD:= MakeColorCAD(acIndexColor, 5);
vDrawing.Converter.Loads(vEllipse);
vHatch := TsgCADCurvePolygon.Create; // creating a hatch
vDrawing.CurrentLayout.AddEntity(vHatch);
vHatch.ColorCAD:= MakeColorCAD(acIndexColor, 1);
vBoundaryList := vHatch.AddBoundaryList();
v2dEllipse:= Tsg2DEllipse.Create; // creating a new ellipse with the same outline as the first one
vBoundaryList.Add(v2dEllipse);
v2dEllipse.CenterPoint := MakeF2DPointFrom3D(vEllipse.Point);
v2dEllipse.MajorPoint := MakeF2DPointFrom3D (vEllipse.RadPt);
v2dEllipse.Radius := vEllipse.Ratio;
vDrawing.Converter.Loads(vHatch);
vDrawing.GetExtents;
FImage.Canvas.StretchDraw(Rect(0, 0,
Round(vDrawing.Width * FImage.Height / vDrawing.Height), FImage.Height), vDrawing);
finally
vDrawing.Free();
end;
end;