How i find the size of the face in the model?


#1

i try to find the face in the sketch model. and to find the scale and the size (height, width, depth)
with the ruby api language


#2

The Ruby API has the object types that can be drawn and displayed (i.e. drawing elements) accessed with an “entities” method. The “entities” method returns a collection of entities, which has some of the methods that support array like operations using the brackets “[]”.
The API describes the method at: http://www.sketchup.com/intl/en/developer/docs/ourdoc/model#entities

A specific entity can be accessed from the collection with an integer index similar to array indexing as the following Ruby statement demonstrates:

face = Sketchup.active_model.entities[4]

The exact statement above could provide a face object if the previous entities 0 to 3 were edges defining a rectangle for example, and the fifth entity was the face associated with the four edges. They would have to be loose ungrouped drawing elements.

The area would then be:

area = face.area

The API is: http://www.sketchup.com/intl/en/developer/docs/ourdoc/face#area

That is the simple version. Of course, you likely don’t know the index number of the entity, and it is likely nested inside other groups and/or components. Accessing a face buried inside of groups and components requires method chaining “entities” methods for groups and “definition.entities” for components.

An alternative is to use some sort of selection tool that allows you to identify a clicked face. A selection tool could give you a reference to the face object like the variable “face”, but you are not done. The face class’s “area” method determines the area based on the internal dimensions of the edges that define the face. What if the face is nested inside of a series of components that are scaled differently? For example, if the component the face is in has been shrunk in all dimensions by a factor of 2, are you still interested in the original area, or are you really interested in the area divided by 2 twice?

As mentioned in the API for “area” method, it accepts an optional argument called “transform” to convert the inner dimensional data to outer data (account for scaling to get the “visual” area of the face).


#3

When working with DC components and some formula resize face’s area, the Face#area(transformation) still works in that case?
If yes, how could I take the “result of the formula” and apply it on some transformation to get the new area?


#4

There are no such properties as scale, width, height for faces. A “Face” can be of any shape and even contain holes. width and height cannot be applied to that - unless you simply consider the rectangular bounds of the polygon. Also height doesn’t apply either as all faces are planar.
Scale also isn’t a property of a face. It is however a property of the transformation for the group or component that contains it.

Can you elaborate a bit more on what you are looking to do? (including screenshots or models is good)


#5

@kimpastro
I’m not sure what you are asking, but I will provide an example.
I went and downloaded a model from the 3D warehouse that uses scaling in dynamic components. It is called “Scalable Object - Skalierbares Objekt dyn. Component” by modeler Rosto99, dated 3/18/14. The downloaded file name is “Objekt+skalieren.skp”. An image is below:

I have used a personal plugin tool to high lite the closest face. My selection tool says that the face is obtained from entities[5].definition.entities[8]. I can obtain the entities, transform, and areas as follows at the Ruby console:

  red_object = Sketchup.active_model.entities[5]
  tform = red_object.transformation
  face = Sketchup.active_model.entities[5].definition.entities[8]
  intrinsic_area = face.area
  extrinsic_area = face.area( tform )
  puts "areas => #{intrinsic_area}; #{extrinsic_area}"

Now the intrinsic area is fixed, but if I use Sketchup’s “Interact” tool to click on one of the rulers, the model rescales. The selected face only resizes when I click the rulers representing X and Z axes scaling.

After I activate the dynamic component with a Interact tool mouse click, the “intrinsic_area” does not change, but the code above has to be re-run to obtain the new transformation and the extrinsic area re-calculated.

Now my testing required that I perform a mouse click with the Interact tool. If you want to do the entire operation in Ruby code, you need to know how to activate the dynamic component with the undocumented global variable $dc_observers. You can google it, but since it is undocumented, it is subject to change.