Sorry, I’m just trying to understand this. Why doesn’t this work in a shared parent group of 4?
Or @Dan Rathbun?
model = Sketchup.active_model
sel = model.selection
entities = model.entities
# Clear the model for a clean test
entities.clear!
# Create parent Group 4
group4 = entities.add_group
group4_entities = group4.entities
# --- Create Cube 1 (100x100x100 mm, base at Z = 0) ---
group1 = group4_entities.add_group
group1_entities = group1.entities
cube1_points = [
Geom::Point3d.new(0, 0, 0),
Geom::Point3d.new(0, 100.mm, 0),
Geom::Point3d.new(100.mm, 100.mm, 0),
Geom::Point3d.new(100.mm, 0, 0)
]
face1 = group1_entities.add_face(cube1_points)
# Ensure the face normal is pointing upwards (Z > 0)
if face1.normal.z < 0
face1.reverse!
end
# Push/pull the face to create a cube of height 100 mm
face1.pushpull(100.mm)
# --- Create Cube 2 (100x100x100 mm, base at Z = 100 mm) ---
group2 = group4_entities.add_group
group2_entities = group2.entities
cube2_points = [
Geom::Point3d.new(0, 0, 0),
Geom::Point3d.new(0, 100.mm, 0),
Geom::Point3d.new(100.mm, 100.mm, 0),
Geom::Point3d.new(100.mm, 0, 0)
]
face2 = group2_entities.add_face(cube2_points)
# Ensure the face normal is pointing upwards (Z > 0)
if face2.normal.z < 0
face2.reverse!
end
# Push/pull the face to create a cube of height 100 mm
face2.pushpull(100.mm)
# Move the second cube group upwards by 100 mm so it sits exactly on top of cube 1
group2.transform!(Geom::Transformation.translation([0, 0, 100.mm]))
# --- Create Cube 3 (100x100x100 mm, base at x = 200 mm) ---
group3 = group4_entities.add_group
group3_entities = group3.entities
cube3_points = [
Geom::Point3d.new(0, 0, 0),
Geom::Point3d.new(0, 100.mm, 0),
Geom::Point3d.new(100.mm, 100.mm, 0),
Geom::Point3d.new(100.mm, 0, 0)
]
face3 = group3_entities.add_face(cube3_points)
# Ensure the face normal is pointing upwards (Z > 0)
if face3.normal.z < 0
face3.reverse!
end
# Push/pull the face to create a cube of height 100 mm
face3.pushpull(100.mm)
# Move the third cube group to position
group3.transform!(Geom::Transformation.translation([50.mm, 300.mm, 0]))
# --- Raytest from each face of Cube 1 within Group 4 ---
faces = group1.entities.grep(Sketchup::Face)
faces.each_with_index { |face, index|
next unless face.valid?
next if face.hidden? # ignore hidden faces
# hide this face from the ray test
face.hidden = true
centroid = face.bounds.center # Center point of the face
normal = face.normal # Normal vector of the face
ray = [centroid, normal] # Ray from the centroid in the direction of the normal
# Perform ray test using model, ignore hidden geometry
hit = model.raytest(ray)
if hit
# record the distance
distance = centroid.distance(hit[0])
# ray test in the reverse direction
reverse_ray = [hit[0], normal.reverse]
reverse_hit = model.raytest(reverse_ray)
container = reverse_hit[1][0] if reverse_hit
# if the reverse ray hits the same container
# there is a coincident faces and or edge pair
distance = 0 if container == hit[1][0]
puts "Face #{index + 1}: Distance to nearest object: #{distance.to_mm.round(1)} mm"
else
puts "Face #{index + 1}: No intersection found"
end
# unhide this face from the ray test
face.hidden = false
}
nil
Face 1: No intersection found
Face 2: Distance to nearest object: 0.0 mm
Face 3: No intersection found
Face 4: No intersection found
Face 5: No intersection found
Face 6: Distance to nearest object: 0.0 mm