How to create sub_component with his own axes from 4 points

Hello
I make the frame in miter section of a surface in a component.
I made an offset of the surface with the call of the TIG plugin
I make a loop and I get the 4 points to create the face in a subcomponent then, I extrude it according to the desired thickness.
But I would like the x axis of these subcomponents to match the outer edge so that the textures can be applied correctly.
In a component, I therefore have the local coordinates of 4 coplanar points.
I want to create a sub component with a face since, it’s 4 points. I want the origin to be the 1st point and the x axis of the new component to be the line going through points 1 and 2 and the z axis to be that of the parent component.

Reading various answers on the forum, it would seem that once the definition has been created, add the face according to the 4 transformed points so that they correspond to the desired axes then place an instance of this new sub-component according to a transformation for position it correctly.

How to get the correct transformation of points and how to get the transformation to place the instance?

thanks in advance

Here is my code as it is

require 'TIG-Smart_offset.rb'


model = Sketchup.active_model
sel = model.selection
compo = sel.grep(Sketchup::ComponentInstance)[0]

model.start_operation("terrasse",true,false,false)

compo_def = compo.definition
compo_ents = compo.definition.entities
compo_face = compo_ents.grep(Sketchup::Face)[0]

#Offset
offset = 145.mm
fi = TIG::Smart_offset.new(compo_face, -offset)

#faces
compo_faces = compo_ents.grep(Sketchup::Face)
if compo_faces[0] == fi
    fo = compo_faces[1]
else
    fo = compo_faces[0]
end

#loops
lo = fo.outer_loop
if lo= fo.loops[0]
  li= fo.loops[1]
else
  li = fo.loops[0]
end

#edges
eo = lo.edges
ei = li.edges

#initialisation while for each edges of loop 
en = eo.length
i = 0

while i < en

  #match outer and inner edges
  if i>0
    j=(en-1)-(i-1)
  else
    j=i
  end
  #points to define face in sub_component
  points = []
    points[0] = eo[i].vertices[0].position
    points[1] =  eo[i].vertices[1].position
    points[2] =  ei[j].vertices[0].position
    points[3] =  ei[j].vertices[1].position
    
    #Sub_component
    lame_c = compo_ents.add_group.to_component
    lame_c_ents = lame_c.definition.entities
    lame_c_face = lame_c_ents.add_face(points)
    lame_c_face.reverse!
    lame_c_face.pushpull(21.mm,true)
    lame_c_face.reverse!
    lame_c.name='lame_cadre'+i.to_s
    layer = Sketchup.active_model.layers.add "terrasse_lame_cadre"
    lame_c.layer =layer

    i= i+1
end #end of while

#cleaning edges offset
ei.each{|e|e.erase!}

model.commit_operation

Can you attach a screenshot or something to visually illustrate what you’re looking to do?

Hello TomTom
Here are 3 screenshots, corresponding to the initial state before code, once the code is applied and what I would like the code to achieve.
thanks in advance



Well, this is not exactly wat you want to achieve, but you might get some idea, at least how to move the axes origin of components…

(BTW. The first method in my code - which could be the most interested by you - came from Thomas )

Thank you Dezmo, it is a beginning of reflection.
The idea in the end is to be able to apply the textures correctly and easily get the dimensions.
The truth is hidden in the transformations, but I’m not yet comfortable with it! I need to get the angle between the outer edge and the x-axis of the parent component and the translation between the point that will serve as the origin and the origin of the component, so these are its local coordinates. Then apply these transformations for each point. Create the component add the face according to these 4 new points, extrude then place the instance according to the inverse transformation. it’s easy in French much less in ruby.

1 Like

I have a start of a solution, but still some errors especially if the parent component does not have the same axes as the model.

require 'TIG-Smart_offset.rb'


model = Sketchup.active_model
sel = model.selection

compo = sel.grep(Sketchup::ComponentInstance)[0]

model.start_operation("terrasse",true,false,false)

compo_def = compo.definition

compo_ents = compo.definition.entities
compo_face = compo_ents.grep(Sketchup::Face)[0]

offset = 145.mm

fi = TIG::Smart_offset.new(compo_face, -offset)
compo_faces = compo_ents.grep(Sketchup::Face)

if compo_faces[0] == fi
    fo = compo_faces[1]
else
    fo = compo_faces[0]
end

lo = fo.outer_loop
if lo == fo.loops[0]
  li = fo.loops[1]
else
  li = fo.loops[0]
end

eo = lo.edges
ei = li.edges

en = eo.length
i = 0

while i < en
  if i>0
    j=(en-1)-(i-1)
  else
    j=i
  end
  points = []
    points[0] = eo[i].vertices[0].position
    points[1] =  eo[i].vertices[1].position
    points[2] =  ei[j].vertices[0].position
    points[3] =  ei[j].vertices[1].position

    vector_edge = points[0].vector_to(points[1])
    vector_axex = Geom::Vector3d.new(1,0,0)
    angle = vector_edge.angle_between vector_axex
    transformation_rot = Geom::Transformation.rotation(points[0], vector_edge, angle)
    transformation_rot_inv = transformation_rot.inverse

    origine_parent = Geom::Point3d.new(0,0,0)
    vector_translation = points[0].vector_to(origine_parent)
    transformation_translation = Geom::Transformation.translation(vector_translation)
    transformation_position = Geom::Transformation.new points[0]

    points_t = []
    points_t[0] = points[0].transform!(transformation_rot).transform!(transformation_translation)
    points_t[1] = points[1].transform!(transformation_rot).transform!(transformation_translation)
    points_t[2] = points[2].transform!(transformation_rot).transform!(transformation_translation)
    points_t[3] = points[3].transform!(transformation_rot).transform!(transformation_translation)





    lame_c_name = 'lame_cadre'+i.to_s
    lame_c = model.definitions.add lame_c_name
    lame_c_ents = lame_c.entities
    lame_c_face = lame_c_ents.add_face(points_t)
    lame_c_face.reverse!
    lame_c_face.pushpull(21.mm,true)
    lame_c_face.reverse!
    lame_c_inst = compo_ents.add_instance(lame_c,transformation_position).transform! transformation_rot_inv


    layer = Sketchup.active_model.layers.add "terrasse_lame_cadre"
    lame_c_inst.layer = layer

    i= i+1
end

ei.each{|e|e.erase!}
model.commit_operation