# How to get dimensions of rectangle in px?

How to get dimensions of the area selected?

The basic thing I can do is …
ss = Sketchup.active_model.selection
ent = ss[0];
origin = ent.transformation.origin;
if ent.class == Sketchup::Group
box = ent.local_bounds
end
if ent.class == Sketchup::ComponentInstance
box = ent.definition.bounds
end

The problem is that the box.max contains units instead of pixels…And I need to get or to calculate the dimensions of the rectange so I can try to change viewport.

The length in pixels would depend on the inclination of the face towards the camera (if it is not perfectly parallel).

Better convert the end points into screen space (pixels) and then compute the lengths. To convert a point to screen space, you need a reference to the view (either inside a Tool, or by `model.active_view`).
`point2d = view.screen_coords(point3d)`

1 Like

Thank you. That’s amazing. I tried this:

``````def getRectangleDimentions();
ss = Sketchup.active_model.selection
ent = ss[0];
origin = ent.transformation.origin;
if ent.class == Sketchup::Group
box = ent.local_bounds
end
if ent.class == Sketchup::ComponentInstance
box = ent.definition.bounds
end
mx = box.max.x;
my = box.max.y;
x = origin[0];
y = origin[1];
z = origin[2];
v = Sketchup.active_model.active_view;
a = v.screen_coords(Geom::Point3d.new(x, y, z))
b = v.screen_coords(Geom::Point3d.new(x, y+my, z))
c = v.screen_coords(Geom::Point3d.new(x+mx, y+my, z))
d = v.screen_coords(Geom::Point3d.new(x+mx, y, z))
#return [, y, z];
end;
``````

When I call the function I got this output:
Point3d(492.467, 398.246, 0.75473)

That’s what makes me confuse. What does mean this numbers and why are they displayed? It does not make sense to me how this happens. Maybe I did it wrong - should I avoid new in the function call?

In ruby, methods implicitely return the value of the last line. In this case, it is the Point3d named `d`.

I personally prefer to explicitely specify `return` when a method is intended to return something reusable. Otherwise the implicitely returned object may be something not intended for reuse, or it may change when someone refactors the code. If one wants to avoid that a method leaks out internal data one can put `nil` on the last line.

Thanks. It works like a charm.

``````def getRectangleDimentions();
ss = Sketchup.active_model.selection
ent = ss[0];
origin = ent.transformation.origin;
if ent.class == Sketchup::Group
box = ent.local_bounds
end
if ent.class == Sketchup::ComponentInstance
box = ent.definition.bounds
end
mx = box.max.x;
my = box.max.y;
x = origin[0];
y = origin[1];
z = origin[2];
v = Sketchup.active_model.active_view;
a = v.screen_coords(Geom::Point3d.new(x, y, z))
b = v.screen_coords(Geom::Point3d.new(x, y+my, z))
c = v.screen_coords(Geom::Point3d.new(x+mx, y+my, z))
d = v.screen_coords(Geom::Point3d.new(x+mx, y, z))
w1 = d[0]-a[0];
h1 = d[1]-c[1];
# w2 = c[0]-b[0];
# h2 = a[1]-b[1];
# puts "#{w} x #{h}"
# return [ x, y, w, h ]
return [a[0], c[1], w1, h1];
end;
``````

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