Problem with placing a group into desired point

I want to make a pointer inside a group or component selected. This group is a triangle and I want to place it on (bounds.center_x, bounds.center_y and bounds.max_z). I have earned to place the triangle into the group but I’m not able to place it in the desired point. I know that it’s something related to transformation but I don’t find anything I’m able to understad.
Can anyone help me?
This is the code

def pointer ()
  model = Sketchup.active_model
  entities = model.active_entities
  sel = model.selection
  sel.each do |ent|
    grupos = ent.definition.entities.select { |e| e.is_a?(Sketchup::Group) && e.name == "_data" }
    componentes = ent.definition.entities.select { |e| e.is_a?(Sketchup::ComponentInstance) && e.name == "_data" }
    # group already exists
    if grupos.length > 0 || componentes.length > 0
      puts "group already exists"
    else
      d = ent.bounds.center
      e = ent.bounds.corner(4)[2]
      x = d[0]/39.3701
      y = d[1]/39.3701
      pts = []
      pts[0] = [x-1,y,e]
      pts[1] = [x+1,y,e]
      pts[2] = [x,y,e+2]
      # Add a group to the model.
      group = entities.add_group
      cc = group.entities.add_face(pts)
      connected = cc.all_connected
      #insert group into group
      seltrans = ent.transformation
      seldef = group.definition
      if ent.is_a? Sketchup::Group
      selinstance = ent.entities.add_instance seldef, seltrans
      elsif ent.is_a? Sketchup::ComponentInstance
      selinstance = ent.definition.entities.add_instance seldef, seltrans
      end
      selinstance.name = "_data"
      group.erase!
    end
  end
end
pointer()

And this is the result

The arrow is the desired place and the circle the wrong one

Something like this.

def bounds_top_center(ent)
  center = ent.bounds.center
  center.z = ent.bounds.max.z
  center
end

def triangle_from_point(pt)
  pts = []
  pts[0] = [pt[0] - 1, pt[1], pt[2]]
  pts[1] = [pt[0] + 1, pt[1], pt[2]]
  pts[2] = [pt[0], pt[1], pt[2] + 2]
  pts
end

entities.add_face(triangle_from_point(bounds_top_center(ent)))

Thanks @Neil_Burkholder I’ll try tomorrow. Maybe I’m doing it more difficult than it really is.

image

Internally, all lengths in SketchUp are stored in inches.
You are “manually” converting inches to meters in x,y coordinates, then you subtract/add inches from it…while the z coordinate, still in inch… this makes no sense for me…

Yes, it seems ridiculous but I don’t know why it worked at that moment. There is a difference between x and e, it seems that d[0] units are differect than ent.bounds.corner(4)[2].
By the way my problem is not the dimensions (I will solve later) but the placement of the gruops I’m creanting that they are not at the desired place inside the parent group or component.

Finally I solved as follow: (@Neil_Burkholder thanks for your help anyway )

def pointer ()
  model = Sketchup.active_model
  entities = model.active_entities
  sel = model.selection
  sel.each do |ent|
    grupos = ent.definition.entities.select { |e| e.is_a?(Sketchup::Group) && e.name == "_data" }
    componentes = ent.definition.entities.select { |e| e.is_a?(Sketchup::ComponentInstance) && e.name == "_data" }
    # group already exists
    if grupos.length > 0 || componentes.length > 0
      puts "group or component already exists"
    else
      pts = []
      pts[0] = [0,0,0]
      pts[1] = [4,0,0]
      pts[2] = [2,0,4]
            
      # Add a group to the model.
      group = entities.add_group
      cc = group.entities.add_face(pts)
      connected = cc.all_connected
      #insert group into group
      #seltrans = ent.transformation
      seldef = group.definition
      start = Geom::Point3d.new(0, 0, 0)
      seltrans = Geom::Transformation.new(start)

      #translation
      stop = start + [ent.bounds.width*0.5,ent.bounds.height*0.5,ent.bounds.depth]
      relvec = start.vector_to(stop)
      relative = Geom::Transformation::translation(relvec)
      
      if ent.is_a? Sketchup::Group
      selinstance = ent.entities.add_instance seldef, seltrans
      elsif ent.is_a? Sketchup::ComponentInstance
      selinstance = ent.definition.entities.add_instance seldef, seltrans
      end
      selinstance.name = "_data"
      selinstance.material = "red"
      selinstance.transform!(relative)
      group.erase!
    end
  end
end
pointer()

P.D. and sorry @dezmo for

x = d[0]/39.3701
y = d[1]/39.3701

:wink:

This is tedious …

Easier to do …

      pts = [
        [0,0,0],
        [4,0,0],
        [2,0,4]
      ]

… or …

      pts = [ [0,0,0], [4,0,0], [2,0,4] ]
2 Likes