⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxf_read.pas

📁 一个比较完整的读写dxf文件的DELPHI程序
💻 PAS
📖 第 1 页 / 共 3 页
字号:
    else if not GotLine then fCode:=-2;
    Result:=fCode;
  end;
end {NextGroupCode};

function DXF_Reader.ValStr: shortstring;
begin Result:=fLine end;

function DXF_Reader.ValDbl: double;
begin
  Val(fLine,Result,ec);
  If ec<>0 then raise DXF_read_exception.Create('Invalid Floating point conversion',line_num);
end;

function DXF_Reader.ValInt: integer;
begin
  Val(fLine,Result,ec);
  If ec<>0 then raise DXF_read_exception.Create('Invalid Integer conversion',line_num);
end;

function DXF_Reader.code_and_string(var group:integer; var s:string) : boolean;
var astr : string;
begin
  result := true;
  group  := NextGroupCode;
  if group>=0 then s := ValStr
  else result := false;
  // useful in debugging
  //  if (group=0) then begin astr := IntToStr(group)+' '+s; alb.Items.Add(astr); end;
end;

function DXF_Reader.code_and_double(var group:integer; var d:double) : boolean;
begin
  result := true;
  group  := NextGroupCode;
  if group>=0 then d := Valdbl
  else result := false;
end;

// This routine is just for the $EXT(max/min) and should be used with care....
function DXF_Reader.read_2Dpoint(var p1:Point3D) : boolean;
var Groupcode : integer;
begin
  repeat Groupcode:=NextGroupCode;
  until (Groupcode=DXF_primary_X) or (Groupcode<0);
  if Groupcode<0 then begin result:=false; exit; end;
  p1.x := Valdbl;
  result := code_and_double(Groupcode,p1.y);  { y next              }
end;

function DXF_Reader.skip_upto_section(name:string) : boolean;
var Group   : integer;
    s       : string;
begin
  result := false;
  repeat
    if not code_and_string(Group,s) then break;
    if (Group=0) then begin
      if (s='SECTION') then begin
        if not code_and_string(Group,s) then break;
        if (Group=DXF_name) then begin
          if (s=name) then result := true
          else exit;
        end
        else if skipped<>nil then Skipped.Add(s);
      end else if skipped<>nil then Skipped.Add(s);
    end;
  until (result);
