printing

Discuss and ask questions about CAD VCL (Delphi and C++ Builder).

Moderators: SDS, support, admin

Post Reply
slbarker
Posts: 12
Joined: 03 Nov 2005, 23:39
Location: New Zealand

printing

Post by slbarker » 17 Apr 2008, 06:17

Hi,

I have a TsgImage which also has an optional bmp overlay. I can print this ok using code similar to the examples.

However, I also have some custom annotations drawn directly onto the canvas. These are drawn during the OnPaint event.

What would be the best way to get these to print as well as the underlying TsgImage?

I've tried a few things but can't seem to get it to work, and wondered if you have any suggestions.

Is there a way to copy the TsgImage.Canvas to the Printer.Canvas so it includes all my annotations? Or should I alter my annotations so they re-paint (at the correct scale) onto the Printer.Canvas?

I can post my code if needed, but at the moment it contains much commented-out testing code so I would need to simplify the problem first.

regards,
Steve

support
Posts: 3272
Joined: 30 Mar 2005, 11:36
Contact:

Re: printing

Post by support » 24 Apr 2008, 17:29

Hi Steve,

Sorry for the delay.
We have sent you respective demo.

Here goes full text of the demo:

Code: Select all

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, sgImage, DXF, DXFImage, StdCtrls, Printers, ExtCtrls;

type

  TMyImage = class(TsgImage)
  protected
    FSubstrate: TPicture;
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Substrate: TPicture read FSubstrate;
  end;

  TForm1 = class(TForm)
    PrintDialog: TPrintDialog;
    Panel1: TPanel;
    Button1: TButton;
    ScrollBox1: TScrollBox;
    procedure FormCreate(Sender: TObject);
    procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure Button1Click(Sender: TObject);
  private
    FsgPaintBox: TMyImage;
  public
    property sgPaintBox: TMyImage read FsgPaintBox;
  end;

var
  Form1: TForm1;

implementation

uses Types;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
  cf: Double;
  W, H: Integer;
begin
  if PrintDialog.Execute then
  begin
    Printer.Orientation := poLandscape;
    Printer.Title := ChangeFileExt(ExtractFileName(Application.ExeName), '') + ' Document';
    Printer.BeginDoc;
    try
      cf := Printer.PageWidth / sgPaintBox.ClientWidth;
      R := sgPaintBox.PictureRect;
      W := R.Right - R.Left;
      H := R.Bottom - R.Top;
      if PtInRect(sgPaintBox.Parent.ClientRect, R.TopLeft) and
        PtInRect(sgPaintBox.Parent.ClientRect, R.BottomRight) then
      begin
        R.Left := 0;
        R.Top := 0;
        R.Right := Round(W * cf);
        R.Bottom := Round(H * cf);
      end
      else
      begin
        R.Left := Round(R.Left * cf);
        R.Right := R.Left + Round(W * cf);
        R.Top := Round(R.Top * cf);
        R.Bottom := R.Top + Round(H * cf);
      end;
      Printer.Canvas.StretchDraw(R, sgPaintBox.Substrate.Graphic);
      Printer.Canvas.StretchDraw(R, sgPaintBox.Picture.Graphic);
    finally
      Printer.EndDoc;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FsgPaintBox := TMyImage.Create(Self);
  FsgPaintBox.Parent := ScrollBox1;
  FsgPaintBox.Align := alClient;
  FsgPaintBox.LoadFromFile('Bracket.dxf');
  FsgPaintBox.IsBigScale := True;
  TsgDXFImage(FsgPaintBox.Picture.Graphic).Transparent := True;
  FsgPaintBox.Substrate.LoadFromFile('sample.bmp');
end;

procedure TForm1.FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  sgPaintBox.ChangeScale(False, 0.5, MousePos);
end;

procedure TForm1.FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  sgPaintBox.ChangeScale(False, 2, MousePos);
end;

{ TMyImage }

constructor TMyImage.Create(AOwner: TComponent);
begin
  inherited;
  FSubstrate := TPicture.Create;
  Transparent := True;
end;

destructor TMyImage.Destroy;
begin
  FSubstrate.Free;
  inherited;
end;

procedure TMyImage.Paint;
var
  R: TRect;
begin
  R := PictureRect;
  OffsetRect(R, -R.Left, -R.Top);
  Canvas.StretchDraw(R, FSubstrate.Graphic);
  inherited;
end;

end.
Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply