# Transforming picked face

**URL:** <https://forums.sketchup.com/t/transforming-picked-face/214292>\
**Category:** Ruby API\
**Created:** [November 28, 2022, 10:17am UTC](https://forums.sketchup.com/t/transforming-picked-face/214292 "2022-11-28T10:17:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sketchupmaster](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/sketchupmaster/32/1301_2.png) [@sketchupmaster](https://forums.sketchup.com/u/sketchupmaster)\
**Post date:** [November 28, 2022, 10:17am UTC](https://forums.sketchup.com/t/transforming-picked-face/214292/1 "2022-11-28T10:17:18Z")

</div>

thanks 😃

how to transform picked face `@face`

Is it possible ?

```auto
dist = 0.3.m
tvec = Geom::Vector3d.new([dist, 0, 0])
Sketchup.active_model.entities.transform_entities(Geom::Transformation.translation(tvec),@face)

```

Thanks

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [November 28, 2022, 1:56pm UTC](https://forums.sketchup.com/t/transforming-picked-face/214292/2 "2022-11-28T13:56:25Z")

</div>

> [@sketchupmaster](#):
>
> how to transform picked face `@face`
> 
> Is it possible ?

Yes. That is possible. Given the code snipped you posted I presume you weren’t getting the expected results?

I’d recommend starting a new thread as this sounds like a different (while tangent) topic. Explain what you have tried and what you got vs what you expected.

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [November 28, 2022, 3:07pm UTC](https://forums.sketchup.com/t/transforming-picked-face/214292/3 "2022-11-28T15:07:31Z")

</div>

The three lines you posted will fail because `@face` isn’t defined.  
If you add a extra line at the start it works OK for me…  
Try adding:  
`@face = Sketchup.active_model.selection[0]`

---

<div class="post-metadata">

**Author:** ![sketchupmaster](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/sketchupmaster/32/1301_2.png) [@sketchupmaster](https://forums.sketchup.com/u/sketchupmaster)\
**Post date:** [November 28, 2022, 7:25pm UTC](https://forums.sketchup.com/t/transforming-picked-face/214292/4 "2022-11-28T19:25:28Z")

</div>

Hello,  
I have reference Dezmo post the code which has use in

```auto
class Testtool
  def initialize
    @face = nil
    @tr = IDENTITY
  end

  def get_trans(ph, obj)
    ph.count.times{|i|
      if ph.leaf_at(i) == obj
        return ph.transformation_at(i)
      end
    }
    IDENTITY
  end

  def onMouseMove(flags, x, y, view)
    ph_fe = view.pick_helper
    ph_fe.do_pick(x, y, 0)
    face = ph_fe.picked_face
    @face = face ? face : nil
    @tr = get_trans( ph_fe, face ) if face
    view.invalidate
  end

  def onLButtonUp(flags, x, y, view)
       @dist = 0.3.m
       # Vector X-axes, Y-axes, Z-axes,
	   @tvec = Geom::Vector3d.new([@dist, 0, 0]) # Vector X-axes
    if @face
       #@face.pushpull @dist, false, # work properly
       Sketchup.active_model.entities.transform_entities(Geom::Transformation.translation(@tvec),@face.parent)
       # face transforming but something wrong
    end
    view.invalidate
	view.refresh
  end

  def draw(view)
    if @face
      loop = @face.outer_loop.vertices.map{|v|
        v.position.transform( @tr )
      }
      view.drawing_color = [255,20,147, 75] 
      view.draw(GL_POLYGON, loop)
    end
  end
end
Sketchup.active_model.select_tool(Testtool.new)

```

please check and revert  
thanks

---

<div class="post-metadata">

**Author:** ![sketchupmaster](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/sketchupmaster/32/1301_2.png) [@sketchupmaster](https://forums.sketchup.com/u/sketchupmaster)\
**Post date:** [November 29, 2022, 5:41am UTC](https://forums.sketchup.com/t/transforming-picked-face/214292/5 "2022-11-29T05:41:51Z")

</div>

yes it is working but please check this post [#select face](https://forums.sketchup.com/t/highlight-the-selection-of-a-face/138820/14)

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [November 29, 2022, 9:52am UTC](https://forums.sketchup.com/t/transforming-picked-face/214292/6 "2022-11-29T09:52:25Z")

</div>

Thomas recommended you already to start new topic and explain what you want to achieve…I’m not sure why you ignored…  
Neverminded.

* * *

In [Geom::Vector3d](https://ruby.sketchup.com/Geom/Vector3d.html) constructor you can see parameters do not need to be enclosed in square brackets.

You do not need to use instance variables (starting with @ ) in a method, if you will not use in other method.  
e.g: ~~`@dist = 0.3.m`~~ \>\> ˛`dist = 0.3.m`  
`tvec = Geom::Vector3d.new( dist, 0, 0 ) `

“axes” is plural, “axis” is singular. e.g.: X-ax **i** s and Y-ax **i** s and Z-ax **i** s form the ax_ **e** _s.

* * *

Long lines in a code make hard to read in a forum (or anyway):

```auto
Sketchup.active_model.entities.transform_entities(Geom::Transformation.translation(@tvec),@face.parent)

```

better to “brake” it e.g.:

```ruby
model = Sketchup.active_model
entities = model.entities
tr = Geom::Transformation.translation( tvec )
entities.transform_entities( tr, @face.parent )

```

* * *

The [Model #entities](https://ruby.sketchup.com/Sketchup/Model.html#entities-instance_method) method returns an [Entities](https://ruby.sketchup.com/Sketchup/Entities.html) object containing the entities in the root of model.

Be aware [Model #active\_entities](https://ruby.sketchup.com/Sketchup/Model.html#entities-instance_method) can be different.  
\_\_  
The [Entities #transform\_entities](https://ruby.sketchup.com/Sketchup/Entities.html#transform_entities-instance_method) method is used to apply a transform to several sub-entities all at once.  
_If you are transforming entities in the active drawing context or any of its parent drawing contexts, the transformation will be interpreted as relative to the global coordinate system. Otherwise the transformation will be interpreted as being on the local coordinate system._

The entities given as a parameter to this method should be part of the same entities collection without nesting.  
In my original example a highlighted face can be in any deep of nesting. Therefore the @face can be nested in a group in group in component … in model entities. Making a transformation as you did, can cause unexpected result.

Moreover:  
The [Entity #parent](https://ruby.sketchup.com/Sketchup/Entity.html#parent-instance_method) method is used to retrieve the parent of the entity. The parent will be a ComponentDefinition, a Group, or a Model, whatever the entity is contained within.  
Therefore your method likely will transform the entities of this returned definition and not the instance.

Would be nice if you try to explain better what you want to achieve and post your model what you are working on.  
~~Again this topic seems is about totally different thing what you want, I would recommend to open new one~~ with clear explanation and examples, screenshots, model etc…

EDIT:  
Thomas moved the relevant posts now, so you can explain here…
