Sorry, it was a “boo-boo” on my part. I did not know you would be inside a group.
When this is true, the instance path array returned by model.raytest
has more than 1 member in it, so [0]
cannot be used because it assumes only 1 member in the array.
This should explain it better …
# encoding: UTF-8
module Majid866
module BoxTest
class Box
def activate
@mod = Sketchup.active_model
# Define 4 points:
pts = [
[0, 0, 0],
[2, 0, 0],
[2, 10, 0],
[0, 10, 0]
]
# Make a group
grp = @mod.active_entities.add_group
face1 = grp.entities.add_face(*pts)
# Pushpull into 3D:
face1.pushpull(-10)
# Draw a face to select:
pts = [
[2, 2, 2],
[2, 8, 2],
[2, 8, 8],
[2, 2, 8]
]
face1 = grp.entities.add_face(*pts)
instance_path = Sketchup::InstancePath.new([grp])
@mod.active_path= instance_path
Sketchup.status_text = "Choose a face ..."
@state = 1
end
def deactivate(view)
view.invalidate
Sketchup.status_text = ""
end
def suspend(view)
view.invalidate
Sketchup.status_text = ""
end
def resume(view)
view.invalidate
Sketchup.status_text = "Choose a face ..." if @state == 1
end
def onLButtonDown(flags, x, y, view)
# @mod is set as the active model
# Select face
input = Sketchup::InputPoint.new
input.pick(view, x, y)
#@model.active_path = input.instance_path.valid? ? input.instance_path : nil
face1 = input.face
if face1 && face1.is_a?(Sketchup::Face)
Sketchup.status_text = ""
@state = 2
push_through(face1)
end
end ### onLButtonDown()
def push_through(face1)
# Given a face referenced as: face1
pt1 = face1.outer_loop.vertices[0].position
# Fire a ray in the reverse direction of face1's normal vector:
result = @mod.raytest( [ pt1, face1.normal.reverse ] )
# Check the result. nil if failure:
if result
# The point that the ray hit:
pt2 = result.first
instance_path = result.last
# The face that the ray hit:
puts "the result object: #{instance_path.last.inspect}"
if instance_path.last.is_a?(Sketchup::Face)
face2 = instance_path.last
# Distance between pt1 and pt2 (on face2):
dist = pt1.distance(pt2)
# PushPull in negative direction:
face1.pushpull(-dist)
end
end
end ### push_through()
end # class Box
unless defined? @loaded
@loaded = true
UI.menu("Plugins").add_item("Box") {
Sketchup.active_model.select_tool(Box.new)
}
end
end # module BoxTest
end # module Majid866