Get Point3D from place_component
-
Is there a Ruby way to get the insertion point of a component. Unfortunately model.place_component does not return a Point3D object, just nil.
Thank you for any help you can offer.
Francis
-
Model.place_component seems to be the tool that is used when you select a component for placement from the Components Browswer. I guess the only way to find the insertion points would be to search for all instances of the component to get the .transformation.origin. of each like this
mod = Sketchup.active_model ent = mod.active_entities sel = mod.selection cmp = mod.definitions["Component#1"] puts "--insertion points of Component#1--" cmp.instances.each{|ci| puts ci.transformation.origin} puts "-----------------------------------"
-
If we are talking about the native tool... then (regardless of what was selected beforehand,) the selection set will contain the newly placed
Sketchup::ComponentInstance
after the user clicks the insertion point.So:
(1) You CAN use a
ToolsObserver
in combination with aSelectionObserver
, to detect the placement of a component instance, and get it's origin (relative to the active entities object,) viasel[0].transformation.origin
(2) Then determine if your at the model level via:
Sketchup.active_model.active_entities == Sketchup.active_model.entities
If
true
then the origin is in "world co-ordinates".If
false
, then you must determine the instance's nesting viaSketchup.active_model.active_path
, and reverse iterate the returned array, combining the origin offsets together.The catch is that perhaps one of the nested parent objects may not be aligned with the world axis. So you would need to take that into account.
This subject has been covered MANY times.
See this recent post: find the position of a group in the active context
Also use the forum search feature to search on terms "Group Origin" "Transformation" etc. -
Now...
model#place_component()
(1) The API dictionary is incorrect, it does NOT return
nil
, it returns a reference to theactive_model
which is basically frivolous, but does allow call chaining.We have logged an API doc typo on this, as well as asking for something more useable to be returned (especially in the case of multiple insertions when the 2nd argument is
true
.)(2) When the 2nd argument is
false
, and only 1 instance is inserted,model#place_component()
works the same as the native tool, ie: the selection is cleared, and the newly placed component instance's reference is pushed into the selection (as it's only member.)So you can get it's insertion point as discussed in the post above.
FYI ... When the 2nd argument to
model#place_component()
istrue
, and multiple instances MIGHT HAVE BEEN inserted, the model's selection object IS NOT CHANGED.
If something was previously selected, it still is. If the selection was empty, it will still be empty. -
@sdmitch said:
Model.place_component seems to be the tool that is used when you select a component for placement from the Components Browswer. I guess the only way to find the insertion points would be to search for all instances of the component to get the .transformation.origin. of each like this
mod = Sketchup.active_model > ent = mod.active_entities > sel = mod.selection > cmp = mod.definitions["Component#1"] > puts "--insertion points of Component#1--" > cmp.instances.each{|ci| puts ci.transformation.origin} > puts "-----------------------------------" > >
Thank you for this it's very helpful. I will try it.
-
@dan rathbun said:
Now...
model#place_component()
(1) The API dictionary is incorrect, it does NOT return
nil
, it returns a reference to theactive_model
which is basically frivolous, but does allow call chaining.We have logged an API doc typo on this, as well as asking for something more useable to be returned (especially in the case of multiple insertions when the 2nd argument is
true
.)(2) When the 2nd argument is
false
, and only 1 instance is inserted,model#place_component()
works the same as the native tool, ie: the selection is cleared, and the newly placed component instance's reference is pushed into the selection (as it's only member.)So you can get it's insertion point as discussed in the post above.
FYI ... When the 2nd argument to
model#place_component()
istrue
, and multiple instances MIGHT HAVE BEEN inserted, the model's selection object IS NOT CHANGED.
If something was previously selected, it still is. If the selection was empty, it will still be empty.Dan,
thank you for you for your two posts which are very helpful. I would have ticked them both as 'resolve' but I had already done this for a previous post and 'the system' wouldn't allow me to tick more than one post as a 'resolved' post. I will be trying your ideas soon. Thanks again. Francis
Advertisement