Change plugin variables within Undo
-
Hi,
In my plugin I define undoable operation with start_operation and commit_operation around my code.
Operation does next:- check some flag value
- based on flag changes materials of multiple surfaces in the model
- sets new value to the flag
If I use undo, after performing operation - materials are changed back correctly, but my internal flag variable is not, so next time I try to run operation staring flag value is wrong.
Question:
Is there some way to define which function Sketchup should call when Edit->Undo is clicked?I've looked Transaction observers, but it seems like they will trigger for every step of my operation (for each surface's material change), which is not the thing I need.
I've tried to find similar question on the forum, and also browsed through API documentation, but haven't found solution.
Thanks in advance,
Marija -
Do you have a reproducible snippet of code?
By flag, I assume you mean attribute or variable?
-
Hi,
Here is example of code.
I have global variable @@dl_materials_changed which enables toggling on-off of faces materials in the model.
Each time user press UI button this function onToggleMaterial is called.So with this code Undo correctly switches materials back, but @@dl_materials_changed flag is not changed, so next time I press button in UI it doesn't have correct value.
def onToggleMaterial() Sketchup.active_model.start_operation("toggle materials") showFCMaterials() Sketchup.active_model.commit_operation() end def showFCMaterials() #load all faces in @faces varialbe @faces = getFaces(Sketchup.active_model.entities) #Set materials if flag is true if @@dl_materials_changed @faces.each {|face| material_name = face.get_attribute("faceData","material_name","") if material_name != "" face.material = materials[material_name] else face.material = nil end } @@dl_materials_changed = false else @faces.each_value {|face| face.material = nil } @@dl_materials_changed = true end end
-
you could store the value/flag on the model instead, then it would also change with the undo steps
model = Sketchup.active_model model.set_attribute('maricanis', 'dl_materials_changed', true) #... model.get_attribute('maricanis', 'dl_materials_changed', false)
Advertisement