private void read_atom(String filename) { atomlist = new List <atom_info>(); try { string line; StreamReader fileReader = new StreamReader(filename, Encoding.Default); // The first two lines of the file are metadata // The first line is the number of atoms // The second line is the types of atoms line = fileReader.ReadLine(); // Convert this line to a int int numAtoms; numAtoms = int.Parse(line); line = fileReader.ReadLine(); string[] atomTypesRAW = line.Split(' '); List <string> atomTypes = new List <string>(); // This line is harder to read because VESTA XYZ files are dumb IMO... for (int i = 0; i < atomTypesRAW.Length; i++) { // Make sure split string is valid // If so add it to our array if (atomTypesRAW[i].Length > 0) { atomTypes.Add(atomTypesRAW[i]); } } // Now read all the atoms in the file. We can read lines for the numAtoms in the file for (int i = 0; i < numAtoms; i++) { line = fileReader.ReadLine(); // For right now, just make a sphere for every atom we see (sphere size of .5 or something) string[] atomDataRAW = line.Split(' '); // There should be 4 pieces of valid data. The first one is the atom name. The rest are float values of position in the molecule bool readName = false; // Simple check to see if we have read a name or not string atomName = "C"; float? x = null; float? y = null; float? z = null; for (int j = 0; j < atomDataRAW.Length; j++) { if (atomDataRAW[j].Length > 0) { if (!readName) { atomName = atomDataRAW[j]; readName = true; } else { if (x == null) { x = float.Parse(atomDataRAW[j], System.Globalization.CultureInfo.InvariantCulture.NumberFormat); } else if (y == null) { y = float.Parse(atomDataRAW[j], System.Globalization.CultureInfo.InvariantCulture.NumberFormat); } else { z = float.Parse(atomDataRAW[j], System.Globalization.CultureInfo.InvariantCulture.NumberFormat); } } } } atom_info atm = new atom_info(atomName, x.GetValueOrDefault() / 22.0f, y.GetValueOrDefault() / 22.0f, z.GetValueOrDefault() / 22.0f, null); atm.set_sphere(draw_atom(atm)); atomlist.Add(atm); } } catch (Exception e) { Debug.LogError("Something Went Wrong: " + e.Message + "\n" + e.StackTrace + " " + e.Source); Debug.LogError(filename); return; } Atom_Parent.transform.parent = null; var children = Atom_Parent.transform.Cast <Transform>().ToList(); var pos = Vector3.zero; foreach (var C in children) { pos += C.position; C.parent = null; } pos /= children.Count; Atom_Parent.transform.position = pos; overallParent.transform.position = pos; foreach (var C in children) { C.parent = Atom_Parent.transform; } Atom_Parent.transform.parent = overallParent.transform; }
GameObject draw_atom(atom_info i) { GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.parent = Atom_Parent.transform; sphere.name = i.name + "\t" + i.pos.ToString(); sphere.transform.position = i.pos; switch (i.name) { case "Pb": sphere.transform.localScale = new Vector3(0.08f, 0.08f, 0.08f); sphere.GetComponent <Renderer>().material.color = colors[1]; i.set_radius(1.47f); i.set_weight(207.2f); break; case "I": sphere.transform.localScale = new Vector3(0.07f, 0.07f, 0.07f); sphere.GetComponent <Renderer>().material.color = colors[2]; i.set_radius(1.33f); i.set_weight(126.904f); break; case "C": sphere.transform.localScale = new Vector3(0.06f, 0.06f, 0.06f); sphere.GetComponent <Renderer>().material.color = colors[3]; i.set_radius(0.77f); i.set_weight(12.011f); break; case "H": sphere.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f); sphere.GetComponent <Renderer>().material.color = colors[4]; i.set_radius(0.37f); i.set_weight(1.008f); break; case "N": sphere.transform.localScale = new Vector3(0.06f, 0.06f, 0.06f); sphere.GetComponent <Renderer>().material.color = colors[5]; i.set_radius(0.75f); i.set_weight(14.007f); break; case "Zn": sphere.transform.localScale = new Vector3(0.07f, 0.07f, 0.07f); sphere.GetComponent <Renderer>().material.color = colors[6]; i.set_radius(1.31f); i.set_weight(65.38f); break; case "Cu": sphere.transform.localScale = new Vector3(0.07f, 0.07f, 0.07f); sphere.GetComponent <Renderer>().material.color = colors[0]; i.set_radius(1.38f); i.set_weight(63.546f); break; case "S": sphere.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f); sphere.GetComponent <Renderer>().material.color = colors[2]; i.set_radius(1.02f); i.set_weight(32.06f); break; default: sphere.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f); sphere.GetComponent <Renderer>().material.color = colors[6]; i.set_radius(1f); i.set_weight(10f); break; } i.set_sphere(sphere); return(sphere); }