Ruby API - How to get Image corners?

Is there a way to get Image corner points?

The Bounding Box corners method does not work for this.

Thanks in advance!

Why? Is the Image object rotated or scaled ?

If so then you likely need to use the image instance’s #transformation. (FYI, an Image object is a special flavor of ComponentInstance, who’s definition has it’s #image? flag set to true. Because manipulating Image objects directly can easily crash SketchUp, they do not have a direct #definition getter method. If you need to access it’s definition’s face and edges, then you’ll need to use one of the Enumerable search iterators, like #find, to search through the DefinitionList collection. But again, do not manipulate the image’s definition or it’s entities via code.)

You also have #origin, #height, and #width instance methods available.

1 Like

Thank you for your reply and for solving my problem!

Entities inside Images do not share the same position as entities outside of Image.

The code below shows this by using the same vertices positions of a face inside an image to create construction points inside the image vs outside the image.

model = Sketchup.active_model
model_entities = model.entities
definitions = model.definitions

images_definitions = definitions.find_all(&:image?)
images_definitions.each do |image_definition|
  image_entities = image_definition.entities
  face = image_entities.grep(Sketchup::Face)[0]
  vertices = face.vertices

  # Create Construction Points Inside Images
  vertices.each { |vertex| image_entities.add_cpoint vertex.position }

  # Create Construction Points Outside Images
  vertices.each { |vertex| model_entities.add_cpoint vertex.position }
end

Thanks for the help!

Here is weird behavior when moving the image entities…

Untitled-1

1 Like

Apply the image’s transformation to your vertices’ positions, then the resultant cpoints should be correctly located relative to the image in its context ?

2 Likes

Of course not. As said above …

So they and their entities behave just like any group or component instance.

And did I not also say …

? :roll_eyes:

1 Like

Applying the image’s transformation did work…Thanks You!

model = Sketchup.active_model
model_entities = model.entities
definitions = model.definitions

images_definitions = definitions.find_all(&:image?)
images_definitions.each do |image_definition|
  image_entities = image_definition.entities
  face = image_entities.grep(Sketchup::Face)[0]
  vertices = face.vertices
  tr = image_definition.instances[0].transformation # <-- Image transformation

  # Create Construction Points Outside Images
  vertices.each do |vertex|
    pt = vertex.position.transform tr # <-- New Point with Image transformation
    model_entities.add_cpoint pt
  end
end

Everything you said was on point…Again thank you for the help!

I can’t figure out how to get the Image entity from Image’s definition. Or get Image’s definition from image entity

I can get the image as an entity with the following code…

image = Sketchup.active_model.selection.grep(Sketchup::Image)

But, from there I can’t find a way to get the image’s definition.

To get Image definition I can do the following…

images_definitions = Sketchup.active_model.definitions.find_all(&:image?)
image_definition = images_definitions[0]

But, from there I can’t find a way to get the image entity which typename = ‘Image’.
How can I add Image to the current selection from having its image definition then?

I am very confused about this and I’ll appreciate the help.

You already showed you know how to get at the image definitions.

images_definitions = Sketchup.active_model.definitions.find_all(&:image?)

… so finding the one that matches the image reference is a matter of searching for match.

Like all component definitions, it has an #instances collection, that in this case contains references to Sketchup::Image instances rather than Sketchup::ComponentInstance or Sketchup::Group instances.

To find the definition that matches the image reference, you search for the definition whose #instances collection contains the reference to image

image_definition = images_definitions.find { |idef|
  idef.instances.include?(image) 
}

The #find search method returns the object (in this case the definition) when the block returns true and otherwise nil if it is not found.

Sidenote: This is how we used to need to find group definitions before the API exposed a #definition getter method.

selection = Sketchup.active_model.selection
selection.add(image_definition.instances.first)

… or one of the image’s other instances if it’s been copied. You would decide how to choose amongst the instances based upon transformation or some attribute.

1 Like

Thanks for helping me out! :star_struck:

1 Like