Files
2026-10-30 08:35:16 +08:00

176 lines
7.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// VDE 格式 CLI:inspect / validate / text<->binary
#include "vde/foundation/vde_format.h"
#include "vde/brep/modeling.h"
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace {
bool read_file(const std::string& path, std::vector<uint8_t>* out) {
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) return false;
const auto n = f.tellg();
if (n < 0) return false;
out->resize(static_cast<size_t>(n));
f.seekg(0);
if (!out->empty())
f.read(reinterpret_cast<char*>(out->data()), static_cast<std::streamsize>(out->size()));
return f.good() || f.eof();
}
bool write_file(const std::string& path, const void* data, size_t size) {
std::ofstream f(path, std::ios::binary | std::ios::trunc);
if (!f) return false;
f.write(static_cast<const char*>(data), static_cast<std::streamsize>(size));
return f.good();
}
std::string as_text(const std::vector<uint8_t>& data) {
return std::string(data.begin(), data.end());
}
const char* section_name(uint16_t id) {
for (const auto& s : vde::foundation::vde_section_registry())
if (static_cast<uint16_t>(s.id) == id) return s.name;
return "UNKNOWN";
}
void print_check(const vde::foundation::VdeFormatCheck& c) {
std::cout << "valid=" << (c.valid ? "true" : "false")
<< " version=" << c.version
<< " flags=0x" << std::hex << c.flags << std::dec << "\n";
if (!c.error.empty()) std::cout << "error: " << c.error << "\n";
for (const auto& s : c.sections_info)
std::cout << "section " << s.id << " (" << section_name(s.id) << ")"
<< " stored=" << s.size_stored << " raw=" << s.size_raw
<< " crc=" << (s.has_crc ? (s.crc_ok ? "ok" : "bad") : "none") << "\n";
for (const auto& w : c.warnings) std::cout << "warning: " << w << "\n";
}
int inspect(const std::string& path) {
std::vector<uint8_t> data;
if (!read_file(path, &data)) { std::cerr << "read failed: " << path << "\n"; return 2; }
const std::string text = as_text(data);
if (vde::foundation::is_vde_text(text)) {
std::cout << "format=VDE-TEXT bytes=" << data.size() << "\n";
try {
if (text.find("\"document_type\": \"assembly\"") != std::string::npos) {
auto d = vde::foundation::parse_vde_text_assembly(text);
std::cout << "document=assembly name=" << d.assembly.name
<< " children=" << d.assembly.root.children.size() << "\n";
} else if (text.find("\"document_type\": \"mbd\"") != std::string::npos) {
auto d = vde::foundation::parse_vde_text_mbd(text);
std::cout << "document=mbd faces=" << d.body.num_faces()
<< " toolpaths=" << d.toolpaths.size() << "\n";
} else {
auto d = vde::foundation::parse_vde_text(text);
std::cout << "document=part faces=" << d.body.num_faces() << "\n";
}
} catch (const std::exception& e) {
std::cerr << "parse failed: " << e.what() << "\n";
return 1;
}
return 0;
}
auto c = vde::foundation::validate_vde_data(data);
print_check(c);
return c.valid ? 0 : 1;
}
int losses(const std::string& path) {
std::vector<uint8_t> data;
if (!read_file(path, &data)) { std::cerr << "read failed: " << path << "\n"; return 2; }
const std::string text = as_text(data);
if (vde::foundation::is_vde_text(text)) {
std::cout << "VDE-TEXT: v1 export not applicable; native text keeps v1.1+ features\n";
return 0;
}
auto c = vde::foundation::validate_vde_data(data);
if (!c.valid) { std::cerr << "invalid: " << c.error << "\n"; return 1; }
unsigned dropped = 0;
for (uint16_t id : c.sections) {
if (id > 5) {
std::cout << "would-drop section " << id << " (" << section_name(id) << ")\n";
++dropped;
}
}
std::cout << "v1-export dropped-sections=" << dropped
<< " (ids>5; materials/assembly/PMI/drawing/mfg/toolpath/naming/preview)\n";
return 0;
}
int convert(const std::string& in, const std::string& out, bool to_text) {
std::vector<uint8_t> data;
if (!read_file(in, &data)) { std::cerr << "read failed: " << in << "\n"; return 2; }
const std::string text = as_text(data);
if (to_text) {
if (vde::foundation::is_vde_text(text)) {
if (!write_file(out, data.data(), data.size())) return 2;
return 0;
}
const std::string converted = vde::foundation::vde_binary_to_text(data);
return write_file(out, converted.data(), converted.size()) ? 0 : 2;
}
if (!vde::foundation::is_vde_text(text)) {
if (!write_file(out, data.data(), data.size())) return 2;
return 0;
}
const auto converted = vde::foundation::vde_text_to_binary(text);
return write_file(out, converted.data(), converted.size()) ? 0 : 2;
}
} // namespace
int sample(const std::string& path) {
auto box = vde::brep::make_box(2.0, 3.0, 4.0);
return vde::foundation::save_vde(box, path) ? 0 : 1;
}
int selftest(const std::string& dir) {
auto box = vde::brep::make_box(2.0, 3.0, 4.0);
const std::string bin = dir + "/vde_format_selftest.vde";
const std::string txt = dir + "/vde_format_selftest.vde.json";
const std::string bin2 = dir + "/vde_format_selftest_roundtrip.vde";
if (!vde::foundation::save_vde(box, bin)) { std::cerr << "save_vde failed\n"; return 1; }
std::vector<uint8_t> data;
if (!read_file(bin, &data)) { std::cerr << "read binary failed\n"; return 1; }
auto c = vde::foundation::validate_vde_data(data);
if (!c.valid) { std::cerr << "validate failed: " << c.error << "\n"; return 1; }
const std::string text = vde::foundation::vde_binary_to_text(data);
if (!write_file(txt, text.data(), text.size())) return 1;
const auto back = vde::foundation::vde_text_to_binary(text);
if (!write_file(bin2, back.data(), back.size())) return 1;
auto c2 = vde::foundation::validate_vde_data(back);
if (!c2.valid) { std::cerr << "roundtrip validate failed: " << c2.error << "\n"; return 1; }
std::cout << "selftest PASS sections=" << c.sections_info.size()
<< " version=" << c.version << "\n";
std::remove(bin.c_str()); std::remove(txt.c_str()); std::remove(bin2.c_str());
return 0;
}
int main(int argc, char** argv) {
if (argc < 3) {
std::cerr << "usage: vde_format_tool inspect|validate <file>\n"
" vde_format_tool losses <file>\n"
" vde_format_tool to-text|to-binary <in> <out>\n"
" vde_format_tool selftest <dir>\n"
" vde_format_tool sample <out.vde>\n";
return 2;
}
const std::string cmd = argv[1];
if (cmd == "sample" && argc == 3) return sample(argv[2]);
if (cmd == "selftest" && argc == 3) return selftest(argv[2]);
if (cmd == "inspect") return inspect(argv[2]);
if (cmd == "validate") return inspect(argv[2]);
if (cmd == "losses") return losses(argv[2]);
if ((cmd == "to-text" || cmd == "to-binary") && argc == 4)
return convert(argv[2], argv[3], cmd == "to-text");
std::cerr << "unknown command\n";
return 2;
}