lib3mf_core/writer/
opc_writer.rs

1use crate::error::Result;
2use crate::writer::xml_writer::XmlWriter;
3use std::io::Write;
4
5/// Writes the `[Content_Types].xml` file for a 3MF package.
6pub fn write_content_types<W: Write>(writer: W) -> Result<()> {
7    let mut xml = XmlWriter::new(writer);
8    xml.write_declaration()?;
9
10    xml.start_element("Types")
11        .attr(
12            "xmlns",
13            "http://schemas.openxmlformats.org/package/2006/content-types",
14        )
15        .write_start()?;
16
17    // Defaults
18    xml.start_element("Default")
19        .attr("Extension", "rels")
20        .attr(
21            "ContentType",
22            "application/vnd.openxmlformats-package.relationships+xml",
23        )
24        .write_empty()?;
25    xml.start_element("Default")
26        .attr("Extension", "model")
27        .attr(
28            "ContentType",
29            "application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
30        )
31        .write_empty()?;
32    xml.start_element("Default")
33        .attr("Extension", "png")
34        .attr("ContentType", "image/png")
35        .write_empty()?;
36
37    // Don't enforce Override for 3D/3dmodel.model if extension match works,
38    // but spec usually recommends explicit override for parts.
39    // For now, minimal valid set.
40
41    xml.end_element("Types")?;
42    Ok(())
43}
44
45/// Writes the `_rels/.rels` OPC relationships file pointing to the model and optional thumbnail.
46pub fn write_relationships<W: Write>(
47    writer: W,
48    model_part: &str,
49    thumbnail_part: Option<&str>,
50) -> Result<()> {
51    let mut xml = XmlWriter::new(writer);
52    xml.write_declaration()?;
53
54    xml.start_element("Relationships")
55        .attr(
56            "xmlns",
57            "http://schemas.openxmlformats.org/package/2006/relationships",
58        )
59        .write_start()?;
60
61    xml.start_element("Relationship")
62        .attr("Target", model_part)
63        .attr("Id", "rel0")
64        .attr(
65            "Type",
66            "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel",
67        )
68        .write_empty()?;
69
70    if let Some(thumb) = thumbnail_part {
71        xml.start_element("Relationship")
72            .attr("Target", thumb)
73            .attr("Id", "rel1")
74            .attr(
75                "Type",
76                "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail",
77            )
78            .write_empty()?;
79    }
80
81    xml.end_element("Relationships")?;
82    Ok(())
83}