How should I calculate the scale factor of the pattern_fill_scale?

@tt_su
I have a picture of 100px * 100px.
Make it into material (width:600mm height:600mm).
When my drawing scale is 1:20, this picture should correspond to the drawing size (width:30mm height: 30mm), then how should I calculate the scale factor .

1:20 =0,05?

1 Like

No,the image (100px * 100px) => document (30mm * 30 mm).
I should get the relation, and This has nothing to do with the drawing scale
I need conversion between pixels and length.

Is this SketchUp or LayOut API?

Do you have a sample model/document to go along with this?

Layout API( Class: Layout::Style#pattern_fill_scale)

test.skp (142.7 KB)

document = Layout::Document.new
rate = 30
face = Sketchup.active_model.entities.grep(Sketchup::Face)[0]

rect = [face.bounds.width / rate, face.bounds.height / rate]
bounds = Geom::Bounds2d.new(0, 0, rect[0], rect[1])
model = Layout::SketchUpModel.new(Sketchup.active_model.path, bounds)
model.scale = 1.0 / rate
model.current_scene = 1
model.render_mode = Layout::SketchUpModel::RASTER_RENDER
model.line_weight = 0.1
document.add_entity model, document.layers.first, document.pages.first

rect = Layout::Rectangle.new(Geom::Bounds2d.new(rect[0], rect[1], rect[0], rect[1]))
mat_img_path = "#{face.material.name}.jpg"
face.material.texture.write(mat_img_path)
style = rect.style
style.pattern_filled = true
style.pattern_fill_path = mat_img_path
# How to calculate the correct pattern_fill_scale
style.pattern_fill_scale = face.material.texture.image_width.to_f / face.material.texture.width 
rect.style = style
document.add_entity rect, document.layers.first, document.pages.first
document.save File.join(__dir__, 'test.layout')

This pattern is a seamless map. In SketchUp, it corresponds to 350mm * 210mm.
reinforced concrete_350x210

I hope the two places (model and rectangle) look the same.

@adam - do you have some suggestions for this?

1 Like

Hi @javalitterboy,

To do this correctly, you need to know the width in pixels of your pattern fill image, the pixels per inch of that image, and the scale of the image in your viewport.

For example, in my case when I extracted your pattern fill to use in LayOut, I got an image that is 474 x 284 pixels, at 300dpi. That translates to (474/300 = 1.58"). That means that in LayOut paper space, the pattern fill will fill a rectangle that is 1.58" wide exactly once. In your case, you have a SketchUp face that is .7m, but your pattern fill is set to .35m scale. That means that it will repeat 2x for that face’s width. With that being the case, you want the LayOut pattern fill image to do the same.

To calculate this, you need to take the width of your LayOut rectangle and divide it by the ratio of the face and material texture (rect[0] / (1.58 * (face.bounds.width / face.material.texture.width)) in your case here). I got a value of 0.435 for the scale of the pattern fill in LayOut.

Hope that helps,
Adam

2 Likes

Thank you. I’ll try.

Yes, It’s ok.

1 Like