Beispiel #1
0
        /// <summary>
        /// Promotes objects to template library.
        /// Items can be promoted when the active context is CircuitEditingContext and all the items are selected modules.</summary>
        /// <param name="items">Items to promote</param>
        public override void PromoteToTemplateLibrary(IEnumerable <object> items)
        {
            var itemsArray = items.ToArray();


            // cache the external connections
            var externalConnectionsDict = new Dictionary <Element, List <Wire> >();
            var circuitEditingContext   = ContextRegistry.GetActiveContext <CircuitEditingContext>();
            var graphContainer          = circuitEditingContext.CircuitContainer;

            foreach (var item in itemsArray)
            {
                var modules             = new HashSet <Element>();
                var internalConnections = new List <Wire>();
                var externalConnections = new List <Wire>();
                CircuitUtil.GetSubGraph(graphContainer, new[] { item }, modules, internalConnections, externalConnections, externalConnections);
                externalConnectionsDict.Add(item.Cast <Element>(), externalConnections);
            }

            // check source guid for templates to be replaced
            var templatingItems = new List <object>();
            var replacingItems  = new List <object>();

            foreach (var item in itemsArray)
            {
                if (item.Is <Module>())
                {
                    var module = item.Cast <Module>();
                    if (module.SourceGuid != Guid.Empty)
                    {
                        var existingTemplate = TemplatingContext.SearchForTemplateByGuid(TemplatingContext.RootFolder, module.SourceGuid);
                        if (existingTemplate != null)
                        {
                            string message = string.Format(
                                "Overwrite the existing \"{0}\"  Template with \"{1}\", or Add new one?\n".Localize(),
                                existingTemplate.Name, module.Name);

                            var dialog = new ConfirmationDialog("Overwrite / Add".Localize(), message);
                            dialog.YesButtonText = "Overwrite".Localize();
                            dialog.NoButtonText  = "Add".Localize();
                            dialog.Size          = new System.Drawing.Size(300, 100);
                            DialogResult result = dialog.ShowDialog();
                            if (result == DialogResult.Yes)
                            {
                                TemplatingContext.ReplaceTemplateModel(existingTemplate, module.Cast <DomNode>());
                                replacingItems.Add(item);
                            }
                            else if (result == DialogResult.No)
                            {
                                templatingItems.Add(item);
                            }
                            //otherwise the item is skipped
                        }
                    }
                    else
                    {
                        templatingItems.Add(item);
                    }
                }
            }

            // pack objects in IDataObject format
            var dataObject = new DataObject();

            dataObject.SetData(typeof(object[]), templatingItems.ToArray());

            // Insert() expects IDataObject
            TemplatingContext.Insert(dataObject);

            // replace the original items with the template instances
            foreach (var originalItem in templatingItems.Concat(replacingItems))
            {
                var template = TemplatingContext.LastPromoted(originalItem);
                var instance = TemplatingContext.CreateReference(template);

                var originalModule = originalItem.Cast <Element>();
                var replacedModule = instance.Cast <Element>();
                replacedModule.Bounds   = originalModule.Bounds;
                replacedModule.Position = originalModule.Position;

                // reroute external connections from original modules to replaced template instances.
                var externalConnections = externalConnectionsDict[originalModule];
                foreach (var connection in externalConnections)
                {
                    if (connection.InputElement.DomNode == originalModule.DomNode)
                    {
                        // input pin, i.e. pin on element that receives connection as input
                        int pinIndex = connection.InputPin.Index;
                        connection.InputPin       = replacedModule.Type.Inputs[pinIndex];
                        connection.InputElement   = replacedModule;
                        connection.InputPinTarget = null;                                // reset
                    }
                    else if (connection.OutputElement.DomNode == originalModule.DomNode) //output pin, i.e., pin on element that receives connection as output
                    {
                        connection.OutputPin       = replacedModule.Type.Outputs[connection.OutputPin.Index];
                        connection.OutputElement   = replacedModule;
                        connection.OutputPinTarget = null;
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                circuitEditingContext.CircuitContainer.Elements.Remove(originalItem.Cast <Element>());
                circuitEditingContext.CircuitContainer.Elements.Add(replacedModule);
            }
        }