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, andsgConststo theusessection. 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
vDrawingand specifyTsgCADDXFImageas its type. - Declare
vEllipseand specifyTsgDXFEllipseas its type. - For hatching, you need to declare
vHatchandvBoundaryList. SpecifyTsgCADCurvePolygonandTsg2DBoundaryListas their types respectively.
- Declare
var
vDrawing: TsgCADImage;
vEllipse: TsgDXFEllipse;
v2dEllipse: Tsg2DEllipse;
vHatch: TsgCADCurvePolygon;
vBoundaryList: Tsg2DBoundaryList;
- Create an instance of the
TsgDXFImageobject. CallInitializeSectionto create theCADobject manually. Remember to use thetry...finallyconstruct 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
Loadsmethod fills the internal data of the entity to prepare it for drawing. Use theMakeColorCADmethod to change theColorCADproperty.
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 theTsgCADCurvePolygonobject. Set its color. Using theAddBoundaryListmethod, 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
v2dEllipsethat will repeat thevEllipseoutline. Then addv2dEllipseto thevBoundaryListlist.
v2dEllipse:= Tsg2DEllipse.Create;
vBoundaryList.Add(v2dEllipse);
v2dEllipse.CenterPoint := MakeF2DPointFrom3D(vEllipse.Point);
v2dEllipse.MajorPoint := MakeF2DPointFrom3D (vEllipse.RadPt);
v2dEllipse.Radius := vEllipse.Ratio;
- Add
vHatchto 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;