-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_extension.rs
More file actions
198 lines (172 loc) · 6.55 KB
/
Copy pathcustom_extension.rs
File metadata and controls
198 lines (172 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Example demonstrating custom 3MF extension support
//!
//! This example shows how to:
//! - Register custom extensions
//! - Implement element handlers for custom elements
//! - Implement custom validation rules
//! - Parse 3MF files with custom extensions
use lib3mf::{CustomElementResult, CustomExtensionContext, Model, ParserConfig};
use std::collections::HashMap;
use std::io::{Cursor, Write};
use std::sync::{Arc, Mutex};
use zip::ZipWriter;
use zip::write::SimpleFileOptions;
/// Custom data structure to store custom extension data
#[derive(Debug, Clone)]
struct CustomExtensionData {
elements: Vec<String>,
attributes: HashMap<String, String>,
}
impl CustomExtensionData {
fn new() -> Self {
Self {
elements: Vec::new(),
attributes: HashMap::new(),
}
}
}
/// Create a 3MF file with a custom extension
fn create_sample_3mf_with_custom_extension() -> Vec<u8> {
let buffer = Vec::new();
let mut zip = ZipWriter::new(Cursor::new(buffer));
// Add Content_Types.xml
zip.start_file(
"[Content_Types].xml",
SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated),
)
.unwrap();
zip.write_all(
br#"<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="model" ContentType="application/vnd.ms-package.3dmanufacturing-3dmodel+xml"/>
</Types>"#,
)
.unwrap();
// Add _rels/.rels
zip.start_file(
"_rels/.rels",
SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated),
)
.unwrap();
zip.write_all(
br#"<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Target="/3D/3dmodel.model" Id="rel0" Type="http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel"/>
</Relationships>"#,
)
.unwrap();
// Add 3D/3dmodel.model with custom extension
zip.start_file(
"3D/3dmodel.model",
SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated),
)
.unwrap();
zip.write_all(
br#"<?xml version="1.0" encoding="UTF-8"?>
<model unit="millimeter"
xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02"
xmlns:custom="http://example.com/3mf-extensions/custom/2024/01"
requiredextensions="custom">
<resources>
<object id="1" type="model">
<mesh>
<vertices>
<vertex x="0" y="0" z="0"/>
<vertex x="10" y="0" z="0"/>
<vertex x="5" y="10" z="0"/>
<vertex x="5" y="5" z="10"/>
</vertices>
<triangles>
<triangle v1="0" v2="1" v3="2"/>
<triangle v1="0" v2="1" v3="3"/>
<triangle v1="1" v2="2" v3="3"/>
<triangle v1="2" v2="0" v3="3"/>
</triangles>
</mesh>
</object>
</resources>
<build>
<item objectid="1"/>
</build>
</model>"#,
)
.unwrap();
let cursor = zip.finish().unwrap();
cursor.into_inner()
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Custom 3MF Extension Example ===\n");
// Create shared state to track custom extension data
let custom_data = Arc::new(Mutex::new(CustomExtensionData::new()));
let custom_data_clone = custom_data.clone();
// Define the custom extension namespace
let custom_namespace = "http://example.com/3mf-extensions/custom/2024/01";
println!("1. Registering custom extension: {}\n", custom_namespace);
// Create a parser configuration with a custom extension handler
let config = ParserConfig::new().with_custom_extension_handlers(
custom_namespace,
"CustomExtension",
// Element handler - called when custom elements are encountered
Arc::new(
move |ctx: &CustomExtensionContext| -> Result<CustomElementResult, String> {
println!(" Element handler called:");
println!(" - Element: {}", ctx.element_name);
println!(" - Namespace: {}", ctx.namespace);
println!(" - Attributes: {:?}", ctx.attributes);
let mut data = custom_data_clone.lock().unwrap();
data.elements.push(ctx.element_name.clone());
for (key, value) in &ctx.attributes {
data.attributes.insert(key.clone(), value.clone());
}
Ok(CustomElementResult::Handled)
},
),
// Validation handler - called during model validation
Arc::new(|model: &Model| -> Result<(), String> {
println!(" Custom validation handler called:");
println!(" - Model has {} objects", model.resources.objects.len());
println!(" - Model has {} build items", model.build.items.len());
// Example custom validation: ensure model has at least one object
if model.resources.objects.is_empty() {
return Err("Custom validation: Model must have at least one object".to_string());
}
println!(" - Custom validation passed!\n");
Ok(())
}),
);
println!("2. Creating sample 3MF file with custom extension...\n");
let data = create_sample_3mf_with_custom_extension();
println!("3. Parsing 3MF file with custom extension support...\n");
let cursor = Cursor::new(data);
let model = Model::from_reader_with_config(cursor, config)?;
println!("4. Parse successful!\n");
println!(" Model information:");
println!(" - Unit: {}", model.unit);
println!(" - Objects: {}", model.resources.objects.len());
println!(" - Build items: {}", model.build.items.len());
println!(
" - Required extensions: {:?}",
model.required_extensions
);
println!(
" - Required custom extensions: {:?}",
model.required_custom_extensions
);
// Display collected custom extension data
let data = custom_data.lock().unwrap();
println!("\n5. Custom extension data collected:");
println!(" - Elements encountered: {:?}", data.elements);
println!(" - Attributes collected: {:?}", data.attributes);
println!("\n=== Example completed successfully! ===");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example_runs() {
// This test ensures the example code compiles and runs
assert!(main().is_ok());
}
}