How to read block attributes?
- Add
DXF
,DXFConv
, andsgConsts
to theuses
section. Create a new procedure to read block attributes.
uses DXF, DXFConv, sgConsts;
...
procedure TForm1.ReadBlockAttributesClick(ASender: TObject);
- Declare the local variable
vDrawing
and specifyTsgDXFImage
as its type.
More information about formats and their corresponding classes
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;
- Create an instance of the
TsgDXFImage
object, and then call theLoadFromFile
method of this class. Remember to use thetry...finally
construct to avoid memory leaks.
begin
vDrawing := TsgCADDXFImage.Create;
try
vDrawing.LoadFromFile('Entities.dxf');
- 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;