end;
{ --------------------------------------------------------------------------- }
{ Header section
{ --------------------------------------------------------------------------- }
function DXF_Reader.move_to_header_section : boolean;
begin
  result := skip_upto_section('HEADER');
end;

function DXF_Reader.read_header : boolean;
var Group : integer;
    s     : string;
begin
  result := false;
  repeat
    if not code_and_string(Group,s) then break;
    if (group=9) and (s='$EXTMAX') then begin
      if not read_2Dpoint(max_extents) then break;
    end;
    if (group=9) and (s='$EXTMIN') then begin
      if not read_2Dpoint(min_extents) then break;
    end;
    if (group=9) and (s='$CECOLOR') then begin
      if (NextGroupCode=DXF_colornum) and (ValInt=256) then colour_BYLAYER := true;
    end;
    result := (Group=0) and (s='ENDSEC');
  until result;
end;

function DXF_Reader.get_min_extent : Point3D;
begin
  result := min_extents;
end;

function DXF_Reader.get_max_extent : Point3D;
begin
  result := max_extents;
end;
{ --------------------------------------------------------------------------- }
{ Blocks section
{ --------------------------------------------------------------------------- }
function DXF_Reader.move_to_blocks_section : boolean;
begin
  result := skip_upto_section('BLOCKS');
end;

function DXF_Reader.read_blocks : boolean;
var Group : integer;
    s     : string;
begin
  result := false;
  repeat
    if not code_and_string(Group,s) then break;
    if (Group=0) and (s='BLOCK') then begin
      if not read_block then break;
    end;
    result := (Group=0) and (s='ENDSEC');
  until result;
end;

function DXF_Reader.read_block : boolean;
var Groupcode  : integer;
    s          : string;
    ent        : abstract_entity;
    block      : Block_;
    layer,lp1  : integer;
    entity     : DXF_Entity;
    base       : Point3D;
begin
  result := false;
  ent := read_generic(layer);
  layer := layer_num('0'); // ALL BLOCKS GOING TO LAYER 0 (makes things easier)
  if layer<0 then layer := DXF_Layers.Add(DXF_Layer.create('0'));
  if ent<>nil then begin
    block := Block_.create(ent.namestr,ent.p1);
    DXF_Layer(DXF_Layers[layer]).add_entity_to_layer(block);
    repeat
      if not code_and_string(Groupcode,s) then break;
      if (Groupcode=0) then begin
        result := read_entity(s,'ENDBLK',entity,layer);
        if entity<>nil then block.entities.Add(entity);
      end;
    until result;
  end;
end;

// we need to know where the blocks are stored for lookup purposes
function DXF_Reader.block_list : Entity_List;
var lp1,lp2 : integer;
    layer   : DXF_Layer;
begin
  for lp1:=0 to DXF_Layers.count -1 do begin
    layer := DXF_Layers[lp1];
    for lp2:=0 to layer.entity_lists.count-1 do begin
      if Entity_List(layer.entity_lists[lp2]).name='Block_' then begin
        result := Entity_List(layer.entity_lists[lp2]);
        exit;
      end;
    end;
  end;
end;
{ --------------------------------------------------------------------------- }
{ Tables (Layers - VPort) section
{ --------------------------------------------------------------------------- }
function DXF_Reader.move_to_tables_section : boolean;
begin
  result := skip_upto_section('TABLES');
end;

function DXF_Reader.read_tables : boolean;
var Group : integer;
    s     : string;
begin
  result := false;
  repeat
    if not code_and_string(Group,s) then break;
    if (Group=0) and (s='TABLE') then begin
      if not code_and_string(Group,s) then break;
      if (Group=DXF_name) then begin
        if (s='LAYER') then read_layer_information
        else if (s='VPORT') then read_vport_information
        else if skipped<>nil then Skipped.Add(s);
      end;
    end;
    result := (Group=0) and (s='ENDSEC');
  until result;
end;

function DXF_Reader.read_layer_information : boolean;
var Group,Lay_num : integer;
    s             : string;
begin
  lay_num := -1;
  result  := false;
  repeat
    if not code_and_string(Group,s) then break;
    if (Group=0) then begin
      if (s='LAYER') then begin
        if not code_and_string(Group,s) then break;
        if (Group=DXF_name) then lay_num := DXF_Layers.Add(DXF_Layer.create(s));
      end
      else if (s='ENDTAB') then result := true
      else if skipped<>nil then Skipped.Add(s);
    end
    else if (Group=DXF_colornum) and (lay_num<>-1) then
      DXF_Layer(DXF_Layers[lay_num]).Colour := ValInt;
  until result;
end;

// This no longer does anything !
function DXF_Reader.read_vport_information : boolean;
var Group : integer;
    s     : string;
begin
  result := false;
  repeat
    if not code_and_string(Group,s) then break;
    if (Group=0) then begin
      if (s='VPORT') then begin
        if not code_and_string(Group,s) then break;
        if (Group=DXF_name) then begin
          if (s='*ACTIVE') then repeat
            if not code_and_string(Group,s) then break;
{ removed Aspectratio stuff since it never seems to make any difference
  and sometimes buggers everything up
            if (Group=DXF_floatvals1) then Aspect := ValDbl;
}
            result := (Group=0) and (s='ENDTAB');
          until (result)
          else if skipped<>nil then Skipped.Add(s);
        end;
      end
      else if skipped<>nil then Skipped.Add(s);
    end
  until (result);
end;

function DXF_Reader.layer_num(layername:string) : integer;
var lp1 : integer;
begin
  result := -1;
  for lp1:=0 to DXF_Layers.count-1 do begin
    if DXF_Layer(DXF_Layers[lp1]).name=layername then begin
      result := lp1;
      exit;
    end;
  end;
end;
{ --------------------------------------------------------------------------- }
{ Entities section
{ --------------------------------------------------------------------------- }
function DXF_Reader.move_to_entity_section : boolean;
begin
  result := skip_upto_section('ENTITIES');
end;

function DXF_Reader.read_entities : boolean;
var Groupcode,layer : integer;
    s               : string;
    entity          : DXF_Entity;
begin
  result := false;
  repeat
    try
      if not code_and_string(Groupcode,s) then break;
      if (Groupcode=0) then begin
        result := read_entity(s,'ENDSEC',entity,layer);
        // put the entity in the layer...
        if entity<>nil then DXF_Layer(DXF_Layers[layer]).add_entity_to_layer(entity);
      end;
    except
      on E:DXF_read_exception do begin
        stopped_thinking;
        if MessageBox(0,@E.message[1],'DXF read error warning',MB_OKCANCEL)=IDCANCEL then
          raise DXF_read_exception.Create('User aborted',-1);
        thinking_bar(nil,'Reading DXF file...');
      end;
      on E:Exception do Showmessage(E.Message);
    end;
  until result;
end;
{ --------------------------------------------------------------------------- }
{ Entity reading code
{ --------------------------------------------------------------------------- }
function DXF_Reader.read_entity_data(ent:abstract_entity) : boolean;
var Groupcode : integer;
begin
  ent.OCS_Z := WCS_Z;
  repeat
    Groupcode := NextGroupCode;
    case Groupcode of
      DXF_primary_X    : ent.p1.x      := Valdbl;
      DXF_primary_Y    : ent.p1.y      := Valdbl;
      DXF_primary_Z    : ent.p1.z      := Valdbl;
      DXF_other_X_1    : ent.p2.x      := Valdbl;
      DXF_other_Y_1    : ent.p2.y      := Valdbl;
      DXF_other_Z_1    : ent.p2.z      := Valdbl;
      DXF_other_X_2    : ent.p3.x      := Valdbl;
      DXF_other_Y_2    : ent.p3.y      := Valdbl;
      DXF_other_Z_2    : ent.p3.z      := Valdbl;
      DXF_other_X_3    : ent.p4.x      := Valdbl;
      DXF_other_Y_3    : ent.p4.y      := Valdbl;
      DXF_other_Z_3    : ent.p4.z      := Valdbl;
      DXF_floatval     : ent.rad_hgt   := Valdbl;
      DXF_floatvals1   : ent.fv1       := Valdbl;
      DXF_floatvals2   : ent.fv2       := Valdbl;
      DXF_floatvals3   : ent.fv3       := Valdbl;
      DXF_angle1       : ent.angle1    := Valdbl;
      DXF_angle2       : ent.angle2    := Valdbl;
      DXF_thickness    : ent.thickness := Valdbl;
      DXF_elevation    : ent.elev      := Valdbl;
      DXF_70Flag       : ent.flag_70   := ValInt;
      DXF_71Flag       : ent.flag_71   := ValInt;
      DXF_72Flag       : ent.flag_72   := ValInt;
      DXF_73Flag       : ent.flag_73   := ValInt;
      DXF_74Flag       : ent.flag_74   := ValInt;
      DXF_colornum     : ent.colour    := ValInt;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -