private List <TessellatedSolid> TessellatedSolidsFromObject(Object obj, string name) { name += obj.name + "_" + obj.id; var result = new List <TessellatedSolid>(); if (obj.mesh != null) { result.Add(TessellatedSolidFromMesh(obj.mesh, obj.MaterialID, name)); } foreach (var comp in obj.components) { result.AddRange(TessellatedSolidsFromComponent(comp, name)); } return(result); }
/// <summary> /// Saves the specified stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="solids">The solids.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> internal static bool SaveModel(Stream stream, IList <TessellatedSolid> solids) { var objects = new List <Object>(); var baseMats = new BaseMaterials { id = 1 }; for (var i = 0; i < solids.Count; i++) { var solid = solids[i]; var thisObject = new Object { name = solid.Name, id = i + 2 }; // this is "+ 2" since the id's start with 1 instead of 0 plus BaseMaterials is typically 1, so start at 2. var triangles = new List <Triangle>(); foreach (var face in solid.Faces) { var colString = (face.Color ?? solid.SolidColor ?? new Color(Constants.DefaultColor)).ToString(); var colorIndex = baseMats.bases.FindIndex(col => col.colorString.Equals(colString)); if (colorIndex == -1) { colorIndex = baseMats.bases.Count; baseMats.bases.Add(new Base { colorString = colString }); } triangles.Add(new Triangle { v1 = face.Vertices[0].IndexInList, v2 = face.Vertices[1].IndexInList, v3 = face.Vertices[2].IndexInList, pid = 1, p1 = colorIndex }); } thisObject.mesh = new Mesh { vertices = solid.Vertices.Select(v => new threemfclasses.Vertex { x = v.X, y = v.Y, z = v.Z }).ToList(), triangles = triangles }; objects.Add(thisObject); } var metaData = new List <Metadata>(); var allRawComments = solids.SelectMany(s => s.Comments); var comments = new List <string>(); foreach (var comment in allRawComments.Where(string.IsNullOrWhiteSpace)) { var arrowIndex = comment.IndexOf("==>"); if (arrowIndex == -1) { comments.Add(comment); } else { var endOfType = arrowIndex - 1; var beginOfValue = arrowIndex + 3; //todo: check this -1 and +3 metaData.Add(new Metadata { type = comment.Substring(0, endOfType), Value = comment.Substring(beginOfValue) }); } } var threeMFData = new ThreeMFFileData { Units = solids[0].Units, Name = solids[0].Name.Split('_')[0], Language = solids[0].Language, metadata = metaData, build = new Build { Items = objects.Select(o => new Item { objectid = o.id }).ToList() }, resources = new Resources { basematerials = new[] { baseMats }.ToList(), //colors = colors, materials = materials, objects = objects } }; threeMFData.Comments.AddRange(comments); try { using (var writer = XmlWriter.Create(stream)) { writer.WriteComment(tvglDateMarkText); if (!string.IsNullOrWhiteSpace(solids[0].FileName)) { writer.WriteComment("Originally loaded from " + solids[0].FileName); } var serializer = new XmlSerializer(typeof(ThreeMFFileData), defXMLNameSpaceModel); serializer.Serialize(writer, threeMFData); } Message.output("Successfully wrote 3MF file to stream.", 3); return(true); } catch (Exception exception) { Message.output("Unable to write in model file.", 1); Message.output("Exception: " + exception.Message, 3); return(false); } }