Problems with ChangeScale method

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

Moderators: SDS, support, admin

Post Reply
fabzouni
Posts: 10
Joined: 15 May 2006, 14:29
Location: France

Problems with ChangeScale method

Post by fabzouni » 15 May 2006, 14:48

Hi,

I'm using TsgImage and TsgDxfImage with delphi 2005.
I would like to display an area (define by a rectangle) of a dxf file into a tSgImage, but i've some problems (bug maybe) with TsgImage.changeScale method.

To do this, I've made the following procedure
I think there is something wrong with "MyImage.GetPoint(" and
"TsgImage.changeScale"
If someone as an solution, I would be great.
Thanks.

Code: Select all

Procedure TFormDxfPlan.ZoomOnRect(Xinf,YInf,XSup,YSup : Double);
Var
    MyImage : TsgDXFImage;

    RectWidth, RectHeight : double ;
    sgWidth, sgHeight : Double;
    ZoomFactor : double ;
    RectCenter3D : TFPoint ;
    ScreenRectCenter : Tpoint ;
    ZoomInOneStep : boolean ;

begin
  // Here we use sgImage1 (TsgImage) to display an autocad file (dwg or dxf format)
  // with a TsgDXFImage

  // This procedure is used to zoom on the rectangle define by the cad coordinates
  // given in paramaters

  // To display all the contens of the dwg file
  sgImage1.Align := AlNone;
  sgImage1.Align := alClient;
  sgImage1.Refresh;
  // MyImage has allready loaded the autocad file
  MyImage        :=  TsgDXFImage(sgImage1.Picture.Graphic);

  // dimension of the rectangle to display
  RectWidth    := (XSup-XInf);
  RectHeight    := (YSup-YInf);
  // dimension of the whole dxf image
  sgWidth        := MyImage.Width;
  sgHeight       := MyImage.Height;
  // zoom factor needed to display the rect into sgimage
  ZoomFactor    := Min( (sgWidth / RectWidth) , (sgHeight / RectHeight) );
  // Center Point of the rectangle
  RectCenter3D.x       := ((XSup+XInf)/ 2);
  RectCenter3D.y       := ((YSup+YInf)/ 2);
  RectCenter3D.z       := 0 ;

  ZoomInOneStep := {TRue ;//} False ;
  if ZoomInOneStep then
  begin
    // If ZoomInOneStep is true the scale is changed in one step (it dont works)

    // convert rectcenter (cad coordinates) into screen coordinates
    ScreenRectCenter   := MyImage.GetPoint(RectCenter3D);
    // Zoom factor is Ok but the view is not centered around ScreenRectCenter
    // but around the center of MyImage
    sgImage1.ChangeScale( True , ZoomFactor , ScreenRectCenter )   ;
  end
  else
  begin
      // if ZoomInOneStep is False the scale is change with many step
      // to get a better result. It's better then in one step (closer to the rectangle to display)
      // but not really good either
      while sgImage1.Scale < ZoomFactor do
      begin
        // convert rectcenter (cad coordinates) into screen coordinates
        // PROBLEM : for each step, this value must be updated but it s not
        // It allways the walue of the first step that I get
        ScreenRectCenter   := MyImage.GetPoint(RectCenter3D);
        sgImage1.ChangeScale( false , 1.2 , ScreenRectCenter ) ;
        sgImage1.Refresh;
      end;
  end;

end;


fabzouni
Posts: 10
Joined: 15 May 2006, 14:29
Location: France

Post by fabzouni » 18 May 2006, 11:37

Hi,

If someone is interrested I found the solution to my problem.
What i do Is :
From cad coord of the rect to display
* calculate the zoom factor to display this rect
* find the center point of this rect
* changing the scale not in one step (it dont works)
- Get the coordinate of the rect center (in pixel) into the sgImage
- Move the sgImage into his parent (scrollbox)to put the rectcenter into the midle of the parent
- Zooming In ( rate 1.2)
- Doing many iteration of this since the scale is the needed zoom factor

This is the code :

Code: Select all

Procedure TFormDxfPlan.ZoomOnRect(Xinf,YInf,XSup,YSup : Double);
Var
    MyImage : TsgDXFImage;

    RectWidth, RectHeight : double ;
    sgWidth, sgHeight : Double;
    ZoomFactor : double ;
    RectCenter3D : TFPoint ;
    ScreenRectCenter : Tpoint ;
begin
  // Here we use sgImage1 (TsgImage) to display an autocad file (dwg or dxf format)
  // with a TsgDXFImage

  // This procedure is used to zoom on the rectangle define by the cad coordinates
  // given in paramaters

  // To display all the contens of the dwg file
  sgImage1.Align := alClient;
  sgImage1.Refresh;
  sgImage1.Align := AlNone;
  // MyImage has allready loaded the autocad file
  MyImage        :=  TsgDXFImage(sgImage1.Picture.Graphic);

  // dimension of the rectangle to display
  RectWidth    := (XSup-XInf);
  RectHeight    := (YSup-YInf);
  // dimension of the whole dxf image
  sgWidth        := MyImage.Width;
  sgHeight       := MyImage.Height;
  // zoom factor needed to display the rect into sgimage
  ZoomFactor    := Min( (sgWidth / RectWidth) , (sgHeight / RectHeight) );
  // Center Point of the rectangle
  RectCenter3D.x       := ((XSup+XInf)/ 2);
  RectCenter3D.y       := ((YSup+YInf)/ 2);
  RectCenter3D.z       := 0 ;

  // the scale is change with many step
  // to get a better result. It's better then in one step
  while sgImage1.Scale < ZoomFactor do
  begin
    // convert rectcenter (cad coordinates) into screen coordinates
    ScreenRectCenter   := MyImage.GetPoint(RectCenter3D);
    // put the center of the rect into the middle of the parent scrollbox
    sgImage1.Left := ( scrollBox1.width div 2 ) - ScreenRectCenter.X ;
    sgImage1.Top := ( scrollBox1.height div 2 ) - ScreenRectCenter.Y ;
    // The rect center coordinates are now the center of the scroll box
    ScreenRectCenter.X := scrollBox1.width div 2 ;
    ScreenRectCenter.Y := scrollBox1.height div 2 ;
    sgImage1.ChangeScale( false , 1.2 , ScreenRectCenter ) ;
    sgImage1.Refresh;
  end;       

end;

Post Reply