/// <summary>
        /// Compiles a Scene Graph into XML.
        /// </summary>
        public static string CompileXML(SceneGraph graph)
        {
            X3DSceneGraphXMLSerializer serializer;

            serializer = new X3DSceneGraphXMLSerializer(graph.GetRoot());

            return(serializer.Serialize());
        }
Пример #2
0
    public static Tree CreateTreeFromSceneGraph(SceneGraph graph)
    {
        Tree           t;
        Stack <_node>  work_items;
        _node          n;
        int            i;
        _node          child;
        SceneGraphNode childsgn;
        TNode          childtn;
        SceneGraphNode rootsgn;
        TNode          roottn;
        _node          root;
        int            numNodes;

        t           = new Tree();
        rootsgn     = graph.GetRoot();
        roottn      = new TNode();
        roottn.Data = rootsgn;
        root        = new _node(roottn, rootsgn);
        work_items  = new Stack <_node>();
        work_items.Push(root);
        numNodes = 0;

        while (work_items.Count > 0)
        {
            n = work_items.Pop();

            n.tnode.Level = n.sgn.Depth;

            numNodes++;

            for (i = 0; i < n.sgn.Children.Count; i++)
            {
                childsgn = n.sgn.Children[i];

                childtn = new TNode()
                {
                    Data = childsgn
                };

                childtn.Parent = n.tnode;
                childtn.Level  = n.sgn.Depth;

                child = new _node(childtn, childsgn);

                n.tnode.Children.Add(childtn);

                work_items.Push(child);
            }
        }

        t.Root  = root.tnode;
        t.Count = numNodes;

        return(t);
    }
        /// <summary>
        /// Builds a CSharp source code representation of a Scene Graph.
        /// </summary>
        /// <param name="graph">
        /// The Specified Scene Graph to compile.
        /// </param>
        /// <returns>
        /// CSharp source code strings.
        /// </returns>
        public static string CompileAsObjects(SceneGraph graph)
        {
            string source;
            Queue <SceneGraphNode> work_items;
            SceneGraphNode         n, root;
            string           nodeName;
            List <attribute> attributes;
            List <string>    lines;
            string           line;
            int i;
            //IEnumerable<KeyValuePair<string, string>> attributeItems;
            const string NODE_IDENTIFIER = "n";

            lines      = new List <string>();
            root       = graph.GetRoot();
            work_items = new Queue <SceneGraphNode>();
            work_items.Enqueue(root);

            source = "";

            while (work_items.Count > 0)
            {
                n = work_items.Dequeue();

                attributes = GetAttributes(n);

                nodeName = n.ToString().Replace("X3D.", "");

                line = string.Format("{0} {2}{1} = new {0}();", nodeName, n._ID, NODE_IDENTIFIER);
                lines.Add(line);

                if (n.Parent != null)
                {
                    line = string.Format("{2}{0}.Parent = {2}{1};", n._ID, n.Parent._ID, NODE_IDENTIFIER);
                    lines.Add(line);
                }

                line = string.Format("{2}{0}.Depth = {1};", n._ID, n.Depth, NODE_IDENTIFIER);
                lines.Add(line);

                //attributeItems = attributes.AllKeys.SelectMany(attributes.GetValues, (k, v) => new KeyValuePair<string,string>(k, v ));

                foreach (attribute attribute in attributes)
                {
                    if (attribute.compiled)
                    {
                        line = string.Format("{4}{3}.{0} = {1}", attribute.name, attribute.value, nodeName, n._ID, NODE_IDENTIFIER);
                    }
                    else
                    {
                        line = string.Format("{4}{3}.{0} = \"{1}\";", attribute.name, attribute.value, nodeName, n._ID, NODE_IDENTIFIER);
                    }

                    lines.Add(line);
                }

                foreach (SceneGraphNode child in n.Children)
                {
                    work_items.Enqueue(child);
                }
            }

            line = string.Format("Application = new SceneGraph({1}{0});", root._ID, NODE_IDENTIFIER);
            lines.Add(line);

            line = "RunApplication();";
            lines.Add(line);

            for (i = 0; i < lines.Count; i++)
            {
                line = lines[i];

                source += line + "\n";
            }

            return(source);
        }