How to read block attributes?
- Add DXF,DXFConv, andsgConststo theusessection. Create a new procedure to read block attributes.
uses DXF, DXFConv, sgConsts;
...
procedure TForm1.ReadBlockAttributesClick(ASender: TObject);
- Declare the local variable vDrawingand specifyTsgDXFImageas its type.
More information about formats and their corresponding classes
| Class | File format | 
|---|---|
| TsgHPGLImage | PLT, HGL, HG, HPG, PLO, HP, HP1, HP2, HP3, HPGL, HPGL2, HPP, GL, GL2, PRN, SPL, RTL, PCL | 
| TsgSVGImage | SVG, SVGZ | 
| TsgDXFImage | DXF | 
| TsgCADDXFImage | DXF | 
| TsgDWFImage | DWF | 
| TsgDWGImage | DWG | 
| TsgCGMImage | CGM | 
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 TsgDXFImageobject, and then call theLoadFromFilemethod of this class. Remember to use thetry...finallyconstruct 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;