Exemple #1
0
    public static RigidNode_Base ReadSkeleton(string path)
    {
        string         jsonData = File.ReadAllText(path);
        RigidNode_Base root     = null;

        JsonConverter[] converters = { new JointConverter(), new DriverMetaConverter() };
        JsonSkeleton    skeleton   = JsonConvert.DeserializeObject <JsonSkeleton>(jsonData, new JsonSerializerSettings()
        {
            Converters = converters
        });
        List <JsonSkeletonNode> nodes = skeleton.Nodes;

        if (nodes.Count < 1)
        {
            Console.Error.WriteLine("0 Nodes Loaded! Failed import");

            return(null);
        }

        foreach (JsonSkeletonNode node in nodes)
        {
            RigidNode_Base newNode = RigidNode_Base.NODE_FACTORY(new Guid(node.GUID));
            newNode.ModelFileName = node.ModelFileName;
            newNode.ModelFullID   = node.ModelID;


            if (node.ParentID == "-1")
            {
                root = newNode;
                root.driveTrainType = skeleton.DriveTrainType;
            }
            else
            {
                root.AddChild(node.joint, newNode);
            }
        }



        return(root);
    }
    /// <summary>
    /// Reads a RigidNode_Base with the given reader, list of nodes, and root node reference.
    /// </summary>
    /// <param name="reader"></param>
    /// <param name="nodes"></param>
    /// <param name="root"></param>
    private static void ReadNode_3_0(XmlReader reader, List <RigidNode_Base> nodes, ref RigidNode_Base root)
    {
        int parentID = -1;

        foreach (string name in IOUtilities.AllElements(reader))
        {
            switch (name)
            {
            case "Node":
                // Adds a new node to the list of RigidNode_Bases.
                nodes.Add(RigidNode_Base.NODE_FACTORY(new Guid(reader["GUID"])));
                break;

            case "ParentID":
                // Stores this ID for later use.
                parentID = reader.ReadElementContentAsInt();

                if (parentID == -1)     // If this is the root...
                {
                    root = nodes[nodes.Count - 1];
                }
                break;

            case "ModelFileName":
                // Assigns the ModelFileName property to the ModelFileName element value.
                nodes[nodes.Count - 1].ModelFileName = reader.ReadElementContentAsString();
                break;

            case "ModelID":
                // Assigns the ModelFullID property to the ModelID element value.
                nodes[nodes.Count - 1].ModelFullID = reader.ReadElementContentAsString();
                break;

            case "BallJoint":
                // Reads the current element as a BallJoint.
                nodes[parentID].AddChild(ReadBallJoint_3_0(reader.ReadSubtree()), nodes[nodes.Count - 1]);
                break;

            case "CylindricalJoint":
                // Reads the current element as a CylindricalJoint.
                nodes[parentID].AddChild(ReadCylindricalJoint_3_0(reader.ReadSubtree()), nodes[nodes.Count - 1]);
                break;

            case "LinearJoint":
                // Reads the current element as a LinearJoint.
                nodes[parentID].AddChild(ReadLinearJoint_3_0(reader.ReadSubtree()), nodes[nodes.Count - 1]);
                break;

            case "PlanarJoint":
                // Reads the current element as a PlanarJoint.
                nodes[parentID].AddChild(ReadPlanarJoint_3_0(reader.ReadSubtree()), nodes[nodes.Count - 1]);
                break;

            case "RotationalJoint":
                // Reads the current elemenet as a RotationalJoint.
                nodes[parentID].AddChild(ReadRotationalJoint_3_0(reader.ReadSubtree()), nodes[nodes.Count - 1]);
                break;

            case "JointDriver":
                // Add a joint driver to the skeletal joint of the current node.
                nodes[nodes.Count - 1].GetSkeletalJoint().cDriver = ReadJointDriver_3_0(reader.ReadSubtree());
                break;

            case "RobotSensor":
                nodes[nodes.Count - 1].GetSkeletalJoint().attachedSensors.Add(ReadRobotSensor_3_0(reader.ReadSubtree()));
                break;
            }
        }
    }