@eneroth3 Thanks for looking at this
So I’m starting from scratch, I have a set of component instances that I’d like to merge together in a single component instance with a choosen pivot point. What I’m doing so far is :
-
Grouping the instances together (the insert method a is custom method that reparent entities into another group)
#Create a set container
set_container = Sketchup.active_model.entities.add_group()
set_container_handler = set_container.entities.add_cpoint([0,0,0])
set_name = current_set[0].definition.name
set_container.name = set_name#Move the items into the set container
current_set.each{|itm| set_container.insert(itm, arg_remove_origin:true)}
set_container_handler.erase!
current_set = set_container.entities.to_a -
Moving the parent group to place the instances where I want them to be regarding the scene origin (assuming that creating a component definition will set its pivot at the scene origin).
#Compute the set container direction and pivot
##Extract compute data
set_container_bbox = set_container.bounds
set_container_vector = set_container_bbox.min.vector_to(set_container_bbox.max)
set_container_length = set_container_vector.to_a.max##Compute as array
set_container_direction = set_container_vector.to_a.collect{|v| ((v/set_container_length).round(6) == 1 ? 1 : 0)}
set_container_pivot = set_container_bbox.min.to_a.zip(set_container_bbox.center.to_a).each_with_index.collect do |v_set, v_index|
if v_index == 2
v_set[0]
else
( v_set[0] * set_container_direction[v_index] + v_set[1] * (1-set_container_direction[v_index]) )
end
end##Convert to Geom objects
set_container_direction = Geom::Vector3d.new(set_container_direction)
set_container_pivot = Geom::Point3d.new(set_container_pivot)#Move the set container pivot to the origin
set_container_matrix = Geom::Transformation.translation(set_container_pivot) * Geom::Transformation.rotation(ORIGIN, Geom::Vector3d.new(0,0,1), Geom::Vector3d.new(1,0,0).angle_between(set_container_direction))
set_container.transform!(set_container_matrix.inverse) -
Exploding the parent group to keep the instances at the right place
-
Merge the instances geometries by instacinating new copies of their definitions at the same place into a newly created group (the Sketchup::ComponentInstance.merge method is used to perform this task, I haven’t post it because its before this that I have something that I can’t understand/handle moreover as Dan said I shouldn’t extend directly SU Classes).
-
Generate the defintion of the merged geometries from the group.
-
Replace it at its original place
#Merge the set items together
set_container.explode
set_instance = Sketchup::ComponentInstance.merge(current_set, set_name, :+, arg_as_component:true, arg_inplace:true)
set_instance.transform!(set_container_matrix)
My problem is that I don’t understand why the component instances transfromations remain the same in my code althought they have been transformed as it isn’t when quering in the Sketchup Ruby Console.
-
At the step 3, when exploding the parent group and then quering the component instances transforms I get their original positions. So when performing 4 and 5 the instances copies aren’t place at the good position and everything runs dirty from then.
-
But if i’m exiting the script between the step 3 and 4 the instances transforms that I can inspect within the SU Ruby Console seems correct and non consistant with the one inspected with the breakpoint.