DOCX JPG PDF XML AMF
  Product Family
AMF

Extract Assets from AMF via Java

Extract Assets from AMF using Java library without any 3D modeling software.

How to Extract Assets from AMF File Using Java

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

Aspose.3D for Java

API which is a feature-rich, powerful and easy to use API for Java platform. You can download its latest version directly from

Aspose Maven Repository

and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository


<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-3d</artifactId>
<version>version of aspose-3d API</version>
<classifier>jdk17</classifier>
</dependency>

Steps to Extract Assets from AMF via Java

Java programmers can easily extract assets from AMF in just a 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 JAVA is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with Java Runtime Environment for JSP/JSF Application and Desktop Applications.
  • Get latest version of Aspose.3D for Java directly from Maven.
 

JAVA code to Extract Assets from AMF


//Source files that need to extract assets
String file = "template.amf";

//create an instance of Scene
Scene scene = new Scene();
scene.open(file, FileFormat.AMF);

//Create zip files and perform asset extraction on source files
String zipOutput ="OutputFile.zip";
File newFile=new File(zipOutput);
newFile.createNewFile();
FileOutputStream output = new FileOutputStream(zipOutput);
Zip za = new Zip(output);
Extract(scene,za,true);

//close stream resource
za.closeIo();

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

//Create a compressed file processing class
class Zip
{
    private ZipOutputStream archive;
    private HashSet<String> entries = new HashSet<String>();

    public Zip(OutputStream stream)
    {
        archive = new ZipOutputStream(stream);
    }

    public void Add(String fileName, byte[] content, boolean enableCompression) throws IOException {
        String entryName = PickName(fileName);
        if(enableCompression){
            archive.setLevel(5);
        }else{
            archive.setLevel(0);
        }
        ZipEntry entry = new ZipEntry(entryName);
        archive.putNextEntry(entry);
        archive.write(content, 0, content.length);
    }

    public void closeIo(){
        if(archive!=null){
            try {
                archive.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String PickName(String fileName)
    {
        if (!entries.contains(fileName))
        {
            entries.add(fileName);
            return fileName;
        }
        String arrFile[]=fileName.split(".");
        String baseName = arrFile[0];
        String ext = arrFile[1];
        for (int idx = 2; ; idx++)
        {
            String newName = baseName + "_" + idx;
            if (!StringUtils.isBlank(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 boolean texture;
    HashSet<A3DObject> visited = new HashSet<A3DObject>();

    public Extractor(Zip zip,boolean texture)
    {
        this.zip = zip;
        this.texture = texture;
    }

    private boolean CanVisit(A3DObject obj)
    {
        if (visited.contains(obj))
            return false;
        visited.add(obj);
        return true;
    }

    public void Extract(Scene scene) throws IOException {
        if (scene.getLibrary() != null && scene.getLibrary().size() > 0)
        {
            for (A3DObject obj:scene.getLibrary())
            {
                Visit(obj);
            }
        }
        VisitNode(scene.getRootNode());
    }

    private void VisitNode(Node node) throws IOException {
        if (!CanVisit(node))
            return;
        if (texture)
        {
            for (Material mat:node.getMaterials())
            {
                VisitMaterial(mat);
            }
        }

        for (Entity entity:node.getEntities())
        {
            if (entity instanceof Mesh){
                Save((Mesh)entity, node.getName());
            }
        }

        for (Node child:node.getChildNodes())
        {
            VisitNode(child);
        }
    }

    private void VisitMaterial(Material mat) throws IOException {
        if (!CanVisit(mat))
            return;
        if (!texture)
            return;
        for (TextureSlot tslot: mat)
        {
            if (tslot.getTexture() instanceof Texture)
            {
                Save((Texture)tslot.getTexture());
            }
        }
    }

    private void Visit(A3DObject obj) throws IOException {
        if (texture && obj instanceof Texture)
        {
            Save((Texture)obj);
        }
            else if (obj instanceof Mesh)
        {
            Save((Mesh)obj, null);
        }
            else if (obj instanceof Node)
        {
            VisitNode((Node)obj);
        }
    }

    private void Save(Mesh mesh, String nodeName) throws IOException {
        if (!CanVisit(mesh))
            return;
        Scene scene = new Scene(mesh);
        MemoryStream ms=new MemoryStream();
        scene.save(ms, FileFormat.FBX7400ASCII);
        String name = nodeName;
        if (StringUtils.isBlank(name))
            name = mesh.getName();
        if (StringUtils.isBlank(name))
            name = "mesh";
        String ext = ".fbx";
        zip.Add(name + ext, ms.toArray(), true);
    }

    private void Save(Texture tex) throws IOException {
        if (tex.getContent() == null || !CanVisit(tex))
            return;
        String fileName=tex.getFileName();
        if(tex.getFileName()!=null){
            String arrFile[]=fileName.split(".");
            fileName = arrFile[0];
        }
        zip.Add(fileName, tex.getContent(), false);
    }
}
 
  • Free App to Extract Assets from AMF

    Extractor AMF right now by visiting our Live Demos website.The live demo has the following benefits

      No need to download Aspose API.
      No need to write any code.
      Just upload AMF file and hit the "Extract" button
      You will get the download link.

    Java 3D Scene Manipulation Library

    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.

    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 JAVA, One can also extract assets from many other file formats including.

    3DS (3D Studio Mesh File Format)
    3MF (3D Manufacturing Format)
    AMF (Additive Manufacturing Format)
    ASE (Aseprite Sprite File)
    DAE (Digital Asset Exchange File Format)
    DXF (Autodesk Drawing Exchange File Format)
    DRC (Google Draco)
    FBX (Filmbox Interchange File)
    GLB (Binary GlTF File)
    GLTF (GL Transmission Format)
    JT (JT File Format)
    OBJ (OBJ File Format)
    PLY (Polygon File Format)
    PDF (3D PDF)
    RVM (RVM File Format)
    STL (Stereolithography)
    U3D (Universal 3D File Format)
    VRML (Virtual Reality Modeling Language)
    X (DirectX Model File)
    USD (Universal Scene Description)
    USDZ (Universal Scene Description Zip Archive)