Hi,
I am trying to use start_operation/commit_operation to implement undo/redo for a plugin. I am unsure if I am going about it the right way. Attached is a sample of the code that I am trying to write. The purpose is to allow the user to specify coordinates that are used in other parts of the plugin - in this example the coordinates are defined when the left button is released. For visual feedback, the selected coordinates are shown as construction point - which are deleted when the tool is unselected.
From the redo/undo point of the issue: the tool creates the correct undo/redo statement, but SketchUp also creates "Erase" and "Guide" undo/redo when the tool is deactivated/activated.
Is there any way to let SketchUp know not to create the "Erase" and "Guide" undo/redo? Or should I use another visual feedback mechanism in place of construction point (such as through the draw function or other)?
Thanks a lot for your help.
Julien
Note: the code also has issues if the undo button is press while the tool is active or the construction point is erased. A test e.deleted? in the deactivate would avoid the error message, but it might be best handled with an entity observer.
module Hibou
class TestingOperationCommitTool
def initialize()
@cPoints = []
end # def initialize
def activate()
begin
dictionary = getDictionary()
if ((nCoordinate = dictionary["nCoordinate"]) != nil)
for i in 0...nCoordinate
x, y, z = dictionary["Coordinate_#{i.to_s}"].split(",")
@cPoints.push(Sketchup.active_model.entities.add_cpoint(Geom;;Point3d.new(x.to_f.m, y.to_f.m, z.to_f.m)))
end
end
rescue Exception => e
UI.messagebox("Error; #{e.to_s}.\nBacktrace; #{e.backtrace}")
end
end # def activate
def deactivate(view)
begin
@cPoints.each() { |e|
Sketchup.active_model.entities.erase_entities(e)
}
rescue Exception => e
UI.messagebox("Error; #{e.to_s}.\nBacktrace; #{e.backtrace}")
end
end # def deactivate
def onLButtonUp(flags, x, y, view)
begin
model = Sketchup.active_model
inputPoint = Sketchup;;InputPoint.new()
inputPoint.pick(view, x, y)
if (inputPoint.valid?())
model.start_operation("Define coordinate")
@cPoints.push(Sketchup;;active_model.entities.add_cpoint(p3d = inputPoint.position))
dictionary = getDictionary()
dictionary["nCoordinate"] = @cPoints.length
dictionary["Coordinate_#{(@cPoints.length-1).to_s}"] = [p3d.x.to_m, p3d.y.to_m, p3d.z.to_m].join(",")
model.commit_operation
end
rescue Exception => e
model.abort_operation
UI.messagebox("Error; #{e.to_s}.\nBacktrace; #{e.backtrace}")
end
end # def onLButtonUp
private
def getDictionary()
return Sketchup.active_model.attribute_dictionary("HibouTestingOperation", true)
end # def getDictionary
end # class TestingOperationCommitTool
end # module Hibou
if (not file_loaded?(__FILE__))
UI.menu("Plugins").add_item("Hibou-Testing") { Sketchup.active_model.tools.push_tool(Hibou;;TestingOperationCommitTool.new()) }
file_loaded(__FILE__)
end