Can I add an attribute to any new entity and not mess up undo/redo?


#1

I have a script that uses the entity observer to add an attribute to any new entities added to the model. I found that adding an attribute adds to the undo history. I’d like to add attribute to occur silently and either not allow it to be undone or package it up with the entity addition in the history queue.

Is this possible using the entity observer or should I be doing this differently?


#2

You would need to wrap the attribute assignment inside an undo operation, and specify a transparent argument so that your operation is appended to the previous operation.

See API: Sketchup::Model#start_operation()

  def add_attribute_to_entity( model, entity, value )
    #
    model.start_operation(@opname,true,false,true)
    ###
      entity.set_attribute( @dict, @key, value )
    ###
    model.commit_operation
    #
  end

#3

Hello DanRathbun,
Is there another way beside appending the operation we want to hide to another operation ?
I’m in the same situation as benwart, I’m saving some metadata in the model and i want to make that operation invisible to the user, so he can’t undo a simple set_attribute operation on the active model.


#4

No, not really.

There is no guarantee the user (or some other cause, like an error,) will not undo the operation in which you have set the attribute(s).

The only thing I can think of is, a forced saving of the model to file. Saving clears the undo/redo stack.


You are not the first programmer to wish for setting some attributes outside the undo/redo stack.

@tt_su (ping)