formatdxffile.pas

来自「FlexGraphics是一套创建矢量图形的VCL组件」· PAS 代码 · 共 578 行 · 第 1/2 页

PAS
578
字号
 else
 if not Assigned(Insert.OCS_WCS) then
  t_matrix := OCS
 else begin
  TempMatrix := MatrixMultiply(Insert.OCS_WCS^, OCS^);
  t_matrix := @TempMatrix;
 end;
 if dgInsert in FGroups then begin
  Result := TFlexGroup.Create(FFlex, FlexParent, FFlexLayer);
  Result.Name := Insert.proper_name;
  FlexParent := Result;
 end else
  Result := Nil;
 for i:=0 to Insert.num_attribs-1 do
  LoadEntity(Insert.attribs[i], t_matrix, FlexParent);
 if Insert.blockname<>'' then LoadEntity(Insert.block, t_matrix, FlexParent);
end;

function TFlexDxfFormat.LoadLine(Line: Line_; OCS:pM;
  FlexParent: TFlexControl): TFlexCurve;
var t_matrix : pMatrix;
    Points: TPointArray;
begin
 // Transform
 t_matrix := update_transformations(Line.OCS_WCS, OCS);
 SetLength(Points, 2);
 Points[0] := MapFn(Line.p1, t_matrix);
 Points[1] := MapFn(Line.p2, t_matrix);
 // Create control
 Result := TFlexCurve.Create(FFlex, FlexParent, FFlexLayer);
 Result.Name := Line.proper_name;
 Result.PenProp.Color := Line.colour;
 Result.PenProp.Width := FPenWidth;
 SetCurveAbsPointsEx(Result, Points, Nil);
end;

function TFlexDxfFormat.LoadPolyLine(PolyLine: PolyLine_; OCS:pM;
  FlexParent: TFlexControl): TFlexCurve;
var Points: TPointArray;
    i: integer;
    t_matrix: pMatrix;
begin
 t_matrix := update_transformations(PolyLine.OCS_WCS, OCS);
 SetLength(Points, PolyLine.numvertices);
 for i:=0 to PolyLine.numvertices-1 do
  Points[i] := MapFn(PolyLine.polypoints[i], t_matrix);
 // Create control
 Result := TFlexCurve.Create(FFlex, FlexParent, FFlexLayer);
 Result.Name := PolyLine.proper_name;
 Result.IsSolidProp.Value := PolyLine.closed;
 Result.PenProp.Color := PolyLine.colour;
 Result.PenProp.Width := FPenWidth;
 SetCurveAbsPointsEx(Result, Points, Nil);
end;

function TFlexDxfFormat.LoadPolygonMesh(PolygonMesh: Polygon_Mesh_;
  OCS:pM; FlexParent: TFlexControl): TFlexGroup;
var Points, PartPoints: TPointArray;
    PartTypes: TPointTypeArray;
    i, j: integer;
    Index: integer;
    t_matrix: pMatrix;
    Curve: TFlexCurve;
begin
 t_matrix := update_transformations(PolygonMesh.OCS_WCS, OCS);
 SetLength(Points, PolygonMesh.numvertices);
 for i:=0 to PolygonMesh.numvertices-1 do
  Points[i] := MapFn(PolygonMesh.polypoints[i], t_matrix);
 // draw the M N-length polylines - we can use the array directly
 //if closeN then begin
 if dgPolygonMesh in FGroups then begin
  Result := TFlexGroup.Create(FFlex, FlexParent, FFlexLayer);
  Result.Name := PolygonMesh.proper_name;
  FlexParent := Result;
 end else
  Result := Nil;
 SetLength(PartPoints, PolygonMesh.N);
 for i:=0 to PolygonMesh.M-1 do begin
  Curve := TFlexCurve.Create(FFlex, FlexParent, FFlexLayer);
  Curve.IsSolidProp.Value := PolygonMesh.closeN;
  Curve.PenProp.Color := PolygonMesh.colour;
  Curve.PenProp.Width := FPenWidth;
  Move(Points[i * PolygonMesh.N], PartPoints[0],
    PolygonMesh.N * SizeOf(PartPoints[0]));
  //Curve.SetAbsPointsEx(PartPoints, Nil);
  SetCurveAbsPointsEx(Curve, PartPoints, Nil);
 end;
 // draw the N M-length polylines - we need to hop along the array in M steps
 if (PolygonMesh.M > 0) and (PolygonMesh.N > 0) then begin
  SetLength(PartPoints, PolygonMesh.M * PolygonMesh.N);
  SetLength(PartTypes, Length(PartPoints));
  Index := 0;
  for i:=0 to PolygonMesh.N-1 do begin
   for j:=0 to PolygonMesh.M-1 do begin
    PartPoints[Index] := Points[j * PolygonMesh.N + i];
    PartTypes[Index] := ptNode;
    inc(Index);
   end;
   if PolygonMesh.closeM
    then PartTypes[Index-1] := ptEndNodeClose
    else PartTypes[Index-1] := ptEndNode;
  end;
  Curve := TFlexCurve.Create(FFlex, FlexParent, FFlexLayer);
  Curve.PenProp.Color := PolygonMesh.colour;
  Curve.PenProp.Width := FPenWidth;
  SetCurveAbsPointsEx(Curve, PartPoints, PartTypes);
 end;
