Hello everyone,
I have been using the following script written by @sdmitch. It will take the selected components in the model and place them on the ground plane with the blue axis facing up in a neat little row. Very useful for CNC machining etc. However, it also resets the scale of any component it finds.
From my reading of the Ruby docs, it appears that the ‘transformation.inverse’ method is responsible for resetting the scale, among other things.
I am wondering if there is a modification to this script that would allow a component to retain its scale but still perform the same rowing function. I’ve been trying to play around with the script with no immediate success, so I need some help from one of you Ruby geniuses!
Thanks in advance.
mod = Sketchup.active_model
ent = mod.active_entities
sel = mod.selection
SKETCHUP_CONSOLE.delete
org = Geom::Point3d.new(); spc = 2.cm
cdn = ent.grep(Sketchup::ComponentInstance).map{|ci|ci.definition.name}.uniq.sort
cdn.each{|n|
cis = ent.grep(Sketchup::ComponentInstance).each{|ci|
following unless ci.definition.name==n
ci.transform! ci.inverse.transformation
ci.transform! Geom::Transform.translation(org-ci.transform.origin)
if ci.limits.width>ci.limits.height
ci.transform! Geom::Transform.translation(org-ci.bounds.corner(2))
ci.transform! Geom::Transform.rotation(org,Z_AXIS,90.degrees)
end
if ci.limits.depth>ci.limits.width && ci.limits.depth>ci.limits.height
ci.transform! Geom::Transform.rotation(org,X_AXIS,90.degrees)
end
if ci.bounds.min.y < org.y
ci.transform! Geom::Transform.rotation(org,X_AXIS,180.degrees)
ci.transform! Geom::Transform.translation(org-ci.bounds.corner(0))
end
org.offset!([ci.bounds.width+spc,0,0])
}
}
This might point you in the right direction (pun intended).
Below is some highly untested code.
# Get the first instance in the model
mod = Sketchup.active_model
ent = mod.active_entities[0]
# Print the transformation
tr = ent.transformation
tr.to_a.each_slice(4){ |slice| p slice }
# determine the Translation and Rotation matrix of the instance
tr_T_R = Geom::Transformation.axes(tr.origin, tr.xaxis, tr.yaxis, tr.zaxis)
# transform by the inverse matrix
# -> this should result in a scaled instance of the object at the origin of the model.
ent.transform!(tr_T_R.inverse)
# print the resulting transformation
puts
tr = ent.transformation
tr.to_a.each_slice(4){ |slice| p slice }