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);
        }
Пример #2
0
        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;
        }