end;

function TFlexDxfFormat.LoadPolyfaceMesh(PolyfaceMesh: Polyface_Mesh_;
  OCS:pM; FlexParent: TFlexControl): TFlexGroup;
var Points: TPointArray;
    i, j, inx, len: integer;
    t_matrix: pMatrix;
    Curve: TFlexCurve;
begin
 t_matrix := update_transformations(PolyfaceMesh.OCS_WCS, OCS);
 if dgPolyfaceMesh in FGroups then begin
  Result := TFlexGroup.Create(FFlex, FlexParent, FFlexLayer);
  Result.Name := PolyfaceMesh.proper_name;
  FlexParent := Result;
 end else
  Result := Nil;
 for i:=0 to PolyfaceMesh.numfaces-1 do begin
  SetLength(Points, 4);
  len := 0;
  for j:=0 to 3 do begin
   inx := PolyfaceMesh.facelist[i].nf[j];
   if inx<0 then break; // index -> -1 = end of vertices
   Points[j] := MapFn(PolyfaceMesh.polypoints[inx], t_matrix);
   inc(len);
  end;
  if len > 0 then begin
   SetLength(Points, len);
   Curve := TFlexCurve.Create(FFlex, FlexParent, FFlexLayer);
   Curve.IsSolidProp.Value := true;
   Curve.PenProp.Color := PolyfaceMesh.colour;
   Curve.PenProp.Width := FPenWidth;
   SetCurveAbsPointsEx(Curve, Points, Nil);
  end;
 end;
end;

function TFlexDxfFormat.LoadCircle(Circle: Circle_; OCS:pM;
  FlexParent: TFlexControl): TFlexEllipse;
var pa,pb    : TPoint;
    t_matrix : pMatrix;
begin
 // Transform
 t_matrix := update_transformations(Circle.OCS_WCS, OCS);
 with Circle do begin
  pa := MapFn(aPoint3D(p1.x-radius, p1.y+radius, p1.z-radius), t_matrix);
  pb := MapFn(aPoint3D(p1.x+radius, p1.y-radius, p1.z+radius), t_matrix);
 end;
 // Create control
 Result := TFlexEllipse.Create(FFlex, FlexParent, FFlexLayer);
 Result.Name := Circle.proper_name;
 Result.PenProp.Color := Circle.colour;
 Result.PenProp.Width := FPenWidth;
 Result.DocRect := Rect(pa.x, pa.y, pb.x, pb.y);
end;

function TFlexDxfFormat.LoadArc(Arc: Arc_; OCS:pM;
  FlexParent: TFlexControl): TFlexEllipse;
var pa,pb    : TPoint;
    t_matrix    : pMatrix;
begin
 // Transform
 t_matrix := update_transformations(Arc.OCS_WCS, OCS);
 with Arc do begin
  pa := MapFn(aPoint3D(p1.x-radius, p1.y+radius, p1.z-radius), t_matrix);
  pb := MapFn(aPoint3D(p1.x+radius, p1.y-radius, p1.z+radius), t_matrix);
 end;
 // Create control
 Result := TFlexEllipse.Create(FFlex, FlexParent, FFlexLayer);
 Result.Name  := Arc.proper_name;
 Result.PenProp.Color := Arc.colour;
 Result.PenProp.Width := FPenWidth;
 Result.DocRect := Rect(pa.x, pa.y, pb.x, pb.y);
 Result.BeginAngleProp.Value := Round(Arc.angle1 * 180.0 / pi * PixelScaleFactor);
 Result.EndAngleProp.Value := Round(Arc.angle2 * 180.0 / pi * PixelScaleFactor);
