Rotate Component Axis
-
Has anyone looked at the ruby in this post enough to know if I can adapt its code into one the will rotate a component's axis (and bounding box), leaving the orientation of the component intact? How will doing this affect all instances of the component in the model?
I have attempted, and failed to do the following: 1. get the model's transformation, 2. rotate the component to the new transformation then apply it to the original component face by face. 3. Return the component to its original transformation.
I have also tried to simply apply the new transformation to the original component by the following method's example:
array_of_entities.transform_entities(new_transformation, ent1, ent2, ent3)
As I understand it, array_of_entities are the original component's entities. What are ent1, ent2, ent3? If these are the transformed entities, how do I get them into the method. A previous post proposed something like:
array_of_entities.transform_entities(new_transformation, array_of_another_entities.to_a)
, but that is currently over my head to make work:-)
-
I think you're saying that the plugin you referenced will change the component axis, but in the process messes up all components in the model already, right? if so:
I think you would need to transform all the geometry inside the component so it aligns how you would like it to. Then you would need to find all the instances of that component inside the model and transform them by the revrse of however you transformed the inner geometry.
That might work.
Chris
-
Hi Chris, I really had 2 questions, one about adapting the previously posted ruby, and one about how to apply:
array_of_entities.transform_entities(new_transformation, ent1, ent2, ent3)
Based on what you said, I should make the selected component unique in order not to affect any other components.
I know how to get the new_transformation, but am not sure what ent1, ent2, ent3 in the method are refering to. My goal is to rotate the component axis, not the inner geometry. So I thought that if I rotated the component then applied the new_transformation to the inner geometry, then return to the original transformation, the axis would be rotated, with the inner geometry in its original place.
-
#1 - yeah, if you don't want it to affect other instances, then make it unique first. But remember, those components won't have their axis change either. So as long that is ok, then that is a good approach.
#2 - You are reading the API wrong I think (though it is confusing the way its written!). The method is:
Entities.transform_entities"Entities" in this case is an "Entities" object. Not an array of entities. It accepts an array of entities as an argument. The method example is this:
entities.transform_entities(transform, ent1, ent2, ent3)and instead of listing the ents one by one, just put them in an array:
entities.transform_entities(transform, [ents])Where do you get that array? From where you want. It is just a list of whatever you want to transform. It might be everything that the user has selected. But in your case, it is all entities inside the component definition. So to transform the entities inside a component definition:
my_comp_def.entities.transform_entities(transform, my_comp_def.entities.to_a)And that will transform the entities of your component definition by the transform that you create.
Did that help at all?
Chris
-
Also, I meant to add that you can't rotate the component axis. You do that by rotating the inner geometry.
-
Chris, Yes, got it, thanks.
selection.each do |e| if e.is_a? Sketchup;;ComponentInstance origin = e.transformation.origin vector_new = Geom;;Vector3d.new([1,2,3])#for example old_transformation = e.transformation new_transformation = Geom;;Transformation.new(origin,vector_new) e.transformation = new_transformation e.definition.entities.transform_entities(new_transformation, e.definition.entities.to_a) end endHow do I remember all of this for later?-) And, lets see if I can apply this to my program.
-
One thing I see there in your code is that youa re iterating over all entities in the model. And if it is an instance, you run the code. Well the transformation is being applied to the geometry inside the definition. So once you do it to one instance, it will be done to ALL instances of that component. So you don't need to keep doing it once its been done once.
If its something you want to do each component definition, then instead of iterating through all entities in the model, you can iterate over all the definitions directly with
comp_defs = Sketchup.active_model.definitions comp_defs.each do |e| etc...Then you know you are iterating over component definitions to begin with, so it saves a LOT of time, instead of testing each entity in the model to see if it is an instance.
Chris
-
Chris, Understood, thanks for looking at my code.
-
Hmm... on further inspection, while I can apply the method, I did not get the desired output. The resulting transformation has the bounding box, and axis (I think) pointing in the right direction, but the component did not return to its original location. Maybe I can figure it out this weekend.
I often use UI.messagebox (variable) to check my work as I go along, but some (?) of the transformations do not seem to display until the program has been completed.
Advertisement