How to create a component with existing entities in a component by ruby?

Can’t you just use:

compo = model.selection[0] ### Assuming you have the one thing selected...
trans = compo.transformation
instance = model.active_entities.add_instance(compo.definition, trans)
### NOW move it to the origin.
translate = Geom::Transformation.translation(trans.origin.vector_to(ORIGIN))
model.active_entities.transform_entities(translate, instance)
### Finally it is now at [0,0,0] with the original scaling and rotation intact

If you want it unrotated but scaled, then as @slbaumgartner says you might be best placing a ‘pure’ instance at the Origin and then scaling it afterwards ?
The API does not give easy access to a transformation’s scaling, however, the DC code adds xscale/yscale/zscale methods, these are also limited because they are always a positive value, so a negative scaling when handing/mirroring is not handled. It’d be possible to extract that from the transformation matrix, but we are having enough problems with the easier stuff thus far… because axial scaling and axial rotations interact in complex ways, it becomes particularly mind-boggling - e.g. mirroring about several axes combined with axial rotations can cancel out and have you back where you started etc…

OK, finally we understand.

#3 : Scaled the same as selected, but unrotated at the origin:
UI.add_context_menu_handler do |popup|
  sel = Sketchup::active_model.selection
  if !sel.empty? && sel.single_object?
    obj = sel.first
    if obj.is_a?(Sketchup::ComponentInstance)
      popup.add_item("Copy Selected Component to Origin (Scaled)") {

        t = obj.transformation

        Sketchup::active_model.active_entities.add_instance(
          obj.definition,
          Geom::Transformation::scaling(
            ORIGIN,
            Geom::Vector3d.new(1,0,0).transform!(t).length,
            Geom::Vector3d.new(0,1,0).transform!(t).length,
            Geom::Vector3d.new(0,0,1).transform!(t).length
          )
        )

      }
    end
  end
end

@slbaumgartner
Thank you ! You are right and I just don’t know how to get the xscale, yscale, and zscale. Thanks to Dan Rathbun. He solve the problem.

@TIG
Thank you! And luckily the component that I use is usually a box,so I don’t have to deal with the mirroring problem.
BTW, It seems that “Sketchup.active_model.active_entities.transform_entities translate, instance” should be add to your code.

@DanRathbun
Thank you very much for being patient to solve my problem! You help me a lot. Thank you again!
It is hard to find the proper method in Sketchup API for me because I have wait for a long time when sketchup.com is loading. Is there any file of Sketchup API that I can download?

Thank you all, excellent dialog/ A+ lecture series. there should be some basic methods where the basics of whatcha do and most likely are gonna do (e…g) basic combo’s creating groups, components, from the lesser animls in this zoo (lines, faces, loops, edges, points…) is done with some eloquent methods, with a video, proforma file, basic fundies. since sketchup does not have the internl code it genertes to perform sequences e.g., like excel vbA taking the basic mouse driven combination of operations, e,g, select an area, and whatever in its “selected and outlined area” can become a group, with given handle ie, name vs definition… and/or a component. then simple methods to copy, move, rotate, reflect, follow me… What also would be nice if, a glossary of terms and concepts and videos e.g., loops, ruby is so good at dynamic method argument identification, why it doesnt convert the object forms which supply the data without much worry. I rhink the honchos who created the Sketchup paradigm deserves credit, but I think the dearth explaination has a psychological basis. , thank you much again all.