ModraUp Ruby API Documentation Ruby API Reference

Class: Layout::Image

Inherits:
Layout::Entity show all

Overview

Images on drawings (png, jpg, bmp, gif, webp, tif, svg). They are managed in a reference mode: the model only records the file path (relative or absolute), size, and modification time; it does not store pixel data. If the file is moved or deleted, a placeholder is displayed on the drawing. Rotation is controlled with Layout::Entity#rotation=, and cropping is managed with #clip_mask=.

Note: ModraUp extension. LayOut stores image pixels in the document; OpenSu only references the file.

Examples:

Insert an image and crop it to a circle

doc = ModraUp.active_model.layout_document
image = Layout::Image.new("/path/to/photo.jpg", Geom::Bounds2d.new(20.mm, 20.mm, 120.mm, 80.mm))
image.clip_mask = Layout::Ellipse.new(Geom::Bounds2d.new(40.mm, 20.mm, 80.mm, 80.mm))
doc.add_entity(image, doc.layers.active, doc.pages.first)

Class Method Summary # collapse

Instance Method Summary # collapse

Inherited from Layout::Entity

Class Method Details

Layout::Image.new(path, bounds) ⇒ Layout::Image

Create an image from a file and then add it to the drawing using Layout::Document#add_entity. When adding, read the file dimensions; if the dimensions cannot be read, raise an error. Multiple images from the same file share a single reference.

Parameters:

  • path (String) Image file
  • bounds (Geom::Bounds2d) Position and size on the layout (in inches)

Returns:

  • (Layout::Image)

Raises:

  • [ArgumentError] File does not exist

Examples:

image = Layout::Image.new("/path/to/plan.png", Geom::Bounds2d.new(1, 1, 4, 3))

Instance Method Details

image.clip_mask ⇒ Layout::Path, Layout::Rectangle, Layout::Ellipse, nil

Clipping Mask: The image is displayed only within the shape. Returns a copy of the shape (in paper coordinates) not in the document; when there is no mask, it returns nil.

After adding the image to the document, an axial corner mask returns a Layout::Rectangle, while others return a Layout::Path.

Returns:

  • (Layout::Path, Layout::Rectangle, Layout::Ellipse, nil)

Examples:

image = Layout::Image.new("/path/to/plan.png", Geom::Bounds2d.new(1, 1, 4, 3))
image.clip_mask = Layout::Rectangle.new(Geom::Bounds2d.new(1.5, 1.5, 2, 2))
image.clip_mask.class  #=> Layout::Rectangle

image.clip_mask = shape

Sets the clip mask; pass nil to remove the mask. The shape must be a closed rectangle, ellipse, or path, and not yet added to the document; after which the mask moves, scales, and rotates with the image. One step of undo.

Parameters:

  • shape (Layout::Path, Layout::Rectangle, Layout::Ellipse, nil) paper coordinates

Returns:

  • (Layout::Path, Layout::Rectangle, Layout::Ellipse, nil)

Raises:

  • [ArgumentError] The shape is not closed, does not intersect with the image, or is already in the document.
  • [TypeError] Not a shape

Examples:

doc = ModraUp.active_model.layout_document
image = Layout::Image.new("/path/to/plan.png", Geom::Bounds2d.new(1, 1, 4, 3))
doc.add_entity(image, doc.layers.active, doc.pages.first)
image.clip_mask = Layout::Ellipse.new(Geom::Bounds2d.new(2, 1, 3, 3))
image.clip_mask = nil

image.image_rep ⇒ ModraUp::ImageRep

The pixel from the current position in the image file.

Returns:

  • (ModraUp::ImageRep)

Raises:

  • [RuntimeError] File lost.
Note: ModraUp extension. The pixels come from the referenced file, not from the document.

Examples:

image = Layout::Image.new("/path/to/plan.png", Geom::Bounds2d.new(1, 1, 4, 3))
image.image_rep.width