Skip to main content

How to read block attributes?

  1. Add DXF, DXFConv, and sgConsts to the uses section. Create a new procedure to read block attributes.
uses DXF, DXFConv, sgConsts;
...
procedure TForm1.ReadBlockAttributesClick(ASender: TObject);
  1. Declare the local variable vDrawing and specify TsgDXFImage as its type.
More information about formats and their corresponding classes
ClassFile format
TsgHPGLImagePLT, HGL, HG, HPG, PLO, HP, HP1, HP2, HP3, HPGL, HPGL2, HPP, GL, GL2, PRN, SPL, RTL, PCL
TsgSVGImageSVG, SVGZ
TsgDXFImageDXF
TsgCADDXFImageDXF
TsgDWFImageDWF
TsgDWGImageDWG
TsgCGMImageCGM

Also, declare the insert. It is a block reference. Several inserts can reference the same block. Declare vInsert and specify TsgDXFInsert as its type.

var
vDrawing: TsgDXFImage;
vInsert: TsgDXFInsert;
I, J: Integer;
sContent: string;
  1. Create an instance of the TsgDXFImage object, and then call the LoadFromFile method of this class. Remember to use the try...finally construct to avoid memory leaks.
begin
vDrawing := TsgCADDXFImage.Create;
try
vDrawing.LoadFromFile('Entities.dxf');
  1. Create a cycle to search entities in the current layout of vDrawing.
for I := 0 to vDrawing.CurrentLayout.Count - 1 do
begin
if vDrawing.CurrentLayout.Entities[I].EntType = ceInsert then
begin
vInsert := TsgDXFInsert(vDrawing.CurrentLayout.Entities[I]);
for J := 0 to vInsert.Attribs.Count - 1 do
sContent := sContent + TsgDXFAttrib(vInsert.Attribs[J]).Tag + ' = ' +
TsgDXFAttrib(vInsert.Attribs[J]).Value + #13;
ShowMessage('Block name: ' + #13 + vInsert.Block.Name + #13 + sContent);
end;
sContent := '';
end;
finally
vDrawing.Free;
end;
end;

You have created the procedure to read block attributes.

The full code listing.

uses DXF, DXFConv, sgConsts;
...

implementation

procedure TForm1.ReadBlockAttributesClick(ASender: TObject);
var
vDrawing: TsgCADDXFImage;
vInsert: TsgDXFInsert;
I, J: Integer;
sContent: string;
begin
vDrawing := TsgCADDXFImage.Create;
try
vDrawing.LoadFromFile('Entities.dxf');
for I := 0 to vDrawing.CurrentLayout.Count - 1 do
begin
if vDrawing.CurrentLayout.Entities[I].EntType = ceInsert then
begin
vInsert := TsgDXFInsert(vDrawing.CurrentLayout.Entities[I]);
for J := 0 to vInsert.Attribs.Count - 1 do
sContent := sContent + TsgDXFAttrib(vInsert.Attribs[J]).Tag + ' = ' +
TsgDXFAttrib(vInsert.Attribs[J]).Value + #13;
ShowMessage('Block name: ' + #13 + vInsert.Block.Name + #13 + sContent);
end;
sContent := '';
end;
finally
vDrawing.Free;
end;
end;