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.
- Add
DXF
,DXFConv
, andsgConsts
to theuses
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);
- Declare the local variables:
- Declare
vDrawing
and specifyTsgCADDXFImage
as its type. - Declare
vEllipse
and specifyTsgDXFEllipse
as its type. - For hatching, you need to declare
vHatch
andvBoundaryList
. SpecifyTsgCADCurvePolygon
andTsg2DBoundaryList
as their types respectively.
- Declare
var
vDrawing: TsgCADImage;
vEllipse: TsgDXFEllipse;
v2dEllipse: Tsg2DEllipse;
vHatch: TsgCADCurvePolygon;
vBoundaryList: Tsg2DBoundaryList;
- Create an instance of the
TsgDXFImage
object. CallInitializeSection
to create theCAD
object manually. Remember to use thetry...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];
- 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 theMakeColorCAD
method to change theColorCAD
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.
- Create
vHatch
, an instance of theTsgCADCurvePolygon
object. Set its color. Using theAddBoundaryList
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();
- Create
v2dEllipse
that will repeat thevEllipse
outline. Then addv2dEllipse
to thevBoundaryList
list.
v2dEllipse:= Tsg2DEllipse.Create;
vBoundaryList.Add(v2dEllipse);
v2dEllipse.CenterPoint := MakeF2DPointFrom3D(vEllipse.Point);
v2dEllipse.MajorPoint := MakeF2DPointFrom3D (vEllipse.RadPt);
v2dEllipse.Radius := vEllipse.Ratio;
- Add
vHatch
to the current layout.
vDrawing.CurrentLayout.AddEntity(vHatch);
- 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.
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;