How path can be face loops?

I have a face (my_face) with one outer loop and some inner loops. I wish to use my_face.loops as path for followme. Can you help me for it? Thank you in advance.

DOES NOT TESTED!!!

# Get all points of loops in a face
# Parameters: 
#   face: <Sketchup::Face>
#   tr: (Geom::Transformation) - optional
#    - combined transformation for the group(s) / comp.instance(s)
#       containing the face
#
# Return nested Arrays with loop's points or empty Array
#   Outer loop points      Inner loop pts (if exist)  Other Iloop..
# [ [Point3d, Point3d,...], [Point3d, Point3d, ...], .............]
def get_path_by_face_loops(face, tr = IDENTITY)
  return [] unless ( face.is_a?(Sketchup::Face) && face.valid? )
  outer_loop_pts = []
  all_loops_nested_ary = []
  face.loops.each{|loop|
    if loop.outer?
      outer_loop_pts = loop.vertices.map{|v| v.position.transform(tr)}
    else
      inner_loop_pts = loop.vertices.map{|v| v.position.transform(tr)}
      all_loops_nested_ary<<inner_loop_pts
    end
  }
  all_loops_nested_ary.unshift( outer_loop_pts )
end

then you can call this from your other method like

model = Sketchup.active_model
ents = model.active_entities
path_nested_ary = get_path_by_face_loops(my_face)
  #or 
  # path_nested_ary = get_path_by_face_loops(my_face, my_face_transformation)

if path_nested_ary[0]
  
  outer_path = ents.add_edges path_nested_ary[0]
   
  # or if you want soft path  
  # outer_path = ents.add_curve path_nested_ary[0]
  
  # assuming the @face1, @face2 are there in other method
  @face1.followme outer_path
  
  inner_path1 = ents.add_edges path_nested_ary[1] if path_nested_ary[1]
  @face2.followme inner_path1

  #...
  #...
  # ...and so on
end

Dear Dezmo,
I need time to check your codes step by step. I wrote following code for a face.

tr = grp.transformation
face.loops.each{|loop|
  if loop.outer?
    @i_loop = loop.vertices.map{|v| v.position.transform(tr)}
  end
}
i_grp = Sketchup.active_model.active_entities.add_group
path = i_grp.entities.add_curve @i_loop

you can see result in following photo. One line is missed. Can you tell me why it happen and how can I solve problem?

I see. You have to connect the first point to last in a @i_loop array , therefore you have to add it’s first element to the end:

path = i_grp.entities.add_curve( @i_loop<<@i_loop.first )

(I missed that in my previous post :blush: )

Recommended reading:

1 Like
2 Likes