Rotate face in ruby
-
Hello !
I am a beginner with ruby, and I want to rotate a face, and retrieve the new face
I have the following code:` model = Sketchup.active_model # Open model
entities = model.entities # All entities in model
selection = model.selection # Current selectionpt1 = [x,y,0]
pt2 = [x1,y1,0]
pt3 = [x1,y1,h]
pt4 = [x,y,h]
fc = entities.add_face pt1,pt2,pt3,pt4,pt1tr = Geom::Transformation.rotation ptm,vector,angle
bas1 = entities.transform_entities(tr,fc)`this returns a status, what I want is the face after rotation. If I try anithing else, I always end up with an error
Is the rotated face still fc or is it an other one and how can I find the new one ?
-
You didn't show us how you get/set
ptm,vector,angle
but let's assume they are setup properly, because you'd get worse errors otherwise
The face '
fc' should remain a reference to the face even after it's transformed: however, it it lands on top of or overlapping another pre-existing face you might loose the reference, or find it refers to an unexpected face...
If you are doing the transformations of a limited number of faces within a container context [some_group.entitiesetc] then you have more control of what's there already, compared with themodel.entities...
If you want real comfort consider adding a temp-group and putting your new face inside that:
**gp = entities.add_group()** fc = **gp.**entities.add_face(pt1,pt2,pt3,pt4)
[Note: there's no need to repeatpt1at the end - unlike with 'add_edges()' if you want all 4 edges created]
Then you can transform the temp-group with 'gp.transform!(tr)'... OR transform the entities inside the temp-group:
gp.entities.transform_entities(tr, gp.entities.to_a)
Although, because the temp-group is unlikely to share the same ORIGIN, you would then need to consider the point about which you are rotating [ptm], relatively to thegp.transformation.origin- Hint: after the face is created insidegpuseptm.offset!(gp.transformation.origin.vector_to(ptm))... THEN define thetrtransformation using the correctedptm
At this point 'fc' is still the same face, because it cannot have interacted with other geometry...
After doing whatever it is you are doing
explode the temp-group gp.explode... -
Thank you very much. This has really been helpful. It was OK the first time I tried
This is the end result:

Advertisement