public static TreeItemType AddNewItem(ITree tree,
            Func<ITreeItem> createProject,
            Func<ITreeItem> createDiagram,
            IdCounter counter)
        {
            var selected = tree.GetSelectedItem() as ITreeItem;
            var type = GetTreeItemType(selected.GetUid());

            if (type == TreeItemType.Diagram)
            {
                var project = selected.GetParent() as ITreeItem;
                AddDiagram(project, true, createDiagram, counter);
                return TreeItemType.Diagram;
            }
            else if (type == TreeItemType.Project)
            {
                AddDiagram(selected, true, createDiagram, counter);
                return TreeItemType.Diagram;
            }
            else if (type == TreeItemType.Solution)
            {
                AddProject(selected, createProject, counter);
                return TreeItemType.Project;
            }

            return TreeItemType.None;
        }
        public static void AddDiagram(ITreeItem project,
            bool select,
            Func<ITreeItem> createDiagram,
            IdCounter counter)
        {
            var diagram = CreateDiagramItem(null, createDiagram, counter);

            project.Add(diagram);

            ModelEditor.Store(null, diagram);

            if (select == true)
                diagram.SetSelected(true);
        }
        public static void CreateDefaultSolution(ITree tree,
            Func<ITreeItem> createSolution,
            Func<ITreeItem> createProject,
            Func<ITreeItem> createDiagram,
            IdCounter counter)
        {
            var solution = CreateSolutionItem(null, createSolution, counter);
            tree.Add(solution);

            var project = CreateProjectItem(null, createProject, counter);
            solution.Add(project);

            var diagram = CreateDiagramItem(null, createDiagram, counter);
            project.Add(diagram);

            diagram.SetSelected(true);
        }
        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);
        }
 public void UpdateCounter(IdCounter original, IdCounter counter)
 {
     ModelEditor.IdsUpdateCounter(original, counter);
 }
        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));
        }
 private void Parse(IdCounter counter,
     IEnumerable<TreeDiagram> diagrams,
     ITreeItem project,
     List<ITreeItem> diagramList)
 {
     foreach (var diagram in diagrams)
         Parse(counter, diagram, project, diagramList);
 }
        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;
        }
        public void Parse(ITree tree,
            TreeSolution solution,
            IdCounter counter,
            Func<ITreeItem> CreateTreeSolutionItem)
        {
            string tagFileName = solution.TagFileName;
            string solutionName = solution.Name;
            var projects = solution.Projects.Reverse();

            TagsLoad(tagFileName);

            var item = TreeEditor.CreateSolutionItem(solutionName, CreateTreeSolutionItem, counter);
            tree.Add(item);

            Parse(projects, counter, item);
        }
 public void Clear(ITree tree, ICanvas canvas, IdCounter counter)
 {
     TreeEditor.Clear(tree);
     counter.Reset();
     TagsReset();
     SelectedListReset();
     canvas.SetTags(null);
 }
 public void SetCounter(IdCounter counter)
 {
     this.Counter = counter;
 }
Exemple #12
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;
        }
        public static ITreeItem CreateDiagramItem(string uid,
            Func<ITreeItem> createDiagram, 
            IdCounter counter)
        {
            var diagram = createDiagram();

            diagram.SetUid(uid == null ? Constants.TagHeaderDiagram + Constants.TagNameSeparator + counter.Next().ToString() : uid);

            return diagram;
        }
 public static void AddProject(ITreeItem solution,
     Func<ITreeItem> createProject,
     IdCounter counter)
 {
     solution.Add(CreateProjectItem(null, createProject, counter));
 }
        public static ITreeItem CreateSolutionItem(string uid, 
            Func<ITreeItem> createSolution, 
            IdCounter counter)
        {
            var solution = createSolution();

            solution.SetUid(uid == null ? Constants.TagHeaderSolution + Constants.TagNameSeparator + counter.Next().ToString() : uid);

            return solution;
        }
        public static ITreeItem CreateProjectItem(string uid,
            Func<ITreeItem> createProject, 
            IdCounter counter)
        {
            var project = createProject();

            project.SetUid(uid == null ? Constants.TagHeaderProject + Constants.TagNameSeparator + counter.Next().ToString() : uid);

            return project;
        }