Adding undo-redo to my extension

Hi there,
I am developing an extension where users use the extension exclusively to do all the operations. I am now facing problems with undo/redo.
Quick overview of the extension. There are predefined models in an HTML window that the designer drags and drops to the SketchUp model. After dropping the model using a new window the designer can update the dropped model(like width, height, add few things to the model. All from the configuration window.).

I have Sketchup.active_model.start_operation and Sketchup.active_model.commit operation at correct places but I still see a bunch of other transaction on the undo/redo stack. I dug a little deeper and found that few Sketchup operations create a transaction on there own like Sketchup.active_model.set_attribute and Sketchup.active_model.active_entities.erase_entities.

Q: Is there a way to avoid having these extra transactions on the undo/redo stack. Maybe I can club them with my transaction so that the end-user can use a smooth and organic operation.

Every single operation what causing model to change, will be recorded to undo stack.

It is a developer responsibility to wrap multiple model changes caused by the same user action into a single undo operation using Sketchup::Model#start_operation and Sketchup::Model#commit_operation.

Actually you can make not only one but more “own club” of undo operations, if your tool is complex and you feel it is necessary.
If you do not include an operation what caused a model change that will be still recorded and will shown as independent Undo action on UI.

So, no, it is not possible to avoid extra transactions and yes, please “*club them* with your transaction so that the end-user can use a smooth and organic operation.”

Homepage | SketchUp Developer extension-ux-guidelines


Ps. I put back your topic to the right category.

1 Like

If you see them added to the undo stack it means there was no operation “open” when you made the changes to the model.

1 Like

Yes there are both some API calls and native features that will close any current operation (an undo step) and start their own. Model#place_component is one of these quirky features.

To “club”, or combine these operations with your own operations, you need to use the 3rd and 4th
transparent arguments to the Model#start_operation method.

1 Like

Is there any list that has all the Sketchup APIs that start a transaction of their own? Like the Sketchup.active_model.active_entities.erase_entities or Model#place_component

Not that I know of, … but we could start a list.
You could also search this category and the API Issue tracker.

Anything that modifies the model will create a transaction if none is open.

Every API call that makes a “physical” change to the model (i.e. not things like moving the camera or changing the model-wide display settings) adds to the undo stack. The methods you list are no exceptions but follow the general rule. Calls for starting and committing an operations makes all these small transaction display to the user as just one big change in the undo stack. This is also how the user would think of a model change that they trigger with e.g. a click, even if the operation internally takes several transactions to carry out.

Maybe you could describe at a higher level what you are trying to achieve and what is confusing you?