Volume calculation issue with scale in group
-
Hello,
I try to get the volume of a component, which is placed in a group and this group is scaled.
For example : I draw a 100010001000mm box, I make a component called "The cube".
On one face of "The cube" (not within the component), I draw a simple line.
Then with the line and the cube, I make a group called "The group".
Then I scale "The group" by factor 2 (double the size).When I look at entity info inspector, the volume is correct (= it's taking into account the scaled), but when I try to get volume from code : it's wrong (= unscaled volume).
Here is the code :
volume=Sketchup.active_model.definitions[1].instances[0].volume
(NB : definitions[1] correspond to "The cube" 's definition).=> then the results is the volume of the unscaled component "The cube" (may the definition's volume).
Do somebody has a any idea why and a simple workaround ?
By my side, the only way looks like getting the parent transformation of "The cube", but if I search for Sketchup.active_model.definitions[1].instances[0].parent, this doesn't return the Group, but the definition...Thank in advance for your help.
Best regards. -
And stronger : if I select the component "The cube" in the model (by modifying "The group") and type in console :
Sketchup.active_model.selection[0].volume
, then I get the correct volume...And more stronger : if I select nothing and type this code :
Sketchup.active_model.selection.add Sketchup.active_model.definitions[1].instances[0] Sketchup.active_model.selection[0].volume
, then the volume is wrong again ! -
When I repeat your first post - with two instances of a group [or component-definition] - with one scaled - and then get the volumes of each of them, I get the correct 'scaled' volumes returned...
Sketchup.active_model.definitions[0].instances.each{|i| p i.volume }
So what is your process that causes issues... -
Hello TIG,
Not sure to understand clearly your answer : I tried duplicating the group "The group", but volume still not good.
Inteloide
-
Make a 1m³ cube [so it's a 'solid'].
Group it.
Select that group and use Move+Ctrl to make a copy off to one side.
Scale that copy [remember - don't edit it because that would make a new definition].
Run this code in the Ruby Console and the the reported volume for the two intsances of the same group-definition should reflect their different scaling...
Sketchup.active_model.definitions[-1].instances.each{|i| p i.volume }
Note how I've used[-1]
so that the last definition is used, that's so this will work on a model with existing definitions... -
Hello TIG,
Thanks for your explainations : your example works.
Nevertheless, this is not my issue : the problem is the volume of a component within a group when group is scaled.Please have a look to attached file.
If you run the code :
Sketchup.active_model.definitions[0].instances.each{|i| p i.volume }
You will have a volume which doesn't correspond with cube size !
Inteloide
-
You can't get the scaled volume of that nested object simply.
A nested component-instance inside a group's entities is still unscaled when the group itself is scaled.
What you have to do is get the nesting hierarchy and accumulate the transformations to find the actual scaling factor and apply that to the instance's returned volume.
You can get the group's volume - which takes into account its scale.
Get its non-scaled volume by placing a temporary instance thus -
` volu=group.volume
defn=group.entities.parentor for a component use: defn=instance.definition
temp=model.entities.add_instance(defn, ORIGIN)
vol0=temp.volume
temp.erase!
group_scale=vol0/voluSo use
defn.entitiesto then find the instance inside the group [depends how many there are, names etc], so you can get the scaling for that instance within its context - use the same steps as for the group to compare it with an unscaled sibling... Now you have the
group_scaleand the
instance_scaleyou can simply combine them. Let's say the group is scaled
x2.0and the instance
x0.9the instance's visible scale is
sf = 2.00.9 >>> 1.8So
instance_volume=instance.volumesf`
For more complex nesting you need to iterate through everything in more steps.When you initialize your code if you are within an 'edit' context [i.e.
model.entities != model.active_entities
], then you can usemodel.active_path
to get an array of the 'containers' - this returns groups and instances, rather than definitions, so you know can use that to your advantage... -
Hello TIG,
Thank you for the explainations. It's what I was thinking when I had the error...
The problem is that the group/component to duplicate can be huge and take time to copy.
If think I will take the problem in this opposite way, making a loop in entities, from model to the component instance collecting each time the transformation of each group/component.Do you think there is an opportunity to find an easiest solution with the Sketchup.active_model.selection (when selecting component in model, the ruby gives good volume, but when selecting component through ruby code, the ruby gives bad volume?
Inteloide
-
If you are in an edit context the
model.active_path
returns an array of the 'containers' - groups/instances - so you can directly get the volume/scaling of those to apply to the object in question ?
This assumes you physically [pre]select the object you are testing - which by its nature sets up the active_path [or nil if in the model] for you.I think there is a possible 'trick' using a new input_point and pickhelper.all_picked or .path_at ?
Getting an array of all of the picked point's nesting ?None of this is easy
Advertisement