private void InitializeEditor()
        {
            Editor = new DiagramEditor();
            Editor.Context = new Context();
            Editor.Context.CurrentTree = this.ExplorerControl.SolutionTree;
            Editor.Context.CurrentCanvas = this.DiagramControl.DiagramCanvas;

            var counter = new IdCounter();
            counter.Set(3);
            this.DiagramControl.DiagramCanvas.SetCounter(counter);

            var prop = DiagramProperties.Default;
            this.DiagramControl.DiagramCanvas.SetProperties(prop);
            SetProperties(prop);

            Editor.Context.IsControlPressed = () => Keyboard.Modifiers == ModifierKeys.Control;
            Editor.Context.UpdateProperties = () => UpdateProperties(Editor.Context.CurrentCanvas.GetProperties());
            Editor.Context.SetProperties = (p) => SetProperties(p);

            Editor.Context.Clipboard = new WindowsClipboard();

            // diagram creator
            Editor.Context.DiagramCreator = GetDiagramCreator();

            // set checkbox states
            EnableInsertLast.IsChecked = Editor.Context.EnableInsertLast;
            EnableSnap.IsChecked = Editor.Context.EnableSnap;
            SnapOnRelease.IsChecked = Editor.Context.SnapOnRelease;

            // explorer control
            this.ExplorerControl.Editor = Editor;
            this.ExplorerControl.DiagramView = this.DiagramControl.RootBorder;

            // tree actions
            Editor.Context.CreateSolution = () => this.ExplorerControl.CreateTreeSolutionItem();
            Editor.Context.CreateProject = () => this.ExplorerControl.CreateTreeProjectItem();
            Editor.Context.CreateDiagram = () => this.ExplorerControl.CreateTreeDiagramItem();

            // update canvas grid
            UpdateDiagramGrid(false);
        }
        private IEnumerable<ITreeItem> Parse(IEnumerable<TreeProject> projects,
            IdCounter counter,
            ITreeItem solution)
        {
            var items = new List<ITreeItem>();

            foreach (var project in projects)
            {
                string name = project.Name;
                var diagrams = project.Diagrams.Reverse();
                var item = TreeEditor.CreateProjectItem(name, Context.CreateProject, counter);
                solution.Add(item);

                int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);
                counter.Set(Math.Max(counter.Count, id + 1));

                Parse(counter, diagrams, item, items);
            }

            var first = items.FirstOrDefault();
            if (first != null)
                first.SetSelected(true);

            return items;
        }
        private void Parse(IdCounter counter,
            TreeDiagram diagram,
            ITreeItem project,
            List<ITreeItem> diagrams)
        {
            var lines = diagram.Reverse();
            var first = lines.First().Split(new char[] { Constants.ArgumentSeparator, '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string name = first.Length >= 1 ? first[1] : null;

            var sb = new StringBuilder();
            foreach (var line in lines)
                sb.AppendLine(line);

            var item = TreeEditor.CreateDiagramItem(name, Context.CreateDiagram, counter);
            item.SetTag(new Diagram(sb.ToString(), null));

            project.Add(item);
            diagrams.Add(item);

            int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);
            counter.Set(Math.Max(counter.Count, id + 1));
        }
Esempio n. 4
0
        public TreeSolution Parse(string model, IDiagramCreator creator, ParseOptions options)
        {
            if (model == null || creator == null || options == null)
                return null;

            double offsetX = options.OffsetX;
            double offsetY = options.OffsetY;
            bool appendIds = options.AppendIds;
            bool updateIds = options.UpdateIds;
            bool select = options.Select;
            bool createElements = options.CreateElements;
            string name = null;
            string root = null;
            var counter = new IdCounter();
            var total = new IdCounter();
            var elements = new List<object>();
            Child child = null;
            var dict = new Dictionary<string, Child>();
            TreeSolution solution = null;
            TreeProjects projects = null;
            TreeProject project = null;
            TreeDiagrams diagrams = null;
            TreeDiagram diagram = null;

            var lines = model.Split(Environment.NewLine.ToCharArray(),
                StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                var args = GetArgs(line);

                int length = args.Length;
                if (length < 2)
                    continue;

                root = args[0];
                name = args[1];

                // root element
                if (StringUtil.Compare(root, Constants.PrefixRoot))
                {
                    // Solution
                    if (StringUtil.StartsWith(name, Constants.TagHeaderSolution) &&
                        (length == 2 || length == 3 || length == 4))
                    {
                        int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);
                        counter.Set(Math.Max(counter.Count, id + 1));

                        total.Next();

                        string tagFileName = (length == 3) ? args[2] : null;
                        string tableFileName = (length == 4) ? args[3] : null;

                        projects = new TreeProjects();
                        solution = new TreeSolution(name, tagFileName, tableFileName, projects);
                    }

                    // Project
                    else if (StringUtil.StartsWith(name, Constants.TagHeaderProject) &&
                        length == 2)
                    {
                        int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);
                        counter.Set(Math.Max(counter.Count, id + 1));

                        total.Next();

                        if (projects != null)
                        {
                            diagrams = new TreeDiagrams();
                            project = new TreeProject(name, diagrams);
                            projects.Push(project);
                        }
                    }

                    // Diagram
                    else if (StringUtil.StartsWith(name, Constants.TagHeaderDiagram) &&
                        length == 13)
                    {
                        int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);
                        counter.Set(Math.Max(counter.Count, id + 1));

                        total.Next();

                        if (diagrams != null)
                        {
                            diagram = new TreeDiagram();
                            diagrams.Push(diagram);
                            diagram.Push(line);
                        }

                        if (createElements == true)
                        {
                            var prop = new DiagramProperties();

                            prop.PageWidth = int.Parse(args[2]);
                            prop.PageHeight = int.Parse(args[3]);
                            prop.GridOriginX = int.Parse(args[4]);
                            prop.GridOriginY = int.Parse(args[5]);
                            prop.GridWidth = int.Parse(args[6]);
                            prop.GridHeight = int.Parse(args[7]);
                            prop.GridSize = int.Parse(args[8]);
                            prop.SnapX = double.Parse(args[9]);
                            prop.SnapY = double.Parse(args[10]);
                            prop.SnapOffsetX = double.Parse(args[11]);
                            prop.SnapOffsetY = double.Parse(args[12]);

                            creator.CreateDiagram(prop);

                            options.Properties = prop;
                        }
                    }

                    // Pin
                    else if (StringUtil.StartsWith(name, Constants.TagElementPin) &&
                        length == 4)
                    {
                        if (diagram != null)
                            diagram.Push(line);

                        total.Next();

                        if (createElements == true)
                        {
                            double x = double.Parse(args[2]);
                            double y = double.Parse(args[3]);
                            int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);

                            counter.Set(Math.Max(counter.Count, id + 1));

                            var element = creator.CreateElement(Constants.TagElementPin,
                                new object[] { id },
                                x + offsetX, y + offsetY, false);
                            elements.Add(element);

                            child = new Child(element, new List<Pin>());

                            if (dict.ContainsKey(name) == false)
                                dict.Add(name, child);
                            else
                                System.Diagnostics.Debug.Print("Dictionary already contains name key: {0}", name);
                        }
                    }

                    // Input
                    else if (StringUtil.StartsWith(name, Constants.TagElementInput) &&
                        (length == 4 || length == 5))
                    {
                        if (diagram != null)
                            diagram.Push(line);

                        total.Next();

                        if (createElements == true)
                        {
                            double x = double.Parse(args[2]);
                            double y = double.Parse(args[3]);
                            int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);
                            int tagId = (length == 5) ? int.Parse(args[4]) : -1;

                            counter.Set(Math.Max(counter.Count, id + 1));

                            var element = creator.CreateElement(Constants.TagElementInput,
                                new object[] { id, tagId },
                                x + offsetX, y + offsetY, false);
                            elements.Add(element);

                            child = new Child(element, new List<Pin>());

                            if (dict.ContainsKey(name) == false)
                                dict.Add(name, child);
                            else
                                System.Diagnostics.Debug.Print("Dictionary already contains name key: {0}", name);
                        }
                    }

                    // Output
                    else if (StringUtil.StartsWith(name, Constants.TagElementOutput) &&
                        (length == 4 || length == 5))
                    {
                        if (diagram != null)
                            diagram.Push(line);

                        total.Next();

                        if (createElements == true)
                        {
                            double x = double.Parse(args[2]);
                            double y = double.Parse(args[3]);
                            int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);
                            int tagId = (length == 5) ? int.Parse(args[4]) : -1;

                            counter.Set(Math.Max(counter.Count, id + 1));

                            var element = creator.CreateElement(Constants.TagElementOutput,
                                new object[] { id, tagId },
                                x + offsetX, y + offsetY, false);
                            elements.Add(element);

                            child = new Child(element, new List<Pin>());

                            if (dict.ContainsKey(name) == false)
                                dict.Add(name, child);
                            else
                                System.Diagnostics.Debug.Print("Dictionary already contains name key: {0}", name);
                        }
                    }

                    // AndGate
                    else if (StringUtil.StartsWith(name, Constants.TagElementAndGate) &&
                        length == 4)
                    {
                        if (diagram != null)
                            diagram.Push(line);

                        total.Next();

                        if (createElements == true)
                        {
                            double x = double.Parse(args[2]);
                            double y = double.Parse(args[3]);
                            int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);

                            counter.Set(Math.Max(counter.Count, id + 1));

                            var element = creator.CreateElement(Constants.TagElementAndGate,
                                new object[] { id },
                                x + offsetX, y + offsetY, false);
                            elements.Add(element);

                            child = new Child(element, new List<Pin>());

                            if (dict.ContainsKey(name) == false)
                                dict.Add(name, child);
                            else
                                System.Diagnostics.Debug.Print("Dictionary already contains name key: {0}", name);
                        }
                    }

                    // OrGate
                    else if (StringUtil.StartsWith(name, Constants.TagElementOrGate) &&
                        length == 4)
                    {
                        if (diagram != null)
                            diagram.Push(line);

                        total.Next();

                        if (createElements == true)
                        {
                            double x = double.Parse(args[2]);
                            double y = double.Parse(args[3]);
                            int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);

                            counter.Set(Math.Max(counter.Count, id + 1));

                            var element = creator.CreateElement(Constants.TagElementOrGate,
                                new object[] { id },
                                x + offsetX, y + offsetY, false);
                            elements.Add(element);

                            child = new Child(element, new List<Pin>());

                            if (dict.ContainsKey(name) == false)
                                dict.Add(name, child);
                            else
                                System.Diagnostics.Debug.Print("Dictionary already contains name key: {0}", name);
                        }
                    }

                    // Wire
                    else if (StringUtil.StartsWith(name, Constants.TagElementWire) &&
                        (length == 6 || length == 8 || length == 10))
                    {
                        if (diagram != null)
                            diagram.Push(line);

                        total.Next();

                        if (createElements == true)
                        {
                            double x1 = double.Parse(args[2]);
                            double y1 = double.Parse(args[3]);
                            double x2 = double.Parse(args[4]);
                            double y2 = double.Parse(args[5]);
                            bool startVisible = (length == 8 || length == 10) ? bool.Parse(args[6]) : false;
                            bool endVisible = (length == 8 || length == 10) ? bool.Parse(args[7]) : false;
                            bool startIsIO = (length == 10) ? bool.Parse(args[8]) : false;
                            bool endIsIO = (length == 10) ? bool.Parse(args[9]) : false;
                            int id = int.Parse(name.Split(Constants.TagNameSeparator)[1]);

                            counter.Set(Math.Max(counter.Count, id + 1));

                            var element = creator.CreateElement(Constants.TagElementWire,
                                new object[]
                                {
                                    x1 + offsetX, y1 + offsetY,
                                    x2 + offsetX, y2 + offsetY,
                                    startVisible, endVisible,
                                    startIsIO, endIsIO,
                                    id
                                },
                                0.0, 0.0, false);
                            elements.Add(element);

                            child = new Child(element, new List<Pin>());

                            if (dict.ContainsKey(name) == false)
                                dict.Add(name, child);
                            else
                                System.Diagnostics.Debug.Print("Dictionary already contains name key: {0}", name);
                        }
                    }
                }

                // child element
                else if (StringUtil.Compare(root, Constants.PrefixChild))
                {
                    if (StringUtil.StartsWith(name, Constants.TagElementWire) &&
                        length == 3)
                    {
                        if (diagram != null)
                            diagram.Push(line);

                        if (createElements == true && child != null)
                        {
                            var pins = child.Pins;

                            pins.Add(new Pin(name, args[2]));
                        }
                    }
                }
            }

            if (createElements == true)
            {
                creator.UpdateConnections(dict);

                if (appendIds == true)
                    creator.AppendIds(elements);

                if (updateIds == true)
                    creator.UpdateCounter(options.Counter, counter);

                creator.InsertElements(elements, select, offsetX, offsetY);
            }

            return solution;
        }