How to select an area on a picture and crop it?

Imported an image, I wanted to draw lines to get a selected area, then cropped to it. How can I implement the operation?

Thanks @pbacot , I imported an image, and exploded it to get the texture image. But how to draw free lines and crop? It seems no such edit function in editing texture image.

:thinking: When you imported (and placed into the model) the image you should know its position and size.
Before you use the explode method. You should record the current entities (model active_entities) , because:

Note that current versions of SketchUp will return an empty array here. To work around this limitation you can iterate over your entities collection to determine which new entities were created.

So after exploding you can determine the difference of the model current active_entities to previously recorded.

Since you know the original image position and size you should be able to draw an edges (or a face) within this exploded image to crop.
Then you can select and delete the edges from the above determined difference. entities

Edit: Actually the documentation for explode method is confusing me, but in the SU version 2021, the example from above method extended with my snippet, crop out the middle of the image:

model = Sketchup.active_model
path = "Plugins/su_dynamiccomponents/images"
image_file = Sketchup.find_support_file("report_tool.png", path)
image = model.active_entities.add_image(image_file, ORIGIN, 300)
entities = image.explode

edges = entities.grep(Sketchup::Edge).to_a
p0 = [100,100,0]
p1 = [200,100,0]
p2 = [200,200,0]
p3 = [100,200,0]
crop = model.active_entities.add_face(p0, p1, p2, p3)
edges.each{|e| e.erase! if e.valid?}
1 Like

All the above should likely be done within a group so as not to merge with other geometry.

1 Like

Side note: When erasing in bulk, perfer entities.erase_entities over entity.erase!. Significant performance differences with larger amount of entities. (I’d recommend reaching for erase_entities as the default method to use when erasing entities.)

More performance recommendations can be found browsing the SketchUp RuboCop documentation:

(GitHub - SketchUp/rubocop-sketchup: Rubocop cops for SketchUp - test against our Extension Warehouse technical requirements and other pitfalls)

1 Like

Yea, okay.
As well as the entities.grep(Sketchup::Edge) does not need to convert to array… :blush:

1 Like