mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 10:47:31 -06:00
EXPORT ignored the version and always wrote the version the drawing was read in, and the Save dialog passed the Default Save Format version only for drawings with no file yet, so an opened 2013 DXF could not be written as 2000. EXPORT now takes SAVEAS's version and binary options for DXF and DWG (other formats refuse them). The Save and Export dialogs share one helper that passes the Default Save Format version for every .dxf or .dwg path, an opened drawing's too. An export leaves the drawing's own version and name alone, and QSAVE still keeps the file's version. The message shown when the version is picked names Save As and Export and goes through the translation system.
110 lines
4.6 KiB
Rust
110 lines
4.6 KiB
Rust
//! #3213, with the real DXF and DWG readers and writers: a drawing read from a 2013 DXF (AC1027)
|
|
//! exported or saved as a 2000 DXF or DWG says AC1015, and a plain export keeps the drawing's own
|
|
//! version.
|
|
//!
|
|
//! In its own test binary, not `tests/it`, because it installs the real io hooks. The hooks are
|
|
//! process-wide and set once (`set_io` and `set_write_as` keep the first value), so in `tests/it`
|
|
//! they would change what the other tests' saves, exports and opens do, by test order. They also
|
|
//! cannot share a binary with `save_version.rs`, whose stub writer must be the one installed.
|
|
|
|
use cadcraft_engine::Session;
|
|
use cadcraft_engine::cmd::file::{IoHooks, base64_decode, set_io, set_write_as};
|
|
use cadcraft_engine::doc::EntityKind;
|
|
use serde_json::json;
|
|
|
|
#[path = "support/temp_dir.rs"]
|
|
mod temp_dir;
|
|
|
|
use temp_dir::TempDir;
|
|
|
|
fn install() {
|
|
set_io(IoHooks {
|
|
read: |b, name| cadcraft_io::read(b, name).map_err(|e| e.to_string()),
|
|
write: |d, name| cadcraft_io::write(d, name).map_err(|e| e.to_string()),
|
|
plot: None,
|
|
});
|
|
set_write_as(|d, name, o| {
|
|
cadcraft_io::write_with(d, name, &cadcraft_io::SaveOptions { version: o.version, binary: o.binary }).map_err(|e| e.to_string())
|
|
});
|
|
}
|
|
|
|
/// The `$ACADVER` an exported DXF declares.
|
|
fn acadver(r: &serde_json::Value) -> String {
|
|
header_version(&String::from_utf8(base64_decode(r["data"].as_str().unwrap()).unwrap()).unwrap())
|
|
}
|
|
|
|
/// The version a DWG file's bytes open with ("AC1015" for 2000).
|
|
fn dwg_version(bytes: &[u8]) -> String {
|
|
String::from_utf8_lossy(bytes.get(..6).unwrap_or_default()).into_owned()
|
|
}
|
|
|
|
/// The radii of the circles in the active drawing.
|
|
fn radii(s: &Session) -> Vec<f64> {
|
|
s.doc().unwrap().model.iter().filter_map(|e| if let EntityKind::Circle(c) = &e.kind { Some(c.radius) } else { None }).collect()
|
|
}
|
|
|
|
/// The `$ACADVER` in the header of the DXF text `text`.
|
|
fn header_version(text: &str) -> String {
|
|
let mut lines = text.lines().map(str::trim);
|
|
while let Some(l) = lines.next() {
|
|
if l == "$ACADVER" {
|
|
lines.next();
|
|
return lines.next().unwrap_or_default().to_string();
|
|
}
|
|
}
|
|
String::new()
|
|
}
|
|
|
|
#[test]
|
|
fn a_2013_drawing_exports_as_a_2000_dxf() {
|
|
install();
|
|
// Removed when the test ends, even when an assertion fails.
|
|
let dir = TempDir::new("export-version");
|
|
let file = dir.path_str("plan.dxf");
|
|
let mut s = Session::new();
|
|
s.cmdline("circle 0,0 5").unwrap();
|
|
s.execute("saveas", &json!({ "path": file, "version": "2013" })).unwrap();
|
|
s.execute("open", &json!({ "path": file })).unwrap();
|
|
|
|
assert_eq!(acadver(&s.execute("export", &json!({ "format": "dxf" })).unwrap()), "AC1027");
|
|
assert_eq!(acadver(&s.execute("export", &json!({ "format": "dxf", "version": "2000" })).unwrap()), "AC1015");
|
|
}
|
|
|
|
#[test]
|
|
fn a_2013_drawing_saved_as_2000_says_ac1015() {
|
|
install();
|
|
let dir = TempDir::new("saveas-version");
|
|
let (file, copy) = (dir.path_str("plan.dxf"), dir.path_str("plan-2000.dxf"));
|
|
let mut s = Session::new();
|
|
s.cmdline("circle 0,0 5").unwrap();
|
|
s.execute("saveas", &json!({ "path": file, "version": "2013" })).unwrap();
|
|
s.execute("open", &json!({ "path": file })).unwrap();
|
|
|
|
s.execute("saveas", &json!({ "path": copy, "version": "2000" })).unwrap();
|
|
assert_eq!(header_version(&std::fs::read_to_string(©).unwrap()), "AC1015");
|
|
}
|
|
|
|
/// The DWG writer itself writes the chosen version: the file opens with AC1015, not the 2013
|
|
/// drawing's AC1027, and reads back with its circle.
|
|
#[test]
|
|
fn a_2013_drawing_exports_and_saves_as_a_2000_dwg() {
|
|
install();
|
|
// The drawing is the test's own, so the DWG conversion may run in this process.
|
|
cadcraft_io::dwg_worker::allow_in_process();
|
|
let dir = TempDir::new("dwg-version");
|
|
let (file, copy) = (dir.path_str("plan.dxf"), dir.path_str("plan-2000.dwg"));
|
|
let mut s = Session::new();
|
|
s.cmdline("circle 0,0 5").unwrap();
|
|
s.execute("saveas", &json!({ "path": file, "version": "2013" })).unwrap();
|
|
s.execute("open", &json!({ "path": file })).unwrap();
|
|
|
|
let exported = |s: &mut Session, p: serde_json::Value| base64_decode(s.execute("export", &p).unwrap()["data"].as_str().unwrap()).unwrap();
|
|
assert_eq!(dwg_version(&exported(&mut s, json!({ "format": "dwg" }))), "AC1027");
|
|
assert_eq!(dwg_version(&exported(&mut s, json!({ "format": "dwg", "version": "2000" }))), "AC1015");
|
|
|
|
s.execute("saveas", &json!({ "path": copy, "version": "2000" })).unwrap();
|
|
assert_eq!(dwg_version(&std::fs::read(©).unwrap()), "AC1015");
|
|
s.execute("open", &json!({ "path": copy })).unwrap();
|
|
assert_eq!(radii(&s), [5.0]);
|
|
}
|