How can I do if I want to solve this problems?(ruby script)

I want to extract the x,y length value of the face that I draged.

Please refer to the attached picture and red box mark.

Could you tell me some script for this?

Please post code correctly, not images of code …


FYI, in the SketchUp world, width is x-wise. depth is y-wise.


width = group.bounds.width
depth = group.bounds.depth

… or if the bounding box of the group is larger because it contains other elements …

# If you still have a reference to the face, ...
# and if the face is aligned with the model axis:
width = face.bounds.width
depth = face.bounds.depth

Otherwise …

face = group.entities.grep(Sketchup::Face).find {|face| face.normal == Z_AXIS }
if face # always test to be sure something was found !
  # Find an edge whose line vector is parallel to the X axis:
  xface = face.edges.find {|edge| edge.line[1].parallel?(X_AXIS) }
  width = xface ? xface.length : 0
  # Find an edge whose line vector is parallel to the Y axis:
  yface = face.edges.find {|edge| edge.line[1].parallel?(Y_AXIS) }
  depth = yface ? yface.length : 0
  # Test for zero lengths as a failure condition !
end

But again this assumes that the face and it’s group have not been rotated away from the model axis.

If the group has been rotated, then you cannot test against the model’s axis.
So, you may need to test using group.transformation.xaxis() and group.transformation.yaxis() methods.


model = Sketchup.active_model
entities = model.active_entities

width = face.bounds.width
depth = face.bounds.depth

pts =
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]

face = entities.add_face(pts)


or

model = Sketchup.active_model
entities = model.active_entities

width = face.bounds.width
depth = face.bounds.depth

pts =
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]

face = entities.add_face(pts)


and unknown error has occured and it says

“undefined method ‘bounds’ for nil:NilClass”

or

“undefined method ‘group’ for nil:NilClass”

How can I fix this?

You need to look at some example code and understand what you are doing.
The first errors show that you haven’t set up a reference to ‘group’, so you can’t find its bounds and so on…
The next throws an error because you haven’t initially set a reference to ‘face’, so you can’t find its bounds and so on, later on you do set up ‘face’ and could find its ‘bounds’ after that…
But you code is self-referencing !
You need to establish ‘width’ and ‘depth’ in some other way…
You can’t find something about ‘face’ before it exists.
After it exists you don’t need the info…

Is your ‘draged’ actually ‘drew’ ?
How do you get the x/y values ?

Your thinking/explanation is very confusing !
Please explain in logical steps what you are trying to do…
Then perhaps someone can help you…
As it is we are going around in circles…

1 Like