Transforming picked face

thanks :smiley:

how to transform picked face @face

Is it possible ?

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

Thanks

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.

1 Like

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]

1 Like

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

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

yes it is working but please check this post #select face

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 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-axis and Y-axis and Z-axis form the axes.


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

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

better to “brake” it e.g.:

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

The Model #entities method returns an Entities object containing the entities in the root of model.

Be aware Model #active_entities can be different.
__
The Entities #transform_entities 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 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…