Ruby extension group-rotation question


I’ve created and used Ruby extensions successfully for a while, but have run into a situation that is perplexing me…

I generate an object consisting of faces and lines. These faces and lines grouped e.g.

            model = Sketchup.active_model
            entities = model.active_entities
            line = entities.add_line ordered_line_coordinates
            group = entities.add_group line

this works well. I now want to rotate this via:

        model = Sketchup.active_model.active_entities[0]
        model_origin = model.transformation.origin
        tr = Geom::Transformation.rotation( model_origin, 'x', 45 )
        model.transform!( tr )

This does not work. I get the following error:
Error: #<NoMethodError: undefined method transformation’ for nil:NilClass>`

I’m obviously doing something wrong, but having a hard time figuring out what. I want, really, to rotate the group
I’ve been adding to, but I’m not sure how to retrieve that with active_model.

Thank you,

If you mean to rotate the group 45 degrees, the rotation operation needs an angle in radians. 45 degrees = PI * 45 / 180.

Also, the second value should be a vector such as [1,0,0] or X_AXIS.

You can simply type 45.degrees to get the angle in radians.

1 Like

You mentioned this before, but I never tested it. I tried to research this, but the only documentation I could find was this:

The degrees method is used to convert from degrees to radians.

For example 90.degrees would return 1.5707963267949


degrees = 90
radians = degrees.degrees
radians - a value in radians if successful
SketchUp 6.0

The note that it was for SketchUp 6 always made me leery of using it, but it works under 2017.

Very handy :slight_smile:

I’ve used it quite frequently. It isn’t always intuitive to read in code, especially the inverse.

PI.radians returns a value in degrees (180).

180.degrees returns a value in radians.

Yeah, IMHO those methods were defined backward. They tell you what units the value started in, not what units you want it converted to! It causes me repeated confusion.

You’re not the only one … I found this by Leonard Richardson:

I named the conversion method degrees by analogy to the methods like hours defined by Rails. This makes the code easy to read, but if you look at the actual numbers, it’s not obvious why 45.degrees should equal the floating-point number 0.785398163397448.

If this troubles you, you could name the method something like degrees_to_radians. Or you could use Lucas Carlson’s units gem, which lets you define customized unit conversions, and tracks which unit is being used for a particular number.

If the methods were named instead as .to_radians and .to_degrees it would have the dual advantage of being easier to remember which way the conversion is going, and also be more consistent with other unit-changing methods, like .to_int, .to_l , and .to_s


Back to your original question, @mvfpadilla, you need something like this. By using a named group (g, in this case), you can refer to it unambiguously.

    ordered_line_coordinates = [[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,0]]
    model = Sketchup.active_model
    entities = model.active_entities
    g = entities.add_group
    model_origin = model.axes.origin
    tr = Geom::Transformation.rotation(model_origin,X_AXIS,45.degrees)

BTW, the model.axes.origin is only defined for SketchUp 2016 forward … you could replace this with [0,0,0].

Thank you. With some minor changes (factoring out the entities.add_group, and adding lines and faces to it in an inner loop), this code seems to work. Thank you very much.

1 Like

The global object ORIGIN has been defined like, … forever. So @mvfpadilla, you do not need the model_origin reference at all:

    model_origin = model.axes.origin
    tr = Geom::Transformation.rotation(model_origin,X_AXIS,45.degrees)


    tr = Geom::Transformation.rotation(ORIGIN,X_AXIS,45.degrees)

Ref: Top Level Namespace — SketchUp Ruby API Documentation

(The global constants are not listed in alpha order for some reason.)

1 Like

For consistency with the methods used for lengths I think one should be called to_degrees and the other degrees. However it took my some time to get into that logic too. Apparently writing 10.m in Ruby is meant to correspond to writing 10 m in human text. I can agree this is quite backward though compared to the usual naming conventions.

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