/// <summary> /// Generate an xml doc and write the workspace to the given path /// </summary> /// <param name="xmlPath">The path to save to</param> /// <param name="workSpace">The workspace</param> /// <returns>Whether the operation was successful</returns> public static bool SaveWorkspace(string xmlPath, dynWorkspaceModel workSpace) { DynamoLogger.Instance.Log("Saving " + xmlPath + "..."); try { var xmlDoc = GetXmlDocFromWorkspace(workSpace, workSpace is HomeWorkspace); xmlDoc.Save(xmlPath); workSpace.FilePath = xmlPath; workSpace.OnWorkspaceSaved(); } catch (Exception ex) { //Log(ex); DynamoLogger.Instance.Log(ex.Message); DynamoLogger.Instance.Log(ex.StackTrace); Debug.WriteLine(ex.Message + " : " + ex.StackTrace); return(false); } return(true); }
/// <summary> /// Generate the xml doc of the workspace from memory /// </summary> /// <param name="workSpace">The workspace</param> /// <param name="savingHomespace"></param> /// <returns>The generated xmldoc</returns> public static XmlDocument GetXmlDocFromWorkspace( dynWorkspaceModel workSpace, bool savingHomespace) { try { //create the xml document var xmlDoc = new XmlDocument(); xmlDoc.CreateXmlDeclaration("1.0", null, null); var root = xmlDoc.CreateElement("dynWorkspace"); //write the root element root.SetAttribute("X", workSpace.X.ToString(CultureInfo.InvariantCulture)); root.SetAttribute("Y", workSpace.Y.ToString(CultureInfo.InvariantCulture)); root.SetAttribute("zoom", workSpace.Zoom.ToString(CultureInfo.InvariantCulture)); root.SetAttribute("Description", workSpace.Description); root.SetAttribute("Category", workSpace.Category); root.SetAttribute("Name", workSpace.Name); if (!savingHomespace) //If we are not saving the home space { var def = dynSettings.Controller.CustomNodeManager.GetDefinitionFromWorkspace(workSpace); Guid guid; if (def != null) { guid = def.FunctionId; } else { guid = Guid.NewGuid(); } root.SetAttribute("ID", guid.ToString()); } xmlDoc.AppendChild(root); var elementList = xmlDoc.CreateElement("dynElements"); //write the root element root.AppendChild(elementList); foreach (var el in workSpace.Nodes) { var typeName = el.GetType().ToString(); var dynEl = xmlDoc.CreateElement(typeName); elementList.AppendChild(dynEl); //set the type attribute dynEl.SetAttribute("type", el.GetType().ToString()); dynEl.SetAttribute("guid", el.GUID.ToString()); dynEl.SetAttribute("nickname", el.NickName); dynEl.SetAttribute("x", el.X.ToString(CultureInfo.InvariantCulture)); dynEl.SetAttribute("y", el.Y.ToString(CultureInfo.InvariantCulture)); dynEl.SetAttribute("isVisible", el.IsVisible.ToString().ToLower()); dynEl.SetAttribute("isUpstreamVisible", el.IsUpstreamVisible.ToString().ToLower()); dynEl.SetAttribute("lacing", el.ArgumentLacing.ToString()); el.Save(xmlDoc, dynEl, SaveContext.File); } //write only the output connectors var connectorList = xmlDoc.CreateElement("dynConnectors"); //write the root element root.AppendChild(connectorList); foreach (var el in workSpace.Nodes) { foreach (var port in el.OutPorts) { foreach ( var c in port.Connectors.Where(c => c.Start != null && c.End != null)) { var connector = xmlDoc.CreateElement(c.GetType().ToString()); connectorList.AppendChild(connector); connector.SetAttribute("start", c.Start.Owner.GUID.ToString()); connector.SetAttribute("start_index", c.Start.Index.ToString()); connector.SetAttribute("end", c.End.Owner.GUID.ToString()); connector.SetAttribute("end_index", c.End.Index.ToString()); if (c.End.PortType == PortType.INPUT) { connector.SetAttribute("portType", "0"); } } } } //save the notes var noteList = xmlDoc.CreateElement("dynNotes"); //write the root element root.AppendChild(noteList); foreach (var n in workSpace.Notes) { var note = xmlDoc.CreateElement(n.GetType().ToString()); noteList.AppendChild(note); note.SetAttribute("text", n.Text); note.SetAttribute("x", n.X.ToString(CultureInfo.InvariantCulture)); note.SetAttribute("y", n.Y.ToString(CultureInfo.InvariantCulture)); } return(xmlDoc); } catch (Exception ex) { Debug.WriteLine(ex.Message + " : " + ex.StackTrace); return(null); } }
/// <summary> /// Updates relevant parameters on save /// </summary> /// <param name="model">The workspace that was just saved</param> private static void OnWorkspaceSaved(dynWorkspaceModel model) { model.LastSaved = DateTime.Now; model.HasUnsavedChanges = false; }