// Create the buffer for vertex positions. private static async Task SetVerticesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DDouble; description.Stride = 3; // Three values per vertex (x, y, z). mesh.VertexPositionsDescription = description; mesh.VertexCount = 8; // 8 total vertices in the cube // Create the buffer into which we will write the vertex positions. mesh.CreateVertexPositions(sizeof(double) * description.Stride * mesh.VertexCount); // Fill the buffer with vertex coordinates. using (var stream = mesh.GetVertexPositions().AsStream()) { double[] vertices = { 0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0, 0, 0, 10, 10, 0, 10, 0, 10, 10, 10, 10, 10, }; byte[] vertexData = vertices.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); await stream.WriteAsync(vertexData, 0, vertexData.Length); } }
// Create the buffer for triangle indices. private static async Task SetTriangleIndicesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DUInt; description.Stride = 3; // 3 vertex position indices per triangle mesh.IndexCount = 12; // 12 triangles in the cube mesh.TriangleIndicesDescription = description; // Create the buffer into which we will write the triangle vertex indices. mesh.CreateTriangleIndices(sizeof(UInt32) * description.Stride * mesh.IndexCount); // Fill the buffer with triangle vertex indices. using (var stream = mesh.GetTriangleIndices().AsStream()) { UInt32[] indices = { 1, 0, 2, 1, 2, 3, 0, 1, 5, 0, 5, 4, 1, 3, 7, 1, 7, 5, 2, 7, 3, 2, 6, 7, 0, 6, 2, 0, 4, 6, 6, 5, 7, 4, 5, 6, }; var vertexData = indices.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); await stream.WriteAsync(vertexData, 0, vertexData.Length); } }
/// <summary> /// Set materialindices on the mesh /// </summary> /// <param name="mesh"></param> /// <returns></returns> //<SnippetMaterialIndices> private static async Task SetMaterialIndicesAsync(Printing3DMesh mesh) { // declare a description of the material indices Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DUInt; // 4 indices for material description per triangle description.Stride = 4; // 12 triangles total mesh.IndexCount = 12; mesh.TriangleMaterialIndicesDescription = description; // create space for storing this data mesh.CreateTriangleMaterialIndices(sizeof(UInt32) * 4 * 12); { // each row is a triangle face (in the order they were created) // first column is the id of the material group, last 3 columns show which material id (within that group) // maps to each triangle vertex (in the order they were listed when creating triangles) UInt32[] indices = { // base materials: // in the BaseMaterialGroup (id=1), the BaseMaterial with id=0 will be applied to these triangle vertices 1, 0, 0, 0, 1, 0, 0, 0, // color materials: // in the ColorMaterialGroup (id=2), the ColorMaterials with these ids will be applied to these triangle vertices 2, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 1, 2, 2, 1, 0, 2, // composite materials: // in the CompositeMaterialGroup (id=3), the CompositeMaterial with id=0 will be applied to these triangles 3, 0, 0, 0, 3, 0, 0, 0, // texture materials: // in the Texture2CoordMaterialGroup (id=4), each texture coordinate is mapped to the appropriate vertex on these // two adjacent triangle faces, so that the square face they create displays the original rectangular image 4, 0, 3, 1, 4, 2, 3, 0, }; // get the current (unassigned) vertex data as a stream and write our new 'indices' data to it. var stream = mesh.GetTriangleMaterialIndices().AsStream(); var vertexData = indices.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); var len = vertexData.Length; await stream.WriteAsync(vertexData, 0, vertexData.Length); } }
private async Task GetVerticesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DDouble; description.Stride = 3; mesh.VertexCount = verticesCount; mesh.CreateVertexPositions(sizeof(double) * 3 * mesh.VertexCount); mesh.VertexPositionsDescription = description; using (var stream = mesh.GetVertexPositions().AsStream()) { var data = verticesPrint.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); await stream.WriteAsync(data, 0, data.Length); } }
private static async Task GetMaterialIndicesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DUInt; description.Stride = 4; mesh.IndexCount = verticesMaterialCount; mesh.TriangleMaterialIndicesDescription = description; mesh.CreateTriangleMaterialIndices(sizeof(UInt32) * 4 * mesh.IndexCount); var stream = mesh.GetTriangleMaterialIndices().AsStream(); { var data = indicesMaterialPrint.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); await stream.WriteAsync(data, 0, data.Length); } }
// Create the buffer for material indices. private static async Task SetMaterialIndicesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DUInt; description.Stride = 4; // 4 indices per material mesh.IndexCount = 12; // 12 triangles with material mesh.TriangleMaterialIndicesDescription = description; // Create the buffer into which we will write the material indices. mesh.CreateTriangleMaterialIndices(sizeof(UInt32) * description.Stride * mesh.IndexCount); // Fill the buffer with material indices. using (var stream = mesh.GetTriangleMaterialIndices().AsStream()) { UInt32[] indices = { // base materials: // in the BaseMaterialGroup, the BaseMaterial with id=0 will be applied to these triangle vertices 2, 0, 0, 0, 2, 0, 0, 0, // color materials: // in the ColorMaterialGroup, the ColorMaterials with these ids will be applied to these triangle vertices 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, // composite materials: // in the CompositeMaterialGroup, the CompositeMaterial with id=0 will be applied to these triangles 3, 0, 0, 0, 3, 0, 0, 0, // texture materials: // in the Texture2CoordMaterialGroup, each texture coordinate is mapped to the appropriate vertex on these // two adjacent triangle faces, so that the square face they create displays the original rectangular image 4, 0, 3, 1, 4, 2, 3, 0, }; var vertexData = indices.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); await stream.WriteAsync(vertexData, 0, vertexData.Length); } }
//</SnippetMaterialIndices> /// <summary> /// Set the triangle indices on the mesh /// </summary> /// <param name="mesh">Printing3DMesh</param> /// <returns></returns> //<SnippetTriangleIndices> private static async Task SetTriangleIndicesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DUInt; // 3 vertex indices description.Stride = 3; // 12 triangles in all in the cube mesh.IndexCount = 12; mesh.TriangleIndicesDescription = description; // allocate space for 12 triangles mesh.CreateTriangleIndices(sizeof(UInt32) * 3 * 12); // get a datastream of the triangle indices (should be blank at this point) var stream2 = mesh.GetTriangleIndices().AsStream(); { // define a set of triangle indices: each row is one triangle. The values in each row // correspond to the index of the vertex. UInt32[] indices = { 1, 0, 2, 1, 2, 3, 0, 1, 5, 0, 5, 4, 1, 3, 7, 1, 7, 5, 2, 7, 3, 2, 6, 7, 0, 6, 2, 0, 4, 6, 6, 5, 7, 4, 5, 6, }; // convert index data to byte array var vertexData = indices.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); var len = vertexData.Length; // write index data to the triangle indices stream await stream2.WriteAsync(vertexData, 0, vertexData.Length); } }
//</SnippetTriangleIndices> //<SnippetVertices> private async Task GetVerticesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DDouble; // have 3 xyz values description.Stride = 3; // have 8 vertices in all in this mesh mesh.CreateVertexPositions(sizeof(double) * 3 * 8); mesh.VertexPositionsDescription = description; // set the locations (in 3D coordinate space) of each vertex using (var stream = mesh.GetVertexPositions().AsStream()) { double[] vertices = { 0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0, 0, 0, 10, 10, 0, 10, 0, 10, 10, 10, 10, 10, }; // convert vertex data to a byte array byte[] vertexData = vertices.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); // write the locations to each vertex await stream.WriteAsync(vertexData, 0, vertexData.Length); } // update vertex count: 8 vertices in the cube mesh.VertexCount = 8; }
// Create the buffer for material indices. private static async Task SetMaterialIndicesAsync(Printing3DMesh mesh) { Printing3DBufferDescription description; description.Format = Printing3DBufferFormat.Printing3DUInt; description.Stride = 4; // 4 indices per material mesh.IndexCount = 12; // 12 triangles with material mesh.TriangleMaterialIndicesDescription = description; // Create the buffer into which we will write the material indices. mesh.CreateTriangleMaterialIndices(sizeof(UInt32) * description.Stride * mesh.IndexCount); // Fill the buffer with material indices. using (var stream = mesh.GetTriangleMaterialIndices().AsStream()) { UInt32[] indices = { // base materials: // in the BaseMaterialGroup, the BaseMaterial with id=0 will be applied to these triangle vertices 2, 0, 0, 0, 2, 0, 0, 0, // color materials: // in the ColorMaterialGroup, the ColorMaterials with these ids will be applied to these triangle vertices 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, // composite materials: // in the CompositeMaterialGroup, the CompositeMaterial with id=0 will be applied to these triangles 3,0,0,0, 3,0,0,0, // texture materials: // in the Texture2CoordMaterialGroup, each texture coordinate is mapped to the appropriate vertex on these // two adjacent triangle faces, so that the square face they create displays the original rectangular image 4, 0, 3, 1, 4, 2, 3, 0, }; var vertexData = indices.SelectMany(v => BitConverter.GetBytes(v)).ToArray(); await stream.WriteAsync(vertexData, 0, vertexData.Length); } }
private async Task CreateData() { package = new Printing3D3MFPackage(); var model = new Printing3DModel(); model.Unit = Printing3DModelUnit.Millimeter; var mesh = new Printing3DMesh(); // save vertices await GetVerticesAsync(mesh); // save indices await GetIndicesAsync(mesh); // save material indices await GetMaterialIndicesAsync(mesh); // to make sure we don't use the same byte array from Unity if (pngBytes != null) { // texture2Coord Group var tex2CoordGroup = new Printing3DTexture2CoordMaterialGroup(groupId); // save texture coords for (int i = 0; i < uvPrint.Length / 2; i++) { var tex2CoordMaterial = new Printing3DTexture2CoordMaterial(); tex2CoordMaterial.U = uvPrint[i * 2]; tex2CoordMaterial.V = uvPrint[i * 2 + 1]; tex2CoordGroup.Texture2Coords.Add(tex2CoordMaterial); } var copyPngBytes = new byte[pngBytes.Length]; pngBytes.CopyTo(copyPngBytes, 0); var randomAccessStream = new InMemoryRandomAccessStream(); await randomAccessStream.WriteAsync(copyPngBytes.AsBuffer()); randomAccessStream.Seek(0); var texture = new Printing3DModelTexture(); Printing3DTextureResource texResource = new Printing3DTextureResource(); texResource.Name = "/3D/Texture/skeleton.png"; texResource.TextureData = new MyRandomAccessStream(randomAccessStream); package.Textures.Add(texResource); // add metadata about the texture model.Metadata.Add("tex" + groupId, "/3D/Texture/skeleton.png"); model.Material.Texture2CoordGroups.Add(tex2CoordGroup); } else { // put color material into material group var newColor = Windows.UI.Color.FromArgb(a, r, g, b); var colrMat = new Printing3DColorMaterial(); colrMat.Color = newColor; var colorGroup = new Printing3DColorMaterialGroup(groupId); colorGroup.Colors.Add(colrMat); model.Material.ColorGroups.Add(colorGroup); } model.Meshes.Add(mesh); Printing3DComponent component = new Printing3DComponent(); component.Mesh = mesh; model.Components.Add(component); var componentWithMatrix = new Printing3DComponentWithMatrix(); componentWithMatrix.Component = component; componentWithMatrix.Matrix = System.Numerics.Matrix4x4.Identity; model.Build.Components.Add(componentWithMatrix); await package.SaveModelToPackageAsync(model); var modelStream = package.ModelPart; package.ModelPart = await FixTextureContentType(modelStream); // save to file and easy to debug // var stream = await package.SaveAsync(); // await SaveStreamTo3MF(stream); }
private static async Task <Printing3D3MFPackage> CreatePackageAsync() { var package = new Printing3D3MFPackage(); var model = new Printing3DModel(); model.Unit = Printing3DModelUnit.Millimeter; // Material indices start at 1. (0 is reserved.) #region Color materials // Create color materials. var colorMaterial1 = new Printing3DColorMaterial { Color = Color.FromArgb(255, 20, 20, 90) }; var colorMaterial2 = new Printing3DColorMaterial { Color = Color.FromArgb(255, 250, 120, 45) }; // Create a color group with id 1 and add the colors to it. var colorGroup = new Printing3DColorMaterialGroup(1); colorGroup.Colors.Add(colorMaterial1); colorGroup.Colors.Add(colorMaterial2); // add the color group to the model. model.Material.ColorGroups.Add(colorGroup); #endregion #region Base materials // Create base materials. var material1 = new Printing3DBaseMaterial { Name = Printing3DBaseMaterial.Pla, Color = colorMaterial1 }; var material2 = new Printing3DBaseMaterial { Name = Printing3DBaseMaterial.Abs, Color = colorMaterial2 }; // Create a new base material group with id 2 and add the base materials to it. var materialGroup = new Printing3DBaseMaterialGroup(2); materialGroup.Bases.Add(material1); materialGroup.Bases.Add(material2); // Add the material group to the base groups on the model. model.Material.BaseGroups.Add(materialGroup); #endregion #region Composite material groups // Create a composite material group with id 3. var compositeMaterialGroup = new Printing3DCompositeMaterialGroup(3); // Add it to the metadata. // The key is the string "composite" followed by the composite material group id. // The value specifies that the default base material group to use is id 2. model.Metadata.Add("composite3", "2"); // The indices are relative to the material indices that will be set by SetMaterialIndicesAsync. compositeMaterialGroup.MaterialIndices.Add(0); compositeMaterialGroup.MaterialIndices.Add(1); // Create new composite materials. // The Values correspond to the materials added in the compositeMaterialGroup, // and they must sum to 1.0. // Composite material 1 consists of 20% material1 and 80% material2. var compositeMaterial1 = new Printing3DCompositeMaterial(); compositeMaterial1.Values.Add(0.2); compositeMaterial1.Values.Add(0.8); // Composite material 2 consists of 50% material1 and 50% material2. var compositeMaterial2 = new Printing3DCompositeMaterial(); compositeMaterial2.Values.Add(0.5); compositeMaterial2.Values.Add(0.5); // Composite material 3 consists of 80% material1 and 20% material2. var compositeMaterial3 = new Printing3DCompositeMaterial(); compositeMaterial3.Values.Add(0.8); compositeMaterial3.Values.Add(0.2); // Composite material 4 consists of 40% material1 and 60% material2. var compositeMaterial4 = new Printing3DCompositeMaterial(); compositeMaterial4.Values.Add(0.4); compositeMaterial4.Values.Add(0.6); // Add the composite materials to the compositeMaterialGroup. compositeMaterialGroup.Composites.Add(compositeMaterial1); compositeMaterialGroup.Composites.Add(compositeMaterial2); compositeMaterialGroup.Composites.Add(compositeMaterial3); compositeMaterialGroup.Composites.Add(compositeMaterial4); // Add the composite material group to the model. model.Material.CompositeGroups.Add(compositeMaterialGroup); #endregion #region Texture resource Printing3DTextureResource textureResource = new Printing3DTextureResource(); // Set the texture path in the 3MF file. textureResource.Name = "/3D/Texture/msLogo.png"; // Load the texture from our package. StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/msLogo.png")); textureResource.TextureData = await file.OpenReadAsync(); package.Textures.Add(textureResource); #endregion #region 2D texture materials // Create a texture material group with id 4. var texture2CoordGroup = new Printing3DTexture2CoordMaterialGroup(4); // Add it to the metadata. // The key is the string "tex" followed by the texture material group id. // The value is the name of the texture resource (see textureResource.Name above). model.Metadata.Add("tex4", "/3D/Texture/msLogo.png"); // Create texture materials and add them to the group. texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 0.0, V = 1.0 }); texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 1.0, V = 1.0 }); texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 0.0, V = 0.0 }); texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 1.0, V = 0.0 }); // Add the texture material group to the model. model.Material.Texture2CoordGroups.Add(texture2CoordGroup); #endregion #region Mesh var mesh = new Printing3DMesh(); await SetVerticesAsync(mesh); await SetTriangleIndicesAsync(mesh); await SetMaterialIndicesAsync(mesh); // add the mesh to the model model.Meshes.Add(mesh); #endregion #region Adding a component to the build // create a component. Printing3DComponent component = new Printing3DComponent(); // assign the mesh to the component's mesh. component.Mesh = mesh; // Add the component to the model. A model can have multiple components. model.Components.Add(component); // The world matrix for the component is the identity matrix. var componentWithMatrix = new Printing3DComponentWithMatrix() { Component = component, Matrix = Matrix4x4.Identity }; // add the componentWithMatrix to the build. // The build defines what is to be printed from within a Printing3DModel. // If you leave a mesh out of the build, it will not be printed. model.Build.Components.Add(componentWithMatrix); #endregion // Save the completed model into a package. await package.SaveModelToPackageAsync(model); // fix any textures in the model file. await FixTextureContentTypeAsync(package); return(package); }
private static async Task<Printing3D3MFPackage> CreatePackageAsync() { var package = new Printing3D3MFPackage(); var model = new Printing3DModel(); model.Unit = Printing3DModelUnit.Millimeter; // Material indices start at 1. (0 is reserved.) #region Color materials // Create color materials. var colorMaterial1 = new Printing3DColorMaterial { Color = Color.FromArgb(255, 20, 20, 90) }; var colorMaterial2 = new Printing3DColorMaterial { Color = Color.FromArgb(255, 250, 120, 45) }; // Create a color group with id 1 and add the colors to it. var colorGroup = new Printing3DColorMaterialGroup(1); colorGroup.Colors.Add(colorMaterial1); colorGroup.Colors.Add(colorMaterial2); // add the color group to the model. model.Material.ColorGroups.Add(colorGroup); #endregion #region Base materials // Create base materials. var material1 = new Printing3DBaseMaterial { Name = Printing3DBaseMaterial.Pla, Color = colorMaterial1 }; var material2 = new Printing3DBaseMaterial { Name = Printing3DBaseMaterial.Abs, Color = colorMaterial2 }; // Create a new base material group with id 2 and add the base materials to it. var materialGroup = new Printing3DBaseMaterialGroup(2); materialGroup.Bases.Add(material1); materialGroup.Bases.Add(material2); // Add the material group to the base groups on the model. model.Material.BaseGroups.Add(materialGroup); #endregion #region Composite material groups // Create a composite material group with id 3. var compositeMaterialGroup = new Printing3DCompositeMaterialGroup(3); // Add it to the metadata. // The key is the string "composite" followed by the composite material group id. // The value specifies that the default base material group to use is id 2. model.Metadata.Add("composite3", "2"); // The indices are relative to the material indices that will be set by SetMaterialIndicesAsync. compositeMaterialGroup.MaterialIndices.Add(0); compositeMaterialGroup.MaterialIndices.Add(1); // Create new composite materials. // The Values correspond to the materials added in the compositeMaterialGroup, // and they must sum to 1.0. // Composite material 1 consists of 20% material1 and 80% material2. var compositeMaterial1 = new Printing3DCompositeMaterial(); compositeMaterial1.Values.Add(0.2); compositeMaterial1.Values.Add(0.8); // Composite material 2 consists of 50% material1 and 50% material2. var compositeMaterial2 = new Printing3DCompositeMaterial(); compositeMaterial2.Values.Add(0.5); compositeMaterial2.Values.Add(0.5); // Composite material 3 consists of 80% material1 and 20% material2. var compositeMaterial3 = new Printing3DCompositeMaterial(); compositeMaterial3.Values.Add(0.8); compositeMaterial3.Values.Add(0.2); // Composite material 4 consists of 40% material1 and 60% material2. var compositeMaterial4 = new Printing3DCompositeMaterial(); compositeMaterial4.Values.Add(0.4); compositeMaterial4.Values.Add(0.6); // Add the composite materials to the compositeMaterialGroup. compositeMaterialGroup.Composites.Add(compositeMaterial1); compositeMaterialGroup.Composites.Add(compositeMaterial2); compositeMaterialGroup.Composites.Add(compositeMaterial3); compositeMaterialGroup.Composites.Add(compositeMaterial4); // Add the composite material group to the model. model.Material.CompositeGroups.Add(compositeMaterialGroup); #endregion #region Texture resource Printing3DTextureResource textureResource = new Printing3DTextureResource(); // Set the texture path in the 3MF file. textureResource.Name = "/3D/Texture/msLogo.png"; // Load the texture from our package. StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/msLogo.png")); textureResource.TextureData = await file.OpenReadAsync(); package.Textures.Add(textureResource); #endregion #region 2D texture materials // Create a texture material group with id 4. var texture2CoordGroup = new Printing3DTexture2CoordMaterialGroup(4); // Add it to the metadata. // The key is the string "tex" followed by the texture material group id. // The value is the name of the texture resource (see textureResource.Name above). model.Metadata.Add("tex4", "/3D/Texture/msLogo.png"); // Create texture materials and add them to the group. texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 0.0, V = 1.0 }); texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 1.0, V = 1.0 }); texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 0.0, V = 0.0 }); texture2CoordGroup.Texture2Coords.Add(new Printing3DTexture2CoordMaterial() { U = 1.0, V = 0.0 }); // Add the texture material group to the model. model.Material.Texture2CoordGroups.Add(texture2CoordGroup); #endregion #region Mesh var mesh = new Printing3DMesh(); await SetVerticesAsync(mesh); await SetTriangleIndicesAsync(mesh); await SetMaterialIndicesAsync(mesh); // add the mesh to the model model.Meshes.Add(mesh); #endregion #region Adding a component to the build // create a component. Printing3DComponent component = new Printing3DComponent(); // assign the mesh to the component's mesh. component.Mesh = mesh; // Add the component to the model. A model can have multiple components. model.Components.Add(component); // The world matrix for the component is the identity matrix. var componentWithMatrix = new Printing3DComponentWithMatrix() { Component = component, Matrix = Matrix4x4.Identity }; // add the componentWithMatrix to the build. // The build defines what is to be printed from within a Printing3DModel. // If you leave a mesh out of the build, it will not be printed. model.Build.Components.Add(componentWithMatrix); #endregion // Save the completed model into a package. await package.SaveModelToPackageAsync(model); // fix any textures in the model file. await FixTextureContentTypeAsync(package); return package; }
private async Task <bool> CreateData() { //<SnippetInitClasses> var localPackage = new Printing3D3MFPackage(); var model = new Printing3DModel(); // specify scaling units for model data model.Unit = Printing3DModelUnit.Millimeter; //</SnippetInitClasses> // create new mesh on model var mesh = new Printing3DMesh(); // create vertices on the mesh await this.GetVerticesAsync(mesh); // create triangles on the mesh await SetTriangleIndicesAsync(mesh); //<SnippetMeshAdd> // add the mesh to the model model.Meshes.Add(mesh); //</SnippetMeshAdd> // create material indices await SetMaterialIndicesAsync(mesh); //<SnippetBaseMaterialGroup> // add material group // all material indices need to start from 1: 0 is a reserved id // create new base materialgroup with id = 1 var baseMaterialGroup = new Printing3DBaseMaterialGroup(1); // create color objects // 'A' should be 255 if alpha = 100% var darkBlue = Windows.UI.Color.FromArgb(255, 20, 20, 90); var orange = Windows.UI.Color.FromArgb(255, 250, 120, 45); var teal = Windows.UI.Color.FromArgb(255, 1, 250, 200); // create new ColorMaterials, assigning color objects var colrMat = new Printing3DColorMaterial(); colrMat.Color = darkBlue; var colrMat2 = new Printing3DColorMaterial(); colrMat2.Color = orange; var colrMat3 = new Printing3DColorMaterial(); colrMat3.Color = teal; // setup new materials using the ColorMaterial objects // set desired material type in the Name property var baseMaterial = new Printing3DBaseMaterial { Name = Printing3DBaseMaterial.Pla, Color = colrMat }; var baseMaterial2 = new Printing3DBaseMaterial { Name = Printing3DBaseMaterial.Abs, Color = colrMat2 }; // add base materials to the basematerialgroup // material group index 0 baseMaterialGroup.Bases.Add(baseMaterial); // material group index 1 baseMaterialGroup.Bases.Add(baseMaterial2); // add material group to the basegroups property of the model model.Material.BaseGroups.Add(baseMaterialGroup); //</SnippetBaseMaterialGroup> //<SnippetColorMaterialGroup> // add ColorMaterials to the Color Material Group (with id 2) var colorGroup = new Printing3DColorMaterialGroup(2); // add the previous ColorMaterial objects to this ColorMaterialGroup colorGroup.Colors.Add(colrMat); colorGroup.Colors.Add(colrMat2); colorGroup.Colors.Add(colrMat3); // add colorGroup to the ColorGroups property on the model model.Material.ColorGroups.Add(colorGroup); //</SnippetColorMaterialGroup> //<SnippetMetadata> model.Metadata.Add("Title", "Cube"); model.Metadata.Add("Designer", "John Smith"); model.Metadata.Add("CreationDate", "1/1/2016"); //</SnippetMetadata> //<SnippetCompositeMaterialGroup> // CompositeGroups // create new composite material group with id = 3 var compositeGroup = new Printing3DCompositeMaterialGroup(3); // indices point to base materials in BaseMaterialGroup with id =1 compositeGroup.MaterialIndices.Add(0); compositeGroup.MaterialIndices.Add(1); // create new composite materials var compMat = new Printing3DCompositeMaterial(); // fraction adds to 1.0 compMat.Values.Add(0.2); // .2 of first base material in BaseMaterialGroup 1 compMat.Values.Add(0.8); // .8 of second base material in BaseMaterialGroup 1 var compMat2 = new Printing3DCompositeMaterial(); // fraction adds to 1.0 compMat2.Values.Add(0.5); compMat2.Values.Add(0.5); var compMat3 = new Printing3DCompositeMaterial(); // fraction adds to 1.0 compMat3.Values.Add(0.8); compMat3.Values.Add(0.2); var compMat4 = new Printing3DCompositeMaterial(); // fraction adds to 1.0 compMat4.Values.Add(0.4); compMat4.Values.Add(0.6); // add composites to group compositeGroup.Composites.Add(compMat); compositeGroup.Composites.Add(compMat2); compositeGroup.Composites.Add(compMat3); compositeGroup.Composites.Add(compMat4); // add group to model model.Material.CompositeGroups.Add(compositeGroup); //</SnippetCompositeMaterialGroup> //<SnippetTextureResource> // texture resource setup Printing3DTextureResource texResource = new Printing3DTextureResource(); // name conveys the path within the 3MF document texResource.Name = "/3D/Texture/msLogo.png"; // in this case, we reference texture data in the sample appx, convert it to // an IRandomAccessStream, and assign it as the TextureData Uri texUri = new Uri("ms-appx:///Assets/msLogo.png"); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(texUri); IRandomAccessStreamWithContentType iRandomAccessStreamWithContentType = await file.OpenReadAsync(); texResource.TextureData = iRandomAccessStreamWithContentType; // add this testure resource to the 3MF Package localPackage.Textures.Add(texResource); // assign this texture resource to a Printing3DModelTexture var modelTexture = new Printing3DModelTexture(); modelTexture.TextureResource = texResource; //</SnippetTextureResource> //<SnippetTexture2CoordMaterialGroup> // texture2Coord Group // create new Texture2CoordMaterialGroup with id = 4 var tex2CoordGroup = new Printing3DTexture2CoordMaterialGroup(4); // create texture materials: // set up four tex2coordmaterial objects with four (u,v) pairs, // mapping to each corner of the image: var tex2CoordMaterial = new Printing3DTexture2CoordMaterial(); tex2CoordMaterial.U = 0.0; tex2CoordMaterial.V = 1.0; tex2CoordGroup.Texture2Coords.Add(tex2CoordMaterial); var tex2CoordMaterial2 = new Printing3DTexture2CoordMaterial(); tex2CoordMaterial2.U = 1.0; tex2CoordMaterial2.V = 1.0; tex2CoordGroup.Texture2Coords.Add(tex2CoordMaterial2); var tex2CoordMaterial3 = new Printing3DTexture2CoordMaterial(); tex2CoordMaterial3.U = 0.0; tex2CoordMaterial3.V = 0.0; tex2CoordGroup.Texture2Coords.Add(tex2CoordMaterial3); var tex2CoordMaterial4 = new Printing3DTexture2CoordMaterial(); tex2CoordMaterial4.U = 1.0; tex2CoordMaterial4.V = 0.0; tex2CoordGroup.Texture2Coords.Add(tex2CoordMaterial4); // add our Printing3DModelTexture to the Texture property of the group tex2CoordGroup.Texture = modelTexture; // add metadata about the texture so that u,v values can be used model.Metadata.Add("tex4", "/3D/Texture/msLogo.png"); // add group to groups on the model's material model.Material.Texture2CoordGroups.Add(tex2CoordGroup); //</SnippetTexture2CoordMaterialGroup> //<SnippetComponents> // create new component Printing3DComponent component = new Printing3DComponent(); // assign mesh to the component's mesh component.Mesh = mesh; // add component to the model's list of all used components // a model can have references to multiple components model.Components.Add(component); // create the transform matrix var componentWithMatrix = new Printing3DComponentWithMatrix(); // assign component to this componentwithmatrix componentWithMatrix.Component = component; // create an identity matrix var identityMatrix = Matrix4x4.Identity; // use the identity matrix as the transform matrix (no transformation) componentWithMatrix.Matrix = identityMatrix; // add component to the build property. model.Build.Components.Add(componentWithMatrix); //</SnippetComponents> //<SnippetSavePackage> // save the model to the package: await localPackage.SaveModelToPackageAsync(model); // get the model stream var modelStream = localPackage.ModelPart; // fix any textures in the model file localPackage.ModelPart = await FixTextureContentType(modelStream); //</SnippetSavePackage> return(true); }