Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Saving Diagram as Image is clipped if negative co-ordinates. How to solve this?
Saving Diagram as Image is clipped if negative co-ordinates. How to solve this? [message #533437] Thu, 13 May 2010 20:58 Go to next message
h1055071 is currently offline h1055071Friend
Messages: 335
Registered: July 2009
Senior Member
The problem is that if any of the diagram's child figures have a
negative x,y, location (for example, x = -100, y = -100) this portion is
not saved to the image. It seems that the figure only saves to image
from origin (0, 0).

I'm using a ScalableFreeformRootEditPart, and a FreeformLayer with
FreeformLayout for my diagram's main edit part.

This is how I save the diagram as an image:

ScalableFreeformRootEditPart rootEditPart =
(ScalableFreeformRootEditPart)diagramViewer.getRootEditPart( );

IFigure figure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);

Rectangle rectangle = figure.getBounds();

Image image = new Image(Display.getDefault(), rectangle.width,
rectangle.height);

GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
figure.paint(graphics);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[]{ image.getImageData() };
loader.save(file, SWT.IMAGE_PNG);

Any ideas? Thanks.

Phil
Re: Saving Diagram as Image is clipped if negative co-ordinates. How to solve this? [message #533495 is a reply to message #533437] Fri, 14 May 2010 09:25 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
This is not possible,

because if the image is clipped then the figure on the canvas also should be clipped.

because both are being drawn using GC,only difference being canvas or image.

can you provide a snippet to proove your point.


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Saving Diagram as Image is clipped if negative co-ordinates. How to solve this? [message #533507 is a reply to message #533495] Fri, 14 May 2010 10:21 Go to previous messageGo to next message
h1055071 is currently offline h1055071Friend
Messages: 335
Registered: July 2009
Senior Member
On 14/05/2010 10:25, vijay wrote:
> This is not possible,
>
> because if the image is clipped then the figure on the canvas also
> should be clipped.
>
> because both are being drawn using GC,only difference being canvas or
> image.
>
> can you provide a snippet to proove your point.

Hi, thanks for help!

I can't really provide more of a "snippet" than the following that I
have implemented:

My Editor extends GraphicalEditorWithFlyoutPalette.

My Graphical Viewer's Root EditPart is a ScalableFreeformRootEditPart:

ScalableFreeformRootEditPart rootEditPart = new
ScalableFreeformRootEditPart();
getGraphicalViewer().setRootEditPart(rootEditPart);

My domain model's main EditPart extends AbstractGraphicalEditPart and
creates a main layer as follows:

protected IFigure createFigure() {
FreeformLayer figure = new FreeformLayer();
figure.setLayoutManager(new FreeformLayout());
return figure;
}

This ensures that I can place child figures anywhere on the diagram
including in negative co-ordinates. If I do this, the scroll bars allow
me to see all of the diagram.

My "Save as Image" action is basically:

ScalableFreeformRootEditPart rootEditPart =
(ScalableFreeformRootEditPart)graphicalViewer.getRootEditPar t();

IFigure figure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);

Rectangle rectangle = figure.getBounds();

Image image = new Image(Display.getDefault(), rectangle.width,
rectangle.height);

GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
figure.paint(graphics);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(file, SWT.IMAGE_PNG);
Re: Saving Diagram as Image is clipped if negative co-ordinates. How to solve this? [message #533508 is a reply to message #533507] Fri, 14 May 2010 10:39 Go to previous message
h1055071 is currently offline h1055071Friend
Messages: 335
Registered: July 2009
Senior Member
On 14/05/2010 11:21, Phillipus wrote:
> On 14/05/2010 10:25, vijay wrote:
>> This is not possible,
>>
>> because if the image is clipped then the figure on the canvas also
>> should be clipped.
>>
>> because both are being drawn using GC,only difference being canvas or
>> image.
>>
>> can you provide a snippet to proove your point.
>
> Hi, thanks for help!
>
> I can't really provide more of a "snippet" than the following that I
> have implemented:
>
> My Editor extends GraphicalEditorWithFlyoutPalette.
>
> My Graphical Viewer's Root EditPart is a ScalableFreeformRootEditPart:
>
> ScalableFreeformRootEditPart rootEditPart = new
> ScalableFreeformRootEditPart();
> getGraphicalViewer().setRootEditPart(rootEditPart);
>
> My domain model's main EditPart extends AbstractGraphicalEditPart and
> creates a main layer as follows:
>
> protected IFigure createFigure() {
> FreeformLayer figure = new FreeformLayer();
> figure.setLayoutManager(new FreeformLayout());
> return figure;
> }
>
> This ensures that I can place child figures anywhere on the diagram
> including in negative co-ordinates. If I do this, the scroll bars allow
> me to see all of the diagram.
>
> My "Save as Image" action is basically:
>
> ScalableFreeformRootEditPart rootEditPart =
> (ScalableFreeformRootEditPart)graphicalViewer.getRootEditPar t();
>
> IFigure figure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
>
> Rectangle rectangle = figure.getBounds();
>
> Image image = new Image(Display.getDefault(), rectangle.width,
> rectangle.height);
>
> GC gc = new GC(image);
> SWTGraphics graphics = new SWTGraphics(gc);
> figure.paint(graphics);
>
> ImageLoader loader = new ImageLoader();
> loader.data = new ImageData[] { image.getImageData() };
> loader.save(file, SWT.IMAGE_PNG);


I found the answer. I need to add the following line:

graphics.translate(rectangle.x * -1, rectangle.y * -1);

So it is now:

ScalableFreeformRootEditPart rootEditPart =
(ScalableFreeformRootEditPart)graphicalViewer.getRootEditPar t();

IFigure figure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);

Rectangle rectangle = figure.getBounds();

Image image = new Image(Display.getDefault(), rectangle.width,
rectangle.height);

GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
graphics.translate(rectangle.x * -1, rectangle.y * -1);
figure.paint(graphics);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(file, SWT.IMAGE_PNG);
Previous Topic:Connection Bendpoints Snap to Grid?
Next Topic:Documentation for UML/OCL
Goto Forum:
  


Current Time: Thu Dec 18 19:32:12 GMT 2014

Powered by FUDForum. Page generated in 0.01693 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software