HTML JPG PDF XML AMF
Aspose.3D  for .NET
AMF

Extract Assets from AMF via C#

Build your own .NET apps to Extract Assets from AMF files using server-side APIs.

How to Extract Assets from AMF File Using C#

In order to extract assets from AMF file, we’ll use

Aspose.3D for .NET

API which is a feature-rich, powerful and easy to use API for C# platform to be used with extract assets. Open

NuGet

package manager, search for Aspose.3D and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


PM> Install-Package Aspose.3D

Steps to Extract Assets from AMF via C#

Aspose.3D makes it easy for the developers to extract assets from the AMF file with just few lines of code.

  • Load AMF file via the constructor of Scene class
  • Create zip file format object as output file format
  • Create archive class and handle extract asset class
  • Call the Extract method and save the file

System Requirements

Aspose.3D for .NET is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Mono
  • Development environment like Microsoft Visual Studio
  • Aspose.3D for .NET referenced in your project
 

C# code to Extract Assets from AMF


//Source files that need to extract assets
string file = "template.amf";
Scene scene = new Scene(file);  

//The output is in a compressed file format, and Directory represents the name of an existing folder
var zipOutput = Path.Combine("Directory", "OutputFile.zip");
using var output = new FileStream(zipOutput, FileMode.Create);
using var za = new Zip(output);

//Call the Extract method to perform asset extraction operations
Extract(scene,za,true);

//Callable Extract method,The parameter texture indicates: whether to extract the texture
private void Extract(Scene scene, Zip za,bool texture)
{
    var extractor = new Extractor(za,texture);
    extractor.Extract(scene);
}

//Create a compressed file processing class
class Zip : IDisposable
{
    private ZipArchive archive;
    private HashSet<string> entries = new HashSet<string>();

    public Zip(Stream stream)
    {
        archive = new ZipArchive(stream, ZipArchiveMode.Create);
    }
    public void Dispose()
    {
        archive.Dispose();
    }

    public void Add(string fileName, byte[] content, bool enableCompression)
    {
        var entryName = PickName(fileName);
        var compressionLevel = enableCompression ? CompressionLevel.Fastest : CompressionLevel.NoCompression;
        var entry = archive.CreateEntry(entryName, compressionLevel);
        using var stream = entry.Open();
        stream.Write(content, 0, content.Length);
    }
    
    private string PickName(string fileName)
    {
        if (!entries.Contains(fileName))
        {
            entries.Add(fileName);
            return fileName;
        }
        var baseName = Path.GetFileNameWithoutExtension(fileName);
        var ext = Path.GetExtension(fileName);
        for (var idx = 2; ; idx++)
        {
            var newName = baseName + "_" + idx;
            if (!string.IsNullOrEmpty(ext))
                newName += ext;
            if (entries.Contains(newName))
                continue;
            entries.Add(newName);
            return newName;
        }
    }
}

//Create an asset extraction processing class
class Extractor
{
    private Zip zip;
    private bool texture;
    HashSet<A3DObject> visited = new HashSet<A3DObject>();
    public Extractor(Zip zip,bool texture)
    {
        this.zip = zip;
        this.texture = texture;
    }

    private bool CanVisit(A3DObject obj)
    {
        if (visited.Contains(obj))
            return false;
        visited.Add(obj);
        return true;
    }
    public void Extract(Scene scene)
    {
        if (scene.Library != null && scene.Library.Count > 0)
        {
            foreach (var obj in scene.Library)
            {
                Visit(obj);
            }
        }
        VisitNode(scene.RootNode);
    }
    private void VisitNode(Node node)
    {
        if (!CanVisit(node))
            return;
        if (texture)
        {
            foreach (var mat in node.Materials)
            {
                VisitMaterial(mat);
            }
        }

        foreach (var entity in node.Entities)
        {
            if (entity is Mesh)
                Save((Mesh)entity, node.Name);
        }
        
        foreach (var child in node.ChildNodes)
        {
            VisitNode(child);
        }
    }
    private void VisitMaterial(Material mat)
    {
        if (!CanVisit(mat))
            return;
        if (!texture)
            return;
        foreach (var tslot in mat)
        {
            if (tslot.Texture is Texture)
            {
                Save((Texture)tslot.Texture);
            }
        }
    }
    private void Visit(A3DObject obj)
    {
        if (texture && obj is Texture)
        {
            Save((Texture)obj);
        }
        else if (obj is Mesh)
        {
            Save((Mesh)obj, null);
        }
        else if (obj is Node)
        {
            VisitNode((Node)obj);
        }
    }
    private void Save(Mesh mesh, string? nodeName)
    {
        if (!CanVisit(mesh))
            return;
        Scene scene = new Scene(mesh);
        using (var ms = new MemoryStream())
        {
            scene.Save(ms, FileFormat.FBX7400ASCII);
            var name = nodeName;
            if (string.IsNullOrEmpty(name))
                name = mesh.Name;
            if (string.IsNullOrEmpty(name))
                name = "mesh";
            var ext = ".fbx";
            zip.Add(name + ext, ms.ToArray(), true);
        }
    }
    private void Save(Texture tex)
    {
        if (tex.Content == null || !CanVisit(tex))
            return;
        var fileName = tex.FileName != null ? Path.GetFileName(tex.FileName) : null;
        zip.Add(fileName, tex.Content, false);
    }
}
 
  • About Aspose.3D for .NET API

    Aspose.3D is a CAD and Gameware API to load, modify and convert 3D files. API is a standalone and does not require any any 3D modeling or rendering software. One can easily use API for Discreet3DS, WavefrontOBJ, STL (ASCII, Binary), Universal3D, FBX (ASCII, Binary), Collada, glTF, PLY, GLB, DirectX and more formats.

    Free App to Extract Assets from AMF

    Check our live demos to Extractor AMF with following benefits.

      No need to download or setup anything
      No need to write or compile code
      Just upload AMF file and hit the "Extract" button
      Download AMF file from the link, if required

    AMF What is AMF File Format?

    Additive Manufacturing file format (AMF) defines open standards for objects description in order to be utilized by additive manufacturing processes such as 3D Printing. CAD programs use the AMF file format by making use of the information such as geometry, color and material of the objects. AMF is different than STL format as the lateral does not support color, materials, lattices, and constellations.

    Read More

    Other Supported App to Extract Assets from Formats

    Using C#, One can also extract assets from many other file formats including.

    3MF (3D Manufacturing Format)
    3DS (3D Studio Mesh File Format)
    ASE (2D Animation File)
    DAE (Digital Asset Exchange)
    DXF (Drawing Interchange Format)
    DRC (Google Draco)
    FBX (3D Format)
    GLB (3D File Binary Representation)
    GLTF (GL Transmission Format)
    JT (Jupiter Tessellation File)
    OBJ (3D File Format)
    PLY (Polygon File Format)
    PDF (3D PDF)
    RVM (AVEVA Plant Design Model)
    STL (Interchangeable 3D Surface Geometry)
    U3D (Universal 3D)
    VRML (Virtual Reality Modeling Language)
    X (DirectX Model Image)
    USD (Universal Scene Description)
    USDZ (Universal Scene Description Zip Archive)