/// <summary> /// Imports templates and template folders stored in an external file</summary> /// <param name="toFolder">Template folder into which to import templates and template folders</param> /// <param name="uri"></param> private void ImportTemplateLibrary(DomNode toFolder, Uri uri) { string filePath = uri.LocalPath; if (File.Exists(filePath)) { // read the existing templates document using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var newReader = new CircuitReader(TypeLoader); DomNode templatesRoot = newReader.Read(stream, uri); ImportTemplates(toFolder, templatesRoot, uri); } } }
/// <summary> /// Imports templates and template folders stored in an external file</summary> /// <param name="toFolder">Template folder into which to import templates and template folders</param> /// <param name="uri"></param> private void ImportTemplateLibrary(DomNode toFolder, Uri uri) { string filePath = uri.LocalPath; if (File.Exists(filePath)) { // read the existing templates document using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var newReader = new CircuitReader(TypeLoader); DomNode templatesRoot = newReader.Read(stream, uri); ImportTemplates(toFolder, templatesRoot, uri); } } }
/// <summary> /// Load circuit templates stored in an external file</summary> /// <param name="uri">Document URI, or null to present file open dialog to user</param> /// <returns>Returns the file path used to load the external templates. /// An empty string indicates no templates were loaded</returns> protected override ImportedContent LoadExternalTemplateLibrary(Uri uri) { string filePath = string.Empty; if (uri == null) { var dlg = new OpenFileDialog(); dlg.Filter = "Circuit Template File (*.circuit)|*.circuit".Localize(); dlg.CheckFileExists = true; if (dlg.ShowDialog() == DialogResult.OK) { uri = new Uri(dlg.FileName, UriKind.RelativeOrAbsolute); filePath = dlg.FileName; } } else { filePath = uri.LocalPath; } if (File.Exists(filePath)) { if (TemplatingContext.ValidateNewFolderUri(uri)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var reader = new CircuitReader(m_schemaLoader); var root = reader.Read(stream, uri); var toFolder = CreateTemplateFolder(); reader.ImportTemplates(toFolder.DomNode, root, uri); return(new ImportedContent(toFolder.DomNode, uri)); } } } return(new ImportedContent(null, null)); }
/// <summary> /// Opens or creates a document at the given URI. /// Creates and configures with control adapters D2dAdaptableControl to display subcircuit</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { CircuitReader reader = new CircuitReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.circuitDocumentType.Type, Schema.circuitRootElement); // create an empty root prototype folder node.SetChild( Schema.circuitDocumentType.prototypeFolderChild, new DomNode(Schema.prototypeFolderType.Type)); } CircuitDocument circuitCircuitDocument = null; if (node != null) { // now that the data is complete, initialize all other extensions to the Dom data node.InitializeExtensions(); AdaptableControl control = CreateCircuitControl(node); control.AddHelp("https://github.com/SonyWWS/ATF/wiki/Adaptable-Controls".Localize()); var viewingContext = node.Cast <ViewingContext>(); viewingContext.Control = control; circuitCircuitDocument = node.Cast <CircuitDocument>(); string fileName = Path.GetFileName(filePath); ControlInfo controlInfo = new ControlInfo(fileName, filePath, StandardControlGroup.Center); //Set IsDocument to true to prevent exception in command service if two files with the // same name, but in different directories, are opened. controlInfo.IsDocument = true; circuitCircuitDocument.ControlInfo = controlInfo; circuitCircuitDocument.Uri = uri; var editingContext = node.Cast <CircuitEditingContext>(); editingContext.GetLocalBound = GetLocalBound; editingContext.GetWorldOffset = GetWorldOffset; editingContext.GetTitleHeight = GetTitleHeight; editingContext.GetLabelHeight = GetLabelHeight; editingContext.GetSubContentOffset = GetSubContentOffset; control.Context = editingContext; editingContext.SchemaLoader = m_schemaLoader; // schema needed for cut and paste between applications m_circuitControlRegistry.RegisterControl(node, control, controlInfo, this); SkinService.ApplyActiveSkin(control); // Set the zoom and translation to show the existing items (if any). var enumerableContext = editingContext.Cast <IEnumerableContext>(); if (viewingContext.CanFrame(enumerableContext.Items)) { viewingContext.Frame(enumerableContext.Items); } //var viewingAdapter = control.As<ViewingAdapter>(); // enable toggle after initial frame operation. // viewingAdapter.ToggleFramingEnabled = true; // toggle frame/unframe. } return(circuitCircuitDocument); }
/// <summary> /// Opens or creates a document at the given URI. /// Creates and configures with control adapters D2dAdaptableControl to display subcircuit</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { CircuitReader reader = new CircuitReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.circuitDocumentType.Type, Schema.circuitRootElement); // create an empty root prototype folder node.SetChild( Schema.circuitDocumentType.prototypeFolderChild, new DomNode(Schema.prototypeFolderType.Type)); } CircuitDocument circuitCircuitDocument = null; if (node != null) { // now that the data is complete, initialize all other extensions to the Dom data node.InitializeExtensions(); AdaptableControl control = CreateCircuitControl(node); control.AddHelp("https://github.com/SonyWWS/ATF/wiki/Adaptable-Controls".Localize()); var viewingContext = node.Cast<ViewingContext>(); viewingContext.Control = control; circuitCircuitDocument = node.Cast<CircuitDocument>(); string fileName = Path.GetFileName(filePath); ControlInfo controlInfo = new ControlInfo(fileName, filePath, StandardControlGroup.Center); //Set IsDocument to true to prevent exception in command service if two files with the // same name, but in different directories, are opened. controlInfo.IsDocument = true; circuitCircuitDocument.ControlInfo = controlInfo; circuitCircuitDocument.Uri = uri; var editingContext = node.Cast<CircuitEditingContext>(); editingContext.GetLocalBound = GetLocalBound; editingContext.GetWorldOffset = GetWorldOffset; editingContext.GetTitleHeight = GetTitleHeight; editingContext.GetLabelHeight = GetLabelHeight; editingContext.GetSubContentOffset = GetSubContentOffset; control.Context = editingContext; editingContext.SchemaLoader = m_schemaLoader; // schema needed for cut and paste between applications m_circuitControlRegistry.RegisterControl(node, control, controlInfo, this); // Set the zoom and translation to show the existing items (if any). var enumerableContext = editingContext.Cast<IEnumerableContext>(); if (viewingContext.CanFrame(enumerableContext.Items)) viewingContext.Frame(enumerableContext.Items); } return circuitCircuitDocument; }
/// <summary> /// Rescan all referenced template documents </summary> protected override void ReloadExternalTemplates() { // 1st, scan the template hierarchy tree and remember all the existing template's guid -> DomNode map var oldNodeGuidDictionary = new Dictionary <string, DomNode>(); foreach (var node in TemplatingContext.RootFolder.DomNode.Subtree) { if (node.Is <Template>()) { var template = node.Cast <Template>(); if (template.Guid != Guid.Empty) { oldNodeGuidDictionary[template.Guid.ToString()] = node; } } } // 2nd, scan the template hierarchy tree again and reload all the external templates var newNodeGuidDictionary = new Dictionary <string, DomNode>(); foreach (var node in TemplatingContext.RootFolder.DomNode.Subtree) { var templateFolder = node.As <Sce.Atf.Dom.TemplateFolder>(); if (templateFolder == null || templateFolder.Url == null) { continue; } if (File.Exists(templateFolder.Url.LocalPath)) { using (FileStream stream = new FileStream(templateFolder.Url.LocalPath, FileMode.Open, FileAccess.Read)) { var reader = new CircuitReader(m_schemaLoader); var rootNode = reader.Read(stream, templateFolder.Url); foreach (var newnode in rootNode.Subtree) { var guidAttr = newnode.Type.GetAttributeInfo("guid"); if (guidAttr == null) { continue; } var guidStr = newnode.GetAttribute(guidAttr) as string; if (!string.IsNullOrEmpty(guidStr)) { newNodeGuidDictionary[guidStr] = newnode; } } reader.ImportTemplates(templateFolder.DomNode, rootNode, templateFolder.Url); } } } // 3rd, replace original nodes with newly loaded, matched by GUIDs foreach (var node in TemplatingContext.RootFolder.DomNode.GetRoot().Subtree) { // currently two types that reference templates: GroupInstance or ModuleInstance DomNode refNode; var groupInstance = node.As <GroupReference>(); if (groupInstance != null) { if (newNodeGuidDictionary.TryGetValue(groupInstance.Template.Guid.ToString(), out refNode)) { groupInstance.Template = refNode.As <Template>(); // need to reset pin targets due to DomNode replacements var graphContainer = groupInstance.DomNode.Parent.Cast <ICircuitContainer>(); foreach (var edge in graphContainer.Wires) { if (edge.OutputElement.DomNode == groupInstance.DomNode) { edge.OutputPinTarget = null; } if (edge.InputElement.DomNode == groupInstance.DomNode) { edge.InputPinTarget = null; } } continue; } } var moduleReference = node.As <ModuleReference>(); if (moduleReference != null) { if (newNodeGuidDictionary.TryGetValue(moduleReference.Template.Guid.ToString(), out refNode)) { moduleReference.Template = refNode.As <Template>(); // need to reset pin targets due to DomNode replacements var graphContainer = moduleReference.DomNode.Parent.Cast <ICircuitContainer>(); foreach (var edge in graphContainer.Wires) { if (edge.OutputElement.DomNode == moduleReference.DomNode) { edge.OutputPinTarget = null; } if (edge.InputElement.DomNode == moduleReference.DomNode) { edge.InputPinTarget = null; } } } } } // finally, prune the old nodes foreach (var keyValue in newNodeGuidDictionary) { DomNode oldNode; if (oldNodeGuidDictionary.TryGetValue(keyValue.Key, out oldNode)) { oldNode.RemoveFromParent(); } } }
/// <summary> /// Rescan all referenced template documents </summary> protected override void ReloadExternalTemplates() { // 1st, scan the template hierarchy tree and remember all the existing template's guid -> DomNode map var oldNodeGuidDictionary = new Dictionary<string, DomNode>(); foreach (var node in TemplatingContext.RootFolder.DomNode.Subtree) { if (node.Is<Template>()) { var template = node.Cast<Template>(); if (template.Guid != Guid.Empty) oldNodeGuidDictionary[template.Guid.ToString()] = node; } } // 2nd, scan the template hierarchy tree again and reload all the external templates var newNodeGuidDictionary = new Dictionary<string, DomNode>(); foreach (var node in TemplatingContext.RootFolder.DomNode.Subtree) { var templateFolder = node.As<Sce.Atf.Dom.TemplateFolder>(); if (templateFolder == null || templateFolder.Url == null) continue; if (File.Exists(templateFolder.Url.LocalPath)) { using (FileStream stream = new FileStream(templateFolder.Url.LocalPath, FileMode.Open, FileAccess.Read)) { var reader = new CircuitReader(m_schemaLoader); var rootNode = reader.Read(stream, templateFolder.Url); foreach (var newnode in rootNode.Subtree) { var guidAttr = newnode.Type.GetAttributeInfo("guid"); if (guidAttr == null) continue; var guidStr = newnode.GetAttribute(guidAttr) as string; if (!string.IsNullOrEmpty(guidStr)) newNodeGuidDictionary[guidStr] = newnode; } reader.ImportTemplates(templateFolder.DomNode, rootNode, templateFolder.Url); } } } // 3rd, replace original nodes with newly loaded, matched by GUIDs foreach (var node in TemplatingContext.RootFolder.DomNode.GetRoot().Subtree) { // currently two types that reference templates: GroupInstance or ModuleInstance DomNode refNode; var groupInstance = node.As<GroupReference>(); if (groupInstance != null) { if (newNodeGuidDictionary.TryGetValue(groupInstance.Template.Guid.ToString(), out refNode)) { groupInstance.Template = refNode.As<Template>(); // need to reset pin targets due to DomNode replacements var graphContainer = groupInstance.DomNode.Parent.Cast<ICircuitContainer>(); foreach (var edge in graphContainer.Wires) { if (edge.OutputElement.DomNode == groupInstance.DomNode) edge.OutputPinTarget = null; if (edge.InputElement.DomNode == groupInstance.DomNode) edge.InputPinTarget = null; } continue; } } var moduleReference = node.As<ModuleReference>(); if (moduleReference != null) { if (newNodeGuidDictionary.TryGetValue(moduleReference.Template.Guid.ToString(), out refNode)) { moduleReference.Template = refNode.As<Template>(); // need to reset pin targets due to DomNode replacements var graphContainer = moduleReference.DomNode.Parent.Cast<ICircuitContainer>(); foreach (var edge in graphContainer.Wires) { if (edge.OutputElement.DomNode == moduleReference.DomNode) edge.OutputPinTarget = null; if (edge.InputElement.DomNode == moduleReference.DomNode) edge.InputPinTarget = null; } } } } // finally, prune the old nodes foreach (var keyValue in newNodeGuidDictionary) { DomNode oldNode; if (oldNodeGuidDictionary.TryGetValue(keyValue.Key, out oldNode)) { oldNode.RemoveFromParent(); } } }
/// <summary> /// Load circuit templates stored in an external file</summary> /// <param name="uri">Document URI, or null to present file open dialog to user</param> /// <returns>Returns the file path used to load the external templates. /// An empty string indicates no templates were loaded</returns> protected override ImportedContent LoadExternalTemplateLibrary(Uri uri) { string filePath = string.Empty; if (uri == null) { var dlg = new OpenFileDialog(); dlg.Filter = "Circuit Template File (*.circuit)|*.circuit".Localize(); dlg.CheckFileExists = true; if (dlg.ShowDialog() == DialogResult.OK) { uri = new Uri(dlg.FileName, UriKind.RelativeOrAbsolute); filePath = dlg.FileName; } } else filePath = uri.LocalPath; if (File.Exists(filePath)) { if (TemplatingContext.ValidateNewFolderUri(uri)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var reader = new CircuitReader(m_schemaLoader); var root = reader.Read(stream, uri); var toFolder = CreateTemplateFolder(); reader.ImportTemplates(toFolder.DomNode, root, uri); return new ImportedContent(toFolder.DomNode, uri); } } } return new ImportedContent(null, null); }