Closest face problem
-
Imagine two selected objects. I'd like to determine the closest pairing of one face from each. I'll be using the faces from the bounding box of each object. My goal is to move one from the BB center to it's opposing BB center, but that is later and I've cobbled code to do so. What I'm currently doing is a distance from CP to origin and then comparing the two. It works some times and fails the rest of the time. This may be influenced by a second problem and that has to do with selection. How do I reliably tell first selection from second? Currently it's the simple minded @sel[0] and @sel[1] as first and second where @sel decends from Sketchup.active_mode.entities.selection.
In sum, I'd like to know how to:
- Determine closest pair of faces from two object bounding boxes?* Determine order of selections
Will bake cookies!!
-
@hsmyers said:
I'll be using the faces from the bounding box of each object.
Bounding Boxes are virtual geometric "helper" objects that do not have faces.
@hsmyers said:
How do I reliably tell first selection from second? Currently it's the simple minded
@sel[0]
and@sel[1]
as first and second where@sel
results fromSketchup.active_model.entities.selection
.You will need to implement a
[%(#BF0000)[SelectionObserver]](http://www.sketchup.com/intl/en/developer/docs/ourdoc/selectionobserver)
subclass. -
@dan rathbun said:
@hsmyers said:
I'll be using the faces from the bounding box of each object.
Bounding Boxes are virtual geometric "helper" objects that do not have faces.
True but it is easy enough to construct the necessary 'face' and then derive the center. I should have been more clear. Hmmmm...that said, how about closest pair of points? A sorted list of 64 lengths? Or is the something less sledgehammerish?
@unknownuser said:
@hsmyers said:
How do I reliably tell first selection from second? Currently it's the simple minded
@sel[0]
and@sel[1]
as first and second where@sel
results fromSketchup.active_model.entities.selection
.You will need to implement a
[%(#BF0000)[SelectionObserver]](http://www.sketchup.com/intl/en/developer/docs/ourdoc/selectionobserver)
subclass.Ah! A little bit deeper into the tool pool...splash!! Thanks for the tip and the correction Any ideas on the revised question?
-
@hsmyers said:
how about closest pair of points?
Since you are only dealing with two objects, it shouldn't matter which is sel[0] or sel[1]
To find the closest corners of two objects
mod = Sketchup.active_model ent = mod.active_entities sel = mod.selection vue = mod.active_view obj0 = sel[0]; obj1 = sel[1]; cor0=[]; cor1=[] [0,1,2,3,4,5,6,7].each{|i| cor0<<obj0.bounds.corner(i)} [0,1,2,3,4,5,6,7].each{|i| cor1<<obj1.bounds.corner(i)} md=1e9;m=n=0 cor0.each_with_index{|c0,i| cor1.each_with_index{|c1,j| d = c0.distance(c1) if d < md md=d;m=i;n=j end } } UI.messagebox "Obj0 Cor#{m} to Obj1 Cor#{n} is #{Sketchup.format_length(md)}"
-
@sdmitch said:
@hsmyers said:
how about closest pair of points?
Since you are only dealing with two objects, it shouldn't matter which is sel[0] or sel[1]
To find the closest corners of two objects
mod = Sketchup.active_model > . > . > . > UI.messagebox "Obj0 Cor#{m} to Obj1 Cor#{n} is #{Sketchup.format_length(md)}" >
Thank you for the snippet!
You are correct excepting that the tool in question will move one of the two objects such that the opposing face centers are touching plus or minus an offset. Sorry for the grossly incomplete spec My plan (very fluid and quite murky ) is take the first selection as the moving object and the second as the fixed object. I've some notion about non-parallel faces requiring ( optionally ) an rotational adjustment. However it's my goal to keep things as simple as possible. My usage is to stack components together to assemble the model much like one would in actuality. Hex nuts, lock washer on threaded rod etc.
Additionally, there is the self-educational aspect of solving the which selection is on first This tool already has four options and looks to acquire one or more in the future. So it seems prudent to solve this now to make ready for the unforeseen. 'Sides any excuse to cut code!!
Advertisement