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> /// Writes out the skeleton file for the skeleton with the base provided to the path provided. /// </summary> /// <param name="path"></param> /// <param name="baseNode"></param> public static void WriteSkeleton(string path, RigidNode_Base baseNode) { //EXPERIMENT // JSONExport(path, baseNode); List <RigidNode_Base> nodes = new List <RigidNode_Base>(); baseNode.ListAllNodes(nodes); // Determine the parent ID for each node in the list. int[] parentID = new int[nodes.Count]; for (int i = 0; i < nodes.Count; i++) { if (nodes[i].GetParent() != null) { parentID[i] = nodes.IndexOf(nodes[i].GetParent()); if (parentID[i] < 0) { throw new Exception("Can't resolve parent ID for " + nodes[i].ToString()); } } else { parentID[i] = -1; } } JsonSkeleton jsonSkeleton = new JsonSkeleton(); jsonSkeleton.Version = BXDJ_CURRENT_VERSION; for (int i = 0; i < nodes.Count; i++) { JsonSkeletonNode node = new JsonSkeletonNode(); node.GUID = nodes[i].GUID.ToString(); node.ParentID = parentID[i].ToString(); node.ModelFileName = FileUtilities.SanatizeFileName("node_" + i + ".bxda"); node.ModelID = nodes[i].GetModelID(); if (parentID[i] >= 0) { node.joint = nodes[i].GetSkeletalJoint(); node.joint.typeSave = node.joint.GetJointType(); if (node.joint.cDriver != null) { node.joint.cDriver.port1 += 1; node.joint.cDriver.port2 += 1; } } //WriteJoint(nodes[i].GetSkeletalJoint(), node); jsonSkeleton.Nodes.Add(node); } jsonSkeleton.DriveTrainType = baseNode.driveTrainType; jsonSkeleton.SoftwareExportedWith = "INVENTOR"; var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }; string toWrite = JsonConvert.SerializeObject(jsonSkeleton, settings); File.WriteAllText(path, toWrite); }