Import PNG Image

image
import

#1

Hello

I am new to Ruby and Sketchup API.
I am searching how to import and apply a transformation to a PNG image.
I want to automate the same behaviour than when you use import a PNG from the file menu, set the origin and set the width of the image, but automatically with the API.
I did experiments the model.import:

model = Sketchup.active_model
status = model.import("/private/tmp/background.jpg", false)

The image is imported, but stick to the mouse pointer and I have still to click and put the scale from mouse.
How could I also set the origin and width from the API only ?

As a bonus, I would like to download the png from a URL and not anymore from a file.png.

Fabrice


#2

It seems I managed to do it from texture :

model = Sketchup.active_model`
model.start_operation(‘Create background’, true)
materials = model.materials
material = materials.add(‘Background’)
material.texture = "/private/tmp/background.jpg"
material.texture.size = [width.mm, height.mm]
entities = model.active_entities
points = [
Geom::Point3d.new(0, 0, 0),
Geom::Point3d.new(width.mm, 0, 0),
Geom::Point3d.new(width.mm, height.mm, 0),
Geom::Point3d.new(0, height.mm, 0)
]
face = entities.add_face(points)
face.material = material
face.back_material = material
model.commit_operation

Is it the good way to do it ?
It possible to load the texture directly from a png through an URL


#3

In my opinion, I believe it is best if you become familiar with Ruby before you can apply the additions that the SketchUp API adds to the Ruby environment.


The simpliest way is to use this method:

Sketchup::Entites#add_image()

Try giving a valid URL for the path argument, to your remote file.

But be aware that some files types (with regard to certain operations) are edition or platform dependant (ie, Pro only, or Mac only.) Ref:
https://help.sketchup.com/en/article/3000153


Breaking down you desire a bit more:

You already found the Sketchup::Model#import() method.

In the API images are imported wrapped within a special component subclass.
This class has methods for setting it’s transformation, height, width, origin, etc.


#4

Hello,
Thank you very much. You just replace 30 lines of codes by 10 ^^.
Have a nice day.
Fabrice


#5

I wouldn’t necessarily agree on this point. You always need to start somewhere and it can just as well be making plugins/scripts for SketchUp as anything else. Having a practical goal can be very motivating when learning something new.


#6

Fine, I edited my previous statement, viz:

This is my opinion, I’ve stated it manys times. There is nothing that will ever make me feel any different.

Again and again, over and over I see newbies struggle needlessly, when they’d not do so if they had done like I, and read the “PickAxe” book cover to cover before even attempting any SketchUp programming.


#7

And my opinion that I’ve stated numerous times is that learning by doing is the best way to learn. I guess we just have to agree to disagree.


#8

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.