Issue with inputbox, attribute_dictionary, and start_operation

Hello,
I’m developing a plugin that uses UI.inputbox with dropdown lists.
My parameters are stored in a dictionary.
The problem is that each time I display the dialog box, I get 5 “Properties” entries in the undo stack.

I was able to isolate the problem with this 2 minimal codes :

# Test with dictionary
def test_with_dict
  model = Sketchup.active_model
  dict = model.attribute_dictionary("Test", true)
  dict['val1'] = "Yes"
  dict['val2'] = "No"
  prompts = ["Option 1:", "Option 2:"]
  defaults = [dict['val1'], dict['val2']]
  list = ["Yes|No", "Yes|No"]
  result = UI.inputbox(prompts, defaults, list, "Test with dictionary")
end
test_with_dict

When running in the Ruby console, this code produce 3 entries in the undo stack.

# Test without dictionary
def test_without_dict
  prompts = ["Option 1:", "Option 2:"]
  defaults = ["Yes", "No"]
  list = ["Yes|No", "Yes|No"]
  result = UI.inputbox(prompts, defaults, list, "Test without dictionary")
end
test_without_dict

0 entries in the undo stack.

I tried extracting the values from the dictionary into local variables before calling inputbox but it doesn’t work.

Is there a solution to avoid these multiple entries in the undo stack?

Thanks in advance for your help.

You need to use
the #start_operation
the #commit_operation

Then the methods in between - which are "physically " changing the model, will be wrapped into one operation.
eg

# Test with dictionary
def test_with_dict
  model = Sketchup.active_model
  model.start_operation("Change Dictionary ", true)
  dict = model.attribute_dictionary("Test", true)
  dict['val1'] = "Yes"
  dict['val2'] = "No"
  model.commit_operation
  prompts = ["Option 1:", "Option 2:"]
  defaults = [dict['val1'], dict['val2']]
  list = ["Yes|No", "Yes|No"]
  result = UI.inputbox(prompts, defaults, list, "Test with dictionary")
end
test_with_dict

Tanks a lot @dezmo , I made the wrong tests, it was not the fault of the inputbox.

1 Like