Issue with RenderTexture creation/edit undo

Hello,

I have an issue with adding/editing RenderTextures in the Rhino document.
The command I implemented creates a new RenderTexture or edits the rotation of an already existing one. I can see the changes in the RenderTextureTable at the end of the command. Although, when I undo the command, the RenderTexture does not undo its changes, like they were not registered inside the command. I tried to use BeginChange and EndChange on the texture table and on the single textures to notify the change, but it did not work.
Any suggestions?

Thanks in advance,
Emma

@Emma8 Could you provide some code that doesn’t work?

Hello, of course! I attach here the piece of code where the texture is edited: it is part of a method called from a plug in command (so derived from a Rhino Command class). I also tried to add RhinoDoc.BeginUndoRecord and EndUndoRecord at the beginning and end of the command, but it made no difference.

var undoid=doc.BeginUndoRecord("edit texture");

(...)

       doc.RenderTextures.BeginChange(RenderContent.ChangeContexts.Program);
      for (int i = 0; i < doc.RenderTextures.Count; ++i) {
        var tex = doc.RenderTextures[i];
        if (tex.Id == textureId) {
          tex.BeginChange(RenderContent.ChangeContexts.Program);
          tex.SetRotation(new Vector3d(0, 0, rotation), RenderContent.ChangeContexts.Program);
          tex.EndChange();
          doc.RenderTextures.EndChange();

          return;
        }
      }

(...)

doc.EndUndoRecord(undoid);

@Emma8 The return inside the loop short-circuits the EndUndoRecord call - that’s my first thought.

@Emma8

This should work:

using (var undo = new ContentUndoHelper(doc))
      { 
        for (int i = 0; i < doc.RenderTextures.Count; ++i)
        {
          var tex = doc.RenderTextures[i];
          if (tex.Id == textureId)
          {
            undo.ModifyContent(tex);

            tex.BeginChange(RenderContent.ChangeContexts.Program);
            tex.SetRotation(new Vector3d(0, 0, 45.0), RenderContent.ChangeContexts.Program);
            tex.EndChange();
          }
        }
      }

That works, thank you very much!