internal void ApplyPreset(PresetModel state)
{
if (state == null)
{
Log("Attempted to apply a PresetState that was null");
return;
}
//start an undoBeginGroup
using (var undoGroup = this.undoRecorder.BeginActionGroup())
{
//reload each node, and record each each modification in the undogroup
foreach (var node in state.Nodes)
{
//check that node still exists in this workspace,
//otherwise bail on this node, check by GUID instead of nodemodel
if (nodes.Select(x=>x.GUID).Contains(node.GUID))
{
var originalpos = node.Position;
var serializedNode = state.SerializedNodes.ToList().Find(x => Guid.Parse(x.GetAttribute("guid")) == node.GUID);
//overwrite the xy coords of the serialized node with the current position, so the node is not moved
serializedNode.SetAttribute("x", originalpos.X.ToString(CultureInfo.InvariantCulture));
serializedNode.SetAttribute("y", originalpos.Y.ToString(CultureInfo.InvariantCulture));
serializedNode.SetAttribute("isPinned", node.PreviewPinned.ToString());
this.undoRecorder.RecordModificationForUndo(node);
this.ReloadModel(serializedNode);
}
}
//select all the modified nodes in the UI
DynamoSelection.Instance.ClearSelection();
foreach(var node in state.Nodes)
{
DynamoSelection.Instance.Selection.Add(node);
}
}
}