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 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); }