How to transform curve vertices?

g.skp (63.5 KB)

g = Sketchup.active_model.selection[0]
vec = Geom::Vector3d.new(0,0,1.m)
tr = Geom::Transformation.new(vec)
vertices = g.entities.grep(Sketchup::Face).map(&:vertices).flatten.uniq
g.entities.transform_entities(tr,vertices)
g.entities.transform_by_vectors(vertices, [vec]*vertices.length)

Hi,

Try the following…

model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
vertices = selection.grep(Sketchup::Face).map(&:vertices).flatten.uniq

# Explode curves if any
vertices.each do |vertex|
  vertex.edges[0].explode_curve
end

vec = Geom::Vector3d.new(0,0,1.m)
tr = Geom::Transformation.new(vec)
entities.transform_entities(tr, vertices)

It seems that the method #transform_entities does not work on vertices that are used in curves. That is why I made sure to explode the curves. I would like to know if this is a BUG or if it is normal.

1 Like

It’s ok,but I don’t want explode the curve.

This worked for me …

# Select the tube group, then ...
grp = Sketchup.active_model.selection[0]
vec = Geom::Vector3d.new(0,0,1.m)
tr = Geom::Transformation.new(vec)
ents = grp.entities
ents.transform_entities(tr, *ents)

EDIT: Fixed the code snippet (posted the wrong snippet, my bad.)

I’m not sure why transformaing the vertices does not work. Perhaps because they share a softened edge ?

BTW: The inside of the tube needs it’s surface reversed.

1 Like

I get the following error on the Ruby console:

“undefined method `entities’ for #Sketchup::Face:0x000001ae797f9968\nDid you mean? entityID (Line 4)”

With my snippet above?

EDIT: Fixed the code snippet (posted the wrong snippet, my bad.)

If, so do you have the whole group (and only that) selected ?

1 Like

I’d suggest logging an API issue for this.

1 Like

It’s still useless when I cancel the smoothness

You are right I was not selecting the group, instead, I was selecting a face.

My confusion was because I didn’t know that variable g was short for ‘group’. I should have figured it out when I saw g.entities but I didn’t. I would recommend to @javalitterboy to not name variables with a single letter next time so the code is understood better.
I would use the following to get a selected group.

model = Sketchup.active_model
selection = model.selection
group = selection.grep(Sketchup::Group)[0]

msg = 'Please select a group before using this tool'
UI.messagebox(msg, MB_OK) if group.nil?

When using the code @DanRathbun posted and I select the group it works, but if you have a circle inside a group it transforms the vertex in the wrong direction. Seems it is using another axis that is different than the group axis.

However when I try running the code on the model that @javalitterboy shared it gives an error.

thanks, I will do this later.

2 Likes

Yea, I tried it also.

We’ve been discussing this in the new API issue thread, where @Fredo6 has reminded us of the Curve#move_vertices method.

In your “g.skp” model, all the vertices belong to curves, so they can be moved with this pattern:

# Select the tube group, then ...
model = Sketchup.active_model
grp = model.selection[0]
vec = Geom::Vector3d.new(0, 0, 1.m)
tr  = Geom::Transformation.new(vec)
edges  = grp.entities.grep(Sketchup::Edge)
curves = edges.map(&:curve).compact.uniq
model.start_operation("Transform Entities",true)
  curves.each do |curve|
    vertices = curve.vertices
    points = vertices.map(&:position)
    destinations = points.map {|pt| pt.transform(tr) }
    curve.move_vertices(destinations)
  end
model.commit_operation
3 Likes