end;

function TFlexDxfFormat.LoadText(Text: Text_; OCS: pM;
  FlexParent: TFlexControl): TFlexText;
var pa, dummy1, dummy2: TPoint;
    FHeight: integer;
    //t_matrix: pMatrix;
    R: TRect;
begin
 Result := Nil;
 // Transform
 //t_matrix := update_transformations(Text.OCS_WCS, OCS);
 // kludgy method for scaling text heights
 with Text do begin
  dummy1  := MapFn(origin3D, nil);
  dummy2  := MapFn(aPoint3D(0,h,0), nil);
  pa      := MapFn(align_pt, OCS_WCS);
 end;
 FHeight := 2 + (dummy1.y - dummy2.y);
 if FHeight = 2 then exit;
 // Create text control
 Result := TFlexText.Create(FFlex, FlexParent, FFlexLayer);
 Result.Name  := Text.proper_name;
 Result.FontProp.Color := Text.colour;
 Result.FontProp.Height := FHeight;
 Result.TextProp.Text := Text.textstr;
 case Text.hor_align of
  0: Result.Alignment := taLeftJustify;
  1: Result.Alignment := taCenter;
  2: Result.Alignment := taRightJustify;
 end;
 // define text size
 Result.AutoSizeProp.Value := true;
 Result.AutoSizeProp.Value := false;
 // Set position
 R := Rect(0, 0, Result.Width, Result.Height);
 case Result.Alignment of
  taLeftJustify  : OffsetRect(R, pa.x, pa.y - Result.Height);
  taCenter       : OffsetRect(R, pa.x - Result.Width div 2,
                     pa.y - Result.Height);
  taRightJustify : OffsetRect(R, pa.x - Result.Width, pa.y - Result.Height);
 end;
 Result.DocRect := R;
end;

procedure TFlexDxfFormat.ImportFromStream(AStream: TStream;
  AFlexPanel: TFlexPanel; const Extension: TFlexFileExtension;
  const AFileName: string);
var
  Dxf: DXF_Object;

 procedure SetParameters(xmn,xmx,ymn,ymx:double; xm,ym:integer);
 var tempx,tempy : integer;
 begin
   with FMapParams do begin
     // Initial ClientWidth/Height
     tempx := 460 -2*xm;
     tempy := 460 -2*ym;
     if tempx<tempy then begin
       if (xmx-xmn)<>0
         then xscale := tempx/(xmx-xmn)
         else xscale := 1;
       if (ymx-ymn)<>0
         then yscale := tempx/(ymx-ymn)
         else yscale := 1;
     end else begin
       if (xmx-xmn)<>0
         then xscale := tempy/(xmx-xmn)
         else xscale := 1;
       if (ymx-ymn)<>0
         then yscale := tempy/(ymx-ymn)
         else yscale := 1;
     end;
     if xscale<yscale then yscale:=xscale else xscale:=yscale;
     xmid := (xmx+xmn)/2;
     ymid := (ymx+ymn)/2;
     {xmin := xmid - (ZoomBox.ClientArea.Width/2)/xscale;
     xmax := xmid + (ZoomBox.ClientArea.Width/2)/xscale;
     ymin := ymid - (ZoomBox.ClientArea.Height/2)/yscale;
     ymax := ymid + (ZoomBox.ClientArea.Height/2)/yscale;}
     xmin := xmn;
     xmax := xmx;
     ymin := ymn;
     ymax := ymx;
   end;
 end;
  
begin
  Dxf := DXF_Object.Create_from_stream(AStream, Nil);
  try
    with Dxf do
      SetParameters(emin.x, emax.x, emin.y, emax.y, 20, 20);
    Convert(AFlexPanel, Dxf);
  finally
    Dxf.Free;
  end;
end;

procedure TFlexDxfFormat.RegisterSupportedExtensions;
begin
  RegisterExtension('dxf', sDxfFileDescription, [skImport]);
end;

initialization
  RegisteredFlexFileFormats.RegisterFormat(TFlexDxfFormat);

end.

⌨️ 快捷键说